Stack.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.Stack"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.collections.Stack"] = true;
  8. dojo.provide("dojox.collections.Stack");
  9. dojo.require("dojox.collections._base");
  10. dojox.collections.Stack=function(/* array? */arr){
  11. // summary
  12. // returns an object of type dojox.collections.Stack
  13. var q=[];
  14. if (arr) q=q.concat(arr);
  15. this.count=q.length;
  16. this.clear=function(){
  17. // summary
  18. // Clear the internal array and reset the count
  19. q=[];
  20. this.count=q.length;
  21. };
  22. this.clone=function(){
  23. // summary
  24. // Create and return a clone of this Stack
  25. return new dojox.collections.Stack(q);
  26. };
  27. this.contains=function(/* object */o){
  28. // summary
  29. // check to see if the stack contains object o
  30. for (var i=0; i<q.length; i++){
  31. if (q[i] == o){
  32. return true; // bool
  33. }
  34. }
  35. return false; // bool
  36. };
  37. this.copyTo=function(/* array */ arr, /* int */ i){
  38. // summary
  39. // copy the stack into array arr at index i
  40. arr.splice(i,0,q);
  41. };
  42. this.forEach=function(/* function */ fn, /* object? */ scope){
  43. // summary
  44. // functional iterator, following the mozilla spec.
  45. dojo.forEach(q, fn, scope);
  46. };
  47. this.getIterator=function(){
  48. // summary
  49. // get an iterator for this collection
  50. return new dojox.collections.Iterator(q); // dojox.collections.Iterator
  51. };
  52. this.peek=function(){
  53. // summary
  54. // Return the next item without altering the stack itself.
  55. return q[(q.length-1)]; // object
  56. };
  57. this.pop=function(){
  58. // summary
  59. // pop and return the next item on the stack
  60. var r=q.pop();
  61. this.count=q.length;
  62. return r; // object
  63. };
  64. this.push=function(/* object */ o){
  65. // summary
  66. // Push object o onto the stack
  67. this.count=q.push(o);
  68. };
  69. this.toArray=function(){
  70. // summary
  71. // create and return an array based on the internal collection
  72. return [].concat(q); // array
  73. };
  74. }
  75. }