Queue.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * Licensed Materials - Property of IBM IBM Cognos Products: Modeling UI (C)
  3. * Copyright IBM Corp. 2016, 2018 US Government Users Restricted Rights - Use,
  4. * duplication or disclosure restricted by GSA ADP Schedule Contract with IBM
  5. * Corp.
  6. */
  7. define([
  8. 'bi/commons/ui/core/Class'
  9. ], function(Class) {
  10. var Queue = Class.extend({
  11. init: function() {
  12. this._nextOut = 0;
  13. this._lastIn = 0;
  14. this._q = {};
  15. },
  16. /**
  17. * Place a new item in the queue
  18. * @param {Object} Object to be placed in the queue
  19. */
  20. enqueue: function(data) {
  21. this._q[this._lastIn] = data;
  22. this._lastIn++;
  23. },
  24. /**
  25. * Remove the next element from the queue, if one exists
  26. * @returns {Object} Object at the head of the queue
  27. */
  28. dequeue: function() {
  29. var data;
  30. if (this.size() > 0) {
  31. data = this._q[this._nextOut];
  32. delete this._q[this._nextOut];
  33. this._nextOut++;
  34. }
  35. return data;
  36. },
  37. /**
  38. * Get the number of elements in the queue
  39. * @returns {Number} Size of the queue
  40. */
  41. size: function() {
  42. return this._lastIn - this._nextOut;
  43. },
  44. /**
  45. * Returns true if the queue is empty (size === 0)
  46. * @returns {Boolean} True if queue is empty
  47. */
  48. isEmpty: function() {
  49. return this.size() <= 0;
  50. }
  51. });
  52. return Queue;
  53. });