-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.cc
239 lines (184 loc) · 5.2 KB
/
tag.cc
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
#include "tag.h"
using namespace std;
// utility helper functions ////////
string makeIndent(int indentLevel) {
string retVal = "";
for (int i = 0; i < indentLevel; ++i) {
retVal += " ";
}
return retVal;
}
void removeAtIndex(Tag **arr, int idx, int len) {
for (int i = idx; i < len; ++i) {
if (i == (len - 1)) {
arr[i] = nullptr;
break;
}
arr[i] = arr[i + 1];
}
}
///////////////////////////////////
// begin Tag methods
// normal constructor
Tag::Tag(const string &type)
: type{type}, id{""}, value{""}, parent{nullptr}, children{nullptr},
childrenLength{0}, childrenCapacity{0} {}
// copy constructor
Tag::Tag(const Tag &other)
: type{other.type}, id{other.id}, value{other.value},
parent{
other
.parent}, // using other.parent for now. Change later if necessary
children{nullptr}, childrenLength{other.childrenLength},
childrenCapacity{other.childrenCapacity} {
if (other.children != nullptr) {
children = new Tag *[other.childrenCapacity];
for (int i = 0; i < other.childrenLength; ++i) {
// for each child, recursively call copy ctor to populate
if (other.children[i] != nullptr)
children[i] = new Tag{*other.children[i]};
}
} else {
children = nullptr;
}
}
void Tag::swap(Tag &other) {
std::swap(type, other.type);
std::swap(id, other.id);
std::swap(value, other.value);
std::swap(parent, other.parent);
std::swap(children, other.children);
std::swap(childrenLength, other.childrenLength);
std::swap(childrenCapacity, other.childrenCapacity);
}
// move constructor
Tag::Tag(Tag &&other)
: type{other.type}, id{other.id}, value{other.value}, parent{other.parent},
children{other.children}, childrenLength{other.childrenLength},
childrenCapacity{other.childrenCapacity} {
other.parent = nullptr;
other.children = nullptr;
}
// copy assignment operator
Tag &Tag::operator=(const Tag &other) {
Tag tmp = other;
this->swap(tmp);
return *this;
}
// move assignment operator
Tag &Tag::operator=(Tag &&other) {
this->swap(other);
return *this;
}
// destructor
Tag::~Tag() {
// destroy the heap allocated children
if (children != nullptr) {
for (int i = 0; i < childrenLength; ++i) {
// destroy each heap alloc'd child in the array
delete children[i];
}
// destroy the array itself
delete[] children;
children = nullptr;
}
parent = nullptr;
}
void Tag::resizeChildren() { // called whenever we intend to add a Tag
if (children == nullptr) {
// create the array
children = new Tag *[1];
childrenCapacity = 1;
return;
}
if (childrenLength == childrenCapacity) {
childrenCapacity *= 2;
Tag **newArr = new Tag *[childrenCapacity];
for (int i = 0; i < childrenLength; ++i) {
newArr[i] = children[i];
children[i] = nullptr;
}
delete[] children;
children = newArr;
}
}
const string &Tag::getType() const { return type; }
const string &Tag::getId() const { return id; }
void Tag::setId(const string &_id) { id = _id; }
const string &Tag::getValue() const { return value; }
void Tag::setValue(const string &_value) { value = _value; }
Tag *Tag::getParent() const { return parent; }
void Tag::setParent(Tag *_parent) { parent = _parent; }
void Tag::print(ostream &out, int indentLevel) const {
string opening = makeIndent(indentLevel) + "<" + type;
if (id != "")
opening += " id='" + id + "'";
if (value != "")
opening += " value='" + value + "'";
opening += ">";
string closing = makeIndent(indentLevel) + "</" + type + ">";
out << opening << endl;
// recursively print all children
for (int i = 0; i < childrenLength; ++i) {
children[i]->print(out, indentLevel + 1);
}
out << closing << endl;
}
void Tag::addChild(Tag *other) {
resizeChildren(); // does nothing if children does not need resizing
children[childrenLength] = other;
++childrenLength;
}
void Tag::removeChild(Tag *other) {
for (int i = 0; i < childrenLength; ++i) {
if (children[i] == other) {
removeAtIndex(children, i, childrenLength);
--childrenLength;
break;
}
}
}
Tag *Tag::findChild(string &type) {
Tag *retVal = nullptr;
for (int i = 0; i < childrenLength; ++i) {
if (children[i]->type == type) {
retVal = children[i];
break;
}
}
return retVal;
}
Tag *Tag::findChildId(string &id) {
Tag *retVal = nullptr;
for (int i = 0; i < childrenLength; ++i) {
if (children[i]->id == id) {
retVal = children[i];
break;
}
}
return retVal;
}
TagIterator Tag::begin() const { return TagIterator{children}; }
TagIterator Tag::end() const {
Tag **thePtr = children + childrenLength;
return TagIterator{thePtr};
}
// end Tag methods
// begin TagIterator methods
TagIterator::TagIterator(Tag **t) : t{t} {}
Tag *TagIterator::operator*() const { return *t; }
TagIterator TagIterator::operator++() {
t = t + 1;
return *this;
}
bool TagIterator::operator==(const TagIterator &other) const {
return t == other.t;
}
bool TagIterator::operator!=(const TagIterator &other) const {
return t != other.t;
}
// end TagIterator methods
ostream &operator<<(ostream &out, const Tag &tag) {
tag.print(out);
return out;
}