#ifndef IuE4Lmq81ZDeAq0u7S79e56kZZQ #define IuE4Lmq81ZDeAq0u7S79e56kZZQ #include #include #include #include "bit_access.hpp" #include "template_helpers.hpp" // size - number of elements // width - size of each elements (in bit!) template 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 inline T Array::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 inline void Array::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