Add initial TapeSet
Dieser Commit ist enthalten in:
Ursprung
4e3ec8c530
Commit
c10465bbc1
@ -92,8 +92,7 @@ public class MWTeam {
|
||||
}
|
||||
|
||||
public void leave(Player p) {
|
||||
if(!players.contains(p))
|
||||
return;
|
||||
if (!players.contains(p)) return;
|
||||
|
||||
players.remove(p);
|
||||
sbteam.removePlayer(p);
|
||||
|
89
src/de/steamwar/misslewars/data/TapeSet.java
Normale Datei
89
src/de/steamwar/misslewars/data/TapeSet.java
Normale Datei
@ -0,0 +1,89 @@
|
||||
package de.steamwar.misslewars.data;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class TapeSet<T> {
|
||||
|
||||
private Map<T, Node<T>> nodeMap = new HashMap<>();
|
||||
private Node<T> last = null;
|
||||
private Node<T> currentIterator = null;
|
||||
|
||||
private class Node<T> {
|
||||
|
||||
private Node<T> next = null;
|
||||
private Node<T> prev = null;
|
||||
private T value;
|
||||
|
||||
public Node(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
private void unlink() {
|
||||
if (!itself()) {
|
||||
next.prev = prev;
|
||||
prev.next = next;
|
||||
}
|
||||
next = null;
|
||||
prev = null;
|
||||
}
|
||||
|
||||
private void link(Node<T> prev) {
|
||||
this.prev = prev;
|
||||
next = prev.next;
|
||||
next.prev = this;
|
||||
prev.next = this;
|
||||
}
|
||||
|
||||
private boolean itself() {
|
||||
return next == prev && next == this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return nodeMap.size();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return nodeMap.isEmpty();
|
||||
}
|
||||
|
||||
public boolean contains(T t) {
|
||||
return nodeMap.containsKey(t);
|
||||
}
|
||||
|
||||
public void add(T t) {
|
||||
if (nodeMap.containsKey(t)) return;
|
||||
Node<T> node = new Node<>(t);
|
||||
nodeMap.put(t, node);
|
||||
if (last == null) {
|
||||
last = node;
|
||||
currentIterator = node;
|
||||
} else {
|
||||
node.link(last);
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(T t) {
|
||||
if (!nodeMap.containsKey(t)) return;
|
||||
Node<T> node = nodeMap.get(t);
|
||||
if (node.itself()) {
|
||||
last = null;
|
||||
currentIterator = null;
|
||||
}
|
||||
if (node == currentIterator) {
|
||||
currentIterator = node.next;
|
||||
}
|
||||
node.unlink();
|
||||
nodeMap.remove(t);
|
||||
}
|
||||
|
||||
public T next() {
|
||||
if (currentIterator == null) return null;
|
||||
T value = currentIterator.value;
|
||||
currentIterator = currentIterator.next;
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
In neuem Issue referenzieren
Einen Benutzer sperren