-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector4.h
225 lines (176 loc) · 6.84 KB
/
Vector4.h
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
216
217
218
219
220
221
222
223
224
225
// ---------
// Vector4.h
// ---------
#ifndef Vector_h
#define Vector_h
#include <algorithm> // copy, equal, lexicographical_compare, max, swap
#include <cassert> // assert
#include <memory> // allocator
#include <stdexcept> // out_of_range
#include <utility> // !=, <=, >, >=
#include "Memory.h" // my_destroy, my_uninitialized_copy, my_uninitialized_fill
/*
namespace std {
namespace rel_ops {
template <typename T>
inline bool operator != (const T& lhs, const T& rhs) {
return !(lhs == rhs);}
template <typename T>
inline bool operator <= (const T& lhs, const T& rhs) {
return !(rhs < lhs);}
template <typename T>
inline bool operator > (const T& lhs, const T& rhs) {
return (rhs < lhs);}
template <typename T>
inline bool operator >= (const T& lhs, const T& rhs) {
return !(lhs < rhs);}
} // rel_ops
} // std;
*/
using namespace std::rel_ops;
template <typename T, typename A = std::allocator<T> >
class my_vector {
public:
typedef A allocator_type;
typedef typename allocator_type::value_type value_type;
typedef typename allocator_type::size_type size_type;
typedef typename allocator_type::difference_type difference_type;
typedef typename allocator_type::pointer pointer;
typedef typename allocator_type::const_pointer const_pointer;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::const_reference const_reference;
typedef typename allocator_type::pointer iterator;
typedef typename allocator_type::const_pointer const_iterator;
public:
friend bool operator == (const my_vector& lhs, const my_vector& rhs) {
return (lhs.size() == rhs.size()) && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
friend bool operator < (const my_vector& lhs, const my_vector& rhs) {
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());}
friend void swap (my_vector& x, my_vector& y) {
x.swap(y);}
private:
allocator_type _a;
pointer _b;
pointer _e; // size
pointer _l; // capacity
private:
bool valid () const {
return (!_b && !_e && !_l) || ((_b <= _e) && (_e <= _l));}
my_vector (const my_vector& that, size_type c) :
_a (that._a) {
assert(c >= that.size());
_b = _a.allocate(c);
_e = _b + that.size();
_l = _b + c;
my_uninitialized_copy(_a, that.begin(), that.end(), begin());
assert(valid());}
public:
explicit my_vector (const allocator_type& a = allocator_type()) :
_a (a) {
_b = _e = _l = 0;
assert(valid());}
explicit my_vector (size_type s, const_reference v = value_type(), const allocator_type& a = allocator_type()) :
_a (a) {
_b = _a.allocate(s);
_e = _l = _b + s;
my_uninitialized_fill(_a, begin(), end(), v);
assert(valid());}
my_vector (const my_vector& that) :
_a (that._a) {
_b = _a.allocate(that.size());
_e = _l = _b + that.size();
my_uninitialized_copy(_a, that.begin(), that.end(), begin());
assert(valid());}
~my_vector () {
if (_b) {
clear();
_a.deallocate(_b, capacity());}
assert(valid());}
my_vector& operator = (const my_vector& that) {
if (this == &that)
return *this;
if (that.size() == size())
std::copy(that.begin(), that.end(), begin());
else if (that.size() < size()) {
std::copy(that.begin(), that.end(), begin());
resize(that.size());}
else if (that.size() <= capacity()) {
std::copy(that.begin(), that.begin() + size(), begin());
_e = my_uninitialized_copy(_a, that.begin() + size(), that.end(), end());}
else {
clear();
reserve(that.size());
_e = my_uninitialized_copy(_a, that.begin(), that.end(), begin());}
assert(valid());
return *this;}
reference operator [] (size_type i) {
return begin()[i];}
const_reference operator [] (size_type i) const {
return const_cast<my_vector&>(*this)[i];}
reference at (size_type i) throw (std::out_of_range) {
if (i >= size())
throw std::out_of_range("vector");
return (*this)[i];}
const_reference at (size_type i) const {
return const_cast<my_vector&>(*this).at(i);}
reference back () {
assert(!empty());
return *(end() - 1);}
const_reference back () const {
return const_cast<my_vector&>(*this).back();}
iterator begin () {
return _b;}
const_iterator begin () const {
return const_cast<my_vector&>(*this).begin();}
size_type capacity () const {
return _l - _b;}
void clear () {
resize(0);
assert(valid());}
bool empty () const {
return size() == 0;}
iterator end () {
return _e;}
const_iterator end () const {
return const_cast<my_vector&>(*this).end();}
reference front () {
assert(!empty());
return *begin();}
const_reference front () const {
return const_cast<my_vector&>(*this).front();}
void pop_back () {
assert(!empty());
resize(size() - 1);
assert(valid());}
void push_back (const_reference v) {
resize(size() + 1, v);
assert(valid());}
void reserve (size_type c) {
if (c > capacity()) {
my_vector x(*this, c);
swap(x);}
assert(valid());}
void resize (size_type s, const_reference v = value_type()) {
if (s == size())
return;
if (s < size())
_e = my_destroy(_a, begin() + s, end());
else if (s <= capacity())
_e = my_uninitialized_fill(_a, end(), begin() + s, v);
else {
reserve(std::max(2 * size(), s));
resize(s, v);}
assert(valid());}
size_type size () const {
return _e - _b;}
void swap (my_vector& that) {
if (_a == that._a) {
std::swap(_b, that._b);
std::swap(_e, that._e);
std::swap(_l, that._l);}
else {
my_vector x(*this);
*this = that;
that = x;}
assert(valid());}};
#endif // Vector_h