ArrayList.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.ArrayList"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.collections.ArrayList"] = true;
  8. dojo.provide("dojox.collections.ArrayList");
  9. dojo.require("dojox.collections._base");
  10. dojox.collections.ArrayList=function(/* array? */arr){
  11. // summary
  12. // Returns a new object of type dojox.collections.ArrayList
  13. var items=[];
  14. if(arr) items=items.concat(arr);
  15. this.count=items.length;
  16. this.add=function(/* object */obj){
  17. // summary
  18. // Add an element to the collection.
  19. items.push(obj);
  20. this.count=items.length;
  21. };
  22. this.addRange=function(/* array */a){
  23. // summary
  24. // Add a range of objects to the ArrayList
  25. if(a.getIterator){
  26. var e=a.getIterator();
  27. while(!e.atEnd()){
  28. this.add(e.get());
  29. }
  30. this.count=items.length;
  31. }else{
  32. for(var i=0; i<a.length; i++){
  33. items.push(a[i]);
  34. }
  35. this.count=items.length;
  36. }
  37. };
  38. this.clear=function(){
  39. // summary
  40. // Clear all elements out of the collection, and reset the count.
  41. items.splice(0, items.length);
  42. this.count=0;
  43. };
  44. this.clone=function(){
  45. // summary
  46. // Clone the array list
  47. return new dojox.collections.ArrayList(items); // dojox.collections.ArrayList
  48. };
  49. this.contains=function(/* object */obj){
  50. // summary
  51. // Check to see if the passed object is a member in the ArrayList
  52. for(var i=0; i < items.length; i++){
  53. if(items[i] == obj) {
  54. return true; // bool
  55. }
  56. }
  57. return false; // bool
  58. };
  59. this.forEach=function(/* function */ fn, /* object? */ scope){
  60. // summary
  61. // functional iterator, following the mozilla spec.
  62. dojo.forEach(items, fn, scope);
  63. };
  64. this.getIterator=function(){
  65. // summary
  66. // Get an Iterator for this object
  67. return new dojox.collections.Iterator(items); // dojox.collections.Iterator
  68. };
  69. this.indexOf=function(/* object */obj){
  70. // summary
  71. // Return the numeric index of the passed object; will return -1 if not found.
  72. for(var i=0; i < items.length; i++){
  73. if(items[i] == obj) {
  74. return i; // int
  75. }
  76. }
  77. return -1; // int
  78. };
  79. this.insert=function(/* int */ i, /* object */ obj){
  80. // summary
  81. // Insert the passed object at index i
  82. items.splice(i,0,obj);
  83. this.count=items.length;
  84. };
  85. this.item=function(/* int */ i){
  86. // summary
  87. // return the element at index i
  88. return items[i]; // object
  89. };
  90. this.remove=function(/* object */obj){
  91. // summary
  92. // Look for the passed object, and if found, remove it from the internal array.
  93. var i=this.indexOf(obj);
  94. if(i >=0) {
  95. items.splice(i,1);
  96. }
  97. this.count=items.length;
  98. };
  99. this.removeAt=function(/* int */ i){
  100. // summary
  101. // return an array with function applied to all elements
  102. items.splice(i,1);
  103. this.count=items.length;
  104. };
  105. this.reverse=function(){
  106. // summary
  107. // Reverse the internal array
  108. items.reverse();
  109. };
  110. this.sort=function(/* function? */ fn){
  111. // summary
  112. // sort the internal array
  113. if(fn){
  114. items.sort(fn);
  115. }else{
  116. items.sort();
  117. }
  118. };
  119. this.setByIndex=function(/* int */ i, /* object */ obj){
  120. // summary
  121. // Set an element in the array by the passed index.
  122. items[i]=obj;
  123. this.count=items.length;
  124. };
  125. this.toArray=function(){
  126. // summary
  127. // Return a new array with all of the items of the internal array concatenated.
  128. return [].concat(items);
  129. }
  130. this.toString=function(/* string */ delim){
  131. // summary
  132. // implementation of toString, follows [].toString();
  133. return items.join((delim||","));
  134. };
  135. };
  136. }