#ifndef ZnnjWYk20gszrSSZENMjL1hM9gI #define ZnnjWYk20gszrSSZENMjL1hM9gI #include #include #include #include #include #include #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_::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 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