-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOptimizedArrayList.cpp
executable file
·169 lines (140 loc) · 3.68 KB
/
OptimizedArrayList.cpp
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
//
// Created by max on 02.06.18.
//
#include <iostream>
#include "OptimizedArrayList.h"
OptimizedArrayList::OptimizedArrayList() : _size(0), isSmall(true) {
data.small = 0;
}
OptimizedArrayList::OptimizedArrayList(OptimizedArrayList &&other) noexcept {
_size = other._size;
isSmall = other.isSmall;
if (isSmall) {
data.small = other.data.small;
} else {
new(&data.big) std::shared_ptr<vector<uint32_t>>(other.data.big);
other.isSmall = true;
other.data.big.~shared_ptr();
other.data.small = 0;
other._size = 0;
}
}
OptimizedArrayList::OptimizedArrayList(OptimizedArrayList const &other) {
_size = other._size;
isSmall = other.isSmall;
if (other.isSmall) {
data.small = other.data.small;
} else {
new(&data.big) std::shared_ptr<vector<uint32_t>>(other.data.big);
}
}
OptimizedArrayList::OptimizedArrayList(std::initializer_list<uint32_t> list) {
isSmall = (list.size() <= 1);
_size = list.size();
if (!isSmall) {
vector<uint32_t> *vect = new vector<uint32_t>(list);
new(&data.big) std::shared_ptr<vector<uint32_t >>(vect);
} else {
data.small = *list.begin();
}
}
OptimizedArrayList &OptimizedArrayList::operator=(OptimizedArrayList const &other) {
_size = other._size;
if (!isSmall) {
data.big.reset();
}
if (other.isSmall) {
data.small = other.data.small;
} else {
new(&data.big) std::shared_ptr<vector<uint32_t>>(other.data.big);
}
isSmall = other.isSmall;
return *this;
}
size_t OptimizedArrayList::size() const {
return _size;
}
uint32_t OptimizedArrayList::back() const {
assert(_size > 0);
return (isSmall ? data.small : data.big->back());
}
void OptimizedArrayList::push_back(uint32_t const x) {
if (isSmall && _size < 1) {
data.small = x;
_size++;
return;
}
make_unique();
make_big();
(*data.big.get()).push_back(x);
_size++;
}
void OptimizedArrayList::make_big() {
if (!isSmall)
return;
isSmall = false;
uint32_t mas = data.small;
vector<uint32_t> *vect = new vector<uint32_t>(_size);
new(&data.big) std::shared_ptr<std::vector<uint32_t>>(vect);
(*data.big.get())[0] = mas;
}
void OptimizedArrayList::pop_back() {
assert(_size > 0);
_size--;
if (isSmall) {
return;
}
make_unique();
(*data.big.get()).pop_back();
if (data.big->size() == 1) {
uint32_t tmp = data.big->back();
data.big.~shared_ptr();
isSmall = true;
data.small = tmp;
}
}
uint32_t &OptimizedArrayList::operator[](size_t ind) {
assert(ind < _size);
if (isSmall)
return data.small;
make_unique();
return (*data.big.get())[ind];
}
uint32_t const &OptimizedArrayList::operator[](size_t ind) const {
assert(ind < _size);
return (isSmall ? data.small : (*data.big.get())[ind]);
}
void OptimizedArrayList::resize(size_t len) {
_size = len;
if (!isSmall) {
data.big.reset();
data.big.~shared_ptr();
}
isSmall = true;
make_big();
for (size_t i = 0; i < _size; i++) {
(*data.big)[i] = 0;
}
}
void OptimizedArrayList::clear() {
make_unique();
if (!isSmall) {
data.big.reset();
data.big.~shared_ptr();
isSmall = true;
}
memset(&data.small, 0, sizeof(uint32_t));
_size = 0;
}
void OptimizedArrayList::make_unique() {
if (isSmall || data.big.unique()) {
return;
}
data.big = std::make_shared<std::vector<uint32_t >>(*data.big);
}
OptimizedArrayList::~OptimizedArrayList() {
if (!isSmall) {
data.big.reset();
data.big.~shared_ptr();
}
}