Queue.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. define("dojox/collections/Queue", ["dojo/_base/kernel", "dojo/_base/array", "./_base"], function(dojo, darray, dxc){
  2. /*=====
  3. var dxc = dojox.collections;
  4. =====*/
  5. dxc.Queue=function(/* array? */arr){
  6. // summary
  7. // return an object of type dojox.collections.Queue
  8. var q=[];
  9. if (arr){
  10. q=q.concat(arr);
  11. }
  12. this.count=q.length;
  13. this.clear=function(){
  14. // summary
  15. // clears the internal collection
  16. q=[];
  17. this.count=q.length;
  18. };
  19. this.clone=function(){
  20. // summary
  21. // creates a new Queue based on this one
  22. return new dxc.Queue(q); // dojox.collections.Queue
  23. };
  24. this.contains=function(/* object */ o){
  25. // summary
  26. // Check to see if the passed object is an element in this queue
  27. for(var i=0; i<q.length; i++){
  28. if (q[i]==o){
  29. return true; // bool
  30. }
  31. }
  32. return false; // bool
  33. };
  34. this.copyTo=function(/* array */ arr, /* int */ i){
  35. // summary
  36. // Copy the contents of this queue into the passed array at index i.
  37. arr.splice(i,0,q);
  38. };
  39. this.dequeue=function(){
  40. // summary
  41. // shift the first element off the queue and return it
  42. var r=q.shift();
  43. this.count=q.length;
  44. return r; // object
  45. };
  46. this.enqueue=function(/* object */ o){
  47. // summary
  48. // put the passed object at the end of the queue
  49. this.count=q.push(o);
  50. };
  51. this.forEach=function(/* function */ fn, /* object? */ scope){
  52. // summary
  53. // functional iterator, following the mozilla spec.
  54. dojo.forEach(q, fn, scope);
  55. };
  56. this.getIterator=function(){
  57. // summary
  58. // get an Iterator based on this queue.
  59. return new dxc.Iterator(q); // dojox.collections.Iterator
  60. };
  61. this.peek=function(){
  62. // summary
  63. // get the next element in the queue without altering the queue.
  64. return q[0];
  65. };
  66. this.toArray=function(){
  67. // summary
  68. // return an array based on the internal array of the queue.
  69. return [].concat(q);
  70. };
  71. };
  72. return dxc.Queue;
  73. });