1 dojo.provide("calitha.collections.TreeSet"); 2 dojo.require("calitha.collections.INavigableSet"); 3 dojo.require("calitha.collections.AbstractSet"); 4 dojo.require("calitha.collections.INavigableMap"); 5 dojo.require("calitha.collections.IComparator"); 6 dojo.require("calitha.collections.TreeMap"); 7 dojo.require("calitha.collections.ISortedSet"); 8 dojo.require("calitha.collections.util"); 9 dojo.require("calitha.exception.IllegalArgumentException"); 10 11 dojo.declare("calitha.collections.TreeSet", [calitha.collections.INavigableSet, calitha.collections.AbstractSet], 12 /** @lends calitha.collections.TreeSet# */ 13 { 14 /** 15 * @constructs 16 * @param arg optional argument which can be a 17 * @extends calitha.collections.INavigableSet 18 * @extends calitha.collections.AbstractSet 19 * @param arg 20 * @description A NavigableSet implementation based on a TreeMap. 21 * <p>This class is similar to the 22 * <a href="http://java.sun.com/javase/6/docs/api/java/util/TreeSet.html">Java TreeSet class</a> 23 * <p>This set needs a comparator object or function, or all elements must be IComparable. 24 * Take a look at the comparator functions in the {@link calitha.collections} namespace. 25 */ 26 constructor: function(/**(calitha.collections.INavigableMap|calitha.collections.IComparator|Function)?*/arg) 27 { 28 if (arg == null) 29 { 30 this._map = new calitha.collections.TreeMap(); 31 } 32 else if (arg.isInstanceOf(calitha.collections.INavigableMap)) 33 { 34 this._map = arg; 35 } 36 else if ((arg.isInstanceOf(calitha.collections.IComparator)) || 37 (arg.isInstanceOf(Function))) 38 { 39 this._map = new calitha.collections.TreeMap(arg); 40 } 41 else 42 { 43 throw new calitha.exception.IllegalArgumentException(Error("arg is not a navigable map, a comparator or null")); 44 } 45 } 46 , 47 add: function(/**Object*/ e) 48 { 49 return this._map.put(e, calitha.collections.TreeSet.PRESENT) === null; 50 } 51 , 52 addAll: function(/**calitha.collections.ICollection*/ collection) 53 { 54 //todo: investigate how useful it is to implement this method here 55 return this.inherited(arguments); 56 } 57 , 58 ceiling: function(/**Object*/ element) 59 { 60 return this._map.ceilingKey(element); 61 } 62 , 63 clear: function() 64 { 65 this._map.clear(); 66 } 67 , 68 contains: function(/**Object*/ o) 69 { 70 return this._map.containsKey(o); 71 } 72 , 73 descendingIterator: function() 74 { 75 return this._map.descendingKeySet().iterator(); 76 } 77 , 78 descendingSet: function() 79 { 80 return new calitha.collections.TreeSet(this._map.descendingMap()); 81 } 82 , 83 floor: function(/**Object*/ element) 84 { 85 return this._map.floorKey(element); 86 } 87 , 88 higher: function(/**Object*/ element) 89 { 90 return this._map.higherKey(element); 91 } 92 , 93 isEmpty: function() 94 { 95 return this._map.isEmpty(); 96 } 97 , 98 iterator: function() 99 { 100 return this._map.navigableKeySet().iterator(); 101 } 102 , 103 lower: function(/**Object*/ element) 104 { 105 return this._map.lowerKey(element); 106 } 107 , 108 pollFirst: function() 109 { 110 var entry = this._map.pollFirstEntry(); 111 return (entry === null) ? null : entry.getKey(); 112 } 113 , 114 pollLast: function() 115 { 116 var entry = this._map.pollLastEntry(); 117 return (entry === null) ? null : entry.getKey(); 118 } 119 , 120 remove: function(/**Object*/ o) 121 { 122 return this._map.remove(o) === calitha.collections.TreeSet.PRESENT; 123 } 124 , 125 size: function() 126 { 127 return this._map.size(); 128 } 129 130 }); 131 132 calitha.collections.TreeSet._PRESENT = new Object(); 133