| 1 |
/** |
| 2 |
Adapted and extended from the work of Stephen Morley - http://code.stephenmorley.org/javascript/queues/ |
| 3 |
|
| 4 |
Queue.js |
| 5 |
|
| 6 |
A function to represent a queue |
| 7 |
|
| 8 |
Created by Stephen Morley - http://code.stephenmorley.org/ - and released under |
| 9 |
the terms of the CC0 1.0 Universal legal code: |
| 10 |
|
| 11 |
http://creativecommons.org/publicdomain/zero/1.0/legalcode |
| 12 |
*/ |
| 13 |
|
| 14 |
/** |
| 15 |
* Creates a new queue. A queue is a first-in-first-out (FIFO) data structure - |
| 16 |
* items are added to the end of the queue and removed from the front. |
| 17 |
* |
| 18 |
* @return {void} |
| 19 |
*/ |
| 20 |
function Updraft_Queue() { |
| 21 |
|
| 22 |
// Initialise the queue and offset. |
| 23 |
var queue = []; |
| 24 |
var offset = 0; |
| 25 |
var locked = false; |
| 26 |
|
| 27 |
/** |
| 28 |
* Returns the length of the queue. |
| 29 |
* |
| 30 |
* @returns {number} - the length of the queue |
| 31 |
*/ |
| 32 |
this.get_length = function () { |
| 33 |
return (queue.length - offset); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Query whether the queue is empty or not |
| 38 |
* |
| 39 |
* @returns {boolean} - returns true if the queue is empty, and false otherwise. |
| 40 |
*/ |
| 41 |
this.is_empty = function () { |
| 42 |
return (queue.length == 0); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Enqueues the specified item. The parameter is: |
| 47 |
* |
| 48 |
* @param {*} item The item to enqueue |
| 49 |
* |
| 50 |
* @return {void} |
| 51 |
*/ |
| 52 |
this.enqueue = function (item) { |
| 53 |
queue.push(item); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns the queue lock status |
| 58 |
* |
| 59 |
* @returns {boolean} - whether the queue is locked or not |
| 60 |
*/ |
| 61 |
this.is_locked = function () { |
| 62 |
return locked; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Attempt to get the queue lock |
| 67 |
* |
| 68 |
* @returns {boolean} - whether the attempt succeeded or not |
| 69 |
*/ |
| 70 |
this.get_lock = function () { |
| 71 |
if (locked) { return false; } |
| 72 |
this.lock(); |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Dequeues an item and returns it. If the queue is empty, the value |
| 78 |
* 'undefined' is returned. |
| 79 |
* |
| 80 |
* @returns {*} - returns and removes the item at the front of the queue, or undefined if the queue is empty |
| 81 |
*/ |
| 82 |
this.dequeue = function () { |
| 83 |
|
| 84 |
// If the queue is empty, return immediately. |
| 85 |
if (queue.length == 0) return undefined; |
| 86 |
|
| 87 |
// Store the item at the front of the queue. |
| 88 |
var item = queue[offset]; |
| 89 |
|
| 90 |
// Increment the offset and remove the free space if necessary. |
| 91 |
if ((++offset * 2) >= queue.length) { |
| 92 |
queue = queue.slice(offset); |
| 93 |
offset = 0; |
| 94 |
} |
| 95 |
|
| 96 |
// Return the dequeued item. |
| 97 |
return item; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Lock the queue |
| 102 |
* |
| 103 |
* @returns {void} |
| 104 |
*/ |
| 105 |
this.lock = function () { |
| 106 |
locked = true; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Unlock the queue |
| 111 |
* |
| 112 |
* @returns {void} |
| 113 |
*/ |
| 114 |
this.unlock = function () { |
| 115 |
locked = false; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Returns the item at the front of the queue (without dequeuing it). If the |
| 120 |
* queue is empty then undefined is returned. |
| 121 |
* |
| 122 |
* @returns {*} - returns the item at the front of the queue, or undefined if the queue is empty |
| 123 |
*/ |
| 124 |
this.peek = function () { |
| 125 |
return (queue.length > 0 ? queue[offset] : undefined); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Replaces the item at the front of the queue (if any) |
| 130 |
* |
| 131 |
* @param {*} item The item to put at the front of the queue. |
| 132 |
* |
| 133 |
* @return {boolean} Whether or not the item was successfully replaced. |
| 134 |
*/ |
| 135 |
this.replace_front = function (item) { |
| 136 |
if (queue.length < 1) { return false; } |
| 137 |
queue[offset] = item; |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Checks if any queue object/element contains the ID |
| 143 |
* |
| 144 |
* @param {string} id The ID to search for. |
| 145 |
* |
| 146 |
* @return {boolean} Whether any queue object/element contains the ID. |
| 147 |
*/ |
| 148 |
this.contains_id = function (id) { |
| 149 |
return queue.some(function (ele) { |
| 150 |
return ele.optimization_id === id; |
| 151 |
}); |
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
|