Cleaned up git
[iotcloud.git] / version1 / src / js / iotjs / src / slotbuffer.js
1 "use strict";
2
3 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; }; }();
4
5 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
7 var SlotBuffer = function () {
8         function SlotBuffer() {
9                 _classCallCheck(this, SlotBuffer);
10
11                 this.DEFAULT_SIZE = 128;
12                 this.array = [];
13                 this.head = 0;
14                 this.tail = 0;
15                 this.oldestseqn = 0;
16         }
17
18         _createClass(SlotBuffer, [{
19                 key: "size",
20                 value: function size() {
21                         if (this.head >= this.tail) {
22                                 return this.head - this.tail;
23                         }
24                         return this.array.length + this.head - this.tail;
25                 }
26         }, {
27                 key: "capacity",
28                 value: function capacity() {
29                         return this.array.length - 1;
30                 }
31         }, {
32                 key: "resize",
33                 value: function resize(newsize) {
34                         if (newsize === this.array.length - 1) {
35                                 return;
36                         }
37                         var newarray = [];
38                         var currsize = this.size();
39                         var index = this.tail;
40                         for (var i = 0; i < currsize; i++) {
41                                 newarray[i] = this.array[index];
42                                 if (++index === this.array.length) {
43                                         index = 0;
44                                 }
45                                 this.array = newarray;
46                                 this.tail = 0;
47                                 this.head = currsize;
48                         }
49                 }
50         }, {
51                 key: "incrementHead",
52                 value: function incrementHead() {
53                         this.head++;
54                         if (this.head >= this.array.length) {
55                                 this.head = 0;
56                         }
57                 }
58         }, {
59                 key: "incrementTail",
60                 value: function incrementTail() {
61                         this.tail++;
62                         if (this.tail >= this.array.length) {
63                                 this.tail = 0;
64                         }
65                 }
66         }, {
67                 key: "putSlot",
68                 value: function putSlot(s) {
69                         if (!(s instanceof Slot)) {
70                                 throw new Error("Error with arguments. Argument should be a slot object");
71                         }
72                         this.array[this.head] = s;
73                         this.incrementHead();
74
75                         if (this.oldestseqn === 0) {
76                                 this.oldestseqn = s.getSequenceNumber();
77                         }
78
79                         if (this.head === this.tail) {
80                                 this.incrementTail();
81                                 this.oldestseqn++;
82                         }
83                 }
84         }, {
85                 key: "getSlot",
86                 value: function getSlot(seqnum) {
87                         var diff = seqnum - this.oldestseqn;
88                         var index = diff + this.tail;
89                         if (index >= this.array.length) {
90                                 if (this.head >= this.tail) {
91                                         return null;
92                                 }
93                                 index = index - this.array.length;
94                         }
95                         if (index >= this.array.length) {
96                                 return null;
97                         }
98
99                         if (this.head >= this.tail && index >= this.head) {
100                                 return null;
101                         }
102
103                         return this.array[index];
104                 }
105         }, {
106                 key: "getOldestSeqNum",
107                 value: function getOldestSeqNum() {
108                         return this.oldestseqn;
109                 }
110         }, {
111                 key: "getNewestSeqNum",
112                 value: function getNewestSeqNum() {
113                         return this.oldestseqn + this.size() - 1;
114                 }
115         }]);
116
117         return SlotBuffer;
118 }();