-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Queue): Adds Queue datastructure
- Loading branch information
Showing
7 changed files
with
94 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import { | |
Embed, | ||
Prompt, | ||
MessageCollector, | ||
Queue, | ||
FunctionQueue, | ||
AutoQueue, | ||
AsyncQueue, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import { | |
Embed, | ||
Prompt, | ||
MessageCollector, | ||
Queue, | ||
FunctionQueue, | ||
AutoQueue, | ||
AsyncQueue, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Queue class. | ||
* | ||
* @author KhaaZ | ||
* | ||
* @template T | ||
* | ||
* @prop {Array<T>} _elements | ||
* @prop {Boolean} max | ||
* @prop {Boolean} replaceOnMax | ||
* | ||
* @class Queue | ||
*/ | ||
class Queue { | ||
/** | ||
* Creates an instance of Queue. | ||
* | ||
* @param {Number} [max=null] - Maximum number of elements that can be added in this queue | ||
* @param {Boolean} [replaceOnMax=true] - Whether to replace value when adding if max is reached (dequeue first element and queue new element) | ||
* @memberof Queue | ||
*/ | ||
constructor(max = null, replaceOnMax = true) { | ||
this._elements = []; | ||
this.max = max; | ||
this.replaceOnMax = replaceOnMax; | ||
} | ||
|
||
/** | ||
* Returns the Queue size | ||
* | ||
* @readonly | ||
* @type {Number} | ||
* @memberof Queue | ||
*/ | ||
get size() { | ||
return this._elements.length; | ||
} | ||
|
||
/** | ||
* Return first element of this queue | ||
* | ||
* @returns {T} | ||
* @memberof Queue | ||
*/ | ||
first() { | ||
return this._elements[0]; | ||
} | ||
|
||
/** | ||
* Queue up an element. | ||
* | ||
* @param {T} elem | ||
* @param {Boolean} replaceOnMax | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
* @returns {Boolean} Whether element was successfully added | ||
* @memberof Queue | ||
*/ | ||
queue(elem, replaceOnMax) { | ||
if (this.max && this._elements.length === this.max) { | ||
if ( (replaceOnMax !== undefined) ? replaceOnMax : this.replaceOnMax) { | ||
this._elements.shift(); | ||
} else { | ||
return false; | ||
} | ||
} | ||
this._elements.push(elem); | ||
return true; | ||
} | ||
|
||
/** | ||
* Dequeue an element and returns it | ||
* | ||
* @returns {T} | ||
* @memberof Queue | ||
*/ | ||
dequeue() { | ||
return this._elements.shift(); | ||
} | ||
} | ||
|
||
export default Queue; |
Array<Queue>