1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
| import java.util.Comparator;
@SuppressWarnings("unchecked") public class SkipList<K, V> { private static final int MAX_LEVEL = 32; private static final double P = 0.25; private int size; private Comparator<K> comparator;
private int level;
private Node<K, V> first; public SkipList(Comparator<K> comparator) { this.comparator = comparator; first = new Node<>(null, null, MAX_LEVEL); } public SkipList() { this(null); } public int size() { return size; } public boolean isEmpty() { return size == 0; } public V get(K key) { keyCheck(key); Node<K, V> node = first; for (int i = level - 1; i >= 0; i--) { int cmp = -1; while (node.nexts[i] != null && (cmp = compare(key, node.nexts[i].key)) > 0) { node = node.nexts[i]; } if (cmp == 0) return node.nexts[i].value; } return null; } public V put(K key, V value) { keyCheck(key); Node<K, V> node = first; Node<K, V>[] prevs = new Node[level]; for (int i = level - 1; i >= 0; i--) { int cmp = -1; while (node.nexts[i] != null && (cmp = compare(key, node.nexts[i].key)) > 0) { node = node.nexts[i]; } if (cmp == 0) { V oldV = node.nexts[i].value; node.nexts[i].value = value; return oldV; } prevs[i] = node; } int newLevel = randomLevel(); Node<K, V> newNode = new Node<>(key, value, newLevel); for (int i = 0; i < newLevel; i++) { if (i >= level) { first.nexts[i] = newNode; } else { newNode.nexts[i] = prevs[i].nexts[i]; prevs[i].nexts[i] = newNode; } } size++; level = Math.max(level, newLevel); return null; } public V remove(K key) { keyCheck(key); Node<K, V> node = first; Node<K, V>[] prevs = new Node[level]; boolean exist = false; for (int i = level - 1; i >= 0; i--) { int cmp = -1; while (node.nexts[i] != null && (cmp = compare(key, node.nexts[i].key)) > 0) { node = node.nexts[i]; } prevs[i] = node; if (cmp == 0) exist = true; } if (!exist) return null; Node<K, V> removedNode = node.nexts[0]; size--; for (int i = 0; i < removedNode.nexts.length; i++) { prevs[i].nexts[i] = removedNode.nexts[i]; } int newLevel = level; while (--newLevel >= 0 && first.nexts[newLevel] == null) { level = newLevel; } return removedNode.value; } private int randomLevel() { int level = 1; while (Math.random() < P && level < MAX_LEVEL) { level++; } return level; } private void keyCheck(K key) { if (key == null) { throw new IllegalArgumentException("key must not be null."); } } private int compare(K k1, K k2) { return comparator != null ? comparator.compare(k1, k2) : ((Comparable<K>)k1).compareTo(k2); } private static class Node<K, V> { K key; V value; Node<K, V>[] nexts;
public Node(K key, V value, int level) { this.key = key; this.value = value; nexts = new Node[level]; } @Override public String toString() { return key + ":" + value + "_" + nexts.length; } } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("一共" + level + "层").append("\n"); for (int i = level - 1; i >= 0; i--) { Node<K, V> node = first; while (node.nexts[i] != null) { sb.append(node.nexts[i]); sb.append(" "); node = node.nexts[i]; } sb.append("\n"); } return sb.toString(); } }
|