-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_1_VectorClass.cpp
More file actions
598 lines (589 loc) · 11.7 KB
/
10_1_VectorClass.cpp
File metadata and controls
598 lines (589 loc) · 11.7 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// UPDATE 18/12/2024 : Modify delete individual element
// 수정 : 개별 원소 삭제는 정의되지 않은 동작 유발.
#include <iostream>
#include <string>
#include <algorithm>
// variable length and sequece array
template<typename T>
class Vector
{
T* data;
int capacity;
int length;
public:
// constructor, deconstructor
Vector(int _cap = 1) : capacity(_cap), length(0), data(new T[_cap]) {}
Vector(int _cap, T _val) : capacity(_cap), length(_cap), data(new T[_cap])
{
for (int i = 0; i < length; ++i)
{
data[i] = _val;
}
}
~Vector()
{
if (data)
{
delete[] data;
}
}
// push_back, pop_back
void push_back(T elem)
{
if (capacity <= length)
{
resize(capacity * 2);
}
data[length] = elem;
++length;
}
T pop_back()
{
return data[--length];
}
// resize (Usually 2 * capacity)
void resize(int new_size)
{
if (new_size < length)
{
return;
}
T* temp = data;
capacity = new_size;
data = new T[capacity];
// copy to new allocation
for (int i = 0; i < length; ++i)
{
data[i] = temp[i];
}
delete[] temp;
}
// []
T operator[] (int idx)
{
return data[idx];
}
// at
void* at(int idx)
{
if (idx < 0 || idx >= length)
{
return nullptr;
}
return &data[idx];
}
// insert
void insert(int idx, T val)
{
if (capacity <= length)
{
resize(capacity * 2);
}
// insert data after copy origin
for (int i = length - 1; i >= idx; --i)
{
data[i + 1] = data[i];
}
data[idx] = val;
++length;
}
// remove : shift left
void remove(int idx)
{
for (int i = idx + 1; i < length; ++i)
{
data[i - 1] = data[i];
}
delete data[--length];
}
// size
int size()
{
return length;
}
int alloc_size()
{
return capacity;
}
// swap
void swap(int i, int j)
{
T temp = data[i];
data[i] = data[j];
data[j] = temp;
}
// Vector has a RandomAccessIterator
class RandomAccessIterator
{
int location;
Vector* vector;
public:
RandomAccessIterator(Vector* _vector, int _idx) : vector(_vector), location(_idx) {}
RandomAccessIterator(const RandomAccessIterator& iter) : vector(iter.vector), location(iter.location) {}
RandomAccessIterator& operator++()
{
++location;
return (*this);
}
RandomAccessIterator& operator--()
{
--location;
return (*this);
}
RandomAccessIterator operator++(int)
{
RandomAccessIterator temp(*this);
++location;
return temp;
}
RandomAccessIterator& operator=(const RandomAccessIterator& iter)
{
vector = iter.vector;
location = iter.location;
return (*this);
}
bool operator!=(const RandomAccessIterator& iter)
{
if (vector == iter.vector && location == iter.location)
{
return false;
}
return true;
}
bool operator==(const RandomAccessIterator& iter)
{
if (vector == iter.vector && location == iter.location)
{
return true;
}
return false;
}
T operator*()
{
return (*vector).operator[](location);
}
// TODO: out of range case
RandomAccessIterator operator+(int offset)
{
/*if(location + offset >= vector->size())
{
throw std::out_of_range("OUT OF RANGE");
}*/
RandomAccessIterator temp(*this);
temp.location += offset;
return temp;
}
RandomAccessIterator operator-(int offset)
{
/*if (location - offset < 0)
{
throw std::out_of_range("OUT OF RANGE");
}*/
RandomAccessIterator temp(*this);
temp.location -= offset;
return temp;
}
bool operator<= (const RandomAccessIterator& iter)
{
if (vector == iter.vector && location <= iter.location)
{
return true;
}
return false;
}
bool operator> (const RandomAccessIterator& iter)
{
if (vector == iter.vector && location > iter.location)
{
return true;
}
return false;
}
Vector* GetContainer()
{
return vector;
}
int GetLocation()
{
return location;
}
};
RandomAccessIterator begin()
{
return RandomAccessIterator(this, 0);
}
RandomAccessIterator end()
{
return RandomAccessIterator(this, length);
}
// swap
void swap(RandomAccessIterator i, RandomAccessIterator j)
{
T temp = (*i);
data[i.GetLocation()] = (*j);
data[j.GetLocation()] = temp;
}
};
template<>
class Vector<bool>
{
unsigned int* data;
int capacity;
int length;
public:
typedef bool value_type;
// constructor, deconstructor
Vector(int n = 1) : capacity(n / 32 + 1), length(0), data(new unsigned int[n / 32 + 1])
{
for (int i = 0; i < capacity; ++i)
{
data[i] = 0;
}
}
Vector(int n, bool _val) : capacity(n / 32 + 1), length(n), data(new unsigned int[n / 32 + 1])
{
unsigned int val = _val == true ? 0xFFFFFFFF : 0;
for (int i = 0; i < capacity; ++i)
{
data[i] = val;
}
}
~Vector()
{
if (data)
{
delete[] data;
}
}
// push_back, pop_back
void push_back(bool elem)
{
if (capacity * 32 <= length)
{
resize(capacity * 2);
}
// if 1 make as 1 at last position
if (elem)
{
data[length / 32] |= (1 << (length % 32));
}
++length;
}
bool pop_back()
{
--length;
return (data[length / 32] & (1 << (length % 32))) != 0;
}
// resize (Usually 2 * capacity)
void resize(int new_size)
{
if (new_size < length)
{
return;
}
unsigned int* temp = data;
data = new unsigned int[new_size];
// copy to new allocation
for (int i = 0; i < capacity; ++i)
{
data[i] = temp[i];
}
// initialize as 0
for (int i = capacity; i < new_size; ++i)
{
data[i] = 0;
}
capacity = new_size;
delete[] temp;
}
// []
bool operator[] (int idx)
{
return (data[idx / 32] & (1 << (idx % 32))) != 0;
}
// at (incomplete) - need modify return type as iterator
void* at(int idx)
{
if (idx < 0 || idx >= length)
{
return nullptr;
}
bool temp;
temp = (data[idx / 32] & (1 << (idx % 32))) != 0;
return &temp;
}
// insert
void insert(int idx, bool val)
{
if (capacity <= length)
{
resize(capacity * 2);
}
// start at end ~ idx, shift right
for (int i = length - 1; i >= idx; --i)
{
int nxt = i + 1, cur = i;
if (data[cur / 32] & (1 << (cur % 32)))
{
data[nxt / 32] |= (1 << (nxt % 32));
}
else
{
unsigned int all_one_except_nxt = 0xFFFFFFFF;
all_one_except_nxt ^= (1 << (nxt % 32));
data[nxt / 32] &= all_one_except_nxt;
}
}
// save new data
if (val)
{
data[idx / 32] |= (1 << (idx % 32));
}
else
{
unsigned int all_one_except_idx = 0xFFFFFFFF;
all_one_except_idx ^= (1 << (idx % 32));
data[idx / 32] &= all_one_except_idx;
}
++length;
}
// remove
void remove(int idx)
{
// start at idx+1 ~ end, shift left
for (int i = idx + 1; i < length; ++i)
{
int prev = i - 1, cur = i;
if (data[cur / 32] & (1 << (cur % 32)))
{
data[prev / 32] |= (1 << (prev % 32));
}
else
{
unsigned int all_one_except_prev = 0xFFFFFFFF;
all_one_except_prev ^= (1 << (prev % 32));
data[prev / 32] &= all_one_except_prev;
}
}
--length;
}
// size
int size()
{
return length;
}
int alloc_size()
{
return capacity;
}
// swap
void swap(int i, int j)
{
// save i -> j
bool temp = operator[](j);
if (operator[](i))
{
data[j / 32] |= (1 << (j % 32));
}
else
{
unsigned int all_one_except_i = 0xFFFFFFFF;
all_one_except_i ^= (1 << (i % 32));
data[j / 32] &= all_one_except_i;
}
// save j -> i
if (temp)
{
data[i / 32] |= (1 << (i % 32));
}
else
{
unsigned int all_one_except_j = 0xFFFFFFFF;
all_one_except_j ^= (1 << (i % 32));
data[i / 32] &= all_one_except_j;
}
}
};
using namespace std;
template <typename Cont>
void quick_sort(Cont& cont, int st, int en);
template <class Iterator>
void quick_sort(Iterator st, Iterator en);
int main(void)
{
Vector<int> int_vec(2, 0);
cout << "alloc_size: " << int_vec.alloc_size() << "\n";
int_vec.push_back(3);
cout << "alloc_size: " << int_vec.alloc_size() << "\n";
int_vec.push_back(2);
cout << "alloc_size: " << int_vec.alloc_size() << "\n";
for (int i = 0; i < int_vec.size(); ++i)
{
cout << i << ": " << int_vec[i] << "\n";
}
Vector<string> str_vec(1, "o");
cout << "alloc_size: " << str_vec.alloc_size() << "\n";
str_vec.push_back("Hello");
cout << "alloc_size: " << str_vec.alloc_size() << "\n";
str_vec.push_back("World");
cout << "alloc_size: " << str_vec.alloc_size() << "\n";
for (int i = 0; i < str_vec.size(); ++i)
{
cout << i << ": " << str_vec[i] << "\n";
}
cout << "\n";
Vector<bool> bool_vec;
bool_vec.push_back(true);
bool_vec.push_back(true);
bool_vec.push_back(false);
bool_vec.push_back(false);
bool_vec.push_back(false);
bool_vec.push_back(true);
bool_vec.push_back(false);
bool_vec.push_back(true);
bool_vec.push_back(false);
bool_vec.push_back(true);
bool_vec.push_back(false);
bool_vec.push_back(true);
bool_vec.push_back(false);
bool_vec.push_back(true);
bool_vec.push_back(false);
bool_vec.push_back(true);
bool_vec.push_back(false);
// 11000101010101010
for (int i = 0; i < bool_vec.size(); ++i)
{
cout << bool_vec[i];
}
cout << "\n";
// 000111101010101010
bool_vec.remove(0);
bool_vec.remove(0);
bool_vec.insert(3, true);
bool_vec.insert(3, true);
bool_vec.insert(3, true);
for (int i = 0; i < bool_vec.size(); ++i)
{
cout << bool_vec[i];
}
cout << "\n";
// always 1 until size is smaller than 32
cout << "alloc_size: " << bool_vec.alloc_size() << "\n";
cout << "size: " << bool_vec.size() << "\n";
// 111101010101010
bool_vec.remove(0);
bool_vec.remove(0);
bool_vec.remove(0);
for (int i = 0; i < bool_vec.size(); ++i)
{
cout << bool_vec[i];
}
// 0
cout << "\n" << bool_vec.pop_back() << '\n';
// 11110101010101
for (int i = 0; i < bool_vec.size(); ++i)
{
cout << bool_vec[i];
}
cout << "\n";
// 11101101010101
bool_vec.swap(3, 4);
for (int i = 0; i < bool_vec.size(); ++i)
{
cout << bool_vec[i];
}
cout << "\n" << "\n";
Vector<int> int_vec2;
int_vec2.push_back(3);
cout << "alloc_size: " << int_vec2.alloc_size() << "\n";
int_vec2.push_back(1);
cout << "alloc_size: " << int_vec2.alloc_size() << "\n";
int_vec2.push_back(2);
cout << "alloc_size: " << int_vec2.alloc_size() << "\n";
int_vec2.push_back(8);
int_vec2.push_back(5);
cout << "alloc_size: " << int_vec2.alloc_size() << "\n";
int_vec2.push_back(3);
cout << "before sort" << "\n";
for (int i = 0; i < int_vec2.size(); ++i)
{
cout << i << ": " << int_vec2[i] << "\n";
}
cout << "after sort" << "\n";
//quick_sort(int_vec2, 0, int_vec2.size());
quick_sort(int_vec2.begin(), int_vec2.end());
for (int i = 0; i < int_vec2.size(); ++i)
{
cout << i << ": " << int_vec2[i] << "\n";
}
cout << "\n";
for (auto iter = int_vec2.begin(); iter != int_vec2.end(); ++iter)
{
cout << *iter << " ";
}
cout << "\n";
// TODO: out of range case
/*Vector<int>::RandomAccessIterator iter = int_vec.begin();
try {
cout << "access out of range iterator: " << *(iter - 1) << "\n";
}
catch (std::out_of_range& e)
{
cout << "EXCEPTION: " << e.what() << "\n";
}*/
}
template <typename Cont>
void quick_sort(Cont& cont, int st, int en)
{
if (en <= st + 1)
{
return;
}
auto pivot = cont[st];
int left = st + 1, right = en - 1;
while (true)
{
while (left <= right && cont[left] <= pivot)
{
++left;
}
while (left <= right && cont[right] >= pivot)
{
--right;
}
if (left > right)
break;
cont.swap(left, right);
}
cont.swap(st, right);
quick_sort(cont, st, right);
quick_sort(cont, right + 1, en);
}
// class == typename
template <class Iterator>
void quick_sort(Iterator st, Iterator en)
{
if (en <= st + 1)
{
return;
}
auto pivot = (*st);
auto left = st + 1, right = en - 1;
while (true)
{
while (left <= right && (*left) <= pivot)
{
++left;
}
while (left <= right && (*right) >= pivot)
{
--right;
}
if (left > right)
break;
(*st.GetContainer()).swap(left, right);
}
(*st.GetContainer()).swap(st, right);
quick_sort(st, right);
quick_sort(right + 1 , en);
}