| 1 |
define(["dojo/_base/kernel", "dojo/_base/array", "./_base"], function(dojo, darray, dxc){ |
| 2 |
|
| 3 |
dxc.SortedList=function( dictionary){ |
| 4 |
|
| 5 |
|
| 6 |
|
| 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( k, v){ |
| 27 |
|
| 28 |
|
| 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 |
|
| 37 |
|
| 38 |
items={}; |
| 39 |
q=[]; |
| 40 |
this.count=q.length; |
| 41 |
}; |
| 42 |
this.clone=function(){ |
| 43 |
|
| 44 |
|
| 45 |
return new dxc.SortedList(this); |
| 46 |
}; |
| 47 |
this.contains=this.containsKey=function( k){ |
| 48 |
|
| 49 |
|
| 50 |
if(testObject[k]){ |
| 51 |
return false; |
| 52 |
} |
| 53 |
return (items[k]!=null); |
| 54 |
}; |
| 55 |
this.containsValue=function( o){ |
| 56 |
|
| 57 |
|
| 58 |
var e=this.getIterator(); |
| 59 |
while (!e.atEnd()){ |
| 60 |
var item=e.get(); |
| 61 |
if(item.value==o){ |
| 62 |
return true; |
| 63 |
} |
| 64 |
} |
| 65 |
return false; |
| 66 |
}; |
| 67 |
this.copyTo=function( arr, i){ |
| 68 |
|
| 69 |
|
| 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( k){ |
| 78 |
|
| 79 |
|
| 80 |
return items[k]; |
| 81 |
}; |
| 82 |
this.forEach=function( fn, scope){ |
| 83 |
|
| 84 |
|
| 85 |
dojo.forEach(q, fn, scope); |
| 86 |
}; |
| 87 |
this.getByIndex=function( i){ |
| 88 |
|
| 89 |
|
| 90 |
return q[i].valueOf(); |
| 91 |
}; |
| 92 |
this.getIterator=function(){ |
| 93 |
|
| 94 |
|
| 95 |
return new dxc.DictionaryIterator(items); |
| 96 |
}; |
| 97 |
this.getKey=function( i){ |
| 98 |
|
| 99 |
|
| 100 |
return q[i].key; |
| 101 |
}; |
| 102 |
this.getKeyList=function(){ |
| 103 |
|
| 104 |
|
| 105 |
var arr=[]; |
| 106 |
var e=this.getIterator(); |
| 107 |
while (!e.atEnd()){ |
| 108 |
arr.push(e.get().key); |
| 109 |
} |
| 110 |
return arr; |
| 111 |
}; |
| 112 |
this.getValueList=function(){ |
| 113 |
|
| 114 |
|
| 115 |
var arr=[]; |
| 116 |
var e=this.getIterator(); |
| 117 |
while (!e.atEnd()){ |
| 118 |
arr.push(e.get().value); |
| 119 |
} |
| 120 |
return arr; |
| 121 |
}; |
| 122 |
this.indexOfKey=function( k){ |
| 123 |
|
| 124 |
|
| 125 |
for (var i=0; i<q.length; i++){ |
| 126 |
if (q[i].key==k){ |
| 127 |
return i; |
| 128 |
} |
| 129 |
} |
| 130 |
return -1; |
| 131 |
}; |
| 132 |
this.indexOfValue=function( o){ |
| 133 |
|
| 134 |
|
| 135 |
for (var i=0; i<q.length; i++){ |
| 136 |
if (q[i].value==o){ |
| 137 |
return i; |
| 138 |
} |
| 139 |
} |
| 140 |
return -1; |
| 141 |
}; |
| 142 |
this.item=function( k){ |
| 143 |
|
| 144 |
|
| 145 |
if(k in items && !testObject[k]){ |
| 146 |
return items[k].valueOf(); |
| 147 |
} |
| 148 |
return undefined; |
| 149 |
}; |
| 150 |
this.remove=function( k){ |
| 151 |
|
| 152 |
|
| 153 |
delete items[k]; |
| 154 |
build(); |
| 155 |
this.count=q.length; |
| 156 |
}; |
| 157 |
this.removeAt=function( i){ |
| 158 |
|
| 159 |
|
| 160 |
delete items[q[i].key]; |
| 161 |
build(); |
| 162 |
this.count=q.length; |
| 163 |
}; |
| 164 |
this.replace=function( k, v){ |
| 165 |
|
| 166 |
|
| 167 |
if (!items[k]){ |
| 168 |
|
| 169 |
this.add(k,v); |
| 170 |
return false; |
| 171 |
}else{ |
| 172 |
|
| 173 |
items[k]=new dxc.DictionaryEntry(k,v); |
| 174 |
build(); |
| 175 |
return true; |
| 176 |
} |
| 177 |
}; |
| 178 |
this.setByIndex=function( i, o){ |
| 179 |
|
| 180 |
|
| 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 |
}); |