diff options
Diffstat (limited to 'core/vector.hpp')
-rw-r--r-- | core/vector.hpp | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/core/vector.hpp b/core/vector.hpp new file mode 100644 index 0000000..b6c7ccb --- /dev/null +++ b/core/vector.hpp @@ -0,0 +1,106 @@ +#ifndef ZnnjWYk20gszrSSZENMjL1hM9gI +#define ZnnjWYk20gszrSSZENMjL1hM9gI + +#include <inttypes.h> +#include <iostream> +#include <assert.h> +#include <boost/static_assert.hpp> +#include <boost/mpl/if.hpp> +#include <string> + +#include "bit_access.hpp" +#include "mempool.hpp" +#include "scalar.hpp" +#include "string_helpers.hpp" +#include "template_helpers.hpp" + + + +namespace VectorImpl { + +using std::string; +using namespace boost::mpl; + +template< + typename RawPayload, + uint32_t width = 8 * sizeof(RawPayload), + typename Writable = true_> // true_ or false_ +class Vector { +public: + typedef uint64_t ptr_t; + typedef typename if_<Writable, + RawPayload, + const RawPayload>::type Payload; + + Vector(const string name, const ptr_t size) : + mem((size * width + 7) / 8, name), + barrierRead("barrier_read_" + name), + barrierWrite("barrier_write_" + name), + size(size) + {} + // Vector(Vector&&) = default; + + // direct access + Payload & operator() (const ptr_t id) const { + BOOST_STATIC_ASSERT((width >= 8)); + assert(id < size); + return *((RawPayload*) (((char*) mem()) + (width / 8) * id)); + } + + // reader and writer + Payload get(const ptr_t id) const { + assert(id < size); + if (width < 8) { + return (uint8_t) bit_extract(mem(), width, id); + }else{ + return *((RawPayload*) (((char*) mem()) + (width / 8) * id)); + } + } + + void set(const ptr_t id, const RawPayload value) const { + if (!Writable::value) DO_NOT_CALL; + assert(id < size); + if (width < 8) { + bit_insert(mem(), (uint8_t) width, id, value); + }else{ + *((RawPayload*) (((char*) mem()) + (width / 8) * id)) = value; + } + + } + + void sync() const { + if (Writable::value) { + mem.sync(); + barrierRead.sync(); + barrierWrite.sync(); + } + } + + void advise(const ptr_t base, const ptr_t length, int adv) { + mem.advise(base * (width / 8), length * (width / 8), adv); + } + + // associated memory container + MemPool mem; + + // associated r/w-barrier + // WARN: these barriers are not checked/updated in Vector but only + // by iterators/ranges using them; Vector checks only against + // the given size and nothing more + Scalar<ptr_t> barrierRead, barrierWrite; + + // maximal number of elements + const ptr_t size; + + // garantuee that size = 2^n + STATIC_ASSERT_IS_POWER_OF_TWO(width); + +private: + Vector(); +}; + +} // namespace VectorImpl + +using VectorImpl::Vector; + +#endif // ZnnjWYk20gszrSSZENMjL1hM9gI |