Cleaned up git
[iotcloud.git] / version1 / src / js / iotjs / orig / slotbuffer.js
diff --git a/version1/src/js/iotjs/orig/slotbuffer.js b/version1/src/js/iotjs/orig/slotbuffer.js
new file mode 100644 (file)
index 0000000..1e76971
--- /dev/null
@@ -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>=this.tail){
+                       return this.head-this.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<currsize ; i++){
+                       newarray[i]=this.array[index];
+                       if((++index) === this.array.length){
+                               index = 0;
+                       }
+                       this.array = newarray;
+                       this.tail=0;
+                       this.head=currsize; 
+               }
+       }
+       incrementHead(){
+               this.head++;
+               if(this.head >= 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 >= this.head){
+                       return null;
+               }
+
+               return this.array[index];
+       }
+
+       getOldestSeqNum(){
+               return this.oldestseqn;
+       }
+       getNewestSeqNum(){
+               return this.oldestseqn + this.size() -1
+       }
+}
\ No newline at end of file