Debian Codegraph @

dojox/collections/SortedList.js

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