1:
38:
39:
40: package ;
41:
42: import ;
43: import ;
44: import ;
45: import ;
46:
47:
94: public class TreeMap<K, V> extends AbstractMap<K, V>
95: implements NavigableMap<K, V>, Cloneable, Serializable
96: {
97:
98:
99:
100:
101:
102:
103:
104:
107: private static final long serialVersionUID = 919286545866124006L;
108:
109:
112: static final int RED = -1,
113: BLACK = 1;
114:
115:
122: static final Node nil = new Node(null, null, BLACK);
123: static
124: {
125:
126: nil.parent = nil;
127: nil.left = nil;
128: nil.right = nil;
129: }
130:
131:
134: private transient Node root;
135:
136:
139: transient int size;
140:
141:
144: private transient Set<Map.Entry<K,V>> entries;
145:
146:
149: private transient NavigableMap<K,V> descendingMap;
150:
151:
154: private transient NavigableSet<K> nKeys;
155:
156:
161: transient int modCount;
162:
163:
168: final Comparator<? super K> comparator;
169:
170:
176: private static final class Node<K, V> extends AbstractMap.SimpleEntry<K, V>
177: {
178:
179:
180: int color;
181:
182:
183: Node<K, V> left = nil;
184:
185: Node<K, V> right = nil;
186:
187: Node<K, V> parent = nil;
188:
189:
194: Node(K key, V value, int color)
195: {
196: super(key, value);
197: this.color = color;
198: }
199: }
200:
201:
210: public TreeMap()
211: {
212: this((Comparator) null);
213: }
214:
215:
224: public TreeMap(Comparator<? super K> c)
225: {
226: comparator = c;
227: fabricateTree(0);
228: }
229:
230:
244: public TreeMap(Map<? extends K, ? extends V> map)
245: {
246: this((Comparator) null);
247: putAll(map);
248: }
249:
250:
258: public TreeMap(SortedMap<K, ? extends V> sm)
259: {
260: this(sm.comparator());
261: int pos = sm.size();
262: Iterator itr = sm.entrySet().iterator();
263:
264: fabricateTree(pos);
265: Node node = firstNode();
266:
267: while (--pos >= 0)
268: {
269: Map.Entry me = (Map.Entry) itr.next();
270: node.key = me.getKey();
271: node.value = me.getValue();
272: node = successor(node);
273: }
274: }
275:
276:
279: public void clear()
280: {
281: if (size > 0)
282: {
283: modCount++;
284: root = nil;
285: size = 0;
286: }
287: }
288:
289:
295: public Object clone()
296: {
297: TreeMap copy = null;
298: try
299: {
300: copy = (TreeMap) super.clone();
301: }
302: catch (CloneNotSupportedException x)
303: {
304: }
305: copy.entries = null;
306: copy.fabricateTree(size);
307:
308: Node node = firstNode();
309: Node cnode = copy.firstNode();
310:
311: while (node != nil)
312: {
313: cnode.key = node.key;
314: cnode.value = node.value;
315: node = successor(node);
316: cnode = copy.successor(cnode);
317: }
318: return copy;
319: }
320:
321:
327: public Comparator<? super K> comparator()
328: {
329: return comparator;
330: }
331:
332:
341: public boolean containsKey(Object key)
342: {
343: return getNode((K) key) != nil;
344: }
345:
346:
353: public boolean containsValue(Object value)
354: {
355: Node node = firstNode();
356: while (node != nil)
357: {
358: if (equals(value, node.value))
359: return true;
360: node = successor(node);
361: }
362: return false;
363: }
364:
365:
378: public Set<Map.Entry<K,V>> entrySet()
379: {
380: if (entries == null)
381:
382:
383: entries = new NavigableEntrySet();
384: return entries;
385: }
386:
387:
393: public K firstKey()
394: {
395: if (root == nil)
396: throw new NoSuchElementException();
397: return firstNode().key;
398: }
399:
400:
414: public V get(Object key)
415: {
416:
417: return getNode((K) key).value;
418: }
419:
420:
437: public SortedMap<K, V> headMap(K toKey)
438: {
439: return headMap(toKey, false);
440: }
441:
442:
458: public NavigableMap<K, V> headMap(K toKey, boolean inclusive)
459: {
460: return new SubMap((K)(Object)nil, inclusive
461: ? successor(getNode(toKey)).key : toKey);
462: }
463:
464:
473: public Set<K> keySet()
474: {
475: if (keys == null)
476:
477:
478: keys = new KeySet();
479: return keys;
480: }
481:
482:
488: public K lastKey()
489: {
490: if (root == nil)
491: throw new NoSuchElementException("empty");
492: return lastNode().key;
493: }
494:
495:
511: public V put(K key, V value)
512: {
513: Node<K,V> current = root;
514: Node<K,V> parent = nil;
515: int comparison = 0;
516:
517:
518: while (current != nil)
519: {
520: parent = current;
521: comparison = compare(key, current.key);
522: if (comparison > 0)
523: current = current.right;
524: else if (comparison < 0)
525: current = current.left;
526: else
527: return current.setValue(value);
528: }
529:
530:
531: Node n = new Node(key, value, RED);
532: n.parent = parent;
533:
534:
535: modCount++;
536: size++;
537: if (parent == nil)
538: {
539:
540: root = n;
541: return null;
542: }
543: if (comparison > 0)
544: parent.right = n;
545: else
546: parent.left = n;
547:
548:
549: insertFixup(n);
550: return null;
551: }
552:
553:
564: public void putAll(Map<? extends K, ? extends V> m)
565: {
566: Iterator itr = m.entrySet().iterator();
567: int pos = m.size();
568: while (--pos >= 0)
569: {
570: Map.Entry<K,V> e = (Map.Entry<K,V>) itr.next();
571: put(e.getKey(), e.getValue());
572: }
573: }
574:
575:
588: public V remove(Object key)
589: {
590: Node<K, V> n = getNode((K)key);
591: if (n == nil)
592: return null;
593:
594: V result = n.value;
595: removeNode(n);
596: return result;
597: }
598:
599:
604: public int size()
605: {
606: return size;
607: }
608:
609:
630: public SortedMap<K, V> subMap(K fromKey, K toKey)
631: {
632: return subMap(fromKey, true, toKey, false);
633: }
634:
635:
655: public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive,
656: K toKey, boolean toInclusive)
657: {
658: return new SubMap(fromInclusive ? fromKey : successor(getNode(fromKey)).key,
659: toInclusive ? successor(getNode(toKey)).key : toKey);
660: }
661:
662:
678: public SortedMap<K, V> tailMap(K fromKey)
679: {
680: return tailMap(fromKey, true);
681: }
682:
683:
699: public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive)
700: {
701: return new SubMap(inclusive ? fromKey : successor(getNode(fromKey)).key,
702: (K)(Object)nil);
703: }
704:
705:
715: public Collection<V> values()
716: {
717: if (values == null)
718:
719:
720: values = new AbstractCollection<V>()
721: {
722: public int size()
723: {
724: return size;
725: }
726:
727: public Iterator<V> iterator()
728: {
729: return new TreeIterator(VALUES);
730: }
731:
732: public void clear()
733: {
734: TreeMap.this.clear();
735: }
736: };
737: return values;
738: }
739:
740:
750: final int compare(K o1, K o2)
751: {
752: return (comparator == null
753: ? ((Comparable) o1).compareTo(o2)
754: : comparator.compare(o1, o2));
755: }
756:
757:
763: private void deleteFixup(Node<K,V> node, Node<K,V> parent)
764: {
765:
766:
767:
768:
769:
770: while (node != root && node.color == BLACK)
771: {
772: if (node == parent.left)
773: {
774:
775: Node<K,V> sibling = parent.right;
776:
777:
778: if (sibling.color == RED)
779: {
780:
781:
782: sibling.color = BLACK;
783: parent.color = RED;
784: rotateLeft(parent);
785: sibling = parent.right;
786: }
787:
788: if (sibling.left.color == BLACK && sibling.right.color == BLACK)
789: {
790:
791:
792: sibling.color = RED;
793: node = parent;
794: parent = parent.parent;
795: }
796: else
797: {
798: if (sibling.right.color == BLACK)
799: {
800:
801:
802: sibling.left.color = BLACK;
803: sibling.color = RED;
804: rotateRight(sibling);
805: sibling = parent.right;
806: }
807:
808:
809: sibling.color = parent.color;
810: parent.color = BLACK;
811: sibling.right.color = BLACK;
812: rotateLeft(parent);
813: node = root;
814: }
815: }
816: else
817: {
818:
819: Node<K,V> sibling = parent.left;
820:
821:
822: if (sibling.color == RED)
823: {
824:
825:
826: sibling.color = BLACK;
827: parent.color = RED;
828: rotateRight(parent);
829: sibling = parent.left;
830: }
831:
832: if (sibling.right.color == BLACK && sibling.left.color == BLACK)
833: {
834:
835:
836: sibling.color = RED;
837: node = parent;
838: parent = parent.parent;
839: }
840: else
841: {
842: if (sibling.left.color == BLACK)
843: {
844:
845:
846: sibling.right.color = BLACK;
847: sibling.color = RED;
848: rotateLeft(sibling);
849: sibling = parent.left;
850: }
851:
852:
853: sibling.color = parent.color;
854: parent.color = BLACK;
855: sibling.left.color = BLACK;
856: rotateRight(parent);
857: node = root;
858: }
859: }
860: }
861: node.color = BLACK;
862: }
863:
864:
870: private void fabricateTree(final int count)
871: {
872: if (count == 0)
873: {
874: root = nil;
875: size = 0;
876: return;
877: }
878:
879:
880:
881:
882:
883:
884:
885: root = new Node(null, null, BLACK);
886: size = count;
887: Node row = root;
888: int rowsize;
889:
890:
891: for (rowsize = 2; rowsize + rowsize <= count; rowsize <<= 1)
892: {
893: Node parent = row;
894: Node last = null;
895: for (int i = 0; i < rowsize; i += 2)
896: {
897: Node left = new Node(null, null, BLACK);
898: Node right = new Node(null, null, BLACK);
899: left.parent = parent;
900: left.right = right;
901: right.parent = parent;
902: parent.left = left;
903: Node next = parent.right;
904: parent.right = right;
905: parent = next;
906: if (last != null)
907: last.right = left;
908: last = right;
909: }
910: row = row.left;
911: }
912:
913:
914: int overflow = count - rowsize;
915: Node parent = row;
916: int i;
917: for (i = 0; i < overflow; i += 2)
918: {
919: Node left = new Node(null, null, RED);
920: Node right = new Node(null, null, RED);
921: left.parent = parent;
922: right.parent = parent;
923: parent.left = left;
924: Node next = parent.right;
925: parent.right = right;
926: parent = next;
927: }
928:
929: if (i - overflow == 0)
930: {
931: Node left = new Node(null, null, RED);
932: left.parent = parent;
933: parent.left = left;
934: parent = parent.right;
935: left.parent.right = nil;
936: }
937:
938: while (parent != nil)
939: {
940: Node next = parent.right;
941: parent.right = nil;
942: parent = next;
943: }
944: }
945:
946:
952: final Node<K, V> firstNode()
953: {
954:
955: Node node = root;
956: while (node.left != nil)
957: node = node.left;
958: return node;
959: }
960:
961:
968: final Node<K, V> getNode(K key)
969: {
970: Node<K,V> current = root;
971: while (current != nil)
972: {
973: int comparison = compare(key, current.key);
974: if (comparison > 0)
975: current = current.right;
976: else if (comparison < 0)
977: current = current.left;
978: else
979: return current;
980: }
981: return current;
982: }
983:
984:
991: final Node<K,V> highestLessThan(K key)
992: {
993: return highestLessThan(key, false);
994: }
995:
996:
1006: final Node<K,V> highestLessThan(K key, boolean equal)
1007: {
1008: if (key == nil)
1009: return lastNode();
1010:
1011: Node<K,V> last = nil;
1012: Node<K,V> current = root;
1013: int comparison = 0;
1014:
1015: while (current != nil)
1016: {
1017: last = current;
1018: comparison = compare(key, current.key);
1019: if (comparison > 0)
1020: current = current.right;
1021: else if (comparison < 0)
1022: current = current.left;
1023: else
1024: return (equal ? last : predecessor(last));
1025: }
1026: return comparison < 0 ? predecessor(last) : last;
1027: }
1028:
1029:
1034: private void insertFixup(Node<K,V> n)
1035: {
1036:
1037:
1038:
1039: while (n.parent.color == RED && n.parent.parent != nil)
1040: {
1041: if (n.parent == n.parent.parent.left)
1042: {
1043: Node uncle = n.parent.parent.right;
1044:
1045: if (uncle.color == RED)
1046: {
1047:
1048:
1049: n.parent.color = BLACK;
1050: uncle.color = BLACK;
1051: uncle.parent.color = RED;
1052: n = uncle.parent;
1053: }
1054: else
1055: {
1056: if (n == n.parent.right)
1057: {
1058:
1059:
1060: n = n.parent;
1061: rotateLeft(n);
1062: }
1063:
1064:
1065: n.parent.color = BLACK;
1066: n.parent.parent.color = RED;
1067: rotateRight(n.parent.parent);
1068: }
1069: }
1070: else
1071: {
1072:
1073: Node uncle = n.parent.parent.left;
1074:
1075: if (uncle.color == RED)
1076: {
1077:
1078:
1079: n.parent.color = BLACK;
1080: uncle.color = BLACK;
1081: uncle.parent.color = RED;
1082: n = uncle.parent;
1083: }
1084: else
1085: {
1086: if (n == n.parent.left)
1087: {
1088:
1089:
1090: n = n.parent;
1091: rotateRight(n);
1092: }
1093:
1094:
1095: n.parent.color = BLACK;
1096: n.parent.parent.color = RED;
1097: rotateLeft(n.parent.parent);
1098: }
1099: }
1100: }
1101: root.color = BLACK;
1102: }
1103:
1104:
1109: private Node<K,V> lastNode()
1110: {
1111:
1112: Node node = root;
1113: while (node.right != nil)
1114: node = node.right;
1115: return node;
1116: }
1117:
1118:
1127: final Node<K,V> lowestGreaterThan(K key, boolean first)
1128: {
1129: return lowestGreaterThan(key, first, true);
1130: }
1131:
1132:
1142: final Node<K,V> lowestGreaterThan(K key, boolean first, boolean equal)
1143: {
1144: if (key == nil)
1145: return first ? firstNode() : nil;
1146:
1147: Node<K,V> last = nil;
1148: Node<K,V> current = root;
1149: int comparison = 0;
1150:
1151: while (current != nil)
1152: {
1153: last = current;
1154: comparison = compare(key, current.key);
1155: if (comparison > 0)
1156: current = current.right;
1157: else if (comparison < 0)
1158: current = current.left;
1159: else
1160: return (equal ? current : successor(current));
1161: }
1162: return comparison > 0 ? successor(last) : last;
1163: }
1164:
1165:
1171: private Node<K,V> predecessor(Node<K,V> node)
1172: {
1173: if (node.left != nil)
1174: {
1175: node = node.left;
1176: while (node.right != nil)
1177: node = node.right;
1178: return node;
1179: }
1180:
1181: Node parent = node.parent;
1182:
1183: while (node == parent.left)
1184: {
1185: node = parent;
1186: parent = node.parent;
1187: }
1188: return parent;
1189: }
1190:
1191:
1203: final void putFromObjStream(ObjectInputStream s, int count,
1204: boolean readValues)
1205: throws IOException, ClassNotFoundException
1206: {
1207: fabricateTree(count);
1208: Node node = firstNode();
1209:
1210: while (--count >= 0)
1211: {
1212: node.key = s.readObject();
1213: node.value = readValues ? s.readObject() : "";
1214: node = successor(node);
1215: }
1216: }
1217:
1218:
1226: final void putKeysLinear(Iterator<K> keys, int count)
1227: {
1228: fabricateTree(count);
1229: Node<K,V> node = firstNode();
1230:
1231: while (--count >= 0)
1232: {
1233: node.key = keys.next();
1234: node.value = (V) "";
1235: node = successor(node);
1236: }
1237: }
1238:
1239:
1248: private void readObject(ObjectInputStream s)
1249: throws IOException, ClassNotFoundException
1250: {
1251: s.defaultReadObject();
1252: int size = s.readInt();
1253: putFromObjStream(s, size, true);
1254: }
1255:
1256:
1262: final void removeNode(Node<K,V> node)
1263: {
1264: Node<K,V> splice;
1265: Node<K,V> child;
1266:
1267: modCount++;
1268: size--;
1269:
1270:
1271: if (node.left == nil)
1272: {
1273:
1274: splice = node;
1275: child = node.right;
1276: }
1277: else if (node.right == nil)
1278: {
1279:
1280: splice = node;
1281: child = node.left;
1282: }
1283: else
1284: {
1285:
1286:
1287: splice = node.left;
1288: while (splice.right != nil)
1289: splice = splice.right;
1290: child = splice.left;
1291: node.key = splice.key;
1292: node.value = splice.value;
1293: }
1294:
1295:
1296: Node parent = splice.parent;
1297: if (child != nil)
1298: child.parent = parent;
1299: if (parent == nil)
1300: {
1301:
1302: root = child;
1303: return;
1304: }
1305: if (splice == parent.left)
1306: parent.left = child;
1307: else
1308: parent.right = child;
1309:
1310: if (splice.color == BLACK)
1311: deleteFixup(child, parent);
1312: }
1313:
1314:
1319: private void rotateLeft(Node<K,V> node)
1320: {
1321: Node child = node.right;
1322:
1323:
1324:
1325:
1326: node.right = child.left;
1327: if (child.left != nil)
1328: child.left.parent = node;
1329:
1330:
1331: child.parent = node.parent;
1332: if (node.parent != nil)
1333: {
1334: if (node == node.parent.left)
1335: node.parent.left = child;
1336: else
1337: node.parent.right = child;
1338: }
1339: else
1340: root = child;
1341:
1342:
1343: child.left = node;
1344: node.parent = child;
1345: }
1346:
1347:
1352: private void rotateRight(Node<K,V> node)
1353: {
1354: Node child = node.left;
1355:
1356:
1357:
1358:
1359: node.left = child.right;
1360: if (child.right != nil)
1361: child.right.parent = node;
1362:
1363:
1364: child.parent = node.parent;
1365: if (node.parent != nil)
1366: {
1367: if (node == node.parent.right)
1368: node.parent.right = child;
1369: else
1370: node.parent.left = child;
1371: }
1372: else
1373: root = child;
1374:
1375:
1376: child.right = node;
1377: node.parent = child;
1378: }
1379:
1380:
1387: final Node<K,V> successor(Node<K,V> node)
1388: {
1389: if (node.right != nil)
1390: {
1391: node = node.right;
1392: while (node.left != nil)
1393: node = node.left;
1394: return node;
1395: }
1396:
1397: Node<K,V> parent = node.parent;
1398:
1399: while (node == parent.right)
1400: {
1401: node = parent;
1402: parent = parent.parent;
1403: }
1404: return parent;
1405: }
1406:
1407:
1415: private void writeObject(ObjectOutputStream s) throws IOException
1416: {
1417: s.defaultWriteObject();
1418:
1419: Node node = firstNode();
1420: s.writeInt(size);
1421: while (node != nil)
1422: {
1423: s.writeObject(node.key);
1424: s.writeObject(node.value);
1425: node = successor(node);
1426: }
1427: }
1428:
1429:
1435: private final class TreeIterator implements Iterator
1436: {
1437:
1441: private final int type;
1442:
1443: private int knownMod = modCount;
1444:
1445: private Node last;
1446:
1447: private Node next;
1448:
1452: private final Node max;
1453:
1454:
1458: TreeIterator(int type)
1459: {
1460: this(type, firstNode(), nil);
1461: }
1462:
1463:
1471: TreeIterator(int type, Node first, Node max)
1472: {
1473: this.type = type;
1474: this.next = first;
1475: this.max = max;
1476: }
1477:
1478:
1482: public boolean hasNext()
1483: {
1484: return next != max;
1485: }
1486:
1487:
1493: public Object next()
1494: {
1495: if (knownMod != modCount)
1496: throw new ConcurrentModificationException();
1497: if (next == max)
1498: throw new NoSuchElementException();
1499: last = next;
1500: next = successor(last);
1501:
1502: if (type == VALUES)
1503: return last.value;
1504: else if (type == KEYS)
1505: return last.key;
1506: return last;
1507: }
1508:
1509:
1515: public void remove()
1516: {
1517: if (last == null)
1518: throw new IllegalStateException();
1519: if (knownMod != modCount)
1520: throw new ConcurrentModificationException();
1521:
1522: removeNode(last);
1523: last = null;
1524: knownMod++;
1525: }
1526: }
1527:
1528:
1536: private final class SubMap
1537: extends AbstractMap<K,V>
1538: implements NavigableMap<K,V>
1539: {
1540:
1544: final K minKey;
1545:
1546:
1550: final K maxKey;
1551:
1552:
1555: private Set<Map.Entry<K,V>> entries;
1556:
1557:
1560: private NavigableMap<K,V> descendingMap;
1561:
1562:
1565: private NavigableSet<K> nKeys;
1566:
1567:
1576: SubMap(K minKey, K maxKey)
1577: {
1578: if (minKey != nil && maxKey != nil && compare(minKey, maxKey) > 0)
1579: throw new IllegalArgumentException("fromKey > toKey");
1580: this.minKey = minKey;
1581: this.maxKey = maxKey;
1582: }
1583:
1584:
1592: boolean keyInRange(K key)
1593: {
1594: return ((minKey == nil || compare(key, minKey) >= 0)
1595: && (maxKey == nil || compare(key, maxKey) < 0));
1596: }
1597:
1598: public Entry<K,V> ceilingEntry(K key)
1599: {
1600: Entry<K,V> n = TreeMap.this.ceilingEntry(key);
1601: if (n != null && keyInRange(n.getKey()))
1602: return n;
1603: return null;
1604: }
1605:
1606: public K ceilingKey(K key)
1607: {
1608: K found = TreeMap.this.ceilingKey(key);
1609: if (keyInRange(found))
1610: return found;
1611: else
1612: return null;
1613: }
1614:
1615: public NavigableSet<K> descendingKeySet()
1616: {
1617: return descendingMap().navigableKeySet();
1618: }
1619:
1620: public NavigableMap<K,V> descendingMap()
1621: {
1622: if (descendingMap == null)
1623: descendingMap = new DescendingMap(this);
1624: return descendingMap;
1625: }
1626:
1627: public void clear()
1628: {
1629: Node next = lowestGreaterThan(minKey, true);
1630: Node max = lowestGreaterThan(maxKey, false);
1631: while (next != max)
1632: {
1633: Node current = next;
1634: next = successor(current);
1635: removeNode(current);
1636: }
1637: }
1638:
1639: public Comparator<? super K> comparator()
1640: {
1641: return comparator;
1642: }
1643:
1644: public boolean containsKey(Object key)
1645: {
1646: return keyInRange((K) key) && TreeMap.this.containsKey(key);
1647: }
1648:
1649: public boolean containsValue(Object value)
1650: {
1651: Node node = lowestGreaterThan(minKey, true);
1652: Node max = lowestGreaterThan(maxKey, false);
1653: while (node != max)
1654: {
1655: if (equals(value, node.getValue()))
1656: return true;
1657: node = successor(node);
1658: }
1659: return false;
1660: }
1661:
1662: public Set<Map.Entry<K,V>> entrySet()
1663: {
1664: if (entries == null)
1665:
1666:
1667: entries = new SubMap.NavigableEntrySet();
1668: return entries;
1669: }
1670:
1671: public Entry<K,V> firstEntry()
1672: {
1673: Node<K,V> node = lowestGreaterThan(minKey, true);
1674: if (node == nil || ! keyInRange(node.key))
1675: return null;
1676: return node;
1677: }
1678:
1679: public K firstKey()
1680: {
1681: Entry<K,V> e = firstEntry();
1682: if (e == null)
1683: throw new NoSuchElementException();
1684: return e.getKey();
1685: }
1686:
1687: public Entry<K,V> floorEntry(K key)
1688: {
1689: Entry<K,V> n = TreeMap.this.floorEntry(key);
1690: if (n != null && keyInRange(n.getKey()))
1691: return n;
1692: return null;
1693: }
1694:
1695: public K floorKey(K key)
1696: {
1697: K found = TreeMap.this.floorKey(key);
1698: if (keyInRange(found))
1699: return found;
1700: else
1701: return null;
1702: }
1703:
1704: public V get(Object key)
1705: {
1706: if (keyInRange((K) key))
1707: return TreeMap.this.get(key);
1708: return null;
1709: }
1710:
1711: public SortedMap<K,V> headMap(K toKey)
1712: {
1713: return headMap(toKey, false);
1714: }
1715:
1716: public NavigableMap<K,V> headMap(K toKey, boolean inclusive)
1717: {
1718: if (!keyInRange(toKey))
1719: throw new IllegalArgumentException("Key outside submap range");
1720: return new SubMap(minKey, (inclusive ?
1721: successor(getNode(toKey)).key : toKey));
1722: }
1723:
1724: public Set<K> keySet()
1725: {
1726: if (this.keys == null)
1727:
1728:
1729: this.keys = new SubMap.KeySet();
1730: return this.keys;
1731: }
1732:
1733: public Entry<K,V> higherEntry(K key)
1734: {
1735: Entry<K,V> n = TreeMap.this.higherEntry(key);
1736: if (n != null && keyInRange(n.getKey()))
1737: return n;
1738: return null;
1739: }
1740:
1741: public K higherKey(K key)
1742: {
1743: K found = TreeMap.this.higherKey(key);
1744: if (keyInRange(found))
1745: return found;
1746: else
1747: return null;
1748: }
1749:
1750: public Entry<K,V> lastEntry()
1751: {
1752: return lowerEntry(maxKey);
1753: }
1754:
1755: public K lastKey()
1756: {
1757: Entry<K,V> e = lastEntry();
1758: if (e == null)
1759: throw new NoSuchElementException();
1760: return e.getKey();
1761: }
1762:
1763: public Entry<K,V> lowerEntry(K key)
1764: {
1765: Entry<K,V> n = TreeMap.this.lowerEntry(key);
1766: if (n != null && keyInRange(n.getKey()))
1767: return n;
1768: return null;
1769: }
1770:
1771: public K lowerKey(K key)
1772: {
1773: K found = TreeMap.this.lowerKey(key);
1774: if (keyInRange(found))
1775: return found;
1776: else
1777: return null;
1778: }
1779:
1780: public NavigableSet<K> navigableKeySet()
1781: {
1782: if (this.nKeys == null)
1783:
1784:
1785: this.nKeys = new SubMap.NavigableKeySet();
1786: return this.nKeys;
1787: }
1788:
1789: public Entry<K,V> pollFirstEntry()
1790: {
1791: Entry<K,V> e = firstEntry();
1792: if (e != null)
1793: removeNode((Node<K,V>) e);
1794: return e;
1795: }
1796:
1797: public Entry<K,V> pollLastEntry()
1798: {
1799: Entry<K,V> e = lastEntry();
1800: if (e != null)
1801: removeNode((Node<K,V>) e);
1802: return e;
1803: }
1804:
1805: public V put(K key, V value)
1806: {
1807: if (! keyInRange(key))
1808: throw new IllegalArgumentException("Key outside range");
1809: return TreeMap.this.put(key, value);
1810: }
1811:
1812: public V remove(Object key)
1813: {
1814: if (keyInRange((K)key))
1815: return TreeMap.this.remove(key);
1816: return null;
1817: }
1818:
1819: public int size()
1820: {
1821: Node node = lowestGreaterThan(minKey, true);
1822: Node max = lowestGreaterThan(maxKey, false);
1823: int count = 0;
1824: while (node != max)
1825: {
1826: count++;
1827: node = successor(node);
1828: }
1829: return count;
1830: }
1831:
1832: public SortedMap<K,V> subMap(K fromKey, K toKey)
1833: {
1834: return subMap(fromKey, true, toKey, false);
1835: }
1836:
1837: public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
1838: K toKey, boolean toInclusive)
1839: {
1840: if (! keyInRange(fromKey) || ! keyInRange(toKey))
1841: throw new IllegalArgumentException("key outside range");
1842: return new SubMap(fromInclusive ? fromKey : successor(getNode(fromKey)).key,
1843: toInclusive ? successor(getNode(toKey)).key : toKey);
1844: }
1845:
1846: public SortedMap<K, V> tailMap(K fromKey)
1847: {
1848: return tailMap(fromKey, true);
1849: }
1850:
1851: public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive)
1852: {
1853: if (! keyInRange(fromKey))
1854: throw new IllegalArgumentException("key outside range");
1855: return new SubMap(inclusive ? fromKey : successor(getNode(fromKey)).key,
1856: maxKey);
1857: }
1858:
1859: public Collection<V> values()
1860: {
1861: if (this.values == null)
1862:
1863:
1864: this.values = new AbstractCollection()
1865: {
1866: public int size()
1867: {
1868: return SubMap.this.size();
1869: }
1870:
1871: public Iterator<V> iterator()
1872: {
1873: Node first = lowestGreaterThan(minKey, true);
1874: Node max = lowestGreaterThan(maxKey, false);
1875: return new TreeIterator(VALUES, first, max);
1876: }
1877:
1878: public void clear()
1879: {
1880: SubMap.this.clear();
1881: }
1882: };
1883: return this.values;
1884: }
1885:
1886: private class KeySet
1887: extends AbstractSet<K>
1888: {
1889: public int size()
1890: {
1891: return SubMap.this.size();
1892: }
1893:
1894: public Iterator<K> iterator()
1895: {
1896: Node first = lowestGreaterThan(minKey, true);
1897: Node max = lowestGreaterThan(maxKey, false);
1898: return new TreeIterator(KEYS, first, max);
1899: }
1900:
1901: public void clear()
1902: {
1903: SubMap.this.clear();
1904: }
1905:
1906: public boolean contains(Object o)
1907: {
1908: if (! keyInRange((K) o))
1909: return false;
1910: return getNode((K) o) != nil;
1911: }
1912:
1913: public boolean remove(Object o)
1914: {
1915: if (! keyInRange((K) o))
1916: return false;
1917: Node n = getNode((K) o);
1918: if (n != nil)
1919: {
1920: removeNode(n);
1921: return true;
1922: }
1923: return false;
1924: }
1925:
1926: }
1927:
1928: private final class NavigableKeySet
1929: extends KeySet
1930: implements NavigableSet<K>
1931: {
1932:
1933: public K ceiling(K k)
1934: {
1935: return SubMap.this.ceilingKey(k);
1936: }
1937:
1938: public Comparator<? super K> comparator()
1939: {
1940: return comparator;
1941: }
1942:
1943: public Iterator<K> descendingIterator()
1944: {
1945: return descendingSet().iterator();
1946: }
1947:
1948: public NavigableSet<K> descendingSet()
1949: {
1950: return new DescendingSet(this);
1951: }
1952:
1953: public K first()
1954: {
1955: return SubMap.this.firstKey();
1956: }
1957:
1958: public K floor(K k)
1959: {
1960: return SubMap.this.floorKey(k);
1961: }
1962:
1963: public SortedSet<K> headSet(K to)
1964: {
1965: return headSet(to, false);
1966: }
1967:
1968: public NavigableSet<K> headSet(K to, boolean inclusive)
1969: {
1970: return SubMap.this.headMap(to, inclusive).navigableKeySet();
1971: }
1972:
1973: public K higher(K k)
1974: {
1975: return SubMap.this.higherKey(k);
1976: }
1977:
1978: public K last()
1979: {
1980: return SubMap.this.lastKey();
1981: }
1982:
1983: public K lower(K k)
1984: {
1985: return SubMap.this.lowerKey(k);
1986: }
1987:
1988: public K pollFirst()
1989: {
1990: return SubMap.this.pollFirstEntry().getKey();
1991: }
1992:
1993: public K pollLast()
1994: {
1995: return SubMap.this.pollLastEntry().getKey();
1996: }
1997:
1998: public SortedSet<K> subSet(K from, K to)
1999: {
2000: return subSet(from, true, to, false);
2001: }
2002:
2003: public NavigableSet<K> subSet(K from, boolean fromInclusive,
2004: K to, boolean toInclusive)
2005: {
2006: return SubMap.this.subMap(from, fromInclusive,
2007: to, toInclusive).navigableKeySet();
2008: }
2009:
2010: public SortedSet<K> tailSet(K from)
2011: {
2012: return tailSet(from, true);
2013: }
2014:
2015: public NavigableSet<K> tailSet(K from, boolean inclusive)
2016: {
2017: return SubMap.this.tailMap(from, inclusive).navigableKeySet();
2018: }
2019:
2020: }
2021:
2022:
2025: private class EntrySet
2026: extends AbstractSet<Entry<K,V>>
2027: {
2028:
2029: public int size()
2030: {
2031: return SubMap.this.size();
2032: }
2033:
2034: public Iterator<Map.Entry<K,V>> iterator()
2035: {
2036: Node first = lowestGreaterThan(minKey, true);
2037: Node max = lowestGreaterThan(maxKey, false);
2038: return new TreeIterator(ENTRIES, first, max);
2039: }
2040:
2041: public void clear()
2042: {
2043: SubMap.this.clear();
2044: }
2045:
2046: public boolean contains(Object o)
2047: {
2048: if (! (o instanceof Map.Entry))
2049: return false;
2050: Map.Entry<K,V> me = (Map.Entry<K,V>) o;
2051: K key = me.getKey();
2052: if (! keyInRange(key))
2053: return false;
2054: Node<K,V> n = getNode(key);
2055: return n != nil && AbstractSet.equals(me.getValue(), n.value);
2056: }
2057:
2058: public boolean remove(Object o)
2059: {
2060: if (! (o instanceof Map.Entry))
2061: return false;
2062: Map.Entry<K,V> me = (Map.Entry<K,V>) o;
2063: K key = me.getKey();
2064: if (! keyInRange(key))
2065: return false;
2066: Node<K,V> n = getNode(key);
2067: if (n != nil && AbstractSet.equals(me.getValue(), n.value))
2068: {
2069: removeNode(n);
2070: return true;
2071: }
2072: return false;
2073: }
2074: }
2075:
2076: private final class NavigableEntrySet
2077: extends EntrySet
2078: implements NavigableSet<Entry<K,V>>
2079: {
2080:
2081: public Entry<K,V> ceiling(Entry<K,V> e)
2082: {
2083: return SubMap.this.ceilingEntry(e.getKey());
2084: }
2085:
2086: public Comparator<? super Entry<K,V>> comparator()
2087: {
2088: return new Comparator<Entry<K,V>>()
2089: {
2090: public int compare(Entry<K,V> t1, Entry<K,V> t2)
2091: {
2092: return comparator.compare(t1.getKey(), t2.getKey());
2093: }
2094: };
2095: }
2096:
2097: public Iterator<Entry<K,V>> descendingIterator()
2098: {
2099: return descendingSet().iterator();
2100: }
2101:
2102: public NavigableSet<Entry<K,V>> descendingSet()
2103: {
2104: return new DescendingSet(this);
2105: }
2106:
2107: public Entry<K,V> first()
2108: {
2109: return SubMap.this.firstEntry();
2110: }
2111:
2112: public Entry<K,V> floor(Entry<K,V> e)
2113: {
2114: return SubMap.this.floorEntry(e.getKey());
2115: }
2116:
2117: public SortedSet<Entry<K,V>> headSet(Entry<K,V> to)
2118: {
2119: return headSet(to, false);
2120: }
2121:
2122: public NavigableSet<Entry<K,V>> headSet(Entry<K,V> to, boolean inclusive)
2123: {
2124: return (NavigableSet<Entry<K,V>>)
2125: SubMap.this.headMap(to.getKey(), inclusive).entrySet();
2126: }
2127:
2128: public Entry<K,V> higher(Entry<K,V> e)
2129: {
2130: return SubMap.this.higherEntry(e.getKey());
2131: }
2132:
2133: public Entry<K,V> last()
2134: {
2135: return SubMap.this.lastEntry();
2136: }
2137:
2138: public Entry<K,V> lower(Entry<K,V> e)
2139: {
2140: return SubMap.this.lowerEntry(e.getKey());
2141: }
2142:
2143: public Entry<K,V> pollFirst()
2144: {
2145: return SubMap.this.pollFirstEntry();
2146: }
2147:
2148: public Entry<K,V> pollLast()
2149: {
2150: return SubMap.this.pollLastEntry();
2151: }
2152:
2153: public SortedSet<Entry<K,V>> subSet(Entry<K,V> from, Entry<K,V> to)
2154: {
2155: return subSet(from, true, to, false);
2156: }
2157:
2158: public NavigableSet<Entry<K,V>> subSet(Entry<K,V> from, boolean fromInclusive,
2159: Entry<K,V> to, boolean toInclusive)
2160: {
2161: return (NavigableSet<Entry<K,V>>)
2162: SubMap.this.subMap(from.getKey(), fromInclusive,
2163: to.getKey(), toInclusive).entrySet();
2164: }
2165:
2166: public SortedSet<Entry<K,V>> tailSet(Entry<K,V> from)
2167: {
2168: return tailSet(from, true);
2169: }
2170:
2171: public NavigableSet<Entry<K,V>> tailSet(Entry<K,V> from, boolean inclusive)
2172: {
2173: return (NavigableSet<Entry<K,V>>)
2174: SubMap.this.tailMap(from.getKey(), inclusive).navigableKeySet();
2175: }
2176:
2177: }
2178:
2179: }
2180:
2181:
2198: public Entry<K,V> ceilingEntry(K key)
2199: {
2200: Node<K,V> n = lowestGreaterThan(key, false);
2201: return (n == nil) ? null : n;
2202: }
2203:
2204:
2220: public K ceilingKey(K key)
2221: {
2222: Entry<K,V> e = ceilingEntry(key);
2223: return (e == null) ? null : e.getKey();
2224: }
2225:
2226:
2236: public NavigableSet<K> descendingKeySet()
2237: {
2238: return descendingMap().navigableKeySet();
2239: }
2240:
2241:
2255: public NavigableMap<K,V> descendingMap()
2256: {
2257: if (descendingMap == null)
2258: descendingMap = new DescendingMap<K,V>(this);
2259: return descendingMap;
2260: }
2261:
2262:
2270: public Entry<K,V> firstEntry()
2271: {
2272: Node<K,V> n = firstNode();
2273: return (n == nil) ? null : n;
2274: }
2275:
2276:
2293: public Entry<K,V> floorEntry(K key)
2294: {
2295: Node<K,V> n = highestLessThan(key, true);
2296: return (n == nil) ? null : n;
2297: }
2298:
2299:
2315: public K floorKey(K key)
2316: {
2317: Entry<K,V> e = floorEntry(key);
2318: return (e == null) ? null : e.getKey();
2319: }
2320:
2321:
2338: public Entry<K,V> higherEntry(K key)
2339: {
2340: Node<K,V> n = lowestGreaterThan(key, false, false);
2341: return (n == nil) ? null : n;
2342: }
2343:
2344:
2360: public K higherKey(K key)
2361: {
2362: Entry<K,V> e = higherEntry(key);
2363: return (e == null) ? null : e.getKey();
2364: }
2365:
2366:
2374: public Entry<K,V> lastEntry()
2375: {
2376: Node<K,V> n = lastNode();
2377: return (n == nil) ? null : n;
2378: }
2379:
2380:
2397: public Entry<K,V> lowerEntry(K key)
2398: {
2399: Node<K,V> n = highestLessThan(key);
2400: return (n == nil) ? null : n;
2401: }
2402:
2403:
2419: public K lowerKey(K key)
2420: {
2421: Entry<K,V> e = lowerEntry(key);
2422: return (e == null) ? null : e.getKey();
2423: }
2424:
2425:
2436: public NavigableSet<K> navigableKeySet()
2437: {
2438: if (nKeys == null)
2439: nKeys = new NavigableKeySet();
2440: return nKeys;
2441: }
2442:
2443:
2452: public Entry<K,V> pollFirstEntry()
2453: {
2454: Entry<K,V> e = firstEntry();
2455: if (e != null)
2456: removeNode((Node<K,V>)e);
2457: return e;
2458: }
2459:
2460:
2469: public Entry<K,V> pollLastEntry()
2470: {
2471: Entry<K,V> e = lastEntry();
2472: if (e != null)
2473: removeNode((Node<K,V>)e);
2474: return e;
2475: }
2476:
2477:
2486: private static final class DescendingMap<DK,DV>
2487: implements NavigableMap<DK,DV>
2488: {
2489:
2490:
2493: private Set<Map.Entry<DK,DV>> entries;
2494:
2495:
2498: private Set<DK> keys;
2499:
2500:
2503: private NavigableSet<DK> nKeys;
2504:
2505:
2508: private Collection<DV> values;
2509:
2510:
2513: private NavigableMap<DK,DV> map;
2514:
2515:
2521: public DescendingMap(NavigableMap<DK,DV> map)
2522: {
2523: this.map = map;
2524: }
2525:
2526: public Map.Entry<DK,DV> ceilingEntry(DK key)
2527: {
2528: return map.floorEntry(key);
2529: }
2530:
2531: public DK ceilingKey(DK key)
2532: {
2533: return map.floorKey(key);
2534: }
2535:
2536: public void clear()
2537: {
2538: map.clear();
2539: }
2540:
2541: public Comparator<? super DK> comparator()
2542: {
2543: return Collections.reverseOrder(map.comparator());
2544: }
2545:
2546: public boolean containsKey(Object o)
2547: {
2548: return map.containsKey(o);
2549: }
2550:
2551: public boolean containsValue(Object o)
2552: {
2553: return map.containsValue(o);
2554: }
2555:
2556: public NavigableSet<DK> descendingKeySet()
2557: {
2558: return descendingMap().navigableKeySet();
2559: }
2560:
2561: public NavigableMap<DK,DV> descendingMap()
2562: {
2563: return map;
2564: }
2565:
2566: public Set<Entry<DK,DV>> entrySet()
2567: {
2568: if (entries == null)
2569: entries =
2570: new DescendingSet<Entry<DK,DV>>((NavigableSet<Entry<DK,DV>>)
2571: map.entrySet());
2572: return entries;
2573: }
2574:
2575: public boolean equals(Object o)
2576: {
2577: return map.equals(o);
2578: }
2579:
2580: public Entry<DK,DV> firstEntry()
2581: {
2582: return map.lastEntry();
2583: }
2584:
2585: public DK firstKey()
2586: {
2587: return map.lastKey();
2588: }
2589:
2590: public Entry<DK,DV> floorEntry(DK key)
2591: {
2592: return map.ceilingEntry(key);
2593: }
2594:
2595: public DK floorKey(DK key)
2596: {
2597: return map.ceilingKey(key);
2598: }
2599:
2600: public DV get(Object key)
2601: {
2602: return map.get(key);
2603: }
2604:
2605: public int hashCode()
2606: {
2607: return map.hashCode();
2608: }
2609:
2610: public SortedMap<DK,DV> headMap(DK toKey)
2611: {
2612: return headMap(toKey, false);
2613: }
2614:
2615: public NavigableMap<DK,DV> headMap(DK toKey, boolean inclusive)
2616: {
2617: return new DescendingMap(map.tailMap(toKey, inclusive));
2618: }
2619:
2620: public Entry<DK,DV> higherEntry(DK key)
2621: {
2622: return map.lowerEntry(key);
2623: }
2624:
2625: public DK higherKey(DK key)
2626: {
2627: return map.lowerKey(key);
2628: }
2629:
2630: public Set<DK> keySet()
2631: {
2632: if (keys == null)
2633: keys = new DescendingSet<DK>(map.navigableKeySet());
2634: return keys;
2635: }
2636:
2637: public boolean isEmpty()
2638: {
2639: return map.isEmpty();
2640: }
2641:
2642: public Entry<DK,DV> lastEntry()
2643: {
2644: return map.firstEntry();
2645: }
2646:
2647: public DK lastKey()
2648: {
2649: return map.firstKey();
2650: }
2651:
2652: public Entry<DK,DV> lowerEntry(DK key)
2653: {
2654: return map.higherEntry(key);
2655: }
2656:
2657: public DK lowerKey(DK key)
2658: {
2659: return map.higherKey(key);
2660: }
2661:
2662: public NavigableSet<DK> navigableKeySet()
2663: {
2664: if (nKeys == null)
2665: nKeys = new DescendingSet<DK>(map.navigableKeySet());
2666: return nKeys;
2667: }
2668:
2669: public Entry<DK,DV> pollFirstEntry()
2670: {
2671: return pollLastEntry();
2672: }
2673:
2674: public Entry<DK,DV> pollLastEntry()
2675: {
2676: return pollFirstEntry();
2677: }
2678:
2679: public DV put(DK key, DV value)
2680: {
2681: return map.put(key, value);
2682: }
2683:
2684: public void putAll(Map<? extends DK, ? extends DV> m)
2685: {
2686: map.putAll(m);
2687: }
2688:
2689: public DV remove(Object key)
2690: {
2691: return map.remove(key);
2692: }
2693:
2694: public int size()
2695: {
2696: return map.size();
2697: }
2698:
2699: public SortedMap<DK,DV> subMap(DK fromKey, DK toKey)
2700: {
2701: return subMap(fromKey, true, toKey, false);
2702: }
2703:
2704: public NavigableMap<DK,DV> subMap(DK fromKey, boolean fromInclusive,
2705: DK toKey, boolean toInclusive)
2706: {
2707: return new DescendingMap(map.subMap(fromKey, fromInclusive,
2708: toKey, toInclusive));
2709: }
2710:
2711: public SortedMap<DK,DV> tailMap(DK fromKey)
2712: {
2713: return tailMap(fromKey, true);
2714: }
2715:
2716: public NavigableMap<DK,DV> tailMap(DK fromKey, boolean inclusive)
2717: {
2718: return new DescendingMap(map.headMap(fromKey, inclusive));
2719: }
2720:
2721: public String toString()
2722: {
2723: StringBuilder r = new StringBuilder("{");
2724: final Iterator<Entry<DK,DV>> it = entrySet().iterator();
2725: while (it.hasNext())
2726: {
2727: final Entry<DK,DV> e = it.next();
2728: r.append(e.getKey());
2729: r.append('=');
2730: r.append(e.getValue());
2731: r.append(", ");
2732: }
2733: r.replace(r.length() - 2, r.length(), "}");
2734: return r.toString();
2735: }
2736:
2737: public Collection<DV> values()
2738: {
2739: if (values == null)
2740:
2741:
2742: values = new AbstractCollection()
2743: {
2744: public int size()
2745: {
2746: return size();
2747: }
2748:
2749: public Iterator<DV> iterator()
2750: {
2751: return new Iterator<DV>()
2752: {
2753:
2754: private Entry<DK,DV> last;
2755:
2756:
2757: private Entry<DK,DV> next = firstEntry();
2758:
2759: public boolean hasNext()
2760: {
2761: return next != null;
2762: }
2763:
2764: public DV next()
2765: {
2766: if (next == null)
2767: throw new NoSuchElementException();
2768: last = next;
2769: next = higherEntry(last.getKey());
2770:
2771: return last.getValue();
2772: }
2773:
2774: public void remove()
2775: {
2776: if (last == null)
2777: throw new IllegalStateException();
2778:
2779: DescendingMap.this.remove(last.getKey());
2780: last = null;
2781: }
2782: };
2783: }
2784:
2785: public void clear()
2786: {
2787: clear();
2788: }
2789: };
2790: return values;
2791: }
2792:
2793: }
2794:
2795:
2798: private class KeySet
2799: extends AbstractSet<K>
2800: {
2801:
2802: public int size()
2803: {
2804: return size;
2805: }
2806:
2807: public Iterator<K> iterator()
2808: {
2809: return new TreeIterator(KEYS);
2810: }
2811:
2812: public void clear()
2813: {
2814: TreeMap.this.clear();
2815: }
2816:
2817: public boolean contains(Object o)
2818: {
2819: return containsKey(o);
2820: }
2821:
2822: public boolean remove(Object key)
2823: {
2824: Node<K,V> n = getNode((K) key);
2825: if (n == nil)
2826: return false;
2827: removeNode(n);
2828: return true;
2829: }
2830: }
2831:
2832:
2837: private final class NavigableKeySet
2838: extends KeySet
2839: implements NavigableSet<K>
2840: {
2841:
2842: public K ceiling(K k)
2843: {
2844: return ceilingKey(k);
2845: }
2846:
2847: public Comparator<? super K> comparator()
2848: {
2849: return comparator;
2850: }
2851:
2852: public Iterator<K> descendingIterator()
2853: {
2854: return descendingSet().iterator();
2855: }
2856:
2857: public NavigableSet<K> descendingSet()
2858: {
2859: return new DescendingSet<K>(this);
2860: }
2861:
2862: public K first()
2863: {
2864: return firstKey();
2865: }
2866:
2867: public K floor(K k)
2868: {
2869: return floorKey(k);
2870: }
2871:
2872: public SortedSet<K> headSet(K to)
2873: {
2874: return headSet(to, false);
2875: }
2876:
2877: public NavigableSet<K> headSet(K to, boolean inclusive)
2878: {
2879: return headMap(to, inclusive).navigableKeySet();
2880: }
2881:
2882: public K higher(K k)
2883: {
2884: return higherKey(k);
2885: }
2886:
2887: public K last()
2888: {
2889: return lastKey();
2890: }
2891:
2892: public K lower(K k)
2893: {
2894: return lowerKey(k);
2895: }
2896:
2897: public K pollFirst()
2898: {
2899: return pollFirstEntry().getKey();
2900: }
2901:
2902: public K pollLast()
2903: {
2904: return pollLastEntry().getKey();
2905: }
2906:
2907: public SortedSet<K> subSet(K from, K to)
2908: {
2909: return subSet(from, true, to, false);
2910: }
2911:
2912: public NavigableSet<K> subSet(K from, boolean fromInclusive,
2913: K to, boolean toInclusive)
2914: {
2915: return subMap(from, fromInclusive,
2916: to, toInclusive).navigableKeySet();
2917: }
2918:
2919: public SortedSet<K> tailSet(K from)
2920: {
2921: return tailSet(from, true);
2922: }
2923:
2924: public NavigableSet<K> tailSet(K from, boolean inclusive)
2925: {
2926: return tailMap(from, inclusive).navigableKeySet();
2927: }
2928:
2929:
2930: }
2931:
2932:
2941: private static final class DescendingSet<D>
2942: implements NavigableSet<D>
2943: {
2944:
2945:
2948: private NavigableSet<D> set;
2949:
2950:
2956: public DescendingSet(NavigableSet<D> set)
2957: {
2958: this.set = set;
2959: }
2960:
2961: public boolean add(D e)
2962: {
2963: return set.add(e);
2964: }
2965:
2966: public boolean addAll(Collection<? extends D> c)
2967: {
2968: return set.addAll(c);
2969: }
2970:
2971: public D ceiling(D e)
2972: {
2973: return set.floor(e);
2974: }
2975:
2976: public void clear()
2977: {
2978: set.clear();
2979: }
2980:
2981: public Comparator<? super D> comparator()
2982: {
2983: return Collections.reverseOrder(set.comparator());
2984: }
2985:
2986: public boolean contains(Object o)
2987: {
2988: return set.contains(o);
2989: }
2990:
2991: public boolean containsAll(Collection<?> c)
2992: {
2993: return set.containsAll(c);
2994: }
2995:
2996: public Iterator<D> descendingIterator()
2997: {
2998: return descendingSet().iterator();
2999: }
3000:
3001: public NavigableSet<D> descendingSet()
3002: {
3003: return set;
3004: }
3005:
3006: public boolean equals(Object o)
3007: {
3008: return set.equals(o);
3009: }
3010:
3011: public D first()
3012: {
3013: return set.last();
3014: }
3015:
3016: public D floor(D e)
3017: {
3018: return set.ceiling(e);
3019: }
3020:
3021: public int hashCode()
3022: {
3023: return set.hashCode();
3024: }
3025:
3026: public SortedSet<D> headSet(D to)
3027: {
3028: return headSet(to, false);
3029: }
3030:
3031: public NavigableSet<D> headSet(D to, boolean inclusive)
3032: {
3033: return new DescendingSet(set.tailSet(to, inclusive));
3034: }
3035:
3036: public D higher(D e)
3037: {
3038: return set.lower(e);
3039: }
3040:
3041: public boolean isEmpty()
3042: {
3043: return set.isEmpty();
3044: }
3045:
3046: public Iterator<D> iterator()
3047: {
3048: return new Iterator<D>()
3049: {
3050:
3051:
3052: private D last;
3053:
3054:
3055: private D next = first();
3056:
3057: public boolean hasNext()
3058: {
3059: return next != null;
3060: }
3061:
3062: public D next()
3063: {
3064: if (next == null)
3065: throw new NoSuchElementException();
3066: last = next;
3067: next = higher(last);
3068:
3069: return last;
3070: }
3071:
3072: public void remove()
3073: {
3074: if (last == null)
3075: throw new IllegalStateException();
3076:
3077: DescendingSet.this.remove(last);
3078: last = null;
3079: }
3080: };
3081: }
3082:
3083: public D last()
3084: {
3085: return set.first();
3086: }
3087:
3088: public D lower(D e)
3089: {
3090: return set.higher(e);
3091: }
3092:
3093: public D pollFirst()
3094: {
3095: return set.pollLast();
3096: }
3097:
3098: public D pollLast()
3099: {
3100: return set.pollFirst();
3101: }
3102:
3103: public boolean remove(Object o)
3104: {
3105: return set.remove(o);
3106: }
3107:
3108: public boolean removeAll(Collection<?> c)
3109: {
3110: return set.removeAll(c);
3111: }
3112:
3113: public boolean retainAll(Collection<?> c)
3114: {
3115: return set.retainAll(c);
3116: }
3117:
3118: public int size()
3119: {
3120: return set.size();
3121: }
3122:
3123: public SortedSet<D> subSet(D from, D to)
3124: {
3125: return subSet(from, true, to, false);
3126: }
3127:
3128: public NavigableSet<D> subSet(D from, boolean fromInclusive,
3129: D to, boolean toInclusive)
3130: {
3131: return new DescendingSet(set.subSet(from, fromInclusive,
3132: to, toInclusive));
3133: }
3134:
3135: public SortedSet<D> tailSet(D from)
3136: {
3137: return tailSet(from, true);
3138: }
3139:
3140: public NavigableSet<D> tailSet(D from, boolean inclusive)
3141: {
3142: return new DescendingSet(set.headSet(from, inclusive));
3143: }
3144:
3145: public Object[] toArray()
3146: {
3147: D[] array = (D[]) set.toArray();
3148: Arrays.sort(array, comparator());
3149: return array;
3150: }
3151:
3152: public <T> T[] toArray(T[] a)
3153: {
3154: T[] array = set.toArray(a);
3155: Arrays.sort(array, (Comparator<? super T>) comparator());
3156: return array;
3157: }
3158:
3159: public String toString()
3160: {
3161: StringBuilder r = new StringBuilder("[");
3162: final Iterator<D> it = iterator();
3163: while (it.hasNext())
3164: {
3165: final D o = it.next();
3166: if (o == this)
3167: r.append("<this>");
3168: else
3169: r.append(o);
3170: r.append(", ");
3171: }
3172: r.replace(r.length() - 2, r.length(), "]");
3173: return r.toString();
3174: }
3175:
3176: }
3177:
3178: private class EntrySet
3179: extends AbstractSet<Entry<K,V>>
3180: {
3181: public int size()
3182: {
3183: return size;
3184: }
3185:
3186: public Iterator<Map.Entry<K,V>> iterator()
3187: {
3188: return new TreeIterator(ENTRIES);
3189: }
3190:
3191: public void clear()
3192: {
3193: TreeMap.this.clear();
3194: }
3195:
3196: public boolean contains(Object o)
3197: {
3198: if (! (o instanceof Map.Entry))
3199: return false;
3200: Map.Entry<K,V> me = (Map.Entry<K,V>) o;
3201: Node<K,V> n = getNode(me.getKey());
3202: return n != nil && AbstractSet.equals(me.getValue(), n.value);
3203: }
3204:
3205: public boolean remove(Object o)
3206: {
3207: if (! (o instanceof Map.Entry))
3208: return false;
3209: Map.Entry<K,V> me = (Map.Entry<K,V>) o;
3210: Node<K,V> n = getNode(me.getKey());
3211: if (n != nil && AbstractSet.equals(me.getValue(), n.value))
3212: {
3213: removeNode(n);
3214: return true;
3215: }
3216: return false;
3217: }
3218: }
3219:
3220: private final class NavigableEntrySet
3221: extends EntrySet
3222: implements NavigableSet<Entry<K,V>>
3223: {
3224:
3225: public Entry<K,V> ceiling(Entry<K,V> e)
3226: {
3227: return ceilingEntry(e.getKey());
3228: }
3229:
3230: public Comparator<? super Entry<K,V>> comparator()
3231: {
3232: return new Comparator<Entry<K,V>>()
3233: {
3234: public int compare(Entry<K,V> t1, Entry<K,V> t2)
3235: {
3236: return comparator.compare(t1.getKey(), t2.getKey());
3237: }
3238: };
3239: }
3240:
3241: public Iterator<Entry<K,V>> descendingIterator()
3242: {
3243: return descendingSet().iterator();
3244: }
3245:
3246: public NavigableSet<Entry<K,V>> descendingSet()
3247: {
3248: return new DescendingSet(this);
3249: }
3250:
3251: public Entry<K,V> first()
3252: {
3253: return firstEntry();
3254: }
3255:
3256: public Entry<K,V> floor(Entry<K,V> e)
3257: {
3258: return floorEntry(e.getKey());
3259: }
3260:
3261: public SortedSet<Entry<K,V>> headSet(Entry<K,V> to)
3262: {
3263: return headSet(to, false);
3264: }
3265:
3266: public NavigableSet<Entry<K,V>> headSet(Entry<K,V> to, boolean inclusive)
3267: {
3268: return (NavigableSet<Entry<K,V>>) headMap(to.getKey(), inclusive).entrySet();
3269: }
3270:
3271: public Entry<K,V> higher(Entry<K,V> e)
3272: {
3273: return higherEntry(e.getKey());
3274: }
3275:
3276: public Entry<K,V> last()
3277: {
3278: return lastEntry();
3279: }
3280:
3281: public Entry<K,V> lower(Entry<K,V> e)
3282: {
3283: return lowerEntry(e.getKey());
3284: }
3285:
3286: public Entry<K,V> pollFirst()
3287: {
3288: return pollFirstEntry();
3289: }
3290:
3291: public Entry<K,V> pollLast()
3292: {
3293: return pollLastEntry();
3294: }
3295:
3296: public SortedSet<Entry<K,V>> subSet(Entry<K,V> from, Entry<K,V> to)
3297: {
3298: return subSet(from, true, to, false);
3299: }
3300:
3301: public NavigableSet<Entry<K,V>> subSet(Entry<K,V> from, boolean fromInclusive,
3302: Entry<K,V> to, boolean toInclusive)
3303: {
3304: return (NavigableSet<Entry<K,V>>) subMap(from.getKey(), fromInclusive,
3305: to.getKey(), toInclusive).entrySet();
3306: }
3307:
3308: public SortedSet<Entry<K,V>> tailSet(Entry<K,V> from)
3309: {
3310: return tailSet(from, true);
3311: }
3312:
3313: public NavigableSet<Entry<K,V>> tailSet(Entry<K,V> from, boolean inclusive)
3314: {
3315: return (NavigableSet<Entry<K,V>>) tailMap(from.getKey(), inclusive).navigableKeySet();
3316: }
3317:
3318: }
3319:
3320: }