-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrope.cpp
More file actions
257 lines (219 loc) · 5.8 KB
/
rope.cpp
File metadata and controls
257 lines (219 loc) · 5.8 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
#include "rope.hpp"
namespace text_editor
{
std::invalid_argument ERROR_OOB_ROPE = std::invalid_argument("Error: string index out of bounds");
rope::rope(void) : rope("")
{
}
rope::rope(const string &str)
{
this->root_ = std::make_unique<rope_node>(str);
}
rope::rope(const rope &r)
{
rope_node newRoot = rope_node(*r.root_);
this->root_ = std::make_unique<rope_node>(newRoot);
}
string rope::toString(void) const
{
if (this->root_ == nullptr)
return "";
return this->root_->treeToString();
}
size_t rope::length(void) const
{
if (this->root_ == nullptr)
return 0;
return this->root_->getLength();
}
char rope::at(size_t index) const
{
if (this->root_ == nullptr)
throw ERROR_OOB_ROPE;
return this->root_->getCharByIndex(index);
}
string rope::substring(size_t start, size_t len) const
{
size_t actualLength = this->length();
if (start > actualLength || (start + len) > actualLength)
throw ERROR_OOB_ROPE;
return this->root_->getSubstring(start, len);
}
void rope::insert(size_t i, const string &str)
{
this->insert(i, rope(str));
}
void rope::insert(size_t i, const rope &r)
{
if (this->length() < i)
{
throw ERROR_OOB_ROPE;
}
else
{
rope tmp = rope(r);
std::pair<handle, handle> origRopeSplit = splitAt(move(this->root_), i);
handle tmpConcat = std::make_unique<rope_node>(move(origRopeSplit.first), move(tmp.root_));
this->root_ = std::make_unique<rope_node>(move(tmpConcat), move(origRopeSplit.second));
}
}
void rope::append(const string &str)
{
rope tmp = rope(str);
this->root_ = std::make_unique<rope_node>(move(this->root_), move(tmp.root_));
}
void rope::append(const rope &r)
{
rope tmp = rope(r);
this->root_ = std::make_unique<rope_node>(move(this->root_), move(tmp.root_));
}
void rope::rdelete(size_t start, size_t len)
{
size_t actualLength = this->length();
if (start > actualLength || start + len > actualLength)
{
throw ERROR_OOB_ROPE;
}
else
{
std::pair<handle, handle> firstSplit = splitAt(move(this->root_), start);
std::pair<handle, handle> secondSplit = splitAt(move(firstSplit.second), len);
secondSplit.first.reset();
this->root_ = std::make_unique<rope_node>(move(firstSplit.first), move(secondSplit.second));
}
}
void rope::print_tree(std::ostream &out, int level) const
{
if (root_)
{
root_->print_tree(out, level);
}
else
{
out << "Empty rope." << std::endl;
}
}
bool rope::isBalanced(void) const
{
if (this->root_ == nullptr)
return true;
size_t d = this->root_->getDepth();
return this->length() >= fib(d + 2);
}
void rope::balance(void)
{
if (!this->isBalanced())
{
std::vector<size_t> intervals = buildFibList(this->length());
std::vector<handle> nodes(intervals.size());
std::vector<rope_node *> leaves;
this->root_->getLeaves(leaves);
size_t i;
size_t max_i = intervals.size() - 1;
size_t currMaxInterval = 0;
handle acc = nullptr;
handle tmp = nullptr;
for (auto &leaf : leaves)
{
size_t len = leaf->getLength();
bool inserted = false;
if (len > 0)
{
acc = std::make_unique<rope_node>(*leaf);
i = 0;
while (!inserted)
{
while (i < max_i && len >= intervals[i + 1])
{
if (nodes[i].get() != nullptr)
{
tmp = std::make_unique<rope_node>(*nodes[i].get());
acc = std::make_unique<rope_node>(*acc.get());
acc = std::make_unique<rope_node>(move(tmp), move(acc));
len = acc->getLength();
if (len >= intervals[i + 1])
nodes[i] = nullptr;
}
i++;
}
if (nodes[i].get() == nullptr)
{
nodes[i].swap(acc);
inserted = true;
if (i > currMaxInterval)
currMaxInterval = i;
}
else
{
tmp = std::make_unique<rope_node>(*nodes[i].get());
acc = std::make_unique<rope_node>(*acc.get());
acc = std::make_unique<rope_node>(move(tmp), move(acc));
len = acc->getLength();
if (len >= intervals[i + 1])
nodes[i] = nullptr;
}
}
}
}
acc = move(nodes[currMaxInterval]);
for (int idx = currMaxInterval; idx >= 0; idx--)
{
if (nodes[idx] != nullptr)
{
tmp = std::make_unique<rope_node>(*nodes[idx].get());
acc = std::make_unique<rope_node>(move(acc), move(tmp));
}
}
this->root_ = move(acc);
}
}
rope &rope::operator=(const rope &rhs)
{
if (this == &rhs)
return *this;
this->root_.reset();
this->root_ = std::make_unique<rope_node>(*(rhs.root_.get()));
return *this;
}
bool rope::operator==(const rope &rhs) const
{
return this->toString() == rhs.toString();
}
bool rope::operator!=(const rope &rhs) const
{
return !(*this == rhs);
}
std::ostream &operator<<(std::ostream &out, const rope &r)
{
return out << r.toString();
}
size_t fib(size_t n)
{
int a = 0, b = 1, next;
if (n == 0)
return a;
for (size_t i = 2; i <= n; i++)
{
next = a + b;
a = b;
b = next;
}
return b;
};
std::vector<size_t> buildFibList(size_t len)
{
int a = 0, b = 1, next;
std::vector<size_t> intervals = std::vector<size_t>();
while (a <= len)
{
if (a > 0)
{
intervals.push_back(b);
}
next = a + b;
a = b;
b = next;
}
return intervals;
}
}