/
optvector.cpp
162 lines (143 loc) · 4.07 KB
/
optvector.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
#include <assert.h>
#include "optvector.h"
shared_data::shared_data() : value(new std::vector<uint32_t>()) {}
shared_data::shared_data(shared_data const &other) : value(other.value) {}
void shared_data::make_own() {
if (!this->value.unique())
this->value = std::make_shared<std::vector<uint32_t>>(*this->value);
}
optvector::optvector() {
this->contains_small = 1;
}
optvector::optvector(optvector const &other) {
this->contains_small = other.contains_small;
if (this->is_small())
this->small_value = other.small_value;
else {
this->data = new shared_data(*other.data);
}
}
optvector::~optvector() {
if (!this->is_small())
delete (data);
}
uint32_t &optvector::back() {
if (this->is_small())
return this->small_value;
else {
this->data->make_own();
return this->data->value.get()->back();
}
}
uint32_t optvector::back() const {
if (this->is_small())
return this->small_value;
else
return this->data->value.get()->back();
}
size_t optvector::size() const {
if (this->is_small())
return (this->small_size() ? 1 : 0);
else
return this->data->value.get()->size();
}
uint32_t optvector::operator[](size_t i) const {
assert(i < size());
if (this->is_small())
return this->small_value;
else
return this->data->value.get()->at(i);
}
uint32_t &optvector::operator[](size_t i) {
assert(i < size());
if (this->is_small()) {
this->contains_small = 3;
return this->small_value;
} else {
this->data->make_own();
return this->data->value.get()->at(i);
}
}
void optvector::swap(optvector &b) {
std::swap(this->data, b.data);
std::swap(this->contains_small, b.contains_small);
}
optvector &optvector::operator=(optvector const &other) {
optvector tmp(other);
this->swap(tmp);
return *this;
}
void optvector::resize(size_t i) {
if (this->is_small()) {
if (i != 1) {
data = new shared_data;
data->value.get()->resize(i);
this->contains_small = 0;
} else if (this->small_size()) {
this->contains_small = 1;
}
} else {
this->data->make_own();
this->data->value.get()->resize(i);
}
}
void optvector::resize(size_t i, uint32_t val) {
if (this->is_small()) {
if (i != 1) {
data = new shared_data;
data->value.get()->resize(i, val);
this->contains_small = 0;
} else if (!this->small_size()) {
this->small_value = val;
this->contains_small = 3;
}
} else {
if (i == 1) {
delete (this->data);
this->small_value = val;
this->contains_small = 3;
} else {
this->data->make_own();
this->data->value.get()->resize(i, val);
}
}
}
uint32_t optvector::pop_back() {
if (this->data->value.get()->size() > 1) {
if (this->data->value.get()->size() == 2) {
uint32_t tmp = this->data->value.get()->at(0);
uint32_t res = this->data->value.get()->at(1);
delete (this->data);
this->small_value = tmp;
this->contains_small = 3;
return res;
} else {
this->data->make_own();
uint32_t res = this->data->value.get()->back();
this->data->value.get()->pop_back();
return res;
}
} else {
uint32_t res = small_value;
small_value = 0;
contains_small = 1;
return res;
}
}
void optvector::push_back(uint32_t val) {
if (this->is_small()) {
if (!this->small_size()) {
this->small_value = val;
this->contains_small = 3;
} else {
uint32_t tmp = this->small_value;
data = new shared_data;
data->value.get()->push_back(tmp);
data->value.get()->push_back(val);
this->contains_small = 0;
}
} else {
this->data->make_own();
this->data->value.get()->push_back(val);
}
}