Sese Framework  x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
Bimap.h
浏览该文件的文档.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
19
20#pragma once
21
22#include <cstddef>
23#include <map>
24#include <initializer_list>
25
26namespace sese {
27
31template<class K, class V>
32class Bimap final {
33public:
34 using BimapKeyType = K;
35 using BimapValueType = V;
36
37 Bimap() = default;
38 ~Bimap() = default;
39 Bimap(const Bimap &) = default;
40 Bimap &operator=(const Bimap &) = default;
41 Bimap(Bimap &&) noexcept = default;
42 Bimap &operator=(Bimap &&) noexcept = default;
43
44 Bimap(const std::initializer_list<std::pair<BimapKeyType, BimapValueType>> &init) {
45 for (const auto &[key, value]: init) {
46 insert(key, value);
47 }
48 }
49
50 decltype(auto) begin() const { return first.begin(); }
51
52 decltype(auto) end() const { return first.end(); }
53
54 void insert(const BimapKeyType &key, const BimapValueType &value) {
55 first[key] = value;
56 second[value] = key;
57 }
58
59 void eraseByKey(const BimapKeyType &key) {
60 BimapValueType value = first[key];
61 first.erase(key);
62 second.erase(value);
63 }
64
65 void eraseByValue(const BimapValueType &value) {
66 BimapKeyType key = second[value];
67 first.erase(key);
68 second.erase(value);
69 }
70
71 bool tryEraseByKey(const BimapKeyType &key) {
72 if (!first.contains(key)) return false;
73 eraseByKey(key);
74 return true;
75 }
76
77 bool tryEraseByValue(const BimapValueType &value) {
78 if (!second.contains(value)) return false;
79 eraseByValue(value);
80 return true;
81 }
82
83 bool existByKey(const BimapKeyType &key) const {
84 return first.contains(key);
85 }
86
87 bool existByValue(const BimapValueType &value) const {
88 return second.contains(value);
89 }
90
91 [[nodiscard]] const BimapValueType &getByKey(const BimapKeyType &key) const {
92 return first.at(key);
93 }
94
95 [[nodiscard]] BimapValueType &getByKey(const BimapKeyType &key) {
96 return first.at(key);
97 }
98
99 [[nodiscard]] const BimapKeyType &getByValue(const BimapValueType &value) const {
100 return second.at(value);
101 }
102
103 [[nodiscard]] BimapKeyType &getByValue(const BimapValueType &value) {
104 return second.at(value);
105 }
106
107 [[nodiscard]] bool empty() const {
108 return first.empty();
109 }
110
111 [[nodiscard]] size_t size() const {
112 return first.size();
113 }
114
115 void clear() {
116 first.clear();
117 second.clear();
118 }
119
120private:
121 std::map<BimapKeyType, BimapValueType> first{};
122 std::map<BimapValueType, BimapKeyType> second{};
123};
124} // namespace sese