rename files
[iotcloud.git] / version2 / src / C / SlotBuffer.cpp
diff --git a/version2/src/C/SlotBuffer.cpp b/version2/src/C/SlotBuffer.cpp
new file mode 100644 (file)
index 0000000..06c46e9
--- /dev/null
@@ -0,0 +1,122 @@
+#include "SlotBuffer.h"
+#include "Slot.h"
+/**
+ * Circular buffer that holds the live set of slots.
+ * @author Brian Demsky
+ * @version 1.0
+ */
+
+SlotBuffer::SlotBuffer() :
+       array(new Array<Slot *>(SlotBuffer_DEFAULT_SIZE + 1)),
+       head(0),
+       tail(0),
+       oldestseqn(0) {
+}
+
+SlotBuffer::~SlotBuffer() {
+       int32_t index = tail;
+       while (index != head) {
+               delete array->get(index);
+               index++;
+               if (index == (int32_t) array->length())
+                       index = 0;
+       }
+       delete array;
+}
+
+int SlotBuffer::size() {
+       if (head >= tail)
+               return head - tail;
+       return (array->length() + head) - tail;
+}
+
+int SlotBuffer::capacity() {
+       return array->length() - 1;
+}
+
+void SlotBuffer::resize(int newsize) {
+       if ((uint32_t)newsize == (array->length() - 1))
+               return;
+
+       Array<Slot *> *newarray = new Array<Slot *>(newsize + 1);
+       int currsize = size();
+       int index = tail;
+       for (int i = 0; i < currsize; i++) {
+               newarray->set(i, array->get(index));
+               if (((uint32_t)++ index) == array->length())
+                       index = 0;
+       }
+       array = newarray;
+       tail = 0;
+       head = currsize;
+}
+
+void SlotBuffer::incrementHead() {
+       head++;
+       if (((uint32_t)head) >= array->length())
+               head = 0;
+}
+
+void SlotBuffer::incrementTail() {
+       delete array->get(tail);
+       tail++;
+       if (((uint32_t)tail) >= array->length())
+               tail = 0;
+}
+
+void SlotBuffer::putSlot(Slot *s) {
+       int64_t checkNum = (getNewestSeqNum() + 1);
+
+       if (checkNum != s->getSequenceNumber()) {
+               int32_t index = tail;
+               while (index != head) {
+                       delete array->get(index);
+                       index++;
+                       if (index == (int32_t) array->length())
+                               index = 0;
+               }
+               oldestseqn = s->getSequenceNumber();
+               tail = 0;
+               head = 1;
+               array->set(0, s);
+               return;
+       }
+
+       array->set(head, s);
+       incrementHead();
+
+       if (oldestseqn == 0) {
+               oldestseqn = s->getSequenceNumber();
+       }
+
+       if (head == tail) {
+               incrementTail();
+               oldestseqn++;
+       }
+}
+
+Slot *SlotBuffer::getSlot(int64_t seqnum) {
+       int32_t diff = (int32_t) (seqnum - oldestseqn);
+       int32_t index = diff + tail;
+
+       if (index < 0) {
+               return NULL;
+       }
+
+       if (((uint32_t)index) >= array->length()) {
+               if (head >= tail) {
+                       return NULL;
+               }
+               index -= (int32_t) array->length();
+       }
+
+       if (((uint32_t)index) >= array->length()) {
+               return NULL;
+       }
+
+       if (head >= tail && index >= head) {
+               return NULL;
+       }
+
+       return array->get(index);
+}