7dc79e37b8a66ae2920a1975aac493add62504fa.svn-base 5.4 KB

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