-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimal.cpp
122 lines (94 loc) · 3.26 KB
/
minimal.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
#include "Map.hpp"
#include <cassert>
// basically an int wrapper
class MyKeyType {
private:
int val;
public:
//not default constructable, not copy assignable, not move assignable
MyKeyType() = delete;
MyKeyType& operator=(const MyKeyType&) = delete;
MyKeyType& operator=(MyKeyType&&) = delete;
// copy constructable and move assignable
MyKeyType(MyKeyType&&) = default;
MyKeyType(const MyKeyType&) = default;
~MyKeyType() = default;
MyKeyType(int i) : val(i) { }
bool operator<(const MyKeyType& other) const {
return this->val < other.val;
}
bool operator==(const MyKeyType &other) const {
return this->val == other.val;
}
};
// same as keytype except no operator<
class MyValueType {
private:
int val;
public:
//not default constructable, not copy assignable, not move assignable
MyValueType() = delete;
MyValueType& operator=(const MyValueType&) = delete;
MyValueType& operator=(MyValueType&&) = delete;
// copy constructable and move assignable
MyValueType(MyValueType&&) = default;
MyValueType(const MyValueType&) = default;
~MyValueType() = default;
MyValueType(int i) : val(i) { }
bool operator==(const MyValueType &other) const {
return this->val == other.val;
}
};
class MyDefaultConstructible {
friend bool operator<(const MyDefaultConstructible &o1, const MyDefaultConstructible &o2) {
return o1.val < o2.val;
}
private:
int val = 0;
public:
// not copy assignable, not move assignable
MyDefaultConstructible& operator=(const MyDefaultConstructible&) = delete;
MyDefaultConstructible& operator=(MyDefaultConstructible&&) = delete;
// default constructable, copy constructable and move assignable
MyDefaultConstructible() = default;
MyDefaultConstructible(MyDefaultConstructible&&) = default;
MyDefaultConstructible(const MyDefaultConstructible&) = default;
~MyDefaultConstructible() = default;
MyDefaultConstructible(int i) : val(i) { }
bool operator==(const MyDefaultConstructible &other) const {
return this->val == other.val;
}
};
class MyAssignable {
private:
int val = 0;
public:
MyAssignable() = default;
MyAssignable(int i) : val(i) { }
bool operator==(const MyAssignable &other) const {
return this->val == other.val;
}
};
// manual instantiation, instantiates every member function instead of
// just the ones called
template class cs440::Map<MyKeyType, MyDefaultConstructible>;
int main() {
cs440::Map<MyKeyType, MyValueType> m{{3, 5}};
m.insert({{2}, {3}});
m.insert({{1}, {3}});
m.insert({{5}, {3}});
m.insert({{7}, {3}});
m.at(2);
auto iter = m.find(2);
m.erase(iter);
auto m_copy = m;
assert(m_copy == m);
cs440::Map<MyKeyType, MyDefaultConstructible> m2{{8, 9}};
m2[10]; // should default construct these values
m2[15];
cs440::Map<MyKeyType, MyAssignable> m3{{6, 7}};
m3[20] = {5}; // move assign
MyAssignable ma{1};
m3[10] = ma; //copy assign
return 0;
}