-
Notifications
You must be signed in to change notification settings - Fork 0
/
TokenList.cpp
375 lines (339 loc) · 9.18 KB
/
TokenList.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <iostream>
#include "TokenList.h"
// default constructor
TokenList::TokenList() : head{}, tail{}, theSize{}
{
// std::cout << "Tokenlist default constructor is called." << std::endl;
}
// copy constructor
TokenList::TokenList(const TokenList& list)
{
// std::cout << "Tokenlist copy constructor is called." << std::endl;
this->theSize = list.size();
this->head = new TNode(list.front());
this->tail = new TNode(list.back());
TokenList::TNode *nodeBeingCopied = list.getHead();
TokenList::TNode *newNode = this->head;
// copy the head node
addAfter(newNode, nodeBeingCopied->theToken);
// copy the rest of the nodes
while (nodeBeingCopied != list.getTail())
{
nodeBeingCopied = nodeBeingCopied->next;
addAfter(newNode, nodeBeingCopied->theToken);
newNode = newNode->next;
}
}
// move constructor
TokenList::TokenList(TokenList&& list)
{
// std::cout << "Tokenlist move constructor is called." << std::endl;
this->theSize = list.size();
this->head = std::move(list.getHead());
this->tail = std::move(list.getTail());
list.theSize = 0;
}
// copy assignment operator
TokenList& TokenList::operator=(const TokenList& rhs)
{
// std::cout << "Tokenlist copy assignment operator is called." << std::endl;
if (&rhs != this)
{
// delete old stuff
TokenList::TNode *oldNode = this->head;
TokenList::TNode *tempNode;
while (oldNode != this->tail)
{
tempNode = oldNode;
oldNode = oldNode->next;
free(tempNode);
}
free(this->tail);
// copy new stuff
this->theSize = rhs.size();
this->head = new TNode(rhs.front());
this->tail = new TNode(rhs.back());
TokenList::TNode *nodeBeingCopied = rhs.getHead();
TokenList::TNode *newNode = this->head;
// copy the head node
addAfter(newNode, nodeBeingCopied->theToken);
// copy the rest of the nodes
while (nodeBeingCopied != rhs.getTail())
{
nodeBeingCopied = nodeBeingCopied->next;
addAfter(newNode, nodeBeingCopied->theToken);
newNode = newNode->next;
}
}
return *this;
}
// move assignment operator
TokenList& TokenList::operator=(TokenList&& rhs)
{
// std::cout << "Tokenlist move assignment operator is called." << std::endl;
if (&rhs != this)
{
// delete old stuff
TokenList::TNode *oldNode = this->head;
TokenList::TNode *tempNode;
while (oldNode != this->tail)
{
tempNode = oldNode;
oldNode = oldNode->next;
free(tempNode);
}
free(this->tail);
// move new stuff
this->theSize = rhs.size();
this->head = std::move(rhs.getHead());
this->tail = std::move(rhs.getTail());
rhs.theSize = 0;
}
return *this;
}
// destructor
TokenList::~TokenList()
{
// std::cout << "Tokenlist destructor is called." << std::endl;
TokenList::TNode *oldNode = this->head;
TokenList::TNode *tempNode;
while (oldNode != this->tail)
{
tempNode = oldNode;
oldNode = oldNode->next;
free(tempNode);
}
free(this->tail);
}
// determines whether the list is empty
bool TokenList::empty() const
{
return this->theSize == 0;
}
// returns theSize
size_t TokenList::size() const
{
return this->theSize;
}
// returns head TNode
TokenList::TNode *TokenList::getHead() const
{
return this->head;
}
// returns tail TNode
TokenList::TNode *TokenList::getTail() const
{
return this->tail;
}
// returns the Token at the front of this TokenList
const Token& TokenList::front() const
{
return this->head->theToken;
}
// returns the Token at the back of this TokenList
const Token& TokenList::back() const
{
return this->tail->theToken;
}
// Adds a new node storing aToken after the node to
// which p points. If p is nullptr, it adds the node to
// the front of the list.
void TokenList::addAfter(TNode* p, const Token& aToken)
{
TokenList::TNode *newNode = new TNode(aToken);
if (p != nullptr)
{
// new Node points to what p was previously pointing to
newNode->next = p->next;
p->next = newNode;
if(this->tail == p)
{
this->tail = newNode;
}
}
else
{
this->head = newNode;
this->tail = newNode;
}
this->theSize++;
}
// If the list is nonempty, removes the node at the front of the list and returns true;
// otherwise, returns false
bool TokenList::removeFront()
{
if(this->head != nullptr)
{
TokenList::TNode *oldNode = this->head;
this->head = oldNode->next;
delete[] oldNode;
this->theSize--;
return true;
}
else
{
return false;
}
}
// If the list is nonempty, removes the node at the end of the list and returns true;
// otherwise, returns false
bool TokenList::removeBack()
{
if(this->tail != nullptr)
{
// get the node before tail
TokenList::TNode *oldNode = this->head;
while (oldNode->next != this->tail)
{
oldNode = oldNode->next;
}
delete[] this->tail;
this->tail = oldNode;
this->tail->next = nullptr;
this->theSize--;
return true;
}
else
{
return false;
}
}
// Removes the node to which nodePtr points and returns true;
// otherwise, returns false.
bool TokenList::remove(TNode* nodePtr)
{
if((nodePtr != this->head) && (nodePtr != this->tail))
{
// get the Node after nodePtr
TokenList::TNode *nextNode = nodePtr->next;
// get the node before nodePtr
TokenList::TNode *oldNode = this->head;
while (oldNode->next != nodePtr)
{
oldNode = oldNode->next;
}
oldNode->next = nextNode;
free(nodePtr);
this->theSize--;
return true;
}
else
{
return false;
}
}
// Adds a new node storing aToken to the front of the list
void TokenList::addFront(const Token& aToken)
{
TokenList::TNode *newNode = new TNode(aToken);
if(this->head != nullptr)
{
newNode->next = this->head;
this->head = newNode;
}
else
{
this->head = this->tail = newNode;
}
this->theSize++;
}
// Adds a new node storing aToken to the end of the list
void TokenList::addBack(const Token& aToken)
{
TokenList::TNode *newNode = new TNode(aToken);
if(this->tail == nullptr)
{
this->head = this->tail = newNode;
}
else
{
this->tail->next = newNode;
this->tail = newNode;
}
this->theSize++;
}
// determines whether aToken is in the list
bool TokenList::search(const Token& aToken) const
{
TokenList::TNode *currentNode = this->head;
while (currentNode != nullptr)
{
if(currentNode->theToken.compare(aToken) == 0)
{
return true;
}
currentNode = currentNode->next;
}
return false;
}
// Adds aToken at its sorted position into the list so
// as to maintain the ascending order of the tokens in the list
void TokenList::addSorted(const Token& aToken)
{
ArrayList list = aToken.getNumberList();
int line_number;
list.get(list.size() - 1, line_number);
addSorted(aToken.c_str(), line_number);
}
void TokenList::addSorted(const std::string& str, int lineNumber)
{
Token aToken(str.c_str(), lineNumber); // create a node with str and lineNumber
TokenList::TNode* nodePtr = lookup(aToken); // look it up in the list
// if nullptr, should be first node
if (nodePtr == nullptr)
{
addFront(aToken);
}
// if same node, should add line number
else if ((aToken).compare(nodePtr->theToken) == 0)
{
(nodePtr->theToken).addLineNumber(lineNumber);
}
// else, add new node where appropriate
else
{
addAfter(nodePtr, aToken);
}
}
// If aToken is in the list, it returns a pointer to the node whose token
// is equal to aToken; otherwise, it returns a pointer to the node after
// which aToken would be inserted in the sorted list.
TokenList::TNode* TokenList::lookup(const Token& aToken) const
{
// nullptr means that aToken must be the first node
if (head == nullptr || (aToken).compare(this->head->theToken) < 0)
{
return nullptr;
}
TokenList::TNode* prev = this->head;
TokenList::TNode* current = this->head->next;
while (current != nullptr)
{
if ((current->theToken).compare(aToken) > 0)
{
return prev;
}
prev = current;
current = current->next;
}
return this->tail;
}
// prints the entire list to sout
void TokenList::print(std::ostream &output) const
{
if(this->theSize == 0)
{
std::cout << "empty TokenList" << std::endl;
}
else
{
TokenList::TNode *current = this->head;
int counter = 0;
do
{
current->theToken.print(output);
current = current->next;
counter++;
} while (theSize != counter);
}
}