-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
78 lines (63 loc) · 2.29 KB
/
Copy pathmain.cpp
File metadata and controls
78 lines (63 loc) · 2.29 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
#include "hashmap.h"
using namespace std;
#include "test_settings.h"
#include <map>
void test();
int student_main() {
cout << "This is student main. You can try using HashMap as a client" << endl;
cout << "and call map.debug() to see the internal state of the hash table." << endl;
cout << "This is a good way to debug if you like a good visual of the linked lists." << endl;
cout << endl;
cout << "To turn on the test harness, go to test_settings.cpp and change RUN_TEST_HARNESS to 1." << endl;
cout << endl << endl;
test();
return 0;
}
template <typename... Ts>
void ensure_at_return_is_const(Ts...) {
cout << "True";
};
template <typename T>
auto ensure_at_return_is_const(const T& map) -> decltype((void) (map.at(0) = 3), void()) {
// ASSERT_TRUE(false);
cout << "false";
throw domain_error("Error.");
// If you run into an error here, this means your return value of const
// at is a non-const reference. Why is that wrong?
// Details about this error (it's a hacky sfinae technique):
// we tried running the following code segment (in the decltype line)
// const T& map;
// map.at(0) = 3;
// if the segment compiles, then this function is run (which fails)
// if the segment does not compile, the variadic template before runs (which passes)
}
void test() {
HashMap<string, int> map(5);
cout << "Hello from your past and current lecturers!" << endl;
auto [anna_iter, anna_found] = map.insert({"Anna", 2});
map.insert({"Avery", 3});
map.insert({"Nikhil", 4});
map.insert({"Ethan", 5});
map.debug();
map.rehash(2);
map.debug();
if (anna_found) map.erase(anna_iter);
auto ethan_iter = map.find("Ethan");
ethan_iter->second = 100;
map.debug();
// auto iter = ethan_iter;
// ++iter;
// iter->second = 200;
// map.debug();
HashMap<int, int> map3;
// std::map<int, int> map3;
map3.insert({0, 0});
map3.insert({1, 1});
ensure_at_return_is_const(map3); // confirm return value is a const reference
}
int main() {
std::cout << "RUNNING STUDENT MAIN" << std::endl;
student_main();
std::cout << "SUCCESSFULLY COMPLETED STUDENT MAIN" << std::endl;
return 0;
}