]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/collections/SortedList.js
Comment class stub
[eow] / static / dojo-release-1.1.1 / dojox / collections / SortedList.js
1 if(!dojo._hasResource["dojox.collections.SortedList"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.collections.SortedList"] = true;
3 dojo.provide("dojox.collections.SortedList");
4 dojo.require("dojox.collections._base");
5
6 dojox.collections.SortedList=function(/* object? */ dictionary){
7         //      summary
8         //      creates a collection that acts like a dictionary but is also internally sorted.
9         //      Note that the act of adding any elements forces an internal resort, making this object potentially slow.
10         var _this=this;
11         var items={};
12         var q=[];
13         var sorter=function(a,b){
14                 if (a.key > b.key) return 1;
15                 if (a.key < b.key) return -1;
16                 return 0;
17         };
18         var build=function(){
19                 q=[];
20                 var e=_this.getIterator();
21                 while (!e.atEnd()){
22                         q.push(e.get());
23                 }
24                 q.sort(sorter);
25         };
26         var testObject={};
27
28         this.count=q.length;
29         this.add=function(/* string */ k,/* object */v){
30                 //      summary
31                 //      add the passed value to the dictionary at location k
32                 if (!items[k]) {
33                         items[k]=new dojox.collections.DictionaryEntry(k,v);
34                         this.count=q.push(items[k]);
35                         q.sort(sorter);
36                 }
37         };
38         this.clear=function(){
39                 //      summary
40                 //      clear the internal collections
41                 items={};
42                 q=[];
43                 this.count=q.length;
44         };
45         this.clone=function(){
46                 //      summary
47                 //      create a clone of this sorted list
48                 return new dojox.collections.SortedList(this);  //      dojox.collections.SortedList
49         };
50         this.contains=this.containsKey=function(/* string */ k){
51                 //      summary
52                 //      Check to see if the list has a location k
53                 if(testObject[k]){
54                         return false;                   //      bool
55                 }
56                 return (items[k]!=null);        //      bool
57         };
58         this.containsValue=function(/* object */ o){
59                 //      summary
60                 //      Check to see if this list contains the passed object
61                 var e=this.getIterator();
62                 while (!e.atEnd()){
63                         var item=e.get();
64                         if(item.value==o){ 
65                                 return true;    //      bool
66                         }
67                 }
68                 return false;   //      bool
69         };
70         this.copyTo=function(/* array */ arr, /* int */ i){
71                 //      summary
72                 //      copy the contents of the list into array arr at index i
73                 var e=this.getIterator();
74                 var idx=i;
75                 while(!e.atEnd()){
76                         arr.splice(idx,0,e.get());
77                         idx++;
78                 }
79         };
80         this.entry=function(/* string */ k){
81                 //      summary
82                 //      return the object at location k
83                 return items[k];        //      dojox.collections.DictionaryEntry
84         };
85         this.forEach=function(/* function */ fn, /* object? */ scope){
86                 //      summary
87                 //      functional iterator, following the mozilla spec.
88                 dojo.forEach(q, fn, scope);
89         };
90         this.getByIndex=function(/* int */ i){
91                 //      summary
92                 //      return the item at index i
93                 return q[i].valueOf();  //      object
94         };
95         this.getIterator=function(){
96                 //      summary
97                 //      get an iterator for this object
98                 return new dojox.collections.DictionaryIterator(items); //      dojox.collections.DictionaryIterator
99         };
100         this.getKey=function(/* int */ i){
101                 //      summary
102                 //      return the key of the item at index i
103                 return q[i].key;
104         };
105         this.getKeyList=function(){
106                 //      summary
107                 //      return an array of the keys set in this list
108                 var arr=[];
109                 var e=this.getIterator();
110                 while (!e.atEnd()){
111                         arr.push(e.get().key);
112                 }
113                 return arr;     //      array
114         };
115         this.getValueList=function(){
116                 //      summary
117                 //      return an array of values in this list
118                 var arr=[];
119                 var e=this.getIterator();
120                 while (!e.atEnd()){
121                         arr.push(e.get().value);
122                 }
123                 return arr;     //      array
124         };
125         this.indexOfKey=function(/* string */ k){
126                 //      summary
127                 //      return the index of the passed key.
128                 for (var i=0; i<q.length; i++){
129                         if (q[i].key==k){
130                                 return i;       //      int
131                         }
132                 }
133                 return -1;      //      int
134         };
135         this.indexOfValue=function(/* object */ o){
136                 //      summary
137                 //      return the first index of object o
138                 for (var i=0; i<q.length; i++){
139                         if (q[i].value==o){
140                                 return i;       //      int
141                         }
142                 }
143                 return -1;      //      int
144         };
145         this.item=function(/* string */ k){
146                 //      summary
147                 //      return the value of the object at location k.
148                 if(k in items && !testObject[k]){
149                         return items[k].valueOf();      //      object
150                 }
151                 return undefined;       //      object
152         };
153         this.remove=function(/* string */k){
154                 //      summary
155                 //      remove the item at location k and rebuild the internal collections.
156                 delete items[k];
157                 build();
158                 this.count=q.length;
159         };
160         this.removeAt=function(/* int */ i){
161                 //      summary
162                 //      remove the item at index i, and rebuild the internal collections.
163                 delete items[q[i].key];
164                 build();
165                 this.count=q.length;
166         };
167         this.replace=function(/* string */ k, /* object */ v){
168                 //      summary
169                 //      Replace an existing item if it's there, and add a new one if not.
170                 if (!items[k]){
171                         //      we're adding a new object, return false
172                         this.add(k,v);
173                         return false; // bool
174                 }else{
175                         //      we're replacing an object, return true
176                         items[k]=new dojox.collections.DictionaryEntry(k,v);
177                         build();
178                         return true; // bool
179                 }
180         };
181         this.setByIndex=function(/* int */ i, /* object */ o){
182                 //      summary
183                 //      set an item by index
184                 items[q[i].key].value=o;
185                 build();
186                 this.count=q.length;
187         };
188         if (dictionary){
189                 var e=dictionary.getIterator();
190                 while (!e.atEnd()){
191                         var item=e.get();
192                         q[q.length]=items[item.key]=new dojox.collections.DictionaryEntry(item.key,item.value);
193                 }
194                 q.sort(sorter);
195         }
196 }
197
198 }