-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.h
326 lines (259 loc) · 7.91 KB
/
vector.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <iostream>
#include <initializer_list>
#include <algorithm>
using namespace std;
template <typename T>
class Vector;
template<typename T>
ostream& operator<< (ostream& os, const Vector<T>& V){
return V.print(os);
}
template <typename T>
class Vector{
public:
class ConstIterator;
class Iterator;
using value_type = T;
using size_type = size_t;
using difference_type = ptrdiff_t;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = value_type*;
using const_pointer = const value_type*;
using iterator = Iterator;
using const_iterator = ConstIterator;
private:
pointer values;
size_type sz;
size_type max_sz;
public:
class ConstIterator{
public:
using value_type = Vector::value_type;
using difference_type = Vector::difference_type;
using reference = Vector::reference;
using const_reference = Vector::const_reference;
using pointer = Vector::pointer;
using iterator_category = forward_iterator_tag;
private:
const_pointer ptr;
public:
ConstIterator(){this->ptr=0;}
ConstIterator(pointer p){this->ptr=p;}
ConstIterator& operator++(){if(*this==end()) throw runtime_error("Out of bounds!"); ptr++; return *this;}
const_pointer operator->() {return ptr;}
friend bool operator!=(const ConstIterator& lop, const ConstIterator& rop){return (lop.ptr!=rop.ptr);}
friend bool operator==(const ConstIterator& lop, const ConstIterator& rop){return (lop.ptr==rop.ptr);}
const_reference operator*() const{if(*this==end()) throw runtime_error("Out of bounds!"); return *ptr;}
//friend bool operator<(const ConstIterator& lop, const ConstIterator& rop);
//bool operator<(const ConstIterator& it){return (*ptr<*it.ptr);}
//bool operator<(const_reference rop)const{return (*ptr<rop);}
friend difference_type operator-(const ConstIterator& lop, const ConstIterator& rop){
return lop.ptr - rop.ptr;
}
};
class Iterator{
public:
using value_type = Vector::value_type;
using difference_type = Vector::difference_type;
using reference = Vector::reference;
using const_reference = Vector::const_reference;
using pointer = Vector::pointer;
using iterator_category = forward_iterator_tag;
private:
pointer ptr;
public:
Iterator(){this->ptr=0;}
Iterator(pointer p){this->ptr=p;}
Iterator& operator++(){ptr++;return *this;}
friend bool operator!=(const Iterator& lop, const Iterator& rop){return static_cast<ConstIterator>(lop)!=rop;}
friend bool operator==(const Iterator& lop, const Iterator& rop){return static_cast<ConstIterator>(lop)==rop;}
//bool operator<(const Iterator& it){return (*ptr>*it.ptr);}
//bool operator<(const_reference it){return (*ptr<it);}
reference operator*(){return *ptr;}
friend difference_type operator-(const Iterator& lop, const Iterator& rop){
return static_cast<ConstIterator>(lop) - rop;
}
//friend bool operator<(const ConstIterator& lop, const ConstIterator& rop);
operator ConstIterator() const{
return ConstIterator(ptr);
}
};
static constexpr size_type min_sz{2};
Vector():Vector(min_sz){};
Vector(const Vector& v):Vector(){
for (size_type i{0}; i < v.sz; i++)
push_back(v.values[i]);
}
Vector(initializer_list<value_type> l):Vector(){
for (const auto& e : l)
push_back(e);
}
Vector& operator=(Vector& v){
swap(sz, v.sz);
swap(max_sz, v.max_sz);
swap(values, v.values);
return *this;
}
Vector(size_type max){
sz = 0;
max_sz = (max > min_sz) ? max : min_sz;
values = new value_type[max_sz];
}
~Vector(){
delete[] values;
}
value_type getvalue(size_type i)const{return this->values[i];}
value_type sum(){
if (empty())
throw runtime_error("Vector ist leer");
value_type sum = 0;
for (size_type i = 0; i < sz; i++)
sum += values[i];
return sum;
}
value_type min(){
if (empty())
throw runtime_error("Vector ist leer");
value_type min = values[0];
for (size_type i = 1; i < sz; i++)
if (min > values[i])
min = values[i];
return min;
}
value_type max() const{
//if (empty())
// throw runtime_error("Vector ist leer");
//value_type max = values[0];
//for (size_type i = 1; i < sz; i++)
// if (max < values[i])
// max = values[i];
//return max;
//, [] (const_reference l, const_reference r){return l==r;}
auto it = max_element(begin(), end());
return *it;
}
size_type size() const{return sz;}
bool empty() const{
if(sz==0)
return true;
return false;
}
void pop_back(){
if (empty())
throw runtime_error("Vector ist leer");
sz--;
shrink_to_fit();
}
void shrink_to_fit(){
if (max_sz > sz) return;
change_size(sz < min_sz ? min_sz : sz);
}
void push_back(value_type n){
if (max_sz == sz)
change_size(2*max_sz);
values[sz] = n;
sz++;
}
void reserve(size_type newsize){change_size(newsize);}
void change_size(size_type newsize){
value_type *buf = new value_type [newsize];
for (size_type i = 0; i < sz; i++)
buf[i] = values[i];
delete[] values;
values = buf;
max_sz = newsize;
}
iterator insert (const_iterator pos, const_reference val){
auto diff = pos-begin();
if(diff<0 || static_cast<size_type>(diff)>sz)
throw runtime_error("Iterator out of bounds");
size_type current{static_cast<size_type>(diff)};
if(sz>=max_sz)
reserve(max_sz*2);
for(size_t i{sz}; i-- > current;)
values[i+1]=values[i];
values[current]=val;
++sz;
return Vector::iterator{values+current};
}
iterator erase (const_iterator pos){
auto diff = pos-begin();
if(diff<0 || static_cast<size_type>(diff)>sz)
throw runtime_error("Iterator out of bounds");
size_type current{static_cast<size_type>(diff)};
for(size_t i{current}; i<sz-1; ++i)
values[i]=values[i+1];
--sz;
return Vector::iterator{values+current};
}
void clear(){
sz = 0;
shrink_to_fit();
}
ostream& printKontra(std::ostream &os) const{
os << "Begin: " << *begin() <<endl;
os << "End: " << *end() << endl;
os << "Size: " << sz << endl;
cout << '[';
bool first{true};
for(const auto& elem : *this){
//for(auto it=begin();it!=end();--it){
if(first)
first = false;
else
os << ", ";
os << elem;
}
os << ']';
return os;
}
//Operatoren
reference operator[](size_type val){
if (val < 0 || val >= sz)
throw runtime_error("Index ist ungueltig");
return values[val];
}
const_reference operator[](size_type val) const{
if (val <= 0 && val >= sz)
throw runtime_error("Index ist ungueltig");
return values[val];
}
iterator begin() {return Iterator{values};}
iterator end() {return Iterator{values+sz};}
const_iterator begin() const{return ConstIterator{values};}
const_iterator end() const{return ConstIterator{values+sz};}
iterator getIteratorOnPos(size_type i){return Iterator{&values[i]};};
const_iterator getIteratorOnPos(size_type i)const{return ConstIterator{&values[i]};};
ostream& print(ostream& os){
os << "[";
for (size_type i = 0; i < sz; i++){
os << values[i];
if (i != sz - 1){
os << ", ";
}else{
os << "]";
break;
}
}
return os;
}
istream& operator>>(istream& is){
char c;
is >> c;
if (c != '[') throw runtime_error ("[ expected");
clear();
is >> c;
if (c != ']')
return is;
is.putback(c);
do {
value_type d;
is >> d;
push_back(d);
is >> c;
if (c != ',' && c != ']') throw runtime_error (", or ] expected");
}while (c != ']');
return is;
}
};