Cleaned up git
[iotcloud.git] / version1 / src / js / iotjs / src / slotbuffer.js
diff --git a/version1/src/js/iotjs/src/slotbuffer.js b/version1/src/js/iotjs/src/slotbuffer.js
new file mode 100644 (file)
index 0000000..9704852
--- /dev/null
@@ -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 >= this.tail) {
+                               return this.head - this.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 >= this.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