diff options
author | graehl@gmail.com <graehl@gmail.com@ec762483-ff6d-05da-a07a-a48fb63a330f> | 2010-08-16 09:11:03 +0000 |
---|---|---|
committer | graehl@gmail.com <graehl@gmail.com@ec762483-ff6d-05da-a07a-a48fb63a330f> | 2010-08-16 09:11:03 +0000 |
commit | 708e59d0e908b24bac36ec36956ad013268253b0 (patch) | |
tree | b568b7afc5b9012aed8595821c3ef4daa4dbffde /utils/small_vector.h | |
parent | d523a48ff2a7097ec5c33054af82f9395774d2d2 (diff) |
greedy binarization - needs testing, may have broke l2r
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@560 ec762483-ff6d-05da-a07a-a48fb63a330f
Diffstat (limited to 'utils/small_vector.h')
-rw-r--r-- | utils/small_vector.h | 56 |
1 files changed, 43 insertions, 13 deletions
diff --git a/utils/small_vector.h b/utils/small_vector.h index 89917d1c..b65c3b38 100644 --- a/utils/small_vector.h +++ b/utils/small_vector.h @@ -22,6 +22,16 @@ template <class T,int SV_MAX=2> class SmallVector { // typedef unsigned short uint16_t; + void Alloc(size_t s) { + size_=s; + assert(s < 0xA000); + if (s>SV_MAX) { + capacity_ = s; + size_ = s; + data_.ptr = new T[s]; // TODO: replace this with allocator or ::operator new(sizeof(T)*s) everywhere + } + } + public: typedef SmallVector<T,SV_MAX> Self; SmallVector() : size_(0) {} @@ -37,30 +47,33 @@ class SmallVector { T *end() { return begin()+size_; } T const* end() const { return begin()+size_; } - explicit SmallVector(size_t s) : size_(s) { - assert(s < 0xA000); + explicit SmallVector(size_t s) { + Alloc(s); if (s <= SV_MAX) { for (int i = 0; i < s; ++i) new(&data_.vals[i]) T(); - } else { - capacity_ = s; - size_ = s; - data_.ptr = new T[s]; // TODO: replace this with allocator or ::operator new(sizeof(T)*s) everywhere - for (int i = 0; i < size_; ++i) new(&data_.ptr[i]) T(); - } + } //TODO: if alloc were raw space, construct here. } - SmallVector(size_t s, T const& v) : size_(s) { - assert(s < 0xA000); + SmallVector(size_t s, T const& v) { + Alloc(s); if (s <= SV_MAX) { for (int i = 0; i < s; ++i) data_.vals[i] = v; } else { - capacity_ = s; - size_ = s; - data_.ptr = new T[s]; for (int i = 0; i < size_; ++i) data_.ptr[i] = v; } } + //TODO: figure out iterator traits to allow this to be selcted for any iterator range + template <class I> + SmallVector(I const* begin,I const* end) { + int s=end-begin; + Alloc(s); + if (s <= SV_MAX) { + for (int i = 0; i < s; ++i,++begin) data_.vals[i] = *begin; + } else + for (int i = 0; i < s; ++i,++begin) data_.ptr[i] = *begin; + } + SmallVector(const Self& o) : size_(o.size_) { if (size_ <= SV_MAX) { std::memcpy(data_.vals,o.data_.vals,size_*sizeof(T)); @@ -72,6 +85,23 @@ class SmallVector { } } + //TODO: test. this invalidates more iterators than std::vector since resize may move from ptr to vals. + T *erase(T *b) { + return erase(b,b+1); + } + T *erase(T *b,T* e) { + T *tb=begin(),*te=end(); + int nbefore=b-tb; + if (e==te) { + resize(nbefore); + } else { + int nafter=te-e; + std::memmove(b,e,nafter*sizeof(T)); + resize(nbefore+nafter); + } + return begin()+nbefore; + } + const Self& operator=(const Self& o) { if (size_ <= SV_MAX) { if (o.size_ <= SV_MAX) { |