-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHW4.cpp
More file actions
294 lines (253 loc) · 8.94 KB
/
Copy pathHW4.cpp
File metadata and controls
294 lines (253 loc) · 8.94 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
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
//HW4 by Jillian Handrahan
// NEtID: jehandra SUID: 837700957
//HW4 Due: 11:59pm, Thursday, March. 9
//You are not allowed to use any helper functions
//You are not alllowed to use any extra class.
#include <iostream>
#include <map>
#include <list>
#include <tuple>
using namespace std;
class course {
public:
string name;
int section;
int credits;
string grade;
course() {}
course(string n, int s, int c, string g) { name = n; section = s; credits = c; grade = g; }
};
template <class T> class Record {
public:
map<int, list< tuple <int*, int, T*>* >*> *pData;
//Record() { }
Record();
Record(const initializer_list < pair<int, initializer_list<tuple <int, int, T> >>>& I);
Record(const Record<T>& R);//Copy Constructor
void operator=(const Record<T>& R);//Copy Assignment
~Record();//Destructor
Record<T> ThreeTimes();//All int objects are multiplied by 3, except those in courses
//and the keys (i.e., the first part of each map elements).
//See sample screenshot.
Record(Record<T>&& R);//Move Constructor
void operator=(Record<T>&& R);//Move Assignment
};
template <class T> Record<T>::Record() {
pData = new map<int, list< tuple <int*, int, T*>* >*>{};
}
template <class T> void Record<T>::operator=(Record<T>&& R) {//Move Assignment
cout << "Move Assignment" << endl;
std::swap(this->pData, R.pData);
R.pData = nullptr;
//Your code
}
template <class T> Record<T>::Record(Record<T>&& R) {//Move Constructor
cout << "Move Constructor" << endl;
std::swap(this->pData, R.pData);
R.pData = nullptr;
//Your code
}
//
//Copy constructor will be called in three situations:
//1. When object is declared and initialized in the same statement.
//2. call by value;
//3. return by value
template<class T> Record<T> Record<T>::ThreeTimes() {//ThreeTimes , deletefirst??
//R3 = R1.threetimes();
//r1 is this
//return 3 times this
//we should not change the value of R1
Record<T> ret {};
ret = *this;
cout << "ThreeTimes" << endl;
// Record<T> ret = std::move(temp);
for (auto& i : *(ret.pData)) {
for (auto& tups : *(i.second)) {
*std::get<0>(*tups) *= 3;
std::get<1>(*tups) *= 3;
}
}
return ret;
}
template<class T> Record<T>::~Record() {//Destructor
if (pData != nullptr) {
cout << "Destructor" << endl;
//Yor code
for (auto& listDat : *pData){
int key = listDat.first;
for (auto& tups : *(listDat.second) ) {
delete get<0>(*tups); //delete first element of tuple
delete get<2>(*tups); // delete T
delete tups; //delete second elem
}
delete listDat.second; //delete list of tuples
// pData->erase(key);
//listDat++;
}
delete pData;
}
else {
cout << "Destructor" << endl;
//exit(0);
}
}
template <class T> void Record<T>::operator=(const Record<T>& R) {//Copy Assignment
cout << "Copy Assignment" << endl;
//this is R1
// R is R3
//Your code
if (this != &R) {
//if Lvalue pdata (R3) != rvalue pdata (R1) (otherwise we would do nothing)
if (R.pData != nullptr) {
// *pData = new std::map<int, std::list<std::tuple<int*, int, T*>*>*>();
map<int, list< tuple <int*, int, T*>* >*> *pData = new map<int, list< tuple <int*, int, T*>* >*>{};
for (const auto& pair : *R.pData) {
int key = pair.first;
std::list<tuple <int*, int, T*>* >* mapLst{ new std::list<tuple <int*, int, T*>* > {} };
for (const auto& t : *(pair.second)) {
int* tup0 = new int(*std::get<0>(*t));
int tup1 = std::get<1>(*t);
T* data = new T(*std::get<2>(*t));
std::tuple<int*, int, T*>* tupp = new std::tuple<int*, int, T*>(tup0,tup1,data);
mapLst->push_back(tupp);
}
pData->insert(std::make_pair(key, mapLst));
}
this->pData = pData;
}
}
}
template <class T> Record<T>::Record(const Record<T>& R) { //Copy constructor
cout << "Copy Constructor" << endl;
//Your code
if (R.pData != nullptr) {
// *pData = new std::map<int, std::list<std::tuple<int*, int, T*>*>*>();
map<int, list< tuple <int*, int, T*>* >*> *pData = new map<int, list< tuple <int*, int, T*>* >*>{};
for (const auto& pair : *R.pData) {
int key = pair.first;
std::list<tuple <int*, int, T*>* >* mapLst{ new std::list<tuple <int*, int, T*>* > {} };
for (const auto& t : *(pair.second)) {
int* tup0 = new int(*std::get<0>(*t));
int tup1 = std::get<1>(*t);
T* data = new T(*std::get<2>(*t));
std::tuple<int*, int, T*>* tupp = new std::tuple<int*, int, T*>(tup0,tup1,data);
mapLst->push_back(tupp);
}
pData->insert(std::make_pair(key, mapLst));
}
this->pData = pData;
}
}
template <class T> Record<T>::Record(const initializer_list < pair<int, initializer_list<tuple <int, int, T>>>>& I) {
//Your code
cout << "Initializer List" << endl;
map<int, list< tuple <int*, int, T*>* >*> *pData = new map<int, list< tuple <int*, int, T*>* >*>{};
for (const auto& i : I) {
int key = i.first;
std::list<tuple <int*, int, T*>* >* mapLst{ new std::list<tuple <int*, int, T*>* > {} };
for (const auto& t : i.second) {
int* tup0 = new int(get<0>(t));
int* tup1 = new int(get<1>(t));
T* data = new T(get<2>(t));
std::tuple<int*, int, T*>* tupp = new std::tuple<int*, int, T*>(tup0,*tup1,data);
mapLst->push_back(tupp);
}
// if (pData == nullptr) {
// std::cerr << "Error: Could not allocate memory for map." << std::endl;
// break;
// }
pData->insert(std::make_pair(key, mapLst));
this->pData = pData;
}
}
//You need to implement all needed overloaded operator<<
template <class T> ostream& operator<<(ostream& str, const Record<T>& R);
ostream& operator<<(ostream& str, const course& t);
int main() {
course C1("CIS554", 1, 3, "A-"), C2("CSE674", 1, 3, "B+"), C3("MAT296", 8, 4, "A"), C4("WRT205", 5, 3, "A");
course C5("CIS351", 2, 3, "A-"), C6("PSY205", 5, 3, "B+"), C7("MAT331", 2, 3, "A"), C8("ECN203", 4, 3, "A");
Record<course> R1{ {10, { {12, 11, C1}, {9, 8, C2} }},
{7, {{8,7, C3}, {3,2, C4}, {1, 0, C5}}},{4, {{21, 20, C6}, {15, 14,C7}} } };
cout << R1 << endl;
Record<course> R2{ R1 };
cout << R2 << endl;
Record<course> R3;
R3 = R1;
cout << R3 << endl;
R3 = R1.ThreeTimes();
cout << R3 << endl;
R3 = R3;
cout << R3 << endl;
return 0;
}
template <class T> ostream& operator<<(ostream& str, const Record<T>& R) {
if (R.pData == nullptr) {
cout << " " << endl;
//exit(0);
}
str << "{" << endl;
for (auto& i : *R.pData) {
str << "{" << i.first << " { ";
list< tuple <int*, int, T*>* >* lsts = i.second;
for (auto& tupes : *lsts) {
tuple <int*, int, T*>* tups = new tuple <int*, int, T*>{};
tups = tupes;
int* tup1 = get<0>(*tups);
int tup2 = get<1>(*tupes);
T* data = get<2>(*tupes);
str << *tup1 << " " << tup2 << " " << "("<< *data << ") ";
}
str << "} }" << endl;
}
str << "}" << endl;
return str;
}
ostream& operator<<(ostream& str, const course& t) {
//Your code
str << t.name << " " << t.section << " " << t.credits << " " << t.grade;
return str;
}
//Initializer List
//{
//{4 { 21 20 (PSY205 5 3 B+) 15 14 (MAT331 2 3 A) } }
//{7 { 8 7 (MAT296 8 4 A) 3 2 (WRT205 5 3 A) 1 0 (CIS351 2 3 A-) } }
//{10 { 12 11 (CIS554 1 3 A-) 9 8 (CSE674 1 3 B+) } }
//}
//
//Copy Constructor
//{
//{4 { 21 20 (PSY205 5 3 B+) 15 14 (MAT331 2 3 A) } }
//{7 { 8 7 (MAT296 8 4 A) 3 2 (WRT205 5 3 A) 1 0 (CIS351 2 3 A-) } }
//{10 { 12 11 (CIS554 1 3 A-) 9 8 (CSE674 1 3 B+) } }
//}
//
//Copy Assignment
//{
//{4 { 21 20 (PSY205 5 3 B+) 15 14 (MAT331 2 3 A) } }
//{7 { 8 7 (MAT296 8 4 A) 3 2 (WRT205 5 3 A) 1 0 (CIS351 2 3 A-) } }
//{10 { 12 11 (CIS554 1 3 A-) 9 8 (CSE674 1 3 B+) } }
//}
//
//Copy Constructor
//ThreeTimes
//Move Constructor
//Destructor
//Move Assignment
//Destructor
//{
//{4 { 63 60 (PSY205 5 3 B+) 45 42 (MAT331 2 3 A) } }
//{7 { 24 21 (MAT296 8 4 A) 9 6 (WRT205 5 3 A) 3 0 (CIS351 2 3 A-) } }
//{10 { 36 33 (CIS554 1 3 A-) 27 24 (CSE674 1 3 B+) } }
//}
//
//Copy Assignment
//{
//{4 { 63 60 (PSY205 5 3 B+) 45 42 (MAT331 2 3 A) } }
//{7 { 24 21 (MAT296 8 4 A) 9 6 (WRT205 5 3 A) 3 0 (CIS351 2 3 A-) } }
//{10 { 36 33 (CIS554 1 3 A-) 27 24 (CSE674 1 3 B+) } }
//}
//
//Destructor
//Destructor
//Destructor
//