|
| 1 | +// --------- |
| 2 | +// Vector4.h |
| 3 | +// --------- |
| 4 | + |
| 5 | +#ifndef Vector_h |
| 6 | +#define Vector_h |
| 7 | + |
| 8 | +#include <algorithm> // copy, equal, lexicographical_compare, max, swap |
| 9 | +#include <cassert> // assert |
| 10 | +#include <memory> // allocator |
| 11 | +#include <stdexcept> // out_of_range |
| 12 | +#include <utility> // !=, <=, >, >= |
| 13 | + |
| 14 | +#include "Memory.h" // my_destroy, my_uninitialized_copy, my_uninitialized_fill |
| 15 | + |
| 16 | +/* |
| 17 | +namespace std { |
| 18 | +namespace rel_ops { |
| 19 | +
|
| 20 | +template <typename T> |
| 21 | +inline bool operator != (const T& lhs, const T& rhs) { |
| 22 | + return !(lhs == rhs);} |
| 23 | +
|
| 24 | +template <typename T> |
| 25 | +inline bool operator <= (const T& lhs, const T& rhs) { |
| 26 | + return !(rhs < lhs);} |
| 27 | +
|
| 28 | +template <typename T> |
| 29 | +inline bool operator > (const T& lhs, const T& rhs) { |
| 30 | + return (rhs < lhs);} |
| 31 | +
|
| 32 | +template <typename T> |
| 33 | +inline bool operator >= (const T& lhs, const T& rhs) { |
| 34 | + return !(lhs < rhs);} |
| 35 | +
|
| 36 | +} // rel_ops |
| 37 | +} // std; |
| 38 | +*/ |
| 39 | + |
| 40 | +using namespace std::rel_ops; |
| 41 | + |
| 42 | +template <typename T, typename A = std::allocator<T> > |
| 43 | +class my_vector { |
| 44 | + public: |
| 45 | + typedef A allocator_type; |
| 46 | + typedef typename allocator_type::value_type value_type; |
| 47 | + |
| 48 | + typedef typename allocator_type::size_type size_type; |
| 49 | + typedef typename allocator_type::difference_type difference_type; |
| 50 | + |
| 51 | + typedef typename allocator_type::pointer pointer; |
| 52 | + typedef typename allocator_type::const_pointer const_pointer; |
| 53 | + |
| 54 | + typedef typename allocator_type::reference reference; |
| 55 | + typedef typename allocator_type::const_reference const_reference; |
| 56 | + |
| 57 | + typedef typename allocator_type::pointer iterator; |
| 58 | + typedef typename allocator_type::const_pointer const_iterator; |
| 59 | + |
| 60 | + public: |
| 61 | + friend bool operator == (const my_vector& lhs, const my_vector& rhs) { |
| 62 | + return (lhs.size() == rhs.size()) && std::equal(lhs.begin(), lhs.end(), rhs.begin());} |
| 63 | + |
| 64 | + friend bool operator < (const my_vector& lhs, const my_vector& rhs) { |
| 65 | + return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());} |
| 66 | + |
| 67 | + friend void swap (my_vector& x, my_vector& y) { |
| 68 | + x.swap(y);} |
| 69 | + |
| 70 | + private: |
| 71 | + allocator_type _a; |
| 72 | + |
| 73 | + pointer _b; |
| 74 | + pointer _e; // size |
| 75 | + pointer _l; // capacity |
| 76 | + |
| 77 | + private: |
| 78 | + bool valid () const { |
| 79 | + return (!_b && !_e && !_l) || ((_b <= _e) && (_e <= _l));} |
| 80 | + |
| 81 | + my_vector (const my_vector& that, size_type c) : |
| 82 | + _a (that._a) { |
| 83 | + assert(c >= that.size()); |
| 84 | + _b = _a.allocate(c); |
| 85 | + _e = _b + that.size(); |
| 86 | + _l = _b + c; |
| 87 | + my_uninitialized_copy(_a, that.begin(), that.end(), begin()); |
| 88 | + assert(valid());} |
| 89 | + |
| 90 | + public: |
| 91 | + explicit my_vector (const allocator_type& a = allocator_type()) : |
| 92 | + _a (a) { |
| 93 | + _b = _e = _l = 0; |
| 94 | + assert(valid());} |
| 95 | + |
| 96 | + explicit my_vector (size_type s, const_reference v = value_type(), const allocator_type& a = allocator_type()) : |
| 97 | + _a (a) { |
| 98 | + _b = _a.allocate(s); |
| 99 | + _e = _l = _b + s; |
| 100 | + my_uninitialized_fill(_a, begin(), end(), v); |
| 101 | + assert(valid());} |
| 102 | + |
| 103 | + my_vector (const my_vector& that) : |
| 104 | + _a (that._a) { |
| 105 | + _b = _a.allocate(that.size()); |
| 106 | + _e = _l = _b + that.size(); |
| 107 | + my_uninitialized_copy(_a, that.begin(), that.end(), begin()); |
| 108 | + assert(valid());} |
| 109 | + |
| 110 | + ~my_vector () { |
| 111 | + if (_b) { |
| 112 | + clear(); |
| 113 | + _a.deallocate(_b, capacity());} |
| 114 | + assert(valid());} |
| 115 | + |
| 116 | + my_vector& operator = (const my_vector& that) { |
| 117 | + if (this == &that) |
| 118 | + return *this; |
| 119 | + if (that.size() == size()) |
| 120 | + std::copy(that.begin(), that.end(), begin()); |
| 121 | + else if (that.size() < size()) { |
| 122 | + std::copy(that.begin(), that.end(), begin()); |
| 123 | + resize(that.size());} |
| 124 | + else if (that.size() <= capacity()) { |
| 125 | + std::copy(that.begin(), that.begin() + size(), begin()); |
| 126 | + _e = my_uninitialized_copy(_a, that.begin() + size(), that.end(), end());} |
| 127 | + else { |
| 128 | + clear(); |
| 129 | + reserve(that.size()); |
| 130 | + _e = my_uninitialized_copy(_a, that.begin(), that.end(), begin());} |
| 131 | + assert(valid()); |
| 132 | + return *this;} |
| 133 | + |
| 134 | + reference operator [] (size_type i) { |
| 135 | + return begin()[i];} |
| 136 | + |
| 137 | + const_reference operator [] (size_type i) const { |
| 138 | + return const_cast<my_vector&>(*this)[i];} |
| 139 | + |
| 140 | + reference at (size_type i) throw (std::out_of_range) { |
| 141 | + if (i >= size()) |
| 142 | + throw std::out_of_range("vector"); |
| 143 | + return (*this)[i];} |
| 144 | + |
| 145 | + const_reference at (size_type i) const { |
| 146 | + return const_cast<my_vector&>(*this).at(i);} |
| 147 | + |
| 148 | + reference back () { |
| 149 | + assert(!empty()); |
| 150 | + return *(end() - 1);} |
| 151 | + |
| 152 | + const_reference back () const { |
| 153 | + return const_cast<my_vector&>(*this).back();} |
| 154 | + |
| 155 | + iterator begin () { |
| 156 | + return _b;} |
| 157 | + |
| 158 | + const_iterator begin () const { |
| 159 | + return const_cast<my_vector&>(*this).begin();} |
| 160 | + |
| 161 | + size_type capacity () const { |
| 162 | + return _l - _b;} |
| 163 | + |
| 164 | + void clear () { |
| 165 | + resize(0); |
| 166 | + assert(valid());} |
| 167 | + |
| 168 | + bool empty () const { |
| 169 | + return size() == 0;} |
| 170 | + |
| 171 | + iterator end () { |
| 172 | + return _e;} |
| 173 | + |
| 174 | + const_iterator end () const { |
| 175 | + return const_cast<my_vector&>(*this).end();} |
| 176 | + |
| 177 | + reference front () { |
| 178 | + assert(!empty()); |
| 179 | + return *begin();} |
| 180 | + |
| 181 | + const_reference front () const { |
| 182 | + return const_cast<my_vector&>(*this).front();} |
| 183 | + |
| 184 | + void pop_back () { |
| 185 | + assert(!empty()); |
| 186 | + resize(size() - 1); |
| 187 | + assert(valid());} |
| 188 | + |
| 189 | + void push_back (const_reference v) { |
| 190 | + resize(size() + 1, v); |
| 191 | + assert(valid());} |
| 192 | + |
| 193 | + void reserve (size_type c) { |
| 194 | + if (c > capacity()) { |
| 195 | + my_vector x(*this, c); |
| 196 | + swap(x);} |
| 197 | + assert(valid());} |
| 198 | + |
| 199 | + void resize (size_type s, const_reference v = value_type()) { |
| 200 | + if (s == size()) |
| 201 | + return; |
| 202 | + if (s < size()) |
| 203 | + _e = my_destroy(_a, begin() + s, end()); |
| 204 | + else if (s <= capacity()) |
| 205 | + _e = my_uninitialized_fill(_a, end(), begin() + s, v); |
| 206 | + else { |
| 207 | + reserve(std::max(2 * size(), s)); |
| 208 | + resize(s, v);} |
| 209 | + assert(valid());} |
| 210 | + |
| 211 | + size_type size () const { |
| 212 | + return _e - _b;} |
| 213 | + |
| 214 | + void swap (my_vector& that) { |
| 215 | + if (_a == that._a) { |
| 216 | + std::swap(_b, that._b); |
| 217 | + std::swap(_e, that._e); |
| 218 | + std::swap(_l, that._l);} |
| 219 | + else { |
| 220 | + my_vector x(*this); |
| 221 | + *this = that; |
| 222 | + that = x;} |
| 223 | + assert(valid());}}; |
| 224 | + |
| 225 | +#endif // Vector_h |
0 commit comments