SteamWar/MissileWars
Archiviert
13
0

Add initial TapeSet

Dieser Commit ist enthalten in:
jojo 2020-09-26 12:20:16 +02:00
Ursprung 4e3ec8c530
Commit c10465bbc1
2 geänderte Dateien mit 92 neuen und 4 gelöschten Zeilen

Datei anzeigen

@ -80,7 +80,7 @@ public class MWTeam {
return spawn;
}
public void join (Player p) {
public void join(Player p) {
players.add(p);
p.teleport(spawn);
p.setGameMode(GameMode.SURVIVAL);
@ -91,9 +91,8 @@ public class MWTeam {
MissileWars.startRound();
}
public void leave (Player p) {
if(!players.contains(p))
return;
public void leave(Player p) {
if (!players.contains(p)) return;
players.remove(p);
sbteam.removePlayer(p);

Datei anzeigen

@ -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;
}
}