-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBimap.cpp
More file actions
63 lines (60 loc) · 2.5 KB
/
Bimap.cpp
File metadata and controls
63 lines (60 loc) · 2.5 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
#include <stdexcept>
#include <optional>
#include <map>
#include <iostream>
#include <vector>
#include <memory>
template <typename Key1, typename Key2, typename Value>
class BiMap {
private:
std::map<Key2, std::shared_ptr<Value>> key2_to_value;
std::map<Key1, std::shared_ptr<Value>> key1_to_value;
public:
// Вставить значение, указав один или оба ключа.
// Генерирует исключение std::invalid_argument("some text") в случае,
// если оба ключа пусты, либо один из ключей уже имеется в хранилище.
void Insert(const std::optional<Key1>& key1 , const std::optional<Key2>& key2,
const Value& value) {
if (key1.has_value()) {
if (key2.has_value()) {
if (!(key1_to_value.contains(key1.value())) &&
!(key2_to_value.contains(key2.value()))) {
key1_to_value[key1.value()] = std::make_shared<Value>(value);
key2_to_value[key2.value()] = key1_to_value[key1.value()];
} else {
throw std::invalid_argument("aaa");
}
} else {
if (!(key1_to_value.contains(key1.value()))) {
key1_to_value[key1.value()] = std::make_shared<Value>(value);
} else {
throw std::invalid_argument("aaa");
}
}
} else if (key2.has_value()) {
if (!(key2_to_value.contains(key2.value()))) {
key2_to_value[key2.value()] = std::make_shared<Value>(value);
} else {
throw std::invalid_argument("aaa");
}
} else {
throw std::invalid_argument("aaa");
}
}
// Получить значение по ключу первого типа.
// Генерирует исключение std::out_of_range("some text")
// в случае отсутствия ключа (как и функция at в std::map).
Value& GetByPrimaryKey(const Key1& key) {
return *key1_to_value.at(key);
}
const Value& GetByPrimaryKey(const Key1& key) const {
return *key1_to_value.at(key);
}
// Аналогичная функция для ключа второго типа.
Value& GetBySecondaryKey(const Key2& key) {
return *key2_to_value.at(key);
}
const Value& GetBySecondaryKey(const Key2& key) const {
return *key2_to_value.at(key);
}
};