-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectorFunc.h
179 lines (160 loc) · 5.42 KB
/
vectorFunc.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
#include <iostream>
#include <memory>
#include <algorithm>
#include <vector>
#include <chrono>
using std::allocator;
using std::allocator_traits;
using std::uninitialized_copy;
using std::vector;
using std::copy;
using std::cout;
using std::count;
using std::endl;
using std::cin;
using std::max;
template <class T>
class Vector {
public:
typedef T* iterator;
typedef const T* const_iterator;
typedef size_t size_type;
typedef T value_type;
Vector() { create(); }
explicit Vector(size_type n, const T& val = T{}) { create(n, val); }
Vector(const Vector& v) {create(v.begin(), v.end()); }
Vector& operator=(const Vector& rhs) {
if(&rhs != this) {
uncreate();
create(rhs.begin(), rhs.end());
}
return *this;
}
Vector& operator=(const std::vector<T>& other) {
if (this != &other) {
uncreate();
create(other.begin(), other.end());
}
return *this;
}
~Vector() { uncreate(); }
size_type size() const { return limit - data; }
T& operator[](size_type i) { return data[i]; }
const T& operator[](size_type i) const { return data[i]; }
iterator begin() { return data; }
const_iterator begin() const { return data; }
iterator end() { return limit; }
const_iterator end() const { return limit; }
void push_back(const T& val) {
if(avail == limit)
grow();
unchecked_append(val);
} void resize(size_type new_size, const T& val = T{}) {
if(new_size < size()) {
iterator new_limit = data + new_size;
while(limit != new_limit)
alloc.destroy(--limit);
}
else if(new_size > size()) {
if(new_size > capacity())
reserve(new_size);
iterator new_limit = data + new_size;
while (limit != new_limit)
unchecked_append(val);
}
limit = data + new_size;
} void reserve(size_type new_capacity) {
if(new_capacity > capacity()) {
iterator new_data = alloc.allocate(new_capacity);
iterator new_avail = uninitialized_copy(data, avail, new_data);
uncreate();
data = new_data;
avail = new_avail;
limit = data + new_capacity;
}
} void pop_back() {
if(avail != data)
alloc.destroy(--avail);
}
void clear() { uncreate();}
bool empty() const { return data == avail; }
size_type capacity() const { return limit - data; }
iterator erase(iterator pos) {
if(pos < begin() || pos >= end())
throw std::out_of_range("Invalid iterator position");
iterator new_end = pos;
alloc.destroy(new_end++);
while(new_end != end()) {
*pos++ = *new_end++;
}
--avail;
return pos;
}
iterator nth_element(iterator first, iterator nth, iterator last) {
if (nth < first || nth >= last)
throw std::out_of_range("Invalid iterator position");
while (true) {
iterator pivot = partition(first, last);
if (pivot == nth)
return pivot;
else if (pivot < nth)
first = pivot + 1;
else
last = pivot;
}
}
private:
iterator data;
iterator avail;
iterator limit;
allocator<T> alloc;
iterator partition(iterator first, iterator last) {
T& pivot = *first;
iterator i = first + 1;
iterator j = last - 1;
while (true) {
while (i < last && *i <= pivot)
++i;
while (j > first && *j >= pivot)
--j;
if (i >= j)
break;
std::swap(*i, *j);
}
std::swap(*first, *j);
return j;
}
void create() {
data = avail = limit = nullptr;
}
void create(size_type n, const T& val) {
data = alloc.allocate(n);
limit = avail = data + n;
uninitialized_fill(data, limit, val);
}
void create(const_iterator i, const_iterator j) {
data = alloc.allocate(j - i);
limit = avail = uninitialized_copy(i,j,data);
}
void uncreate() {
if (data) {
iterator it = avail;
while (it != data)
alloc.destroy(--it);
alloc.deallocate(data, limit - data);
}
data = limit = avail = nullptr;
}
void grow() {
size_type new_size = max(2 * (limit - data), ptrdiff_t(1));
iterator new_data = alloc.allocate(new_size);
iterator new_avail = uninitialized_copy(data, avail, new_data);
uncreate();
data = new_data;
avail = new_avail;
limit = data + new_size;
}
void unchecked_append(const T& val) {
allocator_traits<allocator<T>>::construct(alloc, avail++, val);
}
};