Queue.js 2.1 KB

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