Wed, 10 Jan 2018 22:54:09 +0200
drop Qt4 support
988 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
1222 | 3 | * Copyright (C) 2013 - 2018 Teemu Piippo |
988 | 4 | * |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | #pragma once | |
20 | #include <QMap> | |
21 | ||
22 | template<typename Key, typename Value> | |
23 | class DoubleMap | |
24 | { | |
25 | public: | |
26 | void clear() | |
27 | { | |
28 | m_map.clear(); | |
29 | m_reverseMap.clear(); | |
30 | } | |
31 | ||
1217 | 32 | void insert(const Key& key, const Value& value) |
988 | 33 | { |
34 | m_map[key] = value; | |
35 | m_reverseMap[value] = key; | |
36 | } | |
37 | ||
1217 | 38 | bool containsKey(const Key& key) const |
988 | 39 | { |
1217 | 40 | return m_map.contains(key); |
988 | 41 | } |
42 | ||
1217 | 43 | bool containsValue(const Value& value) const |
988 | 44 | { |
1217 | 45 | return m_reverseMap.contains(value); |
988 | 46 | } |
47 | ||
1217 | 48 | void removeKey(const Key& key) |
988 | 49 | { |
1217 | 50 | m_reverseMap.remove(m_map[key]); |
51 | m_map.remove(key); | |
988 | 52 | } |
53 | ||
1217 | 54 | void removeValue(const Key& key) |
988 | 55 | { |
1217 | 56 | m_reverseMap.remove(m_map[key]); |
57 | m_map.remove(key); | |
988 | 58 | } |
59 | ||
1217 | 60 | Value& lookup(const Key& key) |
988 | 61 | { |
62 | return m_map[key]; | |
63 | } | |
64 | ||
1217 | 65 | const Value& lookup(const Key& key) const |
988 | 66 | { |
67 | return m_map[key]; | |
68 | } | |
69 | ||
1217 | 70 | Key& reverseLookup(const Value& key) |
988 | 71 | { |
72 | return m_reverseMap[key]; | |
73 | } | |
74 | ||
1217 | 75 | const Key& reverseLookup(const Value& key) const |
988 | 76 | { |
77 | return m_reverseMap[key]; | |
78 | } | |
79 | ||
1217 | 80 | Value* find(const Key& key) |
988 | 81 | { |
1217 | 82 | auto iterator = m_map.find(key); |
988 | 83 | return iterator == m_map.end() ? NULL : &(*iterator); |
84 | } | |
85 | ||
1217 | 86 | Key* reverseFind(const Value& value) |
988 | 87 | { |
1217 | 88 | auto iterator = m_reverseMap.find(value); |
988 | 89 | return iterator == m_reverseMap.end() ? NULL : &(*iterator); |
90 | } | |
91 | ||
1217 | 92 | Value& operator[](const Key& key) |
988 | 93 | { |
1217 | 94 | return lookup(key); |
988 | 95 | } |
96 | ||
1217 | 97 | const Value& operator[](const Key& key) const |
988 | 98 | { |
1217 | 99 | return lookup(key); |
988 | 100 | } |
101 | ||
102 | private: | |
103 | QMap<Key, Value> m_map; | |
104 | QMap<Value, Key> m_reverseMap; | |
105 | }; |