1 48 49 package org.ozoneDB.collections; 50 51 import java.util.Collection ; 52 import java.util.Comparator ; 53 import java.util.Iterator ; 54 import java.util.NoSuchElementException ; 55 import java.util.Map ; 56 import java.util.Set ; 57 import java.util.SortedMap ; 58 import java.util.TreeMap ; 59 60 112 public abstract class BaseTreeMapImpl extends AbstractOzoneMap implements BaseTreeMap, OzoneSortedMap, Cloneable { 113 120 private static final long serialVersionUID = 1L; 121 122 129 static final BaseTreeMap.Node nilNode = new NilNode(); 130 131 private static final class NilNode implements BaseTreeMap.Node { 132 133 private static final long serialVersionUID = 1L; 134 135 public int getColor() { 136 return BaseTreeMap.Node.BLACK; 137 } 138 139 public Object getKey() { 140 return null; 141 } 142 143 public BaseTreeMap.Node getLeft() { 144 return this; 145 } 146 147 public BaseTreeMap.Node getParent() { 148 return this; 149 } 150 151 public BaseTreeMap.Node getRight() { 152 return this; 153 } 154 155 public Object getValue() { 156 return null; 157 } 158 159 public boolean isNil() { 160 return true; 161 } 162 163 public void setColor(int color) { 164 if (color != BaseTreeMap.Node.BLACK) throw new UnsupportedOperationException (); 165 } 166 167 public void setLeft(BaseTreeMap.Node left) { 168 throw new UnsupportedOperationException (); 169 } 170 171 public void setParent(BaseTreeMap.Node parent) { 172 throw new UnsupportedOperationException (); 173 } 174 175 public void setRight(BaseTreeMap.Node right) { 176 throw new UnsupportedOperationException (); 177 } 178 179 public Object setValue(Object value) { 180 throw new UnsupportedOperationException (); 181 } 182 183 public void setKey(Object key) { 184 throw new UnsupportedOperationException (); 185 } 186 187 public boolean equals(Object o) { 188 return (o instanceof BaseTreeMap.Node) && ((BaseTreeMap.Node) o).isNil(); 189 } 190 }; 191 192 195 protected BaseTreeMap.Node root = nilNode; 196 197 200 protected int size; 201 202 205 private Set entries; 206 207 211 protected int modCount; 212 213 217 private final Comparator comparator; 218 219 228 public BaseTreeMapImpl() { 229 this((Comparator ) null); 230 } 231 232 241 public BaseTreeMapImpl(Comparator c) { 242 comparator = c; 243 } 244 245 259 public BaseTreeMapImpl(Map map) { 260 this((Comparator ) null); 261 putAll(map); 262 } 263 264 272 public BaseTreeMapImpl(SortedMap sm) { 273 this(sm.comparator()); 274 int pos = sm.size(); 275 Iterator itr = sm.entrySet().iterator(); 276 277 _org_ozoneDB_fabricateTree(pos); 278 BaseTreeMap.Node node = _org_ozoneDB_firstNode(); 279 280 while (--pos >= 0) { 281 Map.Entry me = (Map.Entry ) itr.next(); 282 node.setKey(me.getKey()); 283 node.setValue(me.getValue()); 284 node = _org_ozoneDB_successor(node); 285 } 286 } 287 288 public void onCreate() { 289 super.onCreate(); 290 } 291 292 295 public void clear() { 296 if (size > 0) { 297 modCount++; 298 root = nilNode; 299 size = 0; 300 } 301 } 302 303 309 public Object clone() { 310 BaseTreeMap copy = emptyClone(); 311 copy._org_ozoneDB_resetEntries(); 319 copy._org_ozoneDB_fabricateTree(size); 320 321 BaseTreeMap.Node node = _org_ozoneDB_firstNode(); 322 BaseTreeMap.Node cnode = copy._org_ozoneDB_firstNode(); 323 324 while (!node.isNil()) { 325 cnode.setKey(node.getKey()); 326 cnode.setValue(node.getValue()); 327 node = _org_ozoneDB_successor(node); 328 cnode = copy._org_ozoneDB_successor(cnode); 329 } 330 return copy; 331 } 332 333 339 public Comparator comparator() { 340 return comparator; 341 } 342 343 352 public boolean containsKey(Object key) { 353 return !_org_ozoneDB_getNode(key).isNil(); 354 } 355 356 363 public boolean containsValue(Object value) { 364 BaseTreeMap.Node node = _org_ozoneDB_firstNode(); 365 while (!node.isNil()) { 366 if (equals(value, node.getValue())) { 367 return true; 368 } 369 node = _org_ozoneDB_successor(node); 370 } 371 return false; 372 } 373 374 387 public Set entrySet() { 388 if (entries == null) { 389 return ((BaseTreeMap) self())._org_ozoneDB_entrySet(); 390 } 391 return entries; 392 } 393 394 public Set _org_ozoneDB_entrySet() { 395 if (entries == null) { 396 entries = (Set ) database().createObject( 399 _BaseTreeMap_entrySet.class, 400 new Class [] {BaseTreeMap.class}, 401 new Object [] {self()} 402 ); 403 } 404 return entries; 405 } 406 407 413 public Object firstKey() { 414 if (root.isNil()) { 415 throw new NoSuchElementException (); 416 } 417 return _org_ozoneDB_firstNode().getKey(); 418 } 419 420 434 public Object get(Object key) { 435 return _org_ozoneDB_getNode(key).getValue(); 437 } 438 439 454 public SortedMap headMap(Object toKey) { 455 return (SortedMap ) database().createObject( 458 _BaseTreeMap_SubMapImpl.class, 459 new Class [] {BaseTreeMap.class, Object .class, Object .class}, 460 new Object [] {self(), nilNode, toKey} 461 ); 462 } 463 464 473 public Set keySet() { 474 if (keys == null) { 475 return ((BaseTreeMap) self())._org_ozoneDB_keySet(); 478 } 479 return keys; 480 } 481 482 public Set _org_ozoneDB_keySet() { 483 if (keys == null) { 484 keys = (Set ) database().createObject( 487 _BaseTreeMap_keySet.class, 488 new Class [] {BaseTreeMap.class}, 489 new Object [] {self()} 490 ); 491 492 } 493 return keys; 494 } 495 496 502 public Object lastKey() { 503 if (root.isNil()) { 504 throw new NoSuchElementException ("empty"); 505 } 506 return lastNode().getKey(); 507 } 508 509 525 public Object put(Object key, Object value) { 526 BaseTreeMap.Node current = root; 527 BaseTreeMap.Node parent = nilNode; 528 int comparison = 0; 529 530 while (!current.isNil()) { 532 parent = current; 533 comparison = _org_ozoneDB_compare(key, current.getKey()); 534 if (comparison > 0) { 535 current = current.getRight(); 536 } else if (comparison < 0) { 537 current = current.getLeft(); 538 } else { return current.setValue(value); 540 } 541 } 542 543 BaseTreeMap.Node n = newNode(key, value, BaseTreeMap.Node.RED); 545 n.setParent(parent); 546 547 modCount++; 549 size++; 550 if (parent.isNil()) { 551 root = n; 553 return null; 554 } 555 if (comparison > 0) { 556 parent.setRight(n); 557 } else { 558 parent.setLeft(n); 559 } 560 561 insertFixup(n); 563 return null; 564 } 565 566 577 public void putAll(Map m) { 578 Iterator itr = m.entrySet().iterator(); 579 int pos = m.size(); 580 while (--pos >= 0) { 581 Map.Entry e = (Map.Entry ) itr.next(); 582 put(e.getKey(), e.getValue()); 583 } 584 } 585 586 599 public Object remove(Object key) { 600 BaseTreeMap.Node n = _org_ozoneDB_getNode(key); 601 if (n.isNil()) { 602 return null; 603 } 604 Object result = n.getValue(); 606 _org_ozoneDB_removeNode(n); 607 return result; 608 } 609 610 615 public int size() { 616 return size; 617 } 618 619 638 public SortedMap subMap(Object fromKey, Object toKey) { 639 return (SortedMap ) database().createObject( 642 _BaseTreeMap_SubMapImpl.class, 643 new Class [] {BaseTreeMap.class, Object .class, Object .class}, 644 new Object [] {self(), fromKey, toKey} 645 ); 646 } 647 648 663 public SortedMap tailMap(Object fromKey) { 664 return (SortedMap ) database().createObject( 667 _BaseTreeMap_SubMapImpl.class, 668 new Class [] {BaseTreeMap.class, Object .class, Object .class}, 669 new Object [] {self(), fromKey, nilNode} 670 ); 671 } 672 673 683 public Collection values() { 684 if (values == null) { 685 return ((BaseTreeMap) self())._org_ozoneDB_values(); 686 } 687 return values; 688 } 689 690 public Collection _org_ozoneDB_values() { 691 if (values == null) { 692 values = (Collection ) database().createObject( 695 _BaseTreeMap_values.class, 696 new Class [] {BaseTreeMap.class}, 697 new Object [] {self()} 698 ); 699 } 700 return values; 701 } 702 703 715 public final int _org_ozoneDB_compare(Object o1, Object o2) { 716 return comparator == null ? ((Comparable ) o1).compareTo(o2) : comparator.compare(o1, o2); 717 } 718 719 725 private void deleteFixup(BaseTreeMap.Node node, BaseTreeMap.Node parent) { 726 while (!node.equals(root) && (node.getColor() == BaseTreeMap.Node.BLACK)) { 732 if (node.equals(parent.getLeft())) { 733 BaseTreeMap.Node sibling = parent.getRight(); 735 if (sibling.getColor() == BaseTreeMap.Node.RED) { 738 sibling.setColor(BaseTreeMap.Node.BLACK); 741 parent.setColor(BaseTreeMap.Node.RED); 742 rotateLeft(parent); 743 sibling = parent.getRight(); 744 } 745 746 if ((sibling.getLeft().getColor() == BaseTreeMap.Node.BLACK) && (sibling.getRight().getColor() == BaseTreeMap.Node.BLACK)) { 747 sibling.setColor(BaseTreeMap.Node.RED); 750 node = parent; 751 parent = parent.getParent(); 752 } else { 753 if (sibling.getRight().getColor() == BaseTreeMap.Node.BLACK) { 754 sibling.getLeft().setColor(BaseTreeMap.Node.BLACK); 757 sibling.setColor(BaseTreeMap.Node.RED); 758 rotateRight(sibling); 759 sibling = parent.getRight(); 760 } 761 sibling.setColor(parent.getColor()); 764 parent.setColor(BaseTreeMap.Node.BLACK); 765 sibling.getRight().setColor(BaseTreeMap.Node.BLACK); 766 rotateLeft(parent); 767 node = root; } 769 } else { 770 BaseTreeMap.Node sibling = parent.getLeft(); 772 if (sibling.getColor() == BaseTreeMap.Node.RED) { 775 sibling.setColor(BaseTreeMap.Node.BLACK); 778 parent.setColor(BaseTreeMap.Node.RED); 779 rotateRight(parent); 780 sibling = parent.getLeft(); 781 } 782 783 if ((sibling.getRight().getColor() == BaseTreeMap.Node.BLACK) && (sibling.getLeft().getColor() == BaseTreeMap.Node.BLACK)) { 784 sibling.setColor(BaseTreeMap.Node.RED); 787 node = parent; 788 parent = parent.getParent(); 789 } else { 790 if (sibling.getLeft().getColor() == BaseTreeMap.Node.BLACK) { 791 sibling.getRight().setColor(BaseTreeMap.Node.BLACK); 794 sibling.setColor(BaseTreeMap.Node.RED); 795 rotateLeft(sibling); 796 sibling = parent.getLeft(); 797 } 798 sibling.setColor(parent.getColor()); 801 parent.setColor(BaseTreeMap.Node.BLACK); 802 sibling.getLeft().setColor(BaseTreeMap.Node.BLACK); 803 rotateRight(parent); 804 node = root; } 806 } 807 } 808 node.setColor(BaseTreeMap.Node.BLACK); 809 } 810 811 822 public void _org_ozoneDB_fabricateTree(final int count) { 823 if (count == 0) { 824 return; 825 } 826 827 832 root = newNode(null, null, BaseTreeMap.Node.BLACK); 834 size = count; 835 BaseTreeMap.Node row = root; 836 int rowsize; 837 838 for (rowsize = 2; rowsize + rowsize <= count; rowsize <<= 1) { 840 BaseTreeMap.Node parent = row; 841 BaseTreeMap.Node last = null; 842 for (int i = 0; i < rowsize; i += 2) { 843 BaseTreeMap.Node left = newNode(null, null, BaseTreeMap.Node.BLACK); 844 BaseTreeMap.Node right = newNode(null, null, BaseTreeMap.Node.BLACK); 845 left.setParent(parent); 846 left.setRight(right); 847 right.setParent(parent); 848 parent.setLeft(left); 849 BaseTreeMap.Node next = parent.getRight(); 850 parent.setRight(right); 851 parent = next; 852 if (last != null) { 853 last.setRight(left); 854 } 855 last = right; 856 } 857 row = row.getLeft(); 858 } 859 860 int overflow = count - rowsize; 862 BaseTreeMap.Node parent = row; 863 int i; 864 for (i = 0; i < overflow; i += 2) { 865 BaseTreeMap.Node left = newNode(null, null, BaseTreeMap.Node.RED); 866 BaseTreeMap.Node right = newNode(null, null, BaseTreeMap.Node.RED); 867 left.setParent(parent); 868 right.setParent(parent); 869 parent.setLeft(left); 870 BaseTreeMap.Node next = parent.getRight(); 871 parent.setRight(right); 872 parent = next; 873 } 874 if (i - overflow == 0) { 876 BaseTreeMap.Node left = newNode(null, null, BaseTreeMap.Node.RED); 877 left.setParent(parent); 878 parent.setLeft(left); 879 parent = parent.getRight(); 880 left.getParent().setRight(nilNode); 881 } 882 while (!parent.isNil()) { 884 BaseTreeMap.Node next = parent.getRight(); 885 parent.setRight(nilNode); 886 parent = next; 887 } 888 } 889 890 901 public final BaseTreeMap.Node _org_ozoneDB_firstNode() { 902 BaseTreeMap.Node node = root; 904 while (!node.getLeft().isNil()) { 905 node = node.getLeft(); 906 } 907 return node; 908 } 909 910 922 public final BaseTreeMap.Node _org_ozoneDB_getNode(Object key) { 923 BaseTreeMap.Node current = root; 924 while (!current.isNil()) { 925 int comparison = _org_ozoneDB_compare(key, current.getKey()); 926 if (comparison > 0) { 927 current = current.getRight(); 928 } else if (comparison < 0) { 929 current = current.getLeft(); 930 } else { 931 return current; 932 } 933 } 934 return current; 935 } 936 937 949 public final BaseTreeMap.Node _org_ozoneDB_highestLessThan(Object key) { 950 if ((key instanceof BaseTreeMap.Node) && ((BaseTreeMap.Node) key).isNil()) { 951 return lastNode(); 952 } 953 954 BaseTreeMap.Node last = nilNode; 955 BaseTreeMap.Node current = root; 956 int comparison = 0; 957 958 while (!current.isNil()) { 959 last = current; 960 comparison = _org_ozoneDB_compare(key, current.getKey()); 961 if (comparison > 0) { 962 current = current.getRight(); 963 } else if (comparison < 0) { 964 current = current.getLeft(); 965 } else { return predecessor(last); 967 } 968 } 969 return comparison <= 0 ? predecessor(last) : last; 970 } 971 972 977 private void insertFixup(BaseTreeMap.Node n) { 978 while (n.getParent().getColor() == BaseTreeMap.Node.RED && !n.getParent().getParent().isNil()) { 982 if (n.getParent().equals(n.getParent().getParent().getLeft())) { 983 BaseTreeMap.Node uncle = n.getParent().getParent().getRight(); 984 if (uncle.getColor() == BaseTreeMap.Node.RED) { 986 n.getParent().setColor(BaseTreeMap.Node.BLACK); 989 uncle.setColor(BaseTreeMap.Node.BLACK); 990 uncle.getParent().setColor(BaseTreeMap.Node.RED); 991 n = uncle.getParent(); 992 } else { 993 if (n.equals(n.getParent().getRight())) { 994 n = n.getParent(); 997 rotateLeft(n); 998 } 999 n.getParent().setColor(BaseTreeMap.Node.BLACK); 1002 n.getParent().getParent().setColor(BaseTreeMap.Node.RED); 1003 rotateRight(n.getParent().getParent()); 1004 } 1005 } else { 1006 BaseTreeMap.Node uncle = n.getParent().getParent().getLeft(); 1008 if (uncle.getColor() == BaseTreeMap.Node.RED) { 1010 n.getParent().setColor(BaseTreeMap.Node.BLACK); 1013 uncle.setColor(BaseTreeMap.Node.BLACK); 1014 uncle.getParent().setColor(BaseTreeMap.Node.RED); 1015 n = uncle.getParent(); 1016 } else { 1017 if (n.equals(n.getParent().getLeft())) { 1018 n = n.getParent(); 1021 rotateRight(n); 1022 } 1023 n.getParent().setColor(BaseTreeMap.Node.BLACK); 1026 n.getParent().getParent().setColor(BaseTreeMap.Node.RED); 1027 rotateLeft(n.getParent().getParent()); 1028 } 1029 } 1030 } 1031 root.setColor(BaseTreeMap.Node.BLACK); 1032 } 1033 1034 1039 private BaseTreeMap.Node lastNode() { 1040 BaseTreeMap.Node node = root; 1042 while (!node.getRight().isNil()) { 1043 node = node.getRight(); 1044 } 1045 return node; 1046 } 1047 1048 1062 public final BaseTreeMap.Node _org_ozoneDB_lowestGreaterThan(Object key, boolean first) { 1063 if ((key instanceof BaseTreeMap.Node) && ((BaseTreeMap.Node) key).isNil()) { 1064 return first ? _org_ozoneDB_firstNode() : nilNode; 1065 } 1066 1067 BaseTreeMap.Node last = nilNode; 1068 BaseTreeMap.Node current = root; 1069 int comparison = 0; 1070 1071 while (!current.isNil()) { 1072 last = current; 1073 comparison = _org_ozoneDB_compare(key, current.getKey()); 1074 if (comparison > 0) { 1075 current = current.getRight(); 1076 } else if (comparison < 0) { 1077 current = current.getLeft(); 1078 } else { 1079 return current; 1080 } 1081 } 1082 return comparison > 0 ? _org_ozoneDB_successor(last) : last; 1083 } 1084 1085 1091 private BaseTreeMap.Node predecessor(BaseTreeMap.Node node) { 1092 if (!node.getLeft().isNil()) { 1093 node = node.getLeft(); 1094 while (!node.getRight().isNil()) { 1095 node = node.getRight(); 1096 } 1097 return node; 1098 } 1099 1100 BaseTreeMap.Node parent = node.getParent(); 1101 while (node.equals(parent.getLeft())) { 1103 node = parent; 1104 parent = node.getParent(); 1105 } 1106 return parent; 1107 } 1108 1109 1122 public final void _org_ozoneDB_putKeysLinear(Iterator keys, int count) { 1123 _org_ozoneDB_fabricateTree(count); 1124 BaseTreeMap.Node node = _org_ozoneDB_firstNode(); 1125 1126 while (--count >= 0) { 1127 node.setKey(keys.next()); 1128 node.setValue(""); 1129 node = _org_ozoneDB_successor(node); 1130 } 1131 } 1132 1133 1144 public final void _org_ozoneDB_removeNode(BaseTreeMap.Node node) { 1145 BaseTreeMap.Node splice; 1146 BaseTreeMap.Node child; 1147 1148 modCount++; 1149 size--; 1150 1151 if (node.getLeft().isNil()) { 1153 splice = node; 1155 child = node.getRight(); 1156 } else if (node.getRight().isNil()) { 1157 splice = node; 1159 child = node.getLeft(); 1160 } else { 1161 splice = node.getLeft(); 1164 while (!splice.getRight().isNil()) { 1165 splice = splice.getRight(); 1166 } 1167 child = splice.getLeft(); 1168 node.setKey(splice.getKey()); 1169 node.setValue(splice.getValue()); 1170 } 1171 1172 BaseTreeMap.Node parent = splice.getParent(); 1174 if (!child.isNil()) { 1175 child.setParent(parent); 1176 } 1177 if (parent.isNil()) { 1178 root = child; 1180 return; 1181 } 1182 if (splice.equals(parent.getLeft())) { 1183 parent.setLeft(child); 1184 } else { 1185 parent.setRight(child); 1186 } 1187 1188 if (splice.getColor() == BaseTreeMap.Node.BLACK) { 1189 deleteFixup(child, parent); 1190 } 1191 } 1192 1193 1198 private void rotateLeft(BaseTreeMap.Node node) { 1199 BaseTreeMap.Node child = node.getRight(); 1200 1203 node.setRight(child.getLeft()); 1205 if (!child.getLeft().isNil()) { 1206 child.getLeft().setParent(node); 1207 } 1208 1209 child.setParent(node.getParent()); 1211 if (!node.getParent().isNil()) { 1212 if (node.equals(node.getParent().getLeft())) { 1213 node.getParent().setLeft(child); 1214 } else { 1215 node.getParent().setRight(child); 1216 } 1217 } else { 1218 root = child; 1219 } 1220 1221 child.setLeft(node); 1223 node.setParent(child); 1224 } 1225 1226 1231 private void rotateRight(BaseTreeMap.Node node) { 1232 BaseTreeMap.Node child = node.getLeft(); 1233 1236 node.setLeft(child.getRight()); 1238 if (!child.getRight().isNil()) { 1239 child.getRight().setParent(node); 1240 } 1241 1242 child.setParent(node.getParent()); 1244 if (!node.getParent().isNil()) { 1245 if (node.equals(node.getParent().getRight())) { 1246 node.getParent().setRight(child); 1247 } else { 1248 node.getParent().setLeft(child); 1249 } 1250 } else { 1251 root = child; 1252 } 1253 1254 child.setRight(node); 1256 node.setParent(child); 1257 } 1258 1259 1271 public final BaseTreeMap.Node _org_ozoneDB_successor(BaseTreeMap.Node node) { 1272 if (!node.getRight().isNil()) { 1273 node = node.getRight(); 1274 while (!node.getLeft().isNil()) { 1275 node = node.getLeft(); 1276 } 1277 return node; 1278 } 1279 1280 BaseTreeMap.Node parent = node.getParent(); 1281 1282 while (node == parent.getRight() || node.equals(parent.getRight())) { 1290 node = parent; 1291 parent = parent.getParent(); 1292 } 1293 return parent; 1294 } 1295 1296 1303 public SortedMap getClientSortedMap() { 1304 return getClientTreeMap(); 1305 } 1306 1307 1314 public Map getClientMap() { 1315 return getClientTreeMap(); 1316 } 1317 1318 1325 public TreeMap getClientTreeMap() { 1326 TreeMap result = new TreeMap (comparator()); 1330 for (Iterator i = ((OzoneSet) entrySet())._org_ozoneDB_internalIterator(); i.hasNext(); ) { 1331 Map.Entry entry = (Map.Entry ) i.next(); 1332 result.put(entry.getKey(), entry.getValue()); 1333 } 1334 return result; 1335 } 1336 1337 public void _org_ozoneDB_resetEntries() { 1338 this.entries = null; 1339 } 1340 1341 1346 public int _org_ozoneDB_getModification() { 1347 return modCount; 1348 } 1349 1350 1355 public OzoneSortedMap ozoneHeadMap(Object toKey) { 1356 return (OzoneSortedMap) headMap(toKey); 1357 } 1358 1359 1364 public OzoneSortedMap ozoneSubMap(Object fromKey, Object toKey) { 1365 return (OzoneSortedMap) subMap(fromKey, toKey); 1366 } 1367 1368 1373 public OzoneSortedMap ozoneTailMap(Object toKey) { 1374 return (OzoneSortedMap) tailMap(toKey); 1375 } 1376 1377 protected abstract BaseTreeMap.Node newNode(Object key, Object value, int color); 1378 1379 protected abstract BaseTreeMap emptyClone(); 1380 1381} | Popular Tags |