Line data Source code
1 : // Copyright (C) 2020-2026 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 : #include "optional.h"
21 :
22 : #ifndef BIMAP_H
23 : #define BIMAP_H
24 :
25 : // very simple bi-directional hashmap
26 : template <typename K, typename V> class BiMap
27 : {
28 : public:
29 13908 : BiMap (std::unordered_map<K, V> &&original) : map (std::move (original))
30 : {
31 667584 : for (auto &kv : map)
32 1307352 : rmap.insert ({kv.second, kv.first});
33 13908 : }
34 :
35 6857 : const tl::optional<const V &> lookup (const K &key) const
36 : {
37 6857 : auto itr = map.find (key);
38 6857 : if (itr == map.end ())
39 3 : return tl::nullopt;
40 :
41 6854 : return itr->second;
42 : }
43 79911 : const tl::optional<const K &> lookup (const V &key) const
44 : {
45 79911 : auto itr = rmap.find (key);
46 79911 : if (itr == rmap.end ())
47 0 : return tl::nullopt;
48 :
49 79911 : return itr->second;
50 : }
51 :
52 : private:
53 : std::unordered_map<K, V> map;
54 : std::unordered_map<V, K> rmap;
55 : };
56 :
57 : #endif // !BIMAP_H
|