Sun, 07 May 2017 13:29:58 +0300
Worked more on the library collection thing
988 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
1072 | 3 | * Copyright (C) 2013 - 2017 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 | ||
32 | void insert (const Key& key, const Value& value) | |
33 | { | |
34 | m_map[key] = value; | |
35 | m_reverseMap[value] = key; | |
36 | } | |
37 | ||
38 | bool containsKey (const Key& key) const | |
39 | { | |
40 | return m_map.contains (key); | |
41 | } | |
42 | ||
43 | bool containsValue (const Value& value) const | |
44 | { | |
45 | return m_reverseMap.contains (value); | |
46 | } | |
47 | ||
48 | void removeKey (const Key& key) | |
49 | { | |
50 | m_reverseMap.remove (m_map[key]); | |
51 | m_map.remove (key); | |
52 | } | |
53 | ||
54 | void removeValue (const Key& key) | |
55 | { | |
56 | m_reverseMap.remove (m_map[key]); | |
57 | m_map.remove (key); | |
58 | } | |
59 | ||
60 | Value& lookup (const Key& key) | |
61 | { | |
62 | return m_map[key]; | |
63 | } | |
64 | ||
65 | const Value& lookup (const Key& key) const | |
66 | { | |
67 | return m_map[key]; | |
68 | } | |
69 | ||
70 | Key& reverseLookup (const Value& key) | |
71 | { | |
72 | return m_reverseMap[key]; | |
73 | } | |
74 | ||
75 | const Key& reverseLookup (const Value& key) const | |
76 | { | |
77 | return m_reverseMap[key]; | |
78 | } | |
79 | ||
80 | Value* find (const Key& key) | |
81 | { | |
82 | auto iterator = m_map.find (key); | |
83 | return iterator == m_map.end() ? NULL : &(*iterator); | |
84 | } | |
85 | ||
86 | Key* reverseFind (const Value& value) | |
87 | { | |
88 | auto iterator = m_reverseMap.find (value); | |
89 | return iterator == m_reverseMap.end() ? NULL : &(*iterator); | |
90 | } | |
91 | ||
92 | Value& operator[] (const Key& key) | |
93 | { | |
94 | return lookup (key); | |
95 | } | |
96 | ||
97 | const Value& operator[] (const Key& key) const | |
98 | { | |
99 | return lookup (key); | |
100 | } | |
101 | ||
102 | private: | |
103 | QMap<Key, Value> m_map; | |
104 | QMap<Value, Key> m_reverseMap; | |
105 | }; |