Skip to content
fifo.js 3.66 KiB
Newer Older
var fifo = function () {
    /**
Yuanle Song's avatar
Yuanle Song committed
     * an unbounded fifo queue. Use push and pop to add or remove elements.
     * you can also read the raw elements in this.data.
     */
    const Queue = function () {
	this.data = [];
	this.push = function (e) {
	    this.data.push(e);
	};
	/**
	 * note: pop() will return undefined when there is not enough element.
	 */
	this.pop = function () {
	    return this.data.shift();
	};
	this.clear = function () {
	    this.data = [];
	};
Yuanle Song's avatar
Yuanle Song committed
	this.clone = function () {
	    const r = new Queue();
	    r.data = this.data.slice(0);
	    return r;
	};
Yuanle Song's avatar
Yuanle Song committed
     * a bounded fifo queue. Use push and pop to add or remove elements.
     * you can also read the raw elements in this.data.
     */
    const BoundedQueue = function (capacity) {
	this.capacity = capacity;
	this.data = [];
	/**
	 * push new element to the BoundedQueue. if there is no room for it,
	 * remove the oldest element from the queue.
	 */
	this.push = function (e) {
	    console.assert(this.data.length <= this.capacity, "bounded queue data size is invalid");
	    if (this.data.length === this.capacity) {
		this.data.shift();
	    }
	    this.data.push(e);
	};
	/**
	 * note: pop() will return undefined when there is not enough element.
	 */
	this.pop = function () {
	    return this.data.shift();
	};
	this.clear = function () {
	    this.data = [];
	};
Yuanle Song's avatar
Yuanle Song committed
	this.clone = function () {
	    const r = new BoundedQueue(this.capacity);
	    r.data = this.data.slice(0);
	    return r;
	};
    };

    /**
     * a bounded stack. Use push and pop to add or remove elements.
     * when stack is full, it discard early entries.
     * you can also read the raw elements in this.data.
     */
    const BoundedStack = function (capacity) {
	this.capacity = capacity;
	this.data = [];
	/**
	 * push new element to the BoundedStack. if there is no room for it,
	 * remove the oldest element from the stack.
	 */
	this.push = function (e) {
	    console.assert(this.data.length <= this.capacity, "bounded stack data size is invalid");
	    if (this.data.length === this.capacity) {
		this.data.shift();
	    }
	    this.data.push(e);
	};
	/**
	 * note: pop() will return undefined when there is not enough element.
	 */
	this.pop = function () {
	    return this.data.pop();
	};
	this.clear = function () {
	    this.data = [];
	};
	this.clone = function () {
	    const r = new BoundedStack(this.capacity);
	    r.data = this.data.slice(0);
	    return r;
	};
    };

    const testFIFO = function () {
	var q = new Queue();
	q.push(1);
	q.push(2);
	q.push(3);
	console.assert(q.pop() === 1);
	console.assert(q.pop() === 2);
	console.assert(q.pop() === 3);

	q = new BoundedQueue(3);
	q.push(1);
	q.push(2);
	q.push(3);
	console.assert(q.pop() === 1);
	console.assert(q.pop() === 2);
	console.assert(q.pop() === 3);

	q = new BoundedQueue(3);
	q.push(1);
	q.push(2);
	q.push(3);
	q.push(4);
	console.assert(q.pop() === 2);
	console.assert(q.pop() === 3);
	console.assert(q.pop() === 4);
Yuanle Song's avatar
Yuanle Song committed

	q = new BoundedStack(3);
	q.push(1);
	q.push(2);
	q.push(3);
	console.assert(q.pop() === 3);
	console.assert(q.pop() === 2);
	console.assert(q.pop() === 1);

	q = new BoundedStack(3);
	q.push(1);
	q.push(2);
	q.push(3);
	q.push(4);
	console.assert(q.pop() === 4);
	console.assert(q.pop() === 3);
	console.assert(q.pop() === 2);
    const testClearFunction = function () {
	q = new BoundedQueue(3);
	q.push(1);
	q.push(2);
	q.clear();
	console.assert(q.pop() === undefined);
	console.assert(q.data.length === 0);
    };
    const runAllTests = function () {
	testFIFO();
	testClearFunction();
    };

    // run all tests
    runAllTests();

    return {
	Queue: Queue,
	BoundedQueue: BoundedQueue,
Yuanle Song's avatar
Yuanle Song committed
	BoundedStack: BoundedStack,