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 193 194
| public class LeaderboardTree<Extra extends LeaderboardExtra> {
class LeaderboardNode { public int lowerKey = 0; public int upperKey = 0; public int number = 0;
public ArrayList<Extra> extraList = new ArrayList<Extra>();
public LeaderboardNode left = null; public LeaderboardNode right = null;
public LeaderboardNode prev = null; public LeaderboardNode next = null; }
LeaderboardNode root = null;
LeaderboardNode head = null; LeaderboardNode tail = null;
public void setup(int lowerKey, int upperKey) { root = setupNode(root, lowerKey, upperKey); }
public void insert(int score, Extra extra) { insertIntoNode(root, score, extra); }
public void remove(int score, Extra extra) { removeFromNode(root, score, extra); }
public void change(int oldKey, int newKey, Extra extra) { remove(oldKey, extra); insert(newKey, extra); }
public int getRanking(int score, Extra extra) { return getRankingOfNode(root, score, extra) + 1; }
public ArrayList<LeaderboardData> getTopN(int n) { ArrayList<LeaderboardData> dataList = new ArrayList<LeaderboardData>();
int count = 0; LeaderboardNode cursor = tail; while (cursor != null) { for (Extra extra : cursor.extraList) { LeaderboardData data = new LeaderboardData(); data.ranking = ++count; data.key = cursor.lowerKey; data.extra = extra; dataList.add(data); if (count >= n) { return dataList; } } cursor = cursor.prev; } return dataList; }
private LeaderboardNode setupNode(LeaderboardNode node, int lowerKey, int upperKey) { if (lowerKey > upperKey) { return null; }
node = new LeaderboardNode(); node.lowerKey = lowerKey; node.upperKey = upperKey; node.number = 0; node.extraList.clear();
if (isLeafNode(node)) { if (head == null) { head = node; }
if (tail != null) { tail.next = node; node.prev = tail; }
tail = node; return node; }
if (upperKey > lowerKey) { final int middleKey = getMiddleKey(lowerKey, upperKey); node.left = setupNode(node.left, lowerKey, middleKey); node.right = setupNode(node.right, middleKey + 1, upperKey); }
return node; }
private void insertIntoNode(LeaderboardNode node, int score, Extra extra) { if (node == null) { return; }
if (!isInsideNode(node, score)) { return; }
++node.number;
if (isLeafNode(node)) { node.extraList.add(extra); node.extraList.sort((Extra left, Extra right) -> left.compareTo(right)); return; }
final int middleKey = getMiddleKey(node.lowerKey, node.upperKey); if (score <= middleKey) { insertIntoNode(node.left, score, extra); } else { insertIntoNode(node.right, score, extra); } }
private void removeFromNode(LeaderboardNode node, int score, Extra extra) { if (node == null) { return; }
if (!isInsideNode(node, score)) { return; }
--node.number;
if (isLeafNode(node)) { node.extraList.remove(extra); node.extraList.sort((Extra left, Extra right) -> left.compareTo(right)); return; }
final int middleKey = getMiddleKey(node.lowerKey, node.upperKey); if (score <= middleKey) { removeFromNode(node.left, score, extra); } else { removeFromNode(node.right, score, extra); } }
private int getRankingOfNode(LeaderboardNode node, int score, Extra extra) { int ranking = 0;
if (node == null) { return ranking; }
if (score < node.lowerKey) { ranking += node.number; return ranking; }
if (score > node.upperKey) { ranking += 0; return ranking; }
if (isLeafNode(node)) { ranking += Math.max(node.extraList.indexOf(extra), 0); return ranking; }
final int middleKey = getMiddleKey(node.lowerKey, node.upperKey); if (score <= middleKey) { ranking += node.right != null ? node.right.number : 0; ranking += getRankingOfNode(node.left, score, extra); } else { ranking += getRankingOfNode(node.right, score, extra); }
return ranking; }
private int getMiddleKey(int lowerKey, int upperKey) { final int middleKey = lowerKey + ((upperKey - lowerKey) >> 1); return middleKey; }
private boolean isInsideNode(LeaderboardNode node, int score) { return score >= node.lowerKey && score <= node.upperKey; }
private boolean isLeafNode(LeaderboardNode node) { return node.lowerKey == node.upperKey; } }
|