Branch data Line data Source code
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 : : #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 : 13500 : BiMap (std::unordered_map<K, V> &&original) : map (std::move (original))
30 : : {
31 : 648000 : for (auto &kv : map)
32 : 1269000 : rmap.insert ({kv.second, kv.first});
33 : 13500 : }
34 : :
35 : 7629 : const tl::optional<const V &> lookup (const K &key) const
36 : : {
37 : 7629 : auto itr = map.find (key);
38 : 7629 : if (itr == map.end ())
39 : 30 : return tl::nullopt;
40 : :
41 : 7599 : return itr->second;
42 : : }
43 : 79770 : const tl::optional<const K &> lookup (const V &key) const
44 : : {
45 : 79770 : auto itr = rmap.find (key);
46 : 79770 : if (itr == rmap.end ())
47 : 0 : return tl::nullopt;
48 : :
49 : 79770 : 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
|