forked from RPISEC/MBE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab9A.cpp
199 lines (177 loc) · 4.64 KB
/
lab9A.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
/*
* compile: g++ -fstack-protector-all -z relro -z now -O1 -fPIE -pie ./lab9A.cpp -g -o lab9A
* clark's improved item storage server!
* Changelog:
* * Using HashSets for insta-access!
*
*/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include "utils.h"
ENABLE_TIMEOUT(300)
#define SIZE_HASHSET_VEC 8
void
print_menu(void)
{
printf("+----------- clark's improved item storage -----------+\n"
"| [ -- Now using HashSets for insta-access to items! |\n"
"| 1. Open a lockbox |\n"
"| 2. Add an item to a lockbox |\n"
"| 3. Get an item from a lockbox |\n"
"| 4. Destroy your lockbox and items in it |\n"
"| 5. Exit |\n"
"+-----------------------------------------------------+\n");
}
/********* Storage implementation **********/
// hash functor
class hash_num {
public:
// I'm no mathematician
unsigned int
operator() (unsigned int const &key) const {
return key;
}
};
// Hashset
template<class T, class HashFunc>
class HashSet {
public:
HashSet(unsigned int size) : m_size(size), set_data(new T[size]) {}
virtual ~HashSet() { delete [] set_data; }
virtual void add(T val);
virtual unsigned int find(T val);
virtual T get(unsigned int);
private:
unsigned int m_size;
HashFunc m_hash;
T *set_data;
};
typedef HashSet<int, hash_num> hashset_int;
template<class T, class HashFunc>
void
HashSet<T, HashFunc>::add(T val)
{
int index = this->m_hash(val) % this->m_size;
this->set_data[index] = val;
}
template<class T, class HashFunc>
unsigned int
HashSet<T, HashFunc>::find(T val)
{
return this->m_hash(val) % this->m_size;
}
template<class T, class HashFunc>
T
HashSet<T, HashFunc>::get(unsigned int index)
{
if (index >= m_size) {
std::cout << "Invalid index" << std::endl;
return T();
}
return set_data[index];
}
/********* Storage interface implementation **********/
void
do_new_set(hashset_int **set_vec)
{
int bin = -1;
int choice = -1;
std::cout << "Which lockbox do you want?: ";
bin = get_unum();
if (bin < 0) {
std::cout << "Invalid set ID!" << std::endl;
return;
} else if (bin >= SIZE_HASHSET_VEC) {
std::cout << "No more room!" << std::endl;
return;
}
std::cout << "How many items will you store?: ";
choice = get_unum();
set_vec[bin] = new hashset_int(choice);
}
void
do_add_item(hashset_int **set_vec)
{
int set_id = -1;
int item_val = -1;
std::cout << "Which lockbox?: ";
set_id = get_unum();
if (set_id < 0 || set_id >= SIZE_HASHSET_VEC) {
std::cout << "Invalid set ID!" << std::endl;
return;
}
std::cout << "Item value: ";
item_val = get_unum();
set_vec[set_id]->add(item_val);
}
void
do_find_item(hashset_int **set_vec)
{
int set_id = -1;
int item_val = -1;
int item_index = -1;
std::cout << "Which lockbox?: ";
set_id = get_unum();
if (set_id < 0 || set_id >= SIZE_HASHSET_VEC) {
std::cout << "Invalid set ID!" << std::endl;
return;
}
std::cout << "Item value: ";
item_val = get_unum();
item_index = set_vec[set_id]->find(item_val);
if (item_index == -1) {
std::cout << "Item not found!" << std::endl;
} else {
std::cout << "Item Found" << std::endl;
printf("lockbox[%d] = %d\n", item_index, set_vec[set_id]->get(item_index));
}
}
void
do_del_set(hashset_int **set_vec)
{
int del_id;
std::cout << "Which set?: ";
del_id = get_unum();
if (del_id < 0 || del_id >= SIZE_HASHSET_VEC) {
std::cout << "Invalid ID!" << std::endl;
return;
}
delete set_vec[del_id];
}
int
main(int argc, char *argv[])
{
hashset_int **set_vec = new hashset_int*[SIZE_HASHSET_VEC];
int choice = -1;
bool done = false;
disable_buffering(stdout);
while (!done) {
print_menu();
std::cout << "Enter choice: ";
choice = get_unum();
switch (choice) {
case 1:
do_new_set(set_vec);
break;
case 2:
do_add_item(set_vec);
break;
case 3:
do_find_item(set_vec);
break;
case 4:
do_del_set(set_vec);
break;
case 5:
done = true;
break;
default:
puts("Invalid option!");
break;
}
}
delete [] set_vec;
return EXIT_SUCCESS;
}