1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
#ifndef _GUARD_VEC_H_
#define _GUARD_VEC_H_
#include <algorithm>
#include <memory>
template <typename T> class Vec {
public:
typedef T* iterator;
typedef const T* const_iterator;
typedef size_t size_type;
typedef T value_type;
Vec() {create();}
explicit Vec(size_type n, const T& val = T()) {create(n, val);}
~Vec() {uncreate();}
Vec(const Vec& v) {create(v.begin(), v.end());}
Vec& operator=(const Vec&);
T& operator[](size_type i) {return data[i];}
const T& operator[](size_type i) const {return data[i];}
void push_back(const T& val) {
if (avail == limit) {
grow();
}
unchecked_append(val);
}
iterator erase(iterator position) {
iterator next = position;
if (position < avail) {
++next;
}
if (position >= data) {
shrink(position);
unchecked_remove(position);
}
return next;
}
void clear() {
uncreate();
}
void insert(
iterator position,
const_iterator first,
const_iterator last);
size_type size() const {return avail - data;}
iterator begin() {return data;}
const_iterator begin() const {return data;}
iterator end() {return avail;}
const_iterator end() const {return avail;}
bool empty() const {return data == avail;}
private:
iterator data;
iterator avail;
iterator limit;
std::allocator<T> alloc;
void create();
void create(size_type n, const T& val);
void create(const_iterator b, const_iterator e);
void uncreate();
void grow();
void shrink(iterator position);
void unchecked_append(const T& val);
void unchecked_remove(iterator position);
};
template <typename T> Vec<T>& Vec<T>::operator=(const Vec<T>& rhs) {
// check for self-assignment
if (&rhs != this)
{
// free the array in the left-hand side
uncreate();
// copy elements from the right-hand to the left-hand side
create(rhs.begin(), rhs.end());
}
return *this;
}
template <typename T> void Vec<T>::create() {
data = avail = limit = NULL;
}
template <typename T> void Vec<T>::create(size_type n, const T& val) {
data = alloc.allocate(n);
avail = limit = data + n;
std::uninitialized_fill(data, avail, val);
}
template <typename T>
void Vec<T>::create(const_iterator b, const_iterator e) {
data = alloc.allocate(e - b);
avail = limit = std::uninitialized_copy(b, e, data);
}
template <typename T> void Vec<T>::uncreate() {
if (data) {
iterator it = avail;
while ( it != data)
alloc.destroy(--it);
alloc.deallocate(data, limit - data);
}
data = avail = limit = 0;
}
template <typename T> void Vec<T>::grow() {
size_type new_size = std::max(2 * (limit - data), ptrdiff_t(1));
iterator new_data = alloc.allocate(new_size);
iterator new_avail = std::uninitialized_copy(data, avail, new_data);
uncreate();
data = new_data;
avail = new_avail;
limit = data + new_size;
}
template <typename T> void Vec<T>::unchecked_append(const T& val) {
alloc.construct(avail++, val);
}
template <typename T> void Vec<T>::shrink(iterator position) {
size_type new_size = std::max(limit - data - 1, ptrdiff_t(1));
iterator new_data = alloc.allocate(new_size);
iterator second_portion = std::uninitialized_copy(data, position - 1, new_data);
iterator new_avail = std::uninitialized_copy(position + 1, avail, second_portion);
avail = std::uninitialized_copy(new_data, new_avail, data);
alloc.deallocate(new_data, new_size);
--limit;
}
template <typename T> void Vec<T>::unchecked_remove(iterator position) {
alloc.destroy(position);
}
template <typename T>
void Vec<T>::insert(
iterator position,
const_iterator first,
const_iterator last) {
while (last - first > limit - avail){
grow();
}
if (position == avail) {
avail = std::uninitialized_copy(first, last, position);
}
else {
iterator new_position = position + (last - first);
avail = std::uninitialized_copy(position, avail, new_position);
std::uninitialized_copy(first, last, position);
}
}
#endif
|