This commit is contained in:
Dominic 2019-12-14 15:38:45 +01:00
parent 9d0186fbfe
commit 94a3fd659e
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
5 changed files with 112 additions and 5 deletions

3
.gitignore vendored
View file

@ -6,6 +6,9 @@ vpl/
*~ *~
\#*# \#*#
# javac stuff
*.class
# javadoc stuff # javadoc stuff
*.html *.html
*.js *.js

View file

@ -239,4 +239,16 @@ public class FunctionalSet<E> implements Set<E>
return res; return res;
} }
public E min(Comparator<E> comp)
{
E min = null;
for (E elem : this)
{
if (min == null || comp.compare(min, elem) > 0) // min > elem
min = elem;
}
if (min == null)
throw new MinimumOfEmptySetException();
return min;
}
} }

View file

@ -0,0 +1,67 @@
import java.util.*;
public class FunctionalSetIterator<E> implements Iterator<E>
{
/** Unsere Menge */
private FunctionalSet<E> set;
/** Aktuelles Element */
private SimpleFunctionalSet<E> current;
/** Bereits gesehene Elemente */
private Set<Object> seen = new FunctionalSet<>();
/** Aktuelles Element */
private E currentElem;
/** Ob das aktuelle Element bereits entfernt wurde. */
private boolean hasBeenRemoved = true;
private void skipToNext()
{
while (true)
{
current = current.getRemainingSet();
if (current instanceof RemoveSet)
seen.add(((RemoveSet) current).getObject());
else if (!(current instanceof AddSet) || !seen.contains(((AddSet)current).getElement()))
break;
}
}
public FunctionalSetIterator(FunctionalSet<E> set, SimpleFunctionalSet<E> head)
{
this.set = set;
current = head;
if (current instanceof RemoveSet)
{
seen.add(((RemoveSet)current).getObject());
skipToNext();
}
}
@Override
public boolean hasNext()
{
return !(current instanceof EmptySet);
}
@Override
public E next()
{
if (!hasNext())
throw new NoSuchElementException();
currentElem = ((AddSet<E>) current).getElement();
seen.add(currentElem);
skipToNext();
hasBeenRemoved = false;
return currentElem;
}
@Override
public void remove()
{
if (hasBeenRemoved)
throw new IllegalStateException("Das Element wurde schon entfernt");
set.remove(currentElem);
hasBeenRemoved = true;
}
}

View file

@ -15,15 +15,36 @@ public class Main
case "exit": case "exit":
break; break;
case "add": case "add":
s.add(Integer.parseInt(words[1])); try
System.out.println(s); {
s.add(Integer.parseInt(words[1]));
System.out.println(s);
}
catch (NumberFormatException nfe)
{
System.out.println("FEHLER: Eingabe ungültig!");
}
break; break;
case "remove": case "remove":
s.remove(Integer.parseInt(words[1])); try
System.out.println(s); {
s.remove(Integer.parseInt(words[1]));
System.out.println(s);
}
catch (NumberFormatException nfe)
{
System.out.println("FEHLER: Eingabe ungültig!");
}
break; break;
case "min": case "min":
System.out.println(s.min(Comparator.<Integer>naturalOrder())); try
{
System.out.println(s.min(Comparator.naturalOrder()));
}
catch (MinimumOfEmptySetException moese)
{
System.out.println("FEHLER: Set enthaelt keine Elemente");
}
break; break;
default: default:
System.out.println("Unknown command."); System.out.println("Unknown command.");

View file

@ -0,0 +1,4 @@
public class MinimumOfEmptySetException extends RuntimeException
{
}