tut9
This commit is contained in:
parent
9d0186fbfe
commit
94a3fd659e
5 changed files with 112 additions and 5 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -6,6 +6,9 @@ vpl/
|
|||
*~
|
||||
\#*#
|
||||
|
||||
# javac stuff
|
||||
*.class
|
||||
|
||||
# javadoc stuff
|
||||
*.html
|
||||
*.js
|
||||
|
|
|
@ -239,4 +239,16 @@ public class FunctionalSet<E> implements Set<E>
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
67
tut9/src/FunctionalSetIterator.java
Normal file
67
tut9/src/FunctionalSetIterator.java
Normal 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;
|
||||
}
|
||||
}
|
|
@ -15,15 +15,36 @@ public class Main
|
|||
case "exit":
|
||||
break;
|
||||
case "add":
|
||||
try
|
||||
{
|
||||
s.add(Integer.parseInt(words[1]));
|
||||
System.out.println(s);
|
||||
}
|
||||
catch (NumberFormatException nfe)
|
||||
{
|
||||
System.out.println("FEHLER: Eingabe ungültig!");
|
||||
}
|
||||
break;
|
||||
case "remove":
|
||||
try
|
||||
{
|
||||
s.remove(Integer.parseInt(words[1]));
|
||||
System.out.println(s);
|
||||
}
|
||||
catch (NumberFormatException nfe)
|
||||
{
|
||||
System.out.println("FEHLER: Eingabe ungültig!");
|
||||
}
|
||||
break;
|
||||
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;
|
||||
default:
|
||||
System.out.println("Unknown command.");
|
||||
|
|
4
tut9/src/MinimumOfEmptySetException.java
Normal file
4
tut9/src/MinimumOfEmptySetException.java
Normal file
|
@ -0,0 +1,4 @@
|
|||
public class MinimumOfEmptySetException extends RuntimeException
|
||||
{
|
||||
|
||||
}
|
Reference in a new issue