From 4e4e26e06a26af02e5cce3cd7415e1ec86b460c4 Mon Sep 17 00:00:00 2001 From: graehl Date: Thu, 19 Aug 2010 20:18:43 +0000 Subject: value_array git-svn-id: https://ws10smt.googlecode.com/svn/trunk@601 ec762483-ff6d-05da-a07a-a48fb63a330f --- utils/value_array.h | 141 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 99 insertions(+), 42 deletions(-) (limited to 'utils/value_array.h') diff --git a/utils/value_array.h b/utils/value_array.h index cdf1d697..82e66e8d 100755 --- a/utils/value_array.h +++ b/utils/value_array.h @@ -10,6 +10,7 @@ #include #include #include +#include #ifdef USE_BOOST_SERIALIZE # include # include @@ -35,8 +36,8 @@ public: friend inline std::ostream & operator << (std::ostream &o,Self const& s) { o<<'['; for (unsigned i=0,e=s.size();i + 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 resize(size_type s, const_reference t = T()) { clear(); init(s,t); } + template + 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); + } + template ValueArray(I itr, I end) - : sz(std::distance(itr,end)) - , array(A::allocate(sz)) { - copy_construct(itr,end,array); + init_range(itr,end); + } + template + ValueArray(I itr, I end,F const& map) + { + init_range_map(itr,end,map); } ~ValueArray() { @@ -135,12 +186,11 @@ public: #endif #undef VALUE_ARRAY_BINOP + void clear() { - for (size_type i = sz; i != 0; --i) { - A::destroy(array + (i - 1)); - } - if (array != NULL) A::deallocate(array,sz); + destroy(); + dealloc(); } void swap(ValueArray& other) @@ -177,41 +227,48 @@ public: boost::disable_if< boost::is_integral , ValueArray>::type& operator=(Range const& other) - { - ValueArray(other).swap(*this); - return *this; - } + { + ValueArray(other).swap(*this); + return *this; + } private: -//friend class boost::serialization::access; -template -void copy_construct(I1 itr, I1 end, I2 into) -{ - for (; itr != end; ++itr, ++into) A::construct(into,*itr); -} + template + void copy_construct(I1 itr, I1 end, I2 into) + { + for (; itr != end; ++itr, ++into) A::construct(into,*itr); + } -template -void save(Archive& ar, unsigned int version) const -{ - ar << sz; - for (size_type i = 0; i != sz; ++i) ar << at(i); -} + 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); -} + 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() + BOOST_SERIALIZATION_SPLIT_MEMBER() #endif -size_type sz; -pointer array; +private: + size_type sz; + pointer array; }; -- cgit v1.2.3