-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashmap(1).cpp
209 lines (181 loc) · 4.78 KB
/
Hashmap(1).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
#include <iostream>
#include "HashMap.h"
#include "LinkedList.h"
#include <fstream>
#include <cstring>
#include <string>
#include <QFile>
#include <QDebug>
#include <QTextStream>
#include <QApplication>
using namespace std;
HashMap::HashMap(int maxSize)
: mapCapacity(maxSize), mapSize(0), File((QApplication::applicationDirPath()) + "/To-Do List.txt")
{
map = new LinkedList [mapCapacity];
for (int i = 0; i < mapCapacity; i++)
{
map[i] = LinkedList();
}
}
void HashMap::deleteEntireList()
{
for (int i = 0; i < mapCapacity; i++) {
map[i].~LinkedList();
}
}
HashMap::~HashMap()
{
for (int i = 0; i < mapCapacity; i++) {
map[i].~LinkedList();
}
delete[] map;
}
const HashMap& HashMap::operator=(const HashMap& rhsMap)
{
if (this != &rhsMap)
{
this->~HashMap();
mapSize = rhsMap.mapSize;
mapCapacity = rhsMap.mapCapacity;
if (mapSize > 0)
{
map = new LinkedList [mapCapacity];
for (size_t i = 0; i < mapCapacity; i++)
{
if (!rhsMap.map[i].Empty())
{
map[i] = LinkedList(rhsMap.map[i]);
}
}
}
}
return *this;
}
int HashMap::hashFunction(string key) const
{
return (tolower(key.front()) + tolower(key.back())) % mapCapacity;
}
QStringList HashMap::Search(const string& key)
{
int linkedListIndex = hashFunction(key);
return map[linkedListIndex].Search(key);
}
bool HashMap::isEmpty() const
{
return mapSize == 0;
}
void HashMap::insert(string key, int value, string date, bool status)
{
int linkedListIndex = hashFunction(key);
map[linkedListIndex].insert(key, value, date, status);
mapSize++;
qDebug()<<"inserted";
writeToFile(File);
}
void HashMap::Remove(const string& key, const int& value, const string& date)
{
int linkedListIndex = hashFunction(key);
map[linkedListIndex].Remove(key, value, date);
mapSize--;
writeToFile(File);
}
void HashMap::Edit(const string& oldkey, const int& oldvalue, const string& oldDate, int& newValue, const string& newKey, const string& newdate)
{
if (Exists(oldkey, oldvalue, oldDate))
{
Remove(oldkey, oldvalue, oldDate);
insert(newKey, newValue, newdate);
writeToFile(File);
}
else
{
cout << newKey << " with priority " << newValue << " does not exist\n";
return;
}
}
void HashMap::display(ostream& out) const
{
string line;
fstream File("To-Do List.txt", ios::in);
if (!File.is_open())
{
cerr << "Error opening file." << endl;
}
while (getline(File, line))
{
out << line << endl;
}
File.close();
}
void HashMap::writeToFile(QFile& file) const
{
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qWarning() << "Error opening file for writing:" << file.errorString();
return;
}
QTextStream out(&file);
qDebug() << "else entered";
for (int j = 5; j > 0; j--)
{
for (int i = 0; i < mapCapacity; i++)
{
map[i].Display(j, file);
}
}
file.close();
}
void HashMap::readFromFile(QFile& file)
{
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "Failed to open file:" << file.errorString();
return;
}
QTextStream in(&file);
QString line;
string item, date;
bool status = true;
QStringList stringList;
int priority;
while (!in.atEnd())
{
line = in.readLine();
stringList = line.split(',');
if (stringList.size() >= 2)
{
item = stringList.at(0).toStdString();
priority = stringList.at(1).toInt();
date = stringList.at(2).toStdString();
if(stringList.at(3).contains("Not"))
{
status = false;
}
insert(item, priority, date, status);
}
}
file.close();
}
bool HashMap::Exists(const string& key, const int& value, const string& date)
{
int linkedListIndex = hashFunction(key);
return map[linkedListIndex].Exists(key, value, date);
}
void HashMap::markAsDone(const string& key, const int& value)
{
int nodeIndex = hashFunction(key);
map[nodeIndex].markAsDone(key, value);
}
void HashMap::uncheck(const string& key, const int& value)
{
int nodeIndex = hashFunction(key);
map[nodeIndex].uncheck(key, value);
}
void HashMap::clearMarkedAsDone() {
LinkedList list;
for (int i = 0; i < mapCapacity; i++) {
map[i].clearMarkedAsDone();
}
writeToFile(File);
}