SortedList.js 5.1 KB

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