SortedList.js 5.0 KB

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