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#ifndef SET_H
#define SET_H
#include <iostream>
#include <string>
#include "Object.h"
#include <stdexcept>
#include <fstream>
#include <cctype>
namespace tcp
{
template <class T>
class Set : public Object
{
private:
struct Node {
T data;
Node* next;
Node(T d, Node* n = nullptr) : data(d), next(n) {}
};
Node* head;
size_t sz;
public:
Set() : head(nullptr), sz(0) {}
Set(const Set<T>& obj) : head(nullptr), sz(0) {*this = obj;}
Set<T>& operator=(const Set<T>& rhs)
{
if(this != &rhs) {
// Clear current list
Node* current = head;
while(current) {
Node* temp = current;
current = current->next;
delete temp;
}
head = nullptr;
sz = 0;
// Copy rhs list
Node* rhsCurrent = rhs.head;
Node** currentPtr = &head;
while(rhsCurrent) {
*currentPtr = new Node(rhsCurrent->data);
currentPtr = &((*currentPtr)->next);
rhsCurrent = rhsCurrent->next;
}
sz = rhs.sz;
}
return *this;
}
virtual ~Set() {
Node* current = head;
while(current) {
Node* temp = current;
current = current->next;
delete temp;
}
}
size_t size() const {return sz;}
bool contains(const T& obj) const
{
Node* current = head;
while(current) {
if(current->data == obj) return true;
current = current->next;
}
return false;
}
void insert(const T& obj)
{
if(!contains(obj)) {
head = new Node(obj, head);
sz++;
}
}
const T& operator[](size_t i) const
{
if(i >= sz) throw std::out_of_range("out of bound");
Node* current = head;
for(size_t j = 0; j < i; j++) {
current = current->next;
}
return current->data;
}
std::string toString() const override
{
std::stringstream out;
out << "{";
Node* current = head;
while(current) {
out << current->data;
if(current->next) out << ",";
current = current->next;
}
out << "}";
return out.str();
}
};
class Triple : public Object
{
private:
std::string ends[2];
char mid;
public:
Triple() : ends{"",""}, mid('\0') {}
Triple(std::string a,char b,std::string c) : ends{a,c}, mid(b) {}
Triple(const Triple& obj) {*this = obj;}
Triple& operator=(const Triple& rhs)
{
if(this != &rhs)
{
ends[0] = rhs.ends[0];
ends[1] = rhs.ends[1];
mid = rhs.mid;
}
return *this;
}
const std::string& first() const {return ends[0];}
const char& second() const {return mid;};
const std::string& third() const {return ends[1];}
virtual ~Triple() {}
std::string toString() const override
{
std::stringstream out;
out << "(" << ends[0] << "," << mid << "," << ends[1] << ")";
return out.str();
}
friend bool operator==(const Triple& lhs,const Triple& rhs)
{
return lhs.ends[0] == rhs.ends[0] && lhs.mid == rhs.mid && lhs.ends[1] == rhs.ends[1];
}
friend bool operator!=(const Triple& lhs,const Triple& rhs)
{
return !(lhs == rhs);
}
};
bool Valid(char ch);
bool FileReader(std::string& filename, Set<std::string>& Q, Set<char>& S, Set<Triple>& d, std::string& q0, Set<std::string>& F);
}
#endif