edits
[iotcloud.git] / version2 / src / C / SlotBuffer.h
index 2d27476f7bddbfe78db65dd864f82e4650e28450..ecd2b970255fcb6221957a9e19aaf92a2d0e8973 100644 (file)
@@ -1,3 +1,7 @@
+#ifndef SLOTBUFFER_H
+#define SLOTBUFFER_H
+
+#include"common.h"
 
 /**
  * Circular buffer that holds the live set of slots.
  * @version 1.0
  */
 
-class SlotBuffer {
-       static final int DEFAULT_SIZE = 16;
-
-       private Slot[] array;
-       private int head;
-       private int tail;
-       public int64_t oldestseqn;
-
-       SlotBuffer() {
-               array = new Slot[DEFAULT_SIZE + 1];
-               head = tail = 0;
-               oldestseqn = 0;
-       }
-
-       int size() {
-               if (head >= tail)
-                       return head - tail;
-               return (array.length + head) - tail;
-       }
-
-       int capacity() {
-               return array.length - 1;
-       }
-
-       void resize(int newsize) {
-               if (newsize == (array.length - 1))
-                       return;
-
-               Slot[] newarray = new Slot[newsize + 1];
-               int currsize = size();
-               int index = tail;
-               for (int i = 0; i < currsize; i++) {
-                       newarray[i] = array[index];
-                       if ((++index) == array.length)
-                               index = 0;
-               }
-               array = newarray;
-               tail = 0;
-               head = currsize;
-       }
-
-       private void incrementHead() {
-               head++;
-               if (head >= array.length)
-                       head = 0;
-       }
-
-       private void incrementTail() {
-               tail++;
-               if (tail >= array.length)
-                       tail = 0;
-       }
-
-       void putSlot(Slot s) {
-
-               int64_t checkNum = (getNewestSeqNum() + 1);
+#define SlotBuffer_DEFAULT_SIZE 16
 
-               if (checkNum != s.getSequenceNumber()) {
-                       // We have a gap so expunge all our slots
-                       oldestseqn = s.getSequenceNumber();
-                       tail = 0;
-                       head = 1;
-                       array[0] = s;
-                       return;
-               }
-
-               array[head] = s;
-               incrementHead();
-
-               if (oldestseqn == 0) {
-                       oldestseqn = s.getSequenceNumber();
-               }
-
-               if (head == tail) {
-                       incrementTail();
-                       oldestseqn++;
-               }
-       }
-
-       Slot getSlot(int64_t seqnum) {
-               int diff = (int) (seqnum - oldestseqn);
-               int index = diff + tail;
-
-               if (index < 0) {
-                       // Really old message so we dont have it anymore
-                       return NULL;
-               }
-
-               if (index >= array.length) {
-                       if (head >= tail) {
-                               return NULL;
-                       }
-                       index -= array.length;
-               }
-
-               if (index >= array.length) {
-
-                       return NULL;
-               }
-               if (head >= tail && index >= head) {
-                       return NULL;
-               }
-
-               return array[index];
-       }
-
-       int64_t getOldestSeqNum() {
-               return oldestseqn;
-       }
-
-       int64_t getNewestSeqNum() {
-               return oldestseqn + size() - 1;
-       }
-}
+class SlotBuffer {
+ private:
+       Array<Slot *> * array;
+       int32_t head;
+       int32_t tail;
+       void incrementHead();
+       void incrementTail();
+       
+ public:
+       int64_t oldestseqn;
+
+       SlotBuffer();
+       int32_t size();
+       int32_t capacity();
+       void resize(int newsize);
+       void putSlot(Slot *s);
+       Slot * getSlot(int64_t seqnum);
+       int64_t getOldestSeqNum() { return oldestseqn; }
+       int64_t getNewestSeqNum() { return oldestseqn + size() - 1;}
+};
+#endif