-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.cpp
More file actions
206 lines (189 loc) · 5.1 KB
/
node.cpp
File metadata and controls
206 lines (189 loc) · 5.1 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
#include <iostream>
#include <sstream>
#include "node.hpp"
namespace text_editor
{
using handle = rope_node::handle;
std::invalid_argument ERROR_OOB_NODE = std::invalid_argument("Error: string index out of bounds");
rope_node::rope_node(handle left, handle right) : fragment_("")
{
this->left_ = move(left);
this->right_ = move(right);
this->weight_ = this->left_->getLength();
}
rope_node::rope_node(const std::string &str) : weight_(str.length()), left_(nullptr), right_(nullptr), fragment_(str) {}
rope_node::rope_node(const rope_node &aNode) : weight_(aNode.weight_), fragment_(aNode.fragment_)
{
rope_node *tmpLeft = aNode.left_.get();
rope_node *tmpRight = aNode.right_.get();
if (tmpLeft == nullptr)
this->left_ = nullptr;
else
this->left_ = std::make_unique<rope_node>(*tmpLeft);
if (tmpRight == nullptr)
this->right_ = nullptr;
else
this->right_ = std::make_unique<rope_node>(*tmpRight);
}
bool rope_node::isLeaf(void) const
{
return this->left_ == nullptr && this->right_ == nullptr;
}
size_t rope_node::getLength() const
{
if (this->isLeaf())
return this->weight_;
size_t tmp = (this->right_ == nullptr) ? 0 : this->right_->getLength();
return this->weight_ + tmp;
}
char rope_node::getCharByIndex(size_t index) const
{
size_t w = this->weight_;
if (this->isLeaf())
{
if (index >= this->weight_)
{
throw ERROR_OOB_NODE;
}
else
{
return this->fragment_[index];
}
}
else
{
if (index < w)
{
return this->left_->getCharByIndex(index);
}
else
{
return this->right_->getCharByIndex(index - w);
}
}
}
std::string rope_node::getSubstring(size_t start, size_t len) const
{
size_t w = this->weight_;
if (this->isLeaf())
{
if (len < w)
{
return this->fragment_.substr(start, len);
}
else
{
return this->fragment_;
}
}
else
{
if (start < w)
{
std::string lResult = (this->left_ == nullptr) ? "" : this->left_->getSubstring(start, len);
if ((start + len) > w)
{
size_t tmp = w - start;
std::string rResult = (this->right_ == nullptr) ? "" : this->right_->getSubstring(w, len - tmp);
return lResult.append(rResult);
}
else
{
return lResult;
}
}
else
{
return (this->right_ == nullptr) ? "" : this->right_->getSubstring(start - w, len);
}
}
}
std::string rope_node::treeToString(void) const
{
if (this->isLeaf())
{
return this->fragment_;
}
std::string lResult = (this->left_ == nullptr) ? "" : this->left_->treeToString();
std::string rResult = (this->right_ == nullptr) ? "" : this->right_->treeToString();
return lResult.append(rResult);
}
std::pair<handle, handle> splitAt(handle node, size_t index)
{
size_t w = node->weight_;
if (node->isLeaf())
{
return std::pair<handle, handle>{
std::make_unique<rope_node>(node->fragment_.substr(0, index)),
std::make_unique<rope_node>(node->fragment_.substr(index, w - index))
};
}
handle oldRight = move(node->right_);
if (index < w)
{
node->right_ = nullptr;
node->weight_ = index;
std::pair<handle, handle> splitLeftResult = splitAt(move(node->left_), index);
node->left_ = move(splitLeftResult.first);
return std::pair<handle, handle>{
move(node),
std::make_unique<rope_node>(move(splitLeftResult.second), move(oldRight))};
}
else if (w < index)
{
std::pair<handle, handle> splitRightResult = splitAt(move(oldRight), index - w);
node->right_ = move(splitRightResult.first);
return std::pair<handle, handle>{
move(node),
move(splitRightResult.second)};
}
else
{
return std::pair<handle, handle>{
move(node->left_), move(oldRight)};
}
}
size_t rope_node::getDepth(void) const
{
if (this->isLeaf())
return 0;
size_t lResult = (this->left_ == nullptr) ? 0 : this->left_->getDepth();
size_t rResult = (this->right_ == nullptr) ? 0 : this->right_->getDepth();
return std::max(++lResult, ++rResult);
}
void rope_node::getLeaves(std::vector<rope_node *> &v)
{
if (this->isLeaf())
{
v.push_back(this);
}
else
{
rope_node *tmpLeft = this->left_.get();
if (tmpLeft != nullptr)
tmpLeft->getLeaves(v);
rope_node *tmpRight = this->right_.get();
if (tmpRight != nullptr)
tmpRight->getLeaves(v);
}
}
void rope_node::print_tree(std::ostream &out, int level) const
{
if (this->isLeaf())
{
out << std::string(level * 4, ' ') << "Leaf: \"" << fragment_ << "\" (Length: " << fragment_.length() << ")\n";
}
else
{
out << std::string(level * 4, ' ') << "Node (Total Length: " << this->getLength() << ")\n";
if (left_)
{
left_->print_tree(out, level + 1);
}
if (right_)
{
right_->print_tree(out, level + 1);
}
}
}
}