UNPKG

sangja

Version:

JavaScript data structures library

172 lines (151 loc) 4.13 kB
const Utils = require('./utils'); const LinkedList = require('./linked-list'); /** * @class * @memberof sangja */ class Queue { /** * Creates a new Queue. * Iterable parameter is optional. * @constructor * @param {iterable} [iterable] - Iterator for initialize the queue. * @throws {TypeError} When given parameter is not queue. */ constructor(iterable = []) { if (!Utils.isIterable(iterable)) { throw TypeError(); } this._linkedList = new LinkedList(iterable); } /** * Add value at the rear of the queue. * @param {*} value - The value to enqueue to the queue. */ enqueue(value) { this._linkedList.addLast(value); } /** * Add values in the given iterator at the rear of the queue. * @param {iterable} iterable - The iterable values to enqueue */ enqueueAll(iterable) { [...iterable].forEach(v => this._linkedList.addLast(v)); } /** * Removes the front of the queue and returns the value at the front of the queue. * @returns {*} The value at the front of the queue. If empty, return undefined. */ dequeue() { if (this._linkedList.size() === 0) { return undefined; } return this._linkedList.popFirst(); } /** * Returns the value at the front of the queue without changing the state of the queue. * @returns {*} The value at the front of the queue. If empty, return undefined. */ peek() { if (this._linkedList.size() === 0) { return undefined; } return this._linkedList.getFirst(); } /** * Returns the number of elements in the queue. * @returns {number} The number of elements in the queue. */ size() { return this._linkedList.size(); } /** * Returns whether the queue is empty. * @returns {boolean} True if the queue is empty. */ isEmpty() { return this._linkedList.isEmpty(); } /** * Removes all values in the queue. */ clear() { this._linkedList = new LinkedList(); } /** * For each values in the queue, execute the given procedure f. * @param {function} f - Procedure to execute */ forEach(f) { this._linkedList.forEach(f); } /** * Returns a new Queue whose values are mapped with given function f. * @param {function} f - Function to map values * @return {Stack} Queue([mapped values]) */ map(f) { const queue = new Queue(); queue._linkedList = this._linkedList.map(f); return queue; } /** * Returns a new Queue whose values are mapped with given function f and flattened. * @param {function} f - Function (this.value) => iterable * @return {Stack} Queue([mapped and flattened values]) */ flatMap(f) { const queue = new Queue(); queue._linkedList = this._linkedList.flatMap(f); return queue; } /** * Returns a new Queue whose values are filtered with given predicate f. * @param {function} f - Predicate (this.value) => boolean * @return {Stack} Queue([filtered values]) */ filter(f) { const queue = new Queue(); queue._linkedList = this._linkedList.filter(f); return queue; } /** * Returns a new Queue whose values are reversed in order. * @return {Stack} Queue([reversed values]) */ reversed() { const queue = new Queue(); queue._linkedList = this._linkedList.reversed(); return queue; } /** * If any of containing values satisfies given f, return true. * If none of values satisfy f or not contain any value, return false. * @param {function} f - Predicate * @returns {boolean} */ some(f) { return this._linkedList.some(f); } /** * If all of containing values satisfies given f or not contain any value, return true. * If any of value doesn't satisfy f, return false. * @param {function} f - Predicate * @returns {boolean} */ every(f) { return this._linkedList.every(f); } /** * If contains given value v, return true. * @param {*} v * @returns {boolean} */ includes(v) { return this._linkedList.includes(v); } * [Symbol.iterator]() { yield* this._linkedList; } } module.exports = Queue;