Debian Codegraph @

gcc/rust/util/bi-map.h

raw
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
2
3 // This file is part of GCC.
4
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
8 // version.
9
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 // for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
18
19 #include "rust-system.h"
20
21 #ifndef BIMAP_H
22 #define BIMAP_H
23
24 // very simple bi-directional hashmap
25 template <typename K, typename V> class BiMap
26 {
27 public:
28 BiMap (std::unordered_map<K, V> &&original) : map (std::move (original))
29 {
30 for (auto &kv : map)
31 rmap.insert ({kv.second, kv.first});
32 }
33
34 const tl::optional<const V &> lookup (const K &key) const
35 {
36 auto itr = map.find (key);
37 if (itr == map.end ())
38 return tl::nullopt;
39
40 return itr->second;
41 }
42 const tl::optional<const K &> lookup (const V &key) const
43 {
44 auto itr = rmap.find (key);
45 if (itr == rmap.end ())
46 return tl::nullopt;
47
48 return itr->second;
49 }
50
51 private:
52 std::unordered_map<K, V> map;
53 std::unordered_map<V, K> rmap;
54 };
55
56 #endif // !BIMAP_H