Stack.js 1.8 KB

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