Commit 6c54fb04 authored by Yuanle Song's avatar Yuanle Song
Browse files

add javascript fifo implementation

parent 44dc983d
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -117,7 +117,8 @@
      </div>
    </div>
    <script type="text/javascript" src="vendor/jquery.min.js"></script>
    <script type="text/javascript" src="fsm.js"></script>
    <script type="text/javascript" src="fifo.js"></script>
    <script type="text/javascript" src="fsm.js"></script> <!-- fifo.js is required by fsm.js -->
    <script type="text/javascript">
      (function () {
	  /**

fifo.js

0 → 100644
+77 −0
Original line number Diff line number Diff line
var fifo = function () {
    /**
     * an unbounded fifo queue.
     */
    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();
	};
    };

    /**
     * a bounded fifo queue.
     */
    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();
	};
    };

    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);
    };

    // run all tests
    testFIFO();

    return {
	Queue: Queue,
	BoundedQueue: BoundedQueue,
    };
}();
+19 −1
Original line number Diff line number Diff line
@@ -48,8 +48,26 @@ the parent DOM.

* current                                                             :entry:
** 
** 2017-01-27 make trail work
- when is trail being updated?
  - when a number is committed to number stack. return.
  - when a new number is pushed to stack as an op result. plus, change-sign.
  - swap doesn't change trail.
- this should be part of the FSM.
  what data should I store?
  it's a queue of [(Maybe op, number)].

  as mentioned previously, this data can be made persistent across
  pages/sessions.
- first write a bounded fifo queue in javascript.
- load persistent data for trails when init the FSM.
- update the persistent data when trails has changed.

  // is updating local storage a heavy operation? I hope it doesn't sync to
  disk everytime I update a key.

** 2017-01-26 make trail persistent
trail is a sized fifo queue, it just store recent 1k entries.
trail is a bounded fifo queue, it just store recent 1k entries.
when multiple instances of calculator is working. they all write to a single
trail cache. it's not a problem.