bfc8827141315ca76dd4cfd49b9fb9ad47931e94.svn-base 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. if (!dojo._hasResource["dojox.collections.Stack"]) { // _hasResource checks
  2. // added by build. Do
  3. // not use _hasResource
  4. // directly in your
  5. // code.
  6. dojo._hasResource["dojox.collections.Stack"] = true;
  7. dojo.provide("dojox.collections.Stack");
  8. dojo.require("dojox.collections._base");
  9. dojox.collections.Stack = function(/* array? */arr) {
  10. // summary
  11. // returns an object of type dojox.collections.Stack
  12. var q = [];
  13. if (arr)
  14. 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. }