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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
#ifndef VECTOR_H
#define VECTOR_H
#include <stdexcept>
#include <algorithm>
#include <memory>
template<typename T, typename A>
struct vector_base
{
A alloc; // allocator
T *elem; // start of allocation
int sz; // number of elements
int space; // amount of allocated space
vector_base(const A &a, int n)
: alloc{ a }, elem{ alloc.allocate(n) }, sz{ n }, space{ n } {}
vector_base()
: alloc{}, elem{}, sz{}, space{} {}
~vector_base() { alloc.deallocate(elem, space); }
};
// an almost real vector of Ts
template<typename T, typename A = std::allocator<T>>
class vector : private vector_base<T, A> // read "for all types T" (just like in math)
{
/*
invariant:
if 0<=n<sz, elem[n] is element n
sz<=space;
if sz<space there is space for (space-sz) doubles after elem[sz-1]
*/
A alloc; // use allocate to handle memory for elements
int sz; // the size
T *elem; // pointer to the elements (or 0)
int space; // number of elements plus number of free slots
public:
explicit vector(int s)
: sz{ s },
elem{ new T[s] },
space{ s }
{
for (std::size_t i = 0; i < sz; ++i)
{
elem[i] = 0;
}
}
vector(std::initializer_list<T> lst)
: sz{ lst.size() },
elem{ new T[sz] }
{
std::copy(lst.begin(), lst.end(), elem);
}
vector(const vector<T> &arg)
: sz{ arg.size() },
elem{ new T[arg.sz] }
{
std::copy(arg.elem, arg.elem + arg.sz, elem);
}
vector &operator=(const vector &a); // copy assignment
vector(vector &&a) // move constructor: define move
: sz{ a.sz }, elem{ a.elem }
{
a.sz = 0;
a.elem = nullptr;
}
vector &operator=(vector<T> &&a);
vector() : sz{ 0 }, elem{ nullptr }, space{ 0 } {} // default constructor
~vector() { delete[] elem; } // destructor
std::size_t size() const { return sz; } // return current size
std::size_t capacity() const { return space; }
T &at(int n);
const T &at(int n) const;
T &operator[](std::size_t n) { return elem[n]; } // access: return reference
const T operator[](std::size_t n) const { return elem[n]; }
void reserve(const int newalloc); //growth
void resize(int newsize, T val);
void push_back(const T &val);
void push_front(const T &val);
};
#endif
template<typename T, typename A>
vector<T, A> &vector<T, A>::operator=(const vector<T, A> &a)
{
if (this == &a)
{
return *this; // self-assignment; no work needed
}
if (a.sz <= space) // enough space, no need for new allocation
{
for (std::size_t i = 0; i < a.sz; ++i)
{
elem[i] = a.elem[i]; // copy elements
}
sz = a.sz;
return *this;
}
T *p = new T[a.sz]; // allocate new space
for (std::size_t i = 0; i < a.sz; ++i)
{
p[i] = a.elem[i]; // copy elements
}
delete[] elem; // deallocate old space
space = sz = a.sz; // set new size
elem = p; // set new elements
return *this; // return a self-reference
}
template<typename T, typename A>
void vector<T, A>::resize(int newsize, T val)
// make the vector have newsize elements
// initialize each new element with the default value 0.0
{
reserve(newsize);
for (std::size_t i = sz; i < newsize; ++i)
{
alloc.construct(&elem[i], val);
}
for (std::size_t i = newsize; i <sz; ++i)
{
alloc.destroy(&elem[i]);
}
sz = newsize;
}
template<typename T, typename A>
void vector<T, A>::push_back(const T &val)
{
if (space == 0)
{
reserve(8);
}
else if (sz == space)
{
reserve(2 * space); // get more space
}
alloc.construct(&elem[sz], val); // add val at end
++sz; // increase the size
}
template<typename T, typename A>
void vector<T, A>::push_front(const T &val)
{
if (space == 0)
{
reserve(8);
}
else if (sz == space)
{
reserve(2 * space); // get more space
}
alloc.construct(&elem[0], val); // add val at front
++sz; // increase the size
}
template<typename T, typename A>
void vector<T, A>::reserve(const int newalloc)
{
if (newalloc <= this->space)
{
return; // never decrease allocation
}
vector_base<T, A> b{ this->alloc, newalloc }; // allocate new space
std::uninitialized_copy(b.elem, &b.elem[this->sz], this->elem); // copy
for (int i = 0; i < this->sz; ++i)
{
this->alloc.destroy(&this->elem[i]); // destroy old
}
std::swap<vector_base<T, A>>(*this, b); // swap representations
}
template<typename T, typename A>
vector<T, A> &vector<T, A>::operator=(vector<T> &&a)
{
delete[] elem; // deallocate old space
elem = a.elem; // copy a's elem and sz
sz = a.sz;
a.elem = nullptr; // make a the empty vector
a.sz = 0;
return *this;
}
template<typename T, typename A>
T &vector<T, A>::at(int n)
{
if (n < 0 || sz <= n)
{
throw std::out_of_range{ "vector element out of range" };
}
return elem[n];
}
template<typename T, typename A>
const T &vector<T, A>::at(int n) const
{
if (n < 0 || sz <= n)
{
throw std::out_of_range{ "vector element out of range" };
}
return elem[n];
}
|