-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToken.cpp
174 lines (156 loc) · 3.99 KB
/
Token.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
#include <iostream>
#include <cstring>
#include <cctype>
#include "Token.h"
using namespace std;
// default constructor
Token::Token()
{
// std::cout << "Token default constructor is called." << std::endl;
this->frequency = 0;
this->cstr[0] = '\0';
}
// explicit normal constructor
Token::Token(const char* chars, int line_num)
{
// std::cout << "Token explicit normal constructor is called." << std::endl;
this->cstr = new char[strlen(chars)];
for (int i = 0; i < strlen(chars); i++)
{
this->cstr[i] = chars[i];
}
this->frequency = 1;
this->number_list.pushBack(line_num);
}
// copy constructor
Token::Token(const Token& token)
{
// std::cout << "Token copy constructor is called." << std::endl;
this->frequency = token.getFrequency();
this->number_list = token.getNumberList();
this->cstr = new char[token.size()];
for (int i = 0; i < token.size(); i++)
{
this->cstr[i] = token.c_str()[i];
}
}
// move constructor
Token::Token(Token&& token)
{
// std::cout << "Token move constructor is called." << std::endl;
this->cstr = token.c_str();
this->number_list = std::move(token.number_list);
this->frequency = token.getFrequency();
token.cstr = nullptr;
token.frequency = 0;
}
// copy assignment operator
Token& Token::operator=(const Token& rhs)
{
// std::cout << "Token copy assignment operator is called." << std::endl;
// if the addresses are not the same, proceed with copying
if (&rhs != this)
{
this->frequency = rhs.getFrequency();
this->number_list = rhs.getNumberList();
delete[] this->cstr;
this->cstr = new char[rhs.size()];
for (int i = 0; i < rhs.size(); i++)
{
this->cstr[i] = rhs.c_str()[i];
}
}
return *this;
}
// move assignment operator
Token& Token::operator=(Token &&rhs)
{
// std::cout << "Token move assignment operator is called." << std::endl;
if (&rhs != this)
{
delete[] this->cstr;
this->frequency = rhs.getFrequency();
this->number_list = std::move(rhs.number_list);
this->cstr = rhs.c_str();
rhs.cstr = nullptr;
rhs.frequency = 0;
}
return *this;
}
// destructor
Token::~Token()
{
// std::cout << "Token destructor is called." << std::endl;
delete[] this->cstr;
}
// returns a constant pointer to this token's cstr
char *Token::c_str() const
{
return this->cstr;
}
// returns this token's list of line numbers
const ArrayList& Token::getNumberList() const
{
return this->number_list;
}
// returns this token's frequency
int Token::getFrequency() const
{
return this->frequency;
}
// adds line num to the end of this token's number list
void Token::addLineNumber(int line_num)
{
this->number_list.pushBack(line_num);
this->frequency++;
}
// returns the length of this token's cstr
int Token::size() const
{
if (this->cstr != NULL)
{
return strlen(this->cstr);
}
else
{
return 0;
}
}
// returns -1, 0, or 1, depending on whether this token's
// cstr is less than, equal to, or grater than aToken's cstr
int Token::compare(const Token& aToken) const
{
if(strcmp(this->cstr, aToken.c_str()) < 0)
{
return -1;
}
else if(strcmp(this->cstr, aToken.c_str()) == 0)
{
return 0;
}
else
{
return 1;
}
}
// << operator overload
// prints this token's cstr followed by its number list
std::ostream &operator<<(std::ostream &output, const Token &tokenToPrint)
{
// if used is 0, return empty string because we can't print anything
if (tokenToPrint.size() == 0)
{
return output;
}
for (int i = 0; i < tokenToPrint.size(); i++)
{
output << tokenToPrint.c_str()[i] << " ";
}
output << " " << tokenToPrint.number_list;
return output;
}
void Token::print(std::ostream &output) const
{
output << this->cstr << " " << "(" << this->frequency << ")";
output << " " << this->number_list << endl;
}