From: joelbandi Date: Tue, 2 Aug 2016 10:54:52 +0000 (-0700) Subject: Added slotbuffer.js X-Git-Url: http://plrg.eecs.uci.edu/git/?p=iotcloud.git;a=commitdiff_plain;h=2bd6f7a0cbf2b44d6e12d4cb1445d8f442bba859 Added slotbuffer.js --- diff --git a/src/js/iotjs/orig/slotbuffer.js b/src/js/iotjs/orig/slotbuffer.js new file mode 100644 index 0000000..7c29463 --- /dev/null +++ b/src/js/iotjs/orig/slotbuffer.js @@ -0,0 +1,89 @@ +class SlotBuffer{ + constructor(){ + this.DEFAULT_SIZE = 128; + this.array = []; + this.head=0; + this.tail=0; + this.oldestseqn = 0; + } + size(){ + if(this.head>=tail){ + return head-tail; + } + return (this.array.length + this.head - this.tail); + } + capacity(){ + return this.array.length-1; + } + resize(newsize){ + if(newsize === (this.array.length-1)){ + return; + } + var newarray = []; + var currsize = this.size(); + var index = this.tail; + for(let i = 0 ; i= this.array.length){ + this.head=0; + } + } + incrementTail(){ + this.tail++; + if(this.tail >= this.array.length){ + this.tail=0; + } + } + putSlot(s){ + if(!(s instanceof Slot)){ + throw new Error("Error with arguments. Argument should be a slot object"); + } + this.array[this.head]=s; + this.incrementHead(); + + if(this.oldestseqn ===0){ + this.oldestseqn = s.getSequenceNumber(); + } + + if(this.head === this.tail){ + this.incrementTail(); + this.oldestseqn++; + } + } + getSlot(seqnum){ + var diff = (seqnum - this.oldestseqn); + var index = diff + this.tail; + if(index >= this.array.length){ + if(this.head >= this.tail){ + return null; + } + index = index - this.array.length; + } + if(index >= this.array.length){ + return null; + } + + if(this.head >= this.tail && index >= head){ + return null; + } + + return this.array[index]; + } + + getOldestSeqNum(){ + return this.oldestseqn; + } + getNewestSeqNum(){ + return this.oldestseqn + this.size() -1 + } +} \ No newline at end of file diff --git a/src/js/iotjs/src/slotbuffer.js b/src/js/iotjs/src/slotbuffer.js new file mode 100644 index 0000000..18288ff --- /dev/null +++ b/src/js/iotjs/src/slotbuffer.js @@ -0,0 +1,118 @@ +"use strict"; + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +var SlotBuffer = function () { + function SlotBuffer() { + _classCallCheck(this, SlotBuffer); + + this.DEFAULT_SIZE = 128; + this.array = []; + this.head = 0; + this.tail = 0; + this.oldestseqn = 0; + } + + _createClass(SlotBuffer, [{ + key: "size", + value: function size() { + if (this.head >= tail) { + return head - tail; + } + return this.array.length + this.head - this.tail; + } + }, { + key: "capacity", + value: function capacity() { + return this.array.length - 1; + } + }, { + key: "resize", + value: function resize(newsize) { + if (newsize === this.array.length - 1) { + return; + } + var newarray = []; + var currsize = this.size(); + var index = this.tail; + for (var i = 0; i < currsize; i++) { + newarray[i] = this.array[index]; + if (++index === this.array.length) { + index = 0; + } + this.array = newarray; + this.tail = 0; + this.head = currsize; + } + } + }, { + key: "incrementHead", + value: function incrementHead() { + this.head++; + if (this.head >= this.array.length) { + this.head = 0; + } + } + }, { + key: "incrementTail", + value: function incrementTail() { + this.tail++; + if (this.tail >= this.array.length) { + this.tail = 0; + } + } + }, { + key: "putSlot", + value: function putSlot(s) { + if (!(s instanceof Slot)) { + throw new Error("Error with arguments. Argument should be a slot object"); + } + this.array[this.head] = s; + this.incrementHead(); + + if (this.oldestseqn === 0) { + this.oldestseqn = s.getSequenceNumber(); + } + + if (this.head === this.tail) { + this.incrementTail(); + this.oldestseqn++; + } + } + }, { + key: "getSlot", + value: function getSlot(seqnum) { + var diff = seqnum - this.oldestseqn; + var index = diff + this.tail; + if (index >= this.array.length) { + if (this.head >= this.tail) { + return null; + } + index = index - this.array.length; + } + if (index >= this.array.length) { + return null; + } + + if (this.head >= this.tail && index >= head) { + return null; + } + + return this.array[index]; + } + }, { + key: "getOldestSeqNum", + value: function getOldestSeqNum() { + return this.oldestseqn; + } + }, { + key: "getNewestSeqNum", + value: function getNewestSeqNum() { + return this.oldestseqn + this.size() - 1; + } + }]); + + return SlotBuffer; +}(); \ No newline at end of file