diff options
Diffstat (limited to 'decoder/small_vector.h')
-rw-r--r-- | decoder/small_vector.h | 65 |
1 files changed, 34 insertions, 31 deletions
diff --git a/decoder/small_vector.h b/decoder/small_vector.h index 2c61ab3a..e5c314de 100644 --- a/decoder/small_vector.h +++ b/decoder/small_vector.h @@ -5,16 +5,17 @@ #include <cassert> #include <limits.h> -#define __SV_MAX_STATIC 2 - +//sizeof(T)/sizeof(T*)>1?sizeof(T)/sizeof(T*):1 +template <class T,int SV_MAX=2 > class SmallVector { - + typedef unsigned short uint16_t; public: + typedef SmallVector<T,SV_MAX> Self; SmallVector() : size_(0) {} explicit SmallVector(size_t s, int v = 0) : size_(s) { assert(s < 0x80); - if (s <= __SV_MAX_STATIC) { + if (s <= SV_MAX) { for (int i = 0; i < s; ++i) data_.vals[i] = v; } else { capacity_ = s; @@ -24,9 +25,9 @@ class SmallVector { } } - SmallVector(const SmallVector& o) : size_(o.size_) { - if (size_ <= __SV_MAX_STATIC) { - for (int i = 0; i < __SV_MAX_STATIC; ++i) data_.vals[i] = o.data_.vals[i]; + SmallVector(const Self& o) : size_(o.size_) { + if (size_ <= SV_MAX) { + for (int i = 0; i < SV_MAX; ++i) data_.vals[i] = o.data_.vals[i]; } else { capacity_ = size_ = o.size_; data_.ptr = new int[capacity_]; @@ -34,18 +35,18 @@ class SmallVector { } } - const SmallVector& operator=(const SmallVector& o) { - if (size_ <= __SV_MAX_STATIC) { - if (o.size_ <= __SV_MAX_STATIC) { + const Self& operator=(const Self& o) { + if (size_ <= SV_MAX) { + if (o.size_ <= SV_MAX) { size_ = o.size_; - for (int i = 0; i < __SV_MAX_STATIC; ++i) data_.vals[i] = o.data_.vals[i]; + for (int i = 0; i < SV_MAX; ++i) data_.vals[i] = o.data_.vals[i]; } else { capacity_ = size_ = o.size_; data_.ptr = new int[capacity_]; std::memcpy(data_.ptr, o.data_.ptr, size_ * sizeof(int)); } } else { - if (o.size_ <= __SV_MAX_STATIC) { + if (o.size_ <= SV_MAX) { delete[] data_.ptr; size_ = o.size_; for (int i = 0; i < size_; ++i) data_.vals[i] = o.data_.vals[i]; @@ -64,12 +65,12 @@ class SmallVector { } ~SmallVector() { - if (size_ <= __SV_MAX_STATIC) return; + if (size_ <= SV_MAX) return; delete[] data_.ptr; } void clear() { - if (size_ > __SV_MAX_STATIC) { + if (size_ > SV_MAX) { delete[] data_.ptr; } size_ = 0; @@ -79,7 +80,7 @@ class SmallVector { size_t size() const { return size_; } inline void ensure_capacity(uint16_t min_size) { - assert(min_size > __SV_MAX_STATIC); + assert(min_size > SV_MAX); if (min_size < capacity_) return; uint16_t new_cap = std::max(static_cast<uint16_t>(capacity_ << 1), min_size); int* tmp = new int[new_cap]; @@ -90,18 +91,18 @@ class SmallVector { } inline void copy_vals_to_ptr() { - capacity_ = __SV_MAX_STATIC * 2; + capacity_ = SV_MAX * 2; int* tmp = new int[capacity_]; - for (int i = 0; i < __SV_MAX_STATIC; ++i) tmp[i] = data_.vals[i]; + for (int i = 0; i < SV_MAX; ++i) tmp[i] = data_.vals[i]; data_.ptr = tmp; } inline void push_back(int v) { - if (size_ < __SV_MAX_STATIC) { + if (size_ < SV_MAX) { data_.vals[size_] = v; ++size_; return; - } else if (size_ == __SV_MAX_STATIC) { + } else if (size_ == SV_MAX) { copy_vals_to_ptr(); } else if (size_ == capacity_) { ensure_capacity(size_ + 1); @@ -116,9 +117,9 @@ class SmallVector { const int& front() const { return this->operator[](0); } void resize(size_t s, int v = 0) { - if (s <= __SV_MAX_STATIC) { - if (size_ > __SV_MAX_STATIC) { - int tmp[__SV_MAX_STATIC]; + if (s <= SV_MAX) { + if (size_ > SV_MAX) { + int tmp[SV_MAX]; for (int i = 0; i < s; ++i) tmp[i] = data_.ptr[i]; delete[] data_.ptr; for (int i = 0; i < s; ++i) data_.vals[i] = tmp[i]; @@ -135,7 +136,7 @@ class SmallVector { return; } } else { - if (size_ <= __SV_MAX_STATIC) + if (size_ <= SV_MAX) copy_vals_to_ptr(); if (s > capacity_) ensure_capacity(s); @@ -148,18 +149,18 @@ class SmallVector { } int& operator[](size_t i) { - if (size_ <= __SV_MAX_STATIC) return data_.vals[i]; + if (size_ <= SV_MAX) return data_.vals[i]; return data_.ptr[i]; } const int& operator[](size_t i) const { - if (size_ <= __SV_MAX_STATIC) return data_.vals[i]; + if (size_ <= SV_MAX) return data_.vals[i]; return data_.ptr[i]; } - bool operator==(const SmallVector& o) const { + bool operator==(const Self& o) const { if (size_ != o.size_) return false; - if (size_ <= __SV_MAX_STATIC) { + if (size_ <= SV_MAX) { for (size_t i = 0; i < size_; ++i) if (data_.vals[i] != o.data_.vals[i]) return false; return true; @@ -170,9 +171,13 @@ class SmallVector { } } + friend bool operator!=(const Self& a, const Self& b) { + return !(a==b); + } + private: union StorageType { - int vals[__SV_MAX_STATIC]; + int vals[SV_MAX]; int* ptr; }; StorageType data_; @@ -180,8 +185,6 @@ class SmallVector { uint16_t capacity_; // only defined when size_ >= __SV_MAX_STATIC }; -inline bool operator!=(const SmallVector& a, const SmallVector& b) { - return !(a==b); -} +typedef SmallVector<int,2> SmallVectorInt; #endif |