summaryrefslogtreecommitdiff
path: root/core/array.hpp
diff options
context:
space:
mode:
authorJan Huwald <jh@sotun.de>2012-05-07 20:01:51 (GMT)
committerJan Huwald <jh@sotun.de>2012-05-07 20:01:51 (GMT)
commit420d2ef464d4a741028e132e662d5626806a41f5 (patch)
tree1aca6eb512e4ed0fb5f3c10c528cb998b6ffd695 /core/array.hpp
Initial commitHEADmaster
Diffstat (limited to 'core/array.hpp')
-rw-r--r--core/array.hpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/core/array.hpp b/core/array.hpp
new file mode 100644
index 0000000..e882f92
--- /dev/null
+++ b/core/array.hpp
@@ -0,0 +1,53 @@
+#ifndef IuE4Lmq81ZDeAq0u7S79e56kZZQ
+#define IuE4Lmq81ZDeAq0u7S79e56kZZQ
+
+#include <inttypes.h>
+#include <assert.h>
+#include <boost/static_assert.hpp>
+
+#include "bit_access.hpp"
+#include "template_helpers.hpp"
+
+// size - number of elements
+// width - size of each elements (in bit!)
+template<typename T, uint32_t size, size_t width = 8 * sizeof(T)>
+class Array {
+public:
+ typedef uint32_t ptr_t;
+
+ // reader and writer
+ inline T get(ptr_t id);
+ inline void set(ptr_t id, T value);
+
+ // allow accessing raw memory (this also encodes the correct size of the object)
+ uint8_t mem[ size * ((width + 7) / 8) ];
+
+ // garantuee that all sizes = 2^n
+ STATIC_ASSERT_IS_POWER_OF_TWO(width);
+ STATIC_ASSERT_IS_POWER_OF_TWO(size);
+ // garantue that we do not need bit-level access of the underlying container
+ BOOST_STATIC_ASSERT((uint64_t) width * size >= 8);
+};
+
+template<typename T, uint32_t size, size_t width>
+inline T Array<T, size, width>::get(ptr_t id) {
+ assert(id < size);
+ if (width < 8) {
+ uint8_t res = bit_extract(mem, (uint8_t) width, id);
+ return FORCE_CONV(T, res);
+ }else{
+ return *((T*) (((char*) mem) + (width / 8) * id)); // TO CHECK: improve cast?
+ }
+}
+
+template<typename T, uint32_t size, size_t width>
+inline void Array<T, size, width>::set(ptr_t id, T value) {
+ assert(id < size);
+ if (width < 8) {
+ bit_insert(mem, (uint8_t) width, id, value);
+ }else{
+ *((T*) (((char*) mem) + (width / 8) * id)) = value;
+ }
+}
+
+#endif // IuE4Lmq81ZDeAq0u7S79e56kZZQ
contact: Jan Huwald // Impressum