-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoWayMap.h
169 lines (144 loc) · 3.42 KB
/
TwoWayMap.h
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
#ifndef TWO_WAY_MAP_H
#define TWO_WAY_MAP_H
#include <map>
#include <vector>
#include <algorithm>
#include "Relation.h"
template <typename T1, typename T2>
class TwoWayMap {
private:
std::set<std::pair<T1,T2>> schema;
std::vector<std::pair<T1,T2>> vectAll;
std::vector<T1> vectT1;
std::vector<T2> vectT2;
struct sortSecond {
bool operator ()(std::pair<T1,T2> a, std::pair<T1,T2> b) const {
return a.second < b.second;
}
};
public:
friend class Relation;
TwoWayMap () {
}
TwoWayMap (const TwoWayMap& other) {
schema = other.schema;
vectT1 = other.vectT1;
vectT2 = other.vectT2;
}
TwoWayMap& operator=(TwoWayMap other) {
schema = other.schema;
vectT1 = other.vectT1;
vectT2 = other.vectT2;
return *(this);
}
virtual ~TwoWayMap () {
}
void insert(T1 first,T2 second) {
std::pair<T1,T2> input = {first,second};
schema.insert(input);
}
void insert(T2 second, T1 first) {
std::pair<T1,T2> input = {first,second};
schema.insert(input);
}
void remove(T1 look) {
for (auto it = schema.begin(); it != schema.end(); it++) {
if (it->first == look) {
schema.erase(it);
return;
}
}
throw std::out_of_range ("Search");
}
void remove(T2 look) {
for (auto it = schema.begin(); it != schema.end(); it++) {
if (it->second == look) {
schema.erase(it);
return;
}
}
throw std::out_of_range ("Search");
}
void replace(T1 look, T1 what) {
for (auto it = schema.begin(); it != schema.end(); it++) {
if (it->first == look) {
std::pair<T1,T2> input = {what,it->second};
schema.erase(it);
schema.insert(input);
return;
}
}
throw std::out_of_range ("Search");
}
void replace(T2 look, T2 what) {
for (auto it = schema.begin(); it != schema.end(); it++) {
if (it->second == look) {
std::pair<T1,T2> input = {it->first,what};
schema.erase(it);
schema.insert(input);
return;
}
}
throw std::out_of_range ("Search");
}
T1 at(T2 look) {
for (auto it = schema.begin(); it != schema.end(); it++) {
if (it->second == look) return it->first;
}
throw std::out_of_range ("Search");
}
T2 at(T1 look) {
for (auto it = schema.begin(); it != schema.end(); it++) {
if (it->first == look) return it->second;
}
throw std::out_of_range ("Search");
}
std::vector<std::pair<T1,T2>> toVector() {
if (vectAll.size() != schema.size()) {
vectAll.clear();
for (auto item : schema) {
vectAll.push_back(item);
}
}
return vectAll;
}
std::vector<T1> getFirst() {
if (vectT1.size() != schema.size()) {
vectT1.clear();
for (auto item : schema) {
vectT1.push_back(item.first);
}
}
return vectT1;
}
std::vector<T2> getSecond() {
if (vectT2.size() != schema.size()) {
vectT2.clear();
for (auto item : schema) {
vectT2.push_back(item.second);
}
}
return vectT2;
}
bool contains(T1 look) {
for (auto item : schema) {
if (item.first == look) return true;
}
return false;
}
bool contains(T2 look) {
for (auto item : schema) {
if (item.second == look) return true;
}
return false;
}
void clear() {
schema.clear();
vectT1.clear();
vectT2.clear();
}
unsigned int size() {
return schema.size();
}
};
#endif