#ifndef VALUE_ARRAY_H #define VALUE_ARRAY_H //TODO: option for non-constructed version (type_traits pod?), option for small array optimization (if sz < N, store inline in union, see small_vector.h) #include #include #include #include #include #include #include #include #ifdef USE_BOOST_SERIALIZE # include # include #endif //TODO: use awesome type traits (and/or policy typelist argument) to provide these only when possible? #define VALUE_ARRAY_ADD 1 #define VALUE_ARRAY_MUL 1 #define VALUE_ARRAY_BITWISE 0 #define VALUE_ARRAY_OSTREAM 1 #if VALUE_ARRAY_OSTREAM # include #endif // valarray like in that size is fixed (so saves space compared to vector), but same interface as vector (less resize/push_back/insert, of course) template > class ValueArray : A // private inheritance so stateless allocator adds no size. { typedef ValueArray Self; public: #if VALUE_ARRAY_OSTREAM friend inline std::ostream & operator << (std::ostream &o,Self const& s) { o<<'['; for (unsigned i=0,e=s.size();i1?sizeof(T)/sizeof(T*):1; //space optimization: SV_MAX T will fit inside what would otherwise be a pointer to heap data. todo in the far future if bored. typedef T value_type; typedef T& reference; typedef T const& const_reference; typedef T* iterator; typedef T const* const_iterator; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; typedef T* pointer; size_type size() const { return sz; } bool empty() const { return size() == 0; } iterator begin() { return array; } iterator end() { return array + sz; } const_iterator begin() const { return array; } const_iterator end() const { return array + sz; } reference operator[](size_type pos) { return array[pos]; } const_reference operator[](size_type pos) const { return array[pos]; } reference at(size_type pos) { return array[pos]; } const_reference at(size_type pos) const { return array[pos]; } reference front() { return array[0]; } reference back() { return array[sz-1]; } const_reference front() const { return array[0]; } const_reference back() const { return array[sz-1]; } ValueArray() : sz(0), array(NULL) {} friend inline std::size_t hash_value(Self const& x) { return boost::hash_range(x.begin(),x.end()); } protected: void destroy() { // it's cool that this destroys in reverse order of construction, but why bother? for (size_type i = sz; i != 0;) A::destroy(array + --i); } void dealloc() { if (array != NULL) A::deallocate(array,sz); sz=0; } void alloc(size_type s) { array=A::allocate(sz); sz=s; } void realloc(size_type s) { if (sz!=s) { dealloc(); alloc(s); } } template inline void init_map(C const& c,F const& f) { alloc(c.size()); copy_construct_map(c.begin(),c.end(),array,f); } // warning: std::distance is likely slow on maps (anything other than random access containers. so container version using size will be better template inline void init_range_map(I itr, I end,F const& f) { alloc(std::distance(itr,end)); copy_construct_map(itr,end,array,f); } template inline void init_range(I itr, I end) { alloc(std::distance(itr,end)); copy_construct(itr,end,array); } inline void init(size_type s, const_reference t = T()) { sz=s; array=s ? A::allocate(s) : 0; for (size_type i = 0; i != sz; ++i) { A::construct(array + i,t); } } public: explicit ValueArray(size_type s, const_reference t = T()) { init(s,t); } void reinit(size_type s, const_reference t = T()) { clear(); init(s,t); } //copy any existing data like std::vector. not A::construct exception safe. try blah blah? void resize(size_type s, const_reference t = T()) { pointer na=A::allocate(s); size_type nc=s void reinit(I itr, I end) { clear(); init_range(itr,end); } template void reinit_map(I itr,I end,F const& map) { clear(); init_range_map(itr,end,map); } // warning: std::distance is likely slow on maps,lists (anything other than random access containers. so container version below using size() will be better template void reinit_map(C const& c,F const& map) { clear(); init_map(c,map); } template ValueArray(I itr, I end) { init_range(itr,end); } template ValueArray(I itr, I end,F const& map) { init_range_map(itr,end,map); } ~ValueArray() { clear(); } #undef VALUE_ARRAY_OPEQ #define VALUE_ARRAY_OPEQ(op) template Self & operator op (ValueArray const& o) { assert(sz==o.sz); for (int i=0,e=sz;i<=e;++i) array[i] op o.array[i]; return *this; } #if VALUE_ARRAY_ADD VALUE_ARRAY_OPEQ(+=) VALUE_ARRAY_OPEQ(-=) #endif #if VALUE_ARRAY_MUL VALUE_ARRAY_OPEQ(*=) VALUE_ARRAY_OPEQ(/=) #endif #if VALUE_ARRAY_BITWISE VALUE_ARRAY_OPEQ(|=) VALUE_ARRAY_OPEQ(*=) #endif #undef VALUE_ARRAY_OPEQ #undef VALUE_ARRAY_BINOP #define VALUE_ARRAY_BINOP(op,opeq) template friend inline Self operator op (Self x,ValueArray const& y) { x opeq y; return x; } #if VALUE_ARRAY_ADD VALUE_ARRAY_BINOP(+,+=) VALUE_ARRAY_BINOP(-,-=) #endif #if VALUE_ARRAY_MUL VALUE_ARRAY_BINOP(*,*=) VALUE_ARRAY_BINOP(/,/=) #endif #if VALUE_ARRAY_BITWISE VALUE_ARRAY_BINOP(|,|=) VALUE_ARRAY_BINOP(*,*=) #endif #undef VALUE_ARRAY_BINOP void clear() { destroy(); dealloc(); } void swap(ValueArray& other) { std::swap(sz,other.sz); std::swap(array,other.array); } ValueArray(ValueArray const& other) : A(other) , sz(other.sz) , array(A::allocate(sz)) { copy_construct(other.begin(),other.end(),array); } ValueArray& operator=(ValueArray const& other) { ValueArray(other).swap(*this); return *this; } template ValueArray( Range const& v , typename boost::disable_if< boost::is_integral >::type* = 0) : sz(boost::size(v)) , array(A::allocate(sz)) { copy_construct(boost::begin(v),boost::end(v),array); } template typename boost::disable_if< boost::is_integral , ValueArray>::type& operator=(Range const& other) { ValueArray(other).swap(*this); return *this; } private: template void copy_construct(I1 itr, I1 end, I2 into) { for (; itr != end; ++itr, ++into) A::construct(into,*itr); } template void copy_construct_map(I1 itr, I1 end, I2 into,F const& f) { for (; itr != end; ++itr, ++into) A::construct(into,f(*itr)); } //friend class boost::serialization::access; public: template void save(Archive& ar, unsigned int version) const { ar << sz; for (size_type i = 0; i != sz; ++i) ar << at(i); } template void load(Archive& ar, unsigned int version) { size_type s; ar >> s; ValueArray v(s); for (size_type i = 0; i != s; ++i) ar >> v[i]; this->swap(v); } #ifdef USE_BOOST_SERIALIZE BOOST_SERIALIZATION_SPLIT_MEMBER() #endif private: size_type sz; pointer array; }; template bool operator==(ValueArray const& v1, ValueArray const& v2) { return (v1.size() == v2.size()) and std::equal(v1.begin(),v1.end(),v2.begin()); } template bool operator< (ValueArray const& v1, ValueArray const& v2) { return std::lexicographical_compare( v1.begin() , v1.end() , v2.begin() , v2.end() ); } template void memcpy(void *out,ValueArray const& v) { std::memcpy(out,v.begin(),v.size()*sizeof(T)); } #endif