Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Belle2017-10-22 15:13:55 +0000
committerJan Belle2017-10-22 15:13:55 +0000
commit6f34422d3f9216bc5b620c1ce80e998a2e745b40 (patch)
treef75649c4b3e32db64f7287b26735ea63ae2b30eb /runtime
parent92c3dc64f5c9d841fd5823cd923d552bafb0ac08 (diff)
downloadorg.eclipse.etrice-6f34422d3f9216bc5b620c1ce80e998a2e745b40.tar.gz
org.eclipse.etrice-6f34422d3f9216bc5b620c1ce80e998a2e745b40.tar.xz
org.eclipse.etrice-6f34422d3f9216bc5b620c1ce80e998a2e745b40.zip
[runtime.c] static deque implementation
added tests and documentation for static deque Change-Id: I1631e94cc83df8d683ea5da5de415b6ec1dcb0c5
Diffstat (limited to 'runtime')
-rw-r--r--runtime/org.eclipse.etrice.runtime.c/src/common/container/etStaticDeque.c73
-rw-r--r--runtime/org.eclipse.etrice.runtime.c/src/common/container/etStaticDeque.h74
2 files changed, 147 insertions, 0 deletions
diff --git a/runtime/org.eclipse.etrice.runtime.c/src/common/container/etStaticDeque.c b/runtime/org.eclipse.etrice.runtime.c/src/common/container/etStaticDeque.c
new file mode 100644
index 000000000..09d1e5a2a
--- /dev/null
+++ b/runtime/org.eclipse.etrice.runtime.c/src/common/container/etStaticDeque.c
@@ -0,0 +1,73 @@
+/*******************************************************************************
+ * Copyright (c) 2017 protos software gmbh (http://www.protos.de).
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * CONTRIBUTORS:
+ * Jan Belle (initial contribution)
+ *
+ *******************************************************************************/
+
+#include "etStaticDeque.h"
+
+void etStaticDeque_construct(etStaticDeque* const self, void* memory, int size, int objectSize) {
+ self->size = 0;
+ self->first = 0;
+ self->memory = memory;
+ self->maxSize = size;
+ self->objectSize = objectSize;
+
+ etStaticDeque_clear(self);
+}
+
+void etStaticDeque_clear(etStaticDeque* const self) {
+ self->first = 0;
+ self->size = 0;
+}
+
+void* etStaticDeque_get(const etStaticDeque* const self, int position) {
+ // TODO JB: Handle position out of bounds exception
+ return (void*) &self->memory[(self->first + position * self->objectSize) % (self->objectSize * self->maxSize)];
+}
+
+void etStaticDeque_push_front(etStaticDeque* const self, void* object) {
+ // TODO JB: Handle out of memory exception
+ if(self->size < self->maxSize) {
+ ++self->size;
+ self->first = (self->first + (self->maxSize - 1) * self->objectSize) % (self->objectSize * self->maxSize);
+ memcpy(etStaticDeque_front(self), object, self->objectSize);
+ }
+}
+
+void etStaticDeque_push_back(etStaticDeque* const self, void* object) {
+ // TODO JB: Handle out of memory exception
+ if(self->size < self->maxSize) {
+ ++self->size;
+ memcpy(etStaticDeque_back(self), object, self->objectSize);
+ }
+}
+
+void etStaticDeque_pop_front(etStaticDeque* const self) {
+ // TODO JB: Handle deque empty exception
+ if(self->size > 0) {
+ self->first = (self->first + self->objectSize) % (self->objectSize * self->maxSize);
+ --self->size;
+ }
+}
+
+void etStaticDeque_pop_back(etStaticDeque* const self) {
+ // TODO JB: Handle deque empty exception
+ if(self->size > 0) {
+ --self->size;
+ }
+}
+
+void* etStaticDeque_front(const etStaticDeque* const self) {
+ return etStaticDeque_get(self, 0);
+}
+
+void* etStaticDeque_back(const etStaticDeque* const self) {
+ return etStaticDeque_get(self, self->size - 1);
+}
diff --git a/runtime/org.eclipse.etrice.runtime.c/src/common/container/etStaticDeque.h b/runtime/org.eclipse.etrice.runtime.c/src/common/container/etStaticDeque.h
new file mode 100644
index 000000000..23cc4ca67
--- /dev/null
+++ b/runtime/org.eclipse.etrice.runtime.c/src/common/container/etStaticDeque.h
@@ -0,0 +1,74 @@
+/*******************************************************************************
+ * Copyright (c) 2017 protos software gmbh (http://www.protos.de).
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * CONTRIBUTORS:
+ * Jan Belle (initial contribution)
+ *
+ *******************************************************************************/
+
+#ifndef SRC_COMMON_CONTAINERS_ETSTATICDEQUE_H_
+#define SRC_COMMON_CONTAINERS_ETSTATICDEQUE_H_
+
+#include <stdint.h>
+
+/**
+ * Implementation of a double ended queue with a circular buffer
+ */
+typedef struct etStaticDeque {
+ uint8_t* memory; /* memory for the objects */
+ int first; /* index of the first object */
+ int size; /* current size of the queue */
+ int maxSize; /* the maximum size of the queue */
+ int objectSize; /* the maximum size of the objects contained in this queue */
+} etStaticDeque;
+
+/**
+ * Constructs the Deque on the given memory with specified size and objectSize
+ */
+void etStaticDeque_construct(etStaticDeque* self, void* memory, int size, int objectSize);
+
+/**
+ * Clears the Deque, removes all objects
+ */
+void etStaticDeque_clear(etStaticDeque* const self);
+
+/**
+ * Returns a reference to an arbitrary object at the specified position
+ */
+void* etStaticDeque_get(const etStaticDeque* self, int position);
+
+/**
+ * Copies the object and stores it at the front
+ */
+void etStaticDeque_push_front(etStaticDeque* self, void* object);
+
+/**
+ * Copies the object and stores it at the end
+ */
+void etStaticDeque_push_back(etStaticDeque* self, void* object);
+
+/**
+ * Removes the first object
+ */
+void etStaticDeque_pop_front(etStaticDeque* self);
+
+/**
+ * Removes the last element
+ */
+void etStaticDeque_pop_back(etStaticDeque* self);
+
+/**
+ * Returns a reference to the first object
+ */
+void* etStaticDeque_front(const etStaticDeque* self);
+
+/**
+ * Returns a reference to the last object
+ */
+void* etStaticDeque_back(const etStaticDeque* self);
+
+#endif /* SRC_COMMON_CONTAINERS_ETSTATICDEQUE_H_ */

Back to the top