1 2 17 18 19 package org.apache.poi.util; 20 21 import java.util.*; 22 23 91 public final class BinaryTree 93 extends AbstractMap 94 { 95 private Node[] _root = new Node[] 96 { 97 null, null 98 }; 99 private int _size = 0; 100 private int _modifications = 0; 101 private Set[] _key_set = new Set[] 102 { 103 null, null 104 }; 105 private Set[] _entry_set = new Set[] 106 { 107 null, null 108 }; 109 private Collection[] _value_collection = new Collection[] 110 { 111 null, null 112 }; 113 private static final int _KEY = 0; 114 private static final int _VALUE = 1; 115 private static final int _INDEX_SUM = _KEY + _VALUE; 116 private static final int _MINIMUM_INDEX = 0; 117 private static final int _INDEX_COUNT = 2; 118 private static final String [] _data_name = new String [] 119 { 120 "key", "value" 121 }; 122 123 126 127 public BinaryTree() 128 { 129 } 130 131 148 149 public BinaryTree(final Map map) 150 throws ClassCastException , NullPointerException , 151 IllegalArgumentException 152 { 153 putAll(map); 154 } 155 156 169 170 public Object getKeyForValue(final Object value) 171 throws ClassCastException , NullPointerException 172 { 173 return doGet(( Comparable ) value, _VALUE); 174 } 175 176 184 185 public Object removeValue(final Object value) 186 { 187 return doRemove(( Comparable ) value, _VALUE); 188 } 189 190 209 210 public Set entrySetByValue() 211 { 212 if (_entry_set[ _VALUE ] == null) 213 { 214 _entry_set[ _VALUE ] = new AbstractSet() 215 { 216 public Iterator iterator() 217 { 218 return new BinaryTreeIterator(_VALUE) 219 { 220 protected Object doGetNext() 221 { 222 return _last_returned_node; 223 } 224 }; 225 } 226 227 public boolean contains(Object o) 228 { 229 if (!(o instanceof Map.Entry)) 230 { 231 return false; 232 } 233 Map.Entry entry = ( Map.Entry ) o; 234 Object key = entry.getKey(); 235 Node node = lookup(( Comparable ) entry.getValue(), 236 _VALUE); 237 238 return (node != null) && node.getData(_KEY).equals(key); 239 } 240 241 public boolean remove(Object o) 242 { 243 if (!(o instanceof Map.Entry)) 244 { 245 return false; 246 } 247 Map.Entry entry = ( Map.Entry ) o; 248 Object key = entry.getKey(); 249 Node node = lookup(( Comparable ) entry.getValue(), 250 _VALUE); 251 252 if ((node != null) && node.getData(_KEY).equals(key)) 253 { 254 doRedBlackDelete(node); 255 return true; 256 } 257 return false; 258 } 259 260 public int size() 261 { 262 return BinaryTree.this.size(); 263 } 264 265 public void clear() 266 { 267 BinaryTree.this.clear(); 268 } 269 }; 270 } 271 return _entry_set[ _VALUE ]; 272 } 273 274 292 293 public Set keySetByValue() 294 { 295 if (_key_set[ _VALUE ] == null) 296 { 297 _key_set[ _VALUE ] = new AbstractSet() 298 { 299 public Iterator iterator() 300 { 301 return new BinaryTreeIterator(_VALUE) 302 { 303 protected Object doGetNext() 304 { 305 return _last_returned_node.getData(_KEY); 306 } 307 }; 308 } 309 310 public int size() 311 { 312 return BinaryTree.this.size(); 313 } 314 315 public boolean contains(Object o) 316 { 317 return containsKey(o); 318 } 319 320 public boolean remove(Object o) 321 { 322 int old_size = _size; 323 324 BinaryTree.this.remove(o); 325 return _size != old_size; 326 } 327 328 public void clear() 329 { 330 BinaryTree.this.clear(); 331 } 332 }; 333 } 334 return _key_set[ _VALUE ]; 335 } 336 337 355 356 public Collection valuesByValue() 357 { 358 if (_value_collection[ _VALUE ] == null) 359 { 360 _value_collection[ _VALUE ] = new AbstractCollection() 361 { 362 public Iterator iterator() 363 { 364 return new BinaryTreeIterator(_VALUE) 365 { 366 protected Object doGetNext() 367 { 368 return _last_returned_node.getData(_VALUE); 369 } 370 }; 371 } 372 373 public int size() 374 { 375 return BinaryTree.this.size(); 376 } 377 378 public boolean contains(Object o) 379 { 380 return containsValue(o); 381 } 382 383 public boolean remove(Object o) 384 { 385 int old_size = _size; 386 387 removeValue(o); 388 return _size != old_size; 389 } 390 391 public boolean removeAll(Collection c) 392 { 393 boolean modified = false; 394 Iterator iter = c.iterator(); 395 396 while (iter.hasNext()) 397 { 398 if (removeValue(iter.next()) != null) 399 { 400 modified = true; 401 } 402 } 403 return modified; 404 } 405 406 public void clear() 407 { 408 BinaryTree.this.clear(); 409 } 410 }; 411 } 412 return _value_collection[ _VALUE ]; 413 } 414 415 425 426 private Object doRemove(final Comparable o, final int index) 427 { 428 Node node = lookup(o, index); 429 Object rval = null; 430 431 if (node != null) 432 { 433 rval = node.getData(oppositeIndex(index)); 434 doRedBlackDelete(node); 435 } 436 return rval; 437 } 438 439 449 450 private Object doGet(final Comparable o, final int index) 451 { 452 checkNonNullComparable(o, index); 453 Node node = lookup(o, index); 454 455 return ((node == null) ? null 456 : node.getData(oppositeIndex(index))); 457 } 458 459 466 467 private int oppositeIndex(final int index) 468 { 469 470 return _INDEX_SUM - index; 474 } 475 476 485 486 private Node lookup(final Comparable data, final int index) 487 { 488 Node rval = null; 489 Node node = _root[ index ]; 490 491 while (node != null) 492 { 493 int cmp = compare(data, node.getData(index)); 494 495 if (cmp == 0) 496 { 497 rval = node; 498 break; 499 } 500 else 501 { 502 node = (cmp < 0) ? node.getLeft(index) 503 : node.getRight(index); 504 } 505 } 506 return rval; 507 } 508 509 518 519 private static int compare(final Comparable o1, final Comparable o2) 520 { 521 return (( Comparable ) o1).compareTo(o2); 522 } 523 524 534 535 private static Node leastNode(final Node node, final int index) 536 { 537 Node rval = node; 538 539 if (rval != null) 540 { 541 while (rval.getLeft(index) != null) 542 { 543 rval = rval.getLeft(index); 544 } 545 } 546 return rval; 547 } 548 549 557 558 private Node nextGreater(final Node node, final int index) 559 { 560 Node rval = null; 561 562 if (node == null) 563 { 564 rval = null; 565 } 566 else if (node.getRight(index) != null) 567 { 568 569 rval = leastNode(node.getRight(index), index); 572 } 573 else 574 { 575 576 Node parent = node.getParent(index); 583 Node child = node; 584 585 while ((parent != null) && (child == parent.getRight(index))) 586 { 587 child = parent; 588 parent = parent.getParent(index); 589 } 590 rval = parent; 591 } 592 return rval; 593 } 594 595 603 604 private static void copyColor(final Node from, final Node to, 605 final int index) 606 { 607 if (to != null) 608 { 609 if (from == null) 610 { 611 612 to.setBlack(index); 614 } 615 else 616 { 617 to.copyColor(from, index); 618 } 619 } 620 } 621 622 629 630 private static boolean isRed(final Node node, final int index) 631 { 632 return ((node == null) ? false 633 : node.isRed(index)); 634 } 635 636 643 644 private static boolean isBlack(final Node node, final int index) 645 { 646 return ((node == null) ? true 647 : node.isBlack(index)); 648 } 649 650 656 657 private static void makeRed(final Node node, final int index) 658 { 659 if (node != null) 660 { 661 node.setRed(index); 662 } 663 } 664 665 671 672 private static void makeBlack(final Node node, final int index) 673 { 674 if (node != null) 675 { 676 node.setBlack(index); 677 } 678 } 679 680 687 688 private static Node getGrandParent(final Node node, final int index) 689 { 690 return getParent(getParent(node, index), index); 691 } 692 693 700 701 private static Node getParent(final Node node, final int index) 702 { 703 return ((node == null) ? null 704 : node.getParent(index)); 705 } 706 707 714 715 private static Node getRightChild(final Node node, final int index) 716 { 717 return (node == null) ? null 718 : node.getRight(index); 719 } 720 721 728 729 private static Node getLeftChild(final Node node, final int index) 730 { 731 return (node == null) ? null 732 : node.getLeft(index); 733 } 734 735 746 747 private static boolean isLeftChild(final Node node, final int index) 748 { 749 return (node == null) ? true 750 : ((node.getParent(index) == null) ? false 751 : (node 752 == node.getParent( 753 index).getLeft( 754 index))); 755 } 756 757 768 769 private static boolean isRightChild(final Node node, final int index) 770 { 771 return (node == null) ? true 772 : ((node.getParent(index) == null) ? false 773 : (node 774 == node.getParent( 775 index).getRight( 776 index))); 777 } 778 779 785 786 private void rotateLeft(final Node node, final int index) 787 { 788 Node right_child = node.getRight(index); 789 790 node.setRight(right_child.getLeft(index), index); 791 if (right_child.getLeft(index) != null) 792 { 793 right_child.getLeft(index).setParent(node, index); 794 } 795 right_child.setParent(node.getParent(index), index); 796 if (node.getParent(index) == null) 797 { 798 799 _root[ index ] = right_child; 801 } 802 else if (node.getParent(index).getLeft(index) == node) 803 { 804 node.getParent(index).setLeft(right_child, index); 805 } 806 else 807 { 808 node.getParent(index).setRight(right_child, index); 809 } 810 right_child.setLeft(node, index); 811 node.setParent(right_child, index); 812 } 813 814 820 821 private void rotateRight(final Node node, final int index) 822 { 823 Node left_child = node.getLeft(index); 824 825 node.setLeft(left_child.getRight(index), index); 826 if (left_child.getRight(index) != null) 827 { 828 left_child.getRight(index).setParent(node, index); 829 } 830 left_child.setParent(node.getParent(index), index); 831 if (node.getParent(index) == null) 832 { 833 834 _root[ index ] = left_child; 836 } 837 else if (node.getParent(index).getRight(index) == node) 838 { 839 node.getParent(index).setRight(left_child, index); 840 } 841 else 842 { 843 node.getParent(index).setLeft(left_child, index); 844 } 845 left_child.setRight(node, index); 846 node.setParent(left_child, index); 847 } 848 849 856 857 private void doRedBlackInsert(final Node inserted_node, final int index) 858 { 859 Node current_node = inserted_node; 860 861 makeRed(current_node, index); 862 while ((current_node != null) && (current_node != _root[ index ]) 863 && (isRed(current_node.getParent(index), index))) 864 { 865 if (isLeftChild(getParent(current_node, index), index)) 866 { 867 Node y = getRightChild(getGrandParent(current_node, index), 868 index); 869 870 if (isRed(y, index)) 871 { 872 makeBlack(getParent(current_node, index), index); 873 makeBlack(y, index); 874 makeRed(getGrandParent(current_node, index), index); 875 current_node = getGrandParent(current_node, index); 876 } 877 else 878 { 879 if (isRightChild(current_node, index)) 880 { 881 current_node = getParent(current_node, index); 882 rotateLeft(current_node, index); 883 } 884 makeBlack(getParent(current_node, index), index); 885 makeRed(getGrandParent(current_node, index), index); 886 if (getGrandParent(current_node, index) != null) 887 { 888 rotateRight(getGrandParent(current_node, index), 889 index); 890 } 891 } 892 } 893 else 894 { 895 896 Node y = getLeftChild(getGrandParent(current_node, index), 898 index); 899 900 if (isRed(y, index)) 901 { 902 makeBlack(getParent(current_node, index), index); 903 makeBlack(y, index); 904 makeRed(getGrandParent(current_node, index), index); 905 current_node = getGrandParent(current_node, index); 906 } 907 else 908 { 909 if (isLeftChild(current_node, index)) 910 { 911 current_node = getParent(current_node, index); 912 rotateRight(current_node, index); 913 } 914 makeBlack(getParent(current_node, index), index); 915 makeRed(getGrandParent(current_node, index), index); 916 if (getGrandParent(current_node, index) != null) 917 { 918 rotateLeft(getGrandParent(current_node, index), 919 index); 920 } 921 } 922 } 923 } 924 makeBlack(_root[ index ], index); 925 } 926 927 933 934 private void doRedBlackDelete(final Node deleted_node) 935 { 936 for (int index = _MINIMUM_INDEX; index < _INDEX_COUNT; index++) 937 { 938 939 if ((deleted_node.getLeft(index) != null) 942 && (deleted_node.getRight(index) != null)) 943 { 944 swapPosition(nextGreater(deleted_node, index), deleted_node, 945 index); 946 } 947 Node replacement = ((deleted_node.getLeft(index) != null) 948 ? deleted_node.getLeft(index) 949 : deleted_node.getRight(index)); 950 951 if (replacement != null) 952 { 953 replacement.setParent(deleted_node.getParent(index), index); 954 if (deleted_node.getParent(index) == null) 955 { 956 _root[ index ] = replacement; 957 } 958 else if (deleted_node 959 == deleted_node.getParent(index).getLeft(index)) 960 { 961 deleted_node.getParent(index).setLeft(replacement, index); 962 } 963 else 964 { 965 deleted_node.getParent(index).setRight(replacement, 966 index); 967 } 968 deleted_node.setLeft(null, index); 969 deleted_node.setRight(null, index); 970 deleted_node.setParent(null, index); 971 if (isBlack(deleted_node, index)) 972 { 973 doRedBlackDeleteFixup(replacement, index); 974 } 975 } 976 else 977 { 978 979 if (deleted_node.getParent(index) == null) 981 { 982 983 _root[ index ] = null; 985 } 986 else 987 { 988 989 if (isBlack(deleted_node, index)) 991 { 992 doRedBlackDeleteFixup(deleted_node, index); 993 } 994 if (deleted_node.getParent(index) != null) 995 { 996 if (deleted_node 997 == deleted_node.getParent(index) 998 .getLeft(index)) 999 { 1000 deleted_node.getParent(index).setLeft(null, 1001 index); 1002 } 1003 else 1004 { 1005 deleted_node.getParent(index).setRight(null, 1006 index); 1007 } 1008 deleted_node.setParent(null, index); 1009 } 1010 } 1011 } 1012 } 1013 shrink(); 1014 } 1015 1016 1025 1026 private void doRedBlackDeleteFixup(final Node replacement_node, 1027 final int index) 1028 { 1029 Node current_node = replacement_node; 1030 1031 while ((current_node != _root[ index ]) 1032 && (isBlack(current_node, index))) 1033 { 1034 if (isLeftChild(current_node, index)) 1035 { 1036 Node sibling_node = 1037 getRightChild(getParent(current_node, index), index); 1038 1039 if (isRed(sibling_node, index)) 1040 { 1041 makeBlack(sibling_node, index); 1042 makeRed(getParent(current_node, index), index); 1043 rotateLeft(getParent(current_node, index), index); 1044 sibling_node = 1045 getRightChild(getParent(current_node, index), index); 1046 } 1047 if (isBlack(getLeftChild(sibling_node, index), index) 1048 && isBlack(getRightChild(sibling_node, index), index)) 1049 { 1050 makeRed(sibling_node, index); 1051 current_node = getParent(current_node, index); 1052 } 1053 else 1054 { 1055 if (isBlack(getRightChild(sibling_node, index), index)) 1056 { 1057 makeBlack(getLeftChild(sibling_node, index), index); 1058 makeRed(sibling_node, index); 1059 rotateRight(sibling_node, index); 1060 sibling_node = 1061 getRightChild(getParent(current_node, index), 1062 index); 1063 } 1064 copyColor(getParent(current_node, index), sibling_node, 1065 index); 1066 makeBlack(getParent(current_node, index), index); 1067 makeBlack(getRightChild(sibling_node, index), index); 1068 rotateLeft(getParent(current_node, index), index); 1069 current_node = _root[ index ]; 1070 } 1071 } 1072 else 1073 { 1074 Node sibling_node = 1075 getLeftChild(getParent(current_node, index), index); 1076 1077 if (isRed(sibling_node, index)) 1078 { 1079 makeBlack(sibling_node, index); 1080 makeRed(getParent(current_node, index), index); 1081 rotateRight(getParent(current_node, index), index); 1082 sibling_node = 1083 getLeftChild(getParent(current_node, index), index); 1084 } 1085 if (isBlack(getRightChild(sibling_node, index), index) 1086 && isBlack(getLeftChild(sibling_node, index), index)) 1087 { 1088 makeRed(sibling_node, index); 1089 current_node = getParent(current_node, index); 1090 } 1091 else 1092 { 1093 if (isBlack(getLeftChild(sibling_node, index), index)) 1094 { 1095 makeBlack(getRightChild(sibling_node, index), index); 1096 makeRed(sibling_node, index); 1097 rotateLeft(sibling_node, index); 1098 sibling_node = 1099 getLeftChild(getParent(current_node, index), 1100 index); 1101 } 1102 copyColor(getParent(current_node, index), sibling_node, 1103 index); 1104 makeBlack(getParent(current_node, index), index); 1105 makeBlack(getLeftChild(sibling_node, index), index); 1106 rotateRight(getParent(current_node, index), index); 1107 current_node = _root[ index ]; 1108 } 1109 } 1110 } 1111 makeBlack(current_node, index); 1112 } 1113 1114 1123 1124 private void swapPosition(final Node x, final Node y, final int index) 1125 { 1126 1127 Node x_old_parent = x.getParent(index); 1129 Node x_old_left_child = x.getLeft(index); 1130 Node x_old_right_child = x.getRight(index); 1131 Node y_old_parent = y.getParent(index); 1132 Node y_old_left_child = y.getLeft(index); 1133 Node y_old_right_child = y.getRight(index); 1134 boolean x_was_left_child = 1135 (x.getParent(index) != null) 1136 && (x == x.getParent(index).getLeft(index)); 1137 boolean y_was_left_child = 1138 (y.getParent(index) != null) 1139 && (y == y.getParent(index).getLeft(index)); 1140 1141 if (x == y_old_parent) 1143 { x.setParent(y, index); 1145 if (y_was_left_child) 1146 { 1147 y.setLeft(x, index); 1148 y.setRight(x_old_right_child, index); 1149 } 1150 else 1151 { 1152 y.setRight(x, index); 1153 y.setLeft(x_old_left_child, index); 1154 } 1155 } 1156 else 1157 { 1158 x.setParent(y_old_parent, index); 1159 if (y_old_parent != null) 1160 { 1161 if (y_was_left_child) 1162 { 1163 y_old_parent.setLeft(x, index); 1164 } 1165 else 1166 { 1167 y_old_parent.setRight(x, index); 1168 } 1169 } 1170 y.setLeft(x_old_left_child, index); 1171 y.setRight(x_old_right_child, index); 1172 } 1173 if (y == x_old_parent) 1174 { y.setParent(x, index); 1176 if (x_was_left_child) 1177 { 1178 x.setLeft(y, index); 1179 x.setRight(y_old_right_child, index); 1180 } 1181 else 1182 { 1183 x.setRight(y, index); 1184 x.setLeft(y_old_left_child, index); 1185 } 1186 } 1187 else 1188 { 1189 y.setParent(x_old_parent, index); 1190 if (x_old_parent != null) 1191 { 1192 if (x_was_left_child) 1193 { 1194 x_old_parent.setLeft(y, index); 1195 } 1196 else 1197 { 1198 x_old_parent.setRight(y, index); 1199 } 1200 } 1201 x.setLeft(y_old_left_child, index); 1202 x.setRight(y_old_right_child, index); 1203 } 1204 1205 if (x.getLeft(index) != null) 1207 { 1208 x.getLeft(index).setParent(x, index); 1209 } 1210 if (x.getRight(index) != null) 1211 { 1212 x.getRight(index).setParent(x, index); 1213 } 1214 if (y.getLeft(index) != null) 1215 { 1216 y.getLeft(index).setParent(y, index); 1217 } 1218 if (y.getRight(index) != null) 1219 { 1220 y.getRight(index).setParent(y, index); 1221 } 1222 x.swapColors(y, index); 1223 1224 if (_root[ index ] == x) 1226 { 1227 _root[ index ] = y; 1228 } 1229 else if (_root[ index ] == y) 1230 { 1231 _root[ index ] = x; 1232 } 1233 } 1234 1235 1246 1247 private static void checkNonNullComparable(final Object o, 1248 final int index) 1249 { 1250 if (o == null) 1251 { 1252 throw new NullPointerException (_data_name[ index ] 1253 + " cannot be null"); 1254 } 1255 if (!(o instanceof Comparable )) 1256 { 1257 throw new ClassCastException (_data_name[ index ] 1258 + " must be Comparable"); 1259 } 1260 } 1261 1262 1270 1271 private static void checkKey(final Object key) 1272 { 1273 checkNonNullComparable(key, _KEY); 1274 } 1275 1276 1284 1285 private static void checkValue(final Object value) 1286 { 1287 checkNonNullComparable(value, _VALUE); 1288 } 1289 1290 1300 1301 private static void checkKeyAndValue(final Object key, final Object value) 1302 { 1303 checkKey(key); 1304 checkValue(value); 1305 } 1306 1307 1312 1313 private void modify() 1314 { 1315 _modifications++; 1316 } 1317 1318 1321 1322 private void grow() 1323 { 1324 modify(); 1325 _size++; 1326 } 1327 1328 1331 1332 private void shrink() 1333 { 1334 modify(); 1335 _size--; 1336 } 1337 1338 1346 1347 private void insertValue(final Node newNode) 1348 throws IllegalArgumentException 1349 { 1350 Node node = _root[ _VALUE ]; 1351 1352 while (true) 1353 { 1354 int cmp = compare(newNode.getData(_VALUE), node.getData(_VALUE)); 1355 1356 if (cmp == 0) 1357 { 1358 throw new IllegalArgumentException ( 1359 "Cannot store a duplicate value (\"" 1360 + newNode.getData(_VALUE) + "\") in this Map"); 1361 } 1362 else if (cmp < 0) 1363 { 1364 if (node.getLeft(_VALUE) != null) 1365 { 1366 node = node.getLeft(_VALUE); 1367 } 1368 else 1369 { 1370 node.setLeft(newNode, _VALUE); 1371 newNode.setParent(node, _VALUE); 1372 doRedBlackInsert(newNode, _VALUE); 1373 break; 1374 } 1375 } 1376 else 1377 { if (node.getRight(_VALUE) != null) 1379 { 1380 node = node.getRight(_VALUE); 1381 } 1382 else 1383 { 1384 node.setRight(newNode, _VALUE); 1385 newNode.setParent(node, _VALUE); 1386 doRedBlackInsert(newNode, _VALUE); 1387 break; 1388 } 1389 } 1390 } 1391 } 1392 1393 1394 1395 1402 1403 public int size() 1404 { 1405 return _size; 1406 } 1407 1408 1421 1422 public boolean containsKey(final Object key) 1423 throws ClassCastException , NullPointerException 1424 { 1425 checkKey(key); 1426 return lookup(( Comparable ) key, _KEY) != null; 1427 } 1428 1429 1438 1439 public boolean containsValue(final Object value) 1440 { 1441 checkValue(value); 1442 return lookup(( Comparable ) value, _VALUE) != null; 1443 } 1444 1445 1458 1459 public Object get(final Object key) 1460 throws ClassCastException , NullPointerException 1461 { 1462 return doGet(( Comparable ) key, _KEY); 1463 } 1464 1465 1485 1486 public Object put(final Object key, final Object value) 1487 throws ClassCastException , NullPointerException , 1488 IllegalArgumentException 1489 { 1490 checkKeyAndValue(key, value); 1491 Node node = _root[ _KEY ]; 1492 1493 if (node == null) 1494 { 1495 Node root = new Node(( Comparable ) key, ( Comparable ) value); 1496 1497 _root[ _KEY ] = root; 1498 _root[ _VALUE ] = root; 1499 grow(); 1500 } 1501 else 1502 { 1503 while (true) 1504 { 1505 int cmp = compare(( Comparable ) key, node.getData(_KEY)); 1506 1507 if (cmp == 0) 1508 { 1509 throw new IllegalArgumentException ( 1510 "Cannot store a duplicate key (\"" + key 1511 + "\") in this Map"); 1512 } 1513 else if (cmp < 0) 1514 { 1515 if (node.getLeft(_KEY) != null) 1516 { 1517 node = node.getLeft(_KEY); 1518 } 1519 else 1520 { 1521 Node newNode = new Node(( Comparable ) key, 1522 ( Comparable ) value); 1523 1524 insertValue(newNode); 1525 node.setLeft(newNode, _KEY); 1526 newNode.setParent(node, _KEY); 1527 doRedBlackInsert(newNode, _KEY); 1528 grow(); 1529 break; 1530 } 1531 } 1532 else 1533 { if (node.getRight(_KEY) != null) 1535 { 1536 node = node.getRight(_KEY); 1537 } 1538 else 1539 { 1540 Node newNode = new Node(( Comparable ) key, 1541 ( Comparable ) value); 1542 1543 insertValue(newNode); 1544 node.setRight(newNode, _KEY); 1545 newNode.setParent(node, _KEY); 1546 doRedBlackInsert(newNode, _KEY); 1547 grow(); 1548 break; 1549 } 1550 } 1551 } 1552 } 1553 return null; 1554 } 1555 1556 1564 1565 public Object remove(final Object key) 1566 { 1567 return doRemove(( Comparable ) key, _KEY); 1568 } 1569 1570 1573 1574 public void clear() 1575 { 1576 modify(); 1577 _size = 0; 1578 _root[ _KEY ] = null; 1579 _root[ _VALUE ] = null; 1580 } 1581 1582 1594 1595 public Set keySet() 1596 { 1597 if (_key_set[ _KEY ] == null) 1598 { 1599 _key_set[ _KEY ] = new AbstractSet() 1600 { 1601 public Iterator iterator() 1602 { 1603 return new BinaryTreeIterator(_KEY) 1604 { 1605 protected Object doGetNext() 1606 { 1607 return _last_returned_node.getData(_KEY); 1608 } 1609 }; 1610 } 1611 1612 public int size() 1613 { 1614 return BinaryTree.this.size(); 1615 } 1616 1617 public boolean contains(Object o) 1618 { 1619 return containsKey(o); 1620 } 1621 1622 public boolean remove(Object o) 1623 { 1624 int old_size = _size; 1625 1626 BinaryTree.this.remove(o); 1627 return _size != old_size; 1628 } 1629 1630 public void clear() 1631 { 1632 BinaryTree.this.clear(); 1633 } 1634 }; 1635 } 1636 return _key_set[ _KEY ]; 1637 } 1638 1639 1652 1653 public Collection values() 1654 { 1655 if (_value_collection[ _KEY ] == null) 1656 { 1657 _value_collection[ _KEY ] = new AbstractCollection() 1658 { 1659 public Iterator iterator() 1660 { 1661 return new BinaryTreeIterator(_KEY) 1662 { 1663 protected Object doGetNext() 1664 { 1665 return _last_returned_node.getData(_VALUE); 1666 } 1667 }; 1668 } 1669 1670 public int size() 1671 { 1672 return BinaryTree.this.size(); 1673 } 1674 1675 public boolean contains(Object o) 1676 { 1677 return containsValue(o); 1678 } 1679 1680 public boolean remove(Object o) 1681 { 1682 int old_size = _size; 1683 1684 removeValue(o); 1685 return _size != old_size; 1686 } 1687 1688 public boolean removeAll(Collection c) 1689 { 1690 boolean modified = false; 1691 Iterator iter = c.iterator(); 1692 1693 while (iter.hasNext()) 1694 { 1695 if (removeValue(iter.next()) != null) 1696 { 1697 modified = true; 1698 } 1699 } 1700 return modified; 1701 } 1702 1703 public void clear() 1704 { 1705 BinaryTree.this.clear(); 1706 } 1707 }; 1708 } 1709 return _value_collection[ _KEY ]; 1710 } 1711 1712 1725 1726 public Set entrySet() 1727 { 1728 if (_entry_set[ _KEY ] == null) 1729 { 1730 _entry_set[ _KEY ] = new AbstractSet() 1731 { 1732 public Iterator iterator() 1733 { 1734 return new BinaryTreeIterator(_KEY) 1735 { 1736 protected Object doGetNext() 1737 { 1738 return _last_returned_node; 1739 } 1740 }; 1741 } 1742 1743 public boolean contains(Object o) 1744 { 1745 if (!(o instanceof Map.Entry)) 1746 { 1747 return false; 1748 } 1749 Map.Entry entry = ( Map.Entry ) o; 1750 Object value = entry.getValue(); 1751 Node node = lookup(( Comparable ) entry.getKey(), 1752 _KEY); 1753 1754 return (node != null) 1755 && node.getData(_VALUE).equals(value); 1756 } 1757 1758 public boolean remove(Object o) 1759 { 1760 if (!(o instanceof Map.Entry)) 1761 { 1762 return false; 1763 } 1764 Map.Entry entry = ( Map.Entry ) o; 1765 Object value = entry.getValue(); 1766 Node node = lookup(( Comparable ) entry.getKey(), 1767 _KEY); 1768 1769 if ((node != null) && node.getData(_VALUE).equals(value)) 1770 { 1771 doRedBlackDelete(node); 1772 return true; 1773 } 1774 return false; 1775 } 1776 1777 public int size() 1778 { 1779 return BinaryTree.this.size(); 1780 } 1781 1782 public void clear() 1783 { 1784 BinaryTree.this.clear(); 1785 } 1786 }; 1787 } 1788 return _entry_set[ _KEY ]; 1789 } 1790 1791 1792 private abstract class BinaryTreeIterator 1793 implements Iterator 1794 { 1795 private int _expected_modifications; 1796 protected Node _last_returned_node; 1797 private Node _next_node; 1798 private int _type; 1799 1800 1805 1806 BinaryTreeIterator(final int type) 1807 { 1808 _type = type; 1809 _expected_modifications = BinaryTree.this._modifications; 1810 _last_returned_node = null; 1811 _next_node = leastNode(_root[ _type ], _type); 1812 } 1813 1814 1818 1819 protected abstract Object doGetNext(); 1820 1821 1822 1823 1826 1827 public final boolean hasNext() 1828 { 1829 return _next_node != null; 1830 } 1831 1832 1843 1844 public final Object next() 1845 throws NoSuchElementException, ConcurrentModificationException 1846 { 1847 if (_next_node == null) 1848 { 1849 throw new NoSuchElementException(); 1850 } 1851 if (_modifications != _expected_modifications) 1852 { 1853 throw new ConcurrentModificationException(); 1854 } 1855 _last_returned_node = _next_node; 1856 _next_node = nextGreater(_next_node, _type); 1857 return doGetNext(); 1858 } 1859 1860 1879 1880 public final void remove() 1881 throws IllegalStateException , ConcurrentModificationException 1882 { 1883 if (_last_returned_node == null) 1884 { 1885 throw new IllegalStateException (); 1886 } 1887 if (_modifications != _expected_modifications) 1888 { 1889 throw new ConcurrentModificationException(); 1890 } 1891 doRedBlackDelete(_last_returned_node); 1892 _expected_modifications++; 1893 _last_returned_node = null; 1894 } 1895 1896 1897 } 1899 private static final class Node 1901 implements Map.Entry 1902 { 1903 private Comparable [] _data; 1904 private Node[] _left; 1905 private Node[] _right; 1906 private Node[] _parent; 1907 private boolean[] _black; 1908 private int _hashcode; 1909 private boolean _calculated_hashcode; 1910 1911 1918 1919 Node(final Comparable key, final Comparable value) 1920 { 1921 _data = new Comparable [] 1922 { 1923 key, value 1924 }; 1925 _left = new Node[] 1926 { 1927 null, null 1928 }; 1929 _right = new Node[] 1930 { 1931 null, null 1932 }; 1933 _parent = new Node[] 1934 { 1935 null, null 1936 }; 1937 _black = new boolean[] 1938 { 1939 true, true 1940 }; 1941 _calculated_hashcode = false; 1942 } 1943 1944 1951 1952 private Comparable getData(final int index) 1953 { 1954 return _data[ index ]; 1955 } 1956 1957 1963 1964 private void setLeft(final Node node, final int index) 1965 { 1966 _left[ index ] = node; 1967 } 1968 1969 1976 1977 private Node getLeft(final int index) 1978 { 1979 return _left[ index ]; 1980 } 1981 1982 1988 1989 private void setRight(final Node node, final int index) 1990 { 1991 _right[ index ] = node; 1992 } 1993 1994 2001 2002 private Node getRight(final int index) 2003 { 2004 return _right[ index ]; 2005 } 2006 2007 2013 2014 private void setParent(final Node node, final int index) 2015 { 2016 _parent[ index ] = node; 2017 } 2018 2019 2026 2027 private Node getParent(final int index) 2028 { 2029 return _parent[ index ]; 2030 } 2031 2032 2038 2039 private void swapColors(final Node node, final int index) 2040 { 2041 2042 _black[ index ] ^= node._black[ index ]; 2044 node._black[ index ] ^= _black[ index ]; 2045 _black[ index ] ^= node._black[ index ]; 2046 } 2047 2048 2055 2056 private boolean isBlack(final int index) 2057 { 2058 return _black[ index ]; 2059 } 2060 2061 2068 2069 private boolean isRed(final int index) 2070 { 2071 return !_black[ index ]; 2072 } 2073 2074 2079 2080 private void setBlack(final int index) 2081 { 2082 _black[ index ] = true; 2083 } 2084 2085 2090 2091 private void setRed(final int index) 2092 { 2093 _black[ index ] = false; 2094 } 2095 2096 2102 2103 private void copyColor(final Node node, final int index) 2104 { 2105 _black[ index ] = node._black[ index ]; 2106 } 2107 2108 2109 2110 2113 2114 public Object getKey() 2115 { 2116 return _data[ _KEY ]; 2117 } 2118 2119 2122 2123 public Object getValue() 2124 { 2125 return _data[ _VALUE ]; 2126 } 2127 2128 2138 2139 public Object setValue(Object ignored) 2140 throws UnsupportedOperationException 2141 { 2142 throw new UnsupportedOperationException ( 2143 "Map.Entry.setValue is not supported"); 2144 } 2145 2146 2156 2157 public boolean equals(Object o) 2158 { 2159 if (this == o) 2160 { 2161 return true; 2162 } 2163 if (!(o instanceof Map.Entry)) 2164 { 2165 return false; 2166 } 2167 Map.Entry e = ( Map.Entry ) o; 2168 2169 return _data[ _KEY ].equals(e.getKey()) 2170 && _data[ _VALUE ].equals(e.getValue()); 2171 } 2172 2173 2176 2177 public int hashCode() 2178 { 2179 if (!_calculated_hashcode) 2180 { 2181 _hashcode = _data[ _KEY ].hashCode() 2182 ^ _data[ _VALUE ].hashCode(); 2183 _calculated_hashcode = true; 2184 } 2185 return _hashcode; 2186 } 2187 2188 2189 } 2190} 2192 | Popular Tags |