1 #ifndef BASE1VEC_CDJXJIO 2 #define BASE1VEC_CDJXJIO 3 4 #include <vector> 5 #include <ostream> 6 #include "debug.hpp" 7 8 template<typename T> 9 class vec1 10 { 11 std::vector<T> v; 12 public: 13 typedef typename std::vector<T>::iterator iterator; 14 typedef typename std::vector<T>::const_iterator const_iterator; 15 typedef typename std::vector<T>::value_type value_type; vec1()16 vec1() { } 17 vec1(unsigned size)18 vec1(unsigned size) 19 : v(size) 20 { } 21 #ifdef USE_CPP11 vec1(std::initializer_list<T> l)22 vec1(std::initializer_list<T> l) 23 : v(l) 24 { } 25 #endif vec1(const vec1 & vec)26 vec1(const vec1& vec) : v(vec.v) { } 27 #ifdef USE_CPP11 vec1(vec1 && vec)28 vec1(vec1&& vec) : v(MOVE(vec.v)) { } 29 #endif operator =(const vec1 & vec)30 void operator=(const vec1& vec) 31 { v = vec.v; } 32 #ifdef USE_CPP11 operator =(vec1 && vec)33 void operator=(vec1&& vec) 34 { v = MOVE(vec.v); } 35 #endif 36 template<typename It> vec1(It begin,It end)37 vec1(It begin, It end) 38 : v(begin, end) 39 { } 40 vec1(unsigned size,T val)41 vec1(unsigned size, T val) 42 : v(size, val) 43 { } 44 operator [](int i)45 T& operator[](int i) 46 { 47 D_ASSERT(i <= this->size() && i != 0); 48 return v[i-1]; 49 } 50 operator [](int i) const51 const T& operator[](int i) const 52 { 53 D_ASSERT(i <= this->size() && i != 0); 54 return v[i-1]; 55 } 56 57 // Yes, this is 'int' on purpose. This is to avoid 58 // signed/unsigned warnings all over the place. size() const59 int size() const 60 { return v.size(); } 61 reserve(int i)62 void reserve(int i) 63 { v.reserve(i); } 64 resize(int i)65 void resize(int i) 66 { v.resize(i); } 67 resize(int i,const T & ele)68 void resize(int i, const T& ele) 69 { v.resize(i, ele); } 70 empty() const71 bool empty() const 72 { return v.empty(); } 73 begin()74 iterator begin() 75 { return v.begin(); } 76 begin() const77 const_iterator begin() const 78 { return v.begin(); } 79 end()80 iterator end() 81 { return v.end(); } 82 end() const83 const_iterator end() const 84 { return v.end(); } 85 push_back(const T & t)86 void push_back(const T& t) 87 { v.push_back(t); } 88 #ifdef USE_CPP11 push_back(T && t)89 void push_back(T&& t) 90 { v.push_back(MOVE(t)); } 91 #endif pop_back()92 void pop_back() 93 { v.pop_back(); } 94 clear()95 void clear() 96 { v.clear(); } 97 back()98 T& back() 99 { return v.back(); } 100 back() const101 const T& back() const 102 { return v.back(); } 103 front()104 T& front() 105 { return v.front(); } 106 front() const107 const T& front() const 108 { return v.front(); } 109 110 111 template<typename It> assign(It begin,It end)112 void assign(It begin, It end) 113 { v.assign(begin, end); } 114 operator ==(const vec1<T> & vec) const115 bool operator==(const vec1<T>& vec) const 116 { return v == vec.v; } 117 operator !=(const vec1<T> & vec) const118 bool operator!=(const vec1<T>& vec) const 119 { return v != vec.v; } 120 operator <(const vec1<T> & vec) const121 bool operator<(const vec1<T>& vec) const 122 { return v < vec.v; } 123 operator <<(std::ostream & o,const vec1 & vec)124 friend std::ostream& operator<<(std::ostream& o, const vec1& vec) 125 { 126 o << "["; 127 for(const_iterator it = vec.begin(); it != vec.end(); ++it) 128 { 129 o << *it << " "; 130 } 131 o << "]"; 132 return o; 133 } 134 }; 135 136 #endif 137