summaryrefslogtreecommitdiff
path: root/decoder/value_array.h
diff options
context:
space:
mode:
authorgraehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-07-23 02:27:28 +0000
committergraehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-07-23 02:27:28 +0000
commit8c6536c56c728213b9e1190f0c9f76f7b4948140 (patch)
treeab192e932935ea25f77924836e40e9adf0034caa /decoder/value_array.h
parent9ac87abac855aaaa6c1dcf686b38443092a10ce6 (diff)
bottom-up FF from fsa FF - WordPenaltyFsa - needs debugging
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@373 ec762483-ff6d-05da-a07a-a48fb63a330f
Diffstat (limited to 'decoder/value_array.h')
-rwxr-xr-xdecoder/value_array.h29
1 files changed, 25 insertions, 4 deletions
diff --git a/decoder/value_array.h b/decoder/value_array.h
index 042247a1..0cb5c3d6 100755
--- a/decoder/value_array.h
+++ b/decoder/value_array.h
@@ -1,12 +1,15 @@
#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 <cstdlib>
#include <algorithm>
#include <new>
#include <boost/range.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits.hpp>
+#include <cstring>
#ifdef USE_BOOST_SERIALIZE
# include <boost/serialization/split_member.hpp>
# include <boost/serialization/access.hpp>
@@ -17,7 +20,7 @@ template <class T, class A = std::allocator<T> >
class ValueArray : A // private inheritance so stateless allocator adds no size.
{
public:
- const int SV_MAX=sizeof(T)/sizeof(T*)>1?sizeof(T)/sizeof(T*):1;
+ static const int SV_MAX=sizeof(T)/sizeof(T*)>1?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;
@@ -51,11 +54,21 @@ public:
ValueArray() : sz(0), array(NULL) {}
explicit ValueArray(size_type s, const_reference t = T())
- : sz(s)
- , array(A::allocate(s))
{
+ init(s,t);
+ }
+
+protected:
+ inline void init(size_type s, const_reference t = T()) {
+ sz=s;
+ array=A::allocate(s);
for (size_type i = 0; i != sz; ++i) { A::construct(array + i,t); }
}
+public:
+ void resize(size_type s, const_reference t = T()) {
+ clear();
+ init(s,t);
+ }
template <class I>
ValueArray(I itr, I end)
@@ -65,7 +78,11 @@ public:
copy_construct(itr,end,array);
}
- ~ValueArray()
+ ~ValueArray() {
+ clear();
+ }
+
+ void clear()
{
for (size_type i = sz; i != 0; --i) {
A::destroy(array + (i - 1));
@@ -160,6 +177,10 @@ bool operator< (ValueArray<T,A> const& v1, ValueArray<T,A> const& v2)
, v2.end() );
}
+template <class T,class A>
+void memcpy(void *out,ValueArray<T,A> const& v) {
+ std::memcpy(out,v.begin(),v.size()*sizeof(T));
+}
#endif