BIMHome v1.0.0
BIMHome接口文档说明
LinkedHashMap.h
浏览该文件的文档.
1/************************************************************************
2* @file LinkedHashMap.h
3*
4* @brief 链式哈希映射表
5*
6* @details 链式哈希映射表
7*
8* @author wukx
9*
10* @version v1.0
11*
12* @date 2016.9.29
13*
14* @license 北京华科软科技有限公司
15*
16*************************************************************************/
17
18#ifndef BIMHOMEBASE_LINKEDHASHMAP_H
19#define BIMHOMEBASE_LINKEDHASHMAP_H
20
21#include <algorithm>
22#include <list>
23
24
25namespace Base
26{
36 template <class KeyType, class MappedType,
37 class Hash = std::hash<KeyType>,
38 class Pred = std::equal_to<KeyType> >
40 {
41 public:
42
43 typedef std::pair<const KeyType, MappedType> EntryType;
44 typedef typename std::list<EntryType>::iterator iterator;
45 typedef typename std::list<EntryType>::const_iterator const_iterator;
47 public:
48
55 std::pair<iterator, bool> insert(const EntryType& new_entry)
56 {
57 _map_itr pIt = _map_key2entry.find(new_entry.first);
58 if (pIt == _map_key2entry.end())
59 {
60 _values.push_back(new_entry);
61 _map_key2entry.insert(std::pair<KeyType, iterator>(new_entry.first, --_values.end()));
62 ++_size;
63 }
64 else
65 {
66 _values.erase(pIt->second);
67 _values.push_back(new_entry);
68 pIt->second = --_values.end();
69 }
70 return std::pair<iterator, bool>(--_values.end(), true);
71 }
72
81 {
82 _map_itr pIt = _map_key2entry.find(new_entry.first);
83 iterator _it;
84 if (pIt != _map_key2entry.end()) {
85 _values.erase(pIt->second);
86 --_size;
87 _it = _values.insert(it, new_entry);
88 _map_key2entry[new_entry.first] = _it;
89 }
90 else {
91 _it = _values.insert(it, new_entry);
92 _map_key2entry.insert(std::pair<KeyType, iterator>(new_entry.first, _it));
93 }
94
95 ++_size;
96 return _it;
97 }
98
104 inline iterator begin() { return _values.begin(); }
105
111 inline const_iterator begin() const { return _values.begin(); }
112
118 inline iterator end() { return _values.end(); }
119
125 inline const_iterator end() const { return _values.end(); }
126
133 iterator find(const KeyType& key)
134 {
135 if (_size > 0)
136 {
137 _map_itr it = _map_key2entry.find(key);
138 if (it == _map_key2entry.end())
139 {
140 return _values.end();
141 }
142 iterator find_it = it->second;
143 return find_it;
144 }
145 return _values.end();
146 }
147
154 const_iterator find(const KeyType& key) const
155 {
156 if (_size > 0)
157 {
158 _map_citr it = _map_key2entry.find(key);
159 if (it == _map_key2entry.end())
160 {
161 return _values.end();
162 }
163 const_iterator find_it = it->second;
164 return find_it;
165 }
166 return _values.end();
167 }
168
174 void erase(iterator pos)
175 {
176 assert(pos != _values.end());
177 assert(_size != 0);
178
179 EntryType v = *pos;
180 _values.erase(pos);
181 _map_key2entry.erase(v.first);
182 --_size;
183 }
184
190 void erase(const KeyType& key)
191 {
192 iterator it = find(key);
193 if (it != _values.end())
194 {
195 erase(it);
196 }
197
198 }
199
204 void clear()
205 {
206 _values.clear();
207 _map_key2entry.clear();
208 _size = 0;
209 }
210
216 inline size_t size() const { return _size; }
217
223 inline bool empty() const { return _values.empty(); }
224
231 MappedType& operator[](const KeyType& key)
232 {
233 return find(key)->second;
234 }
241 bool operator==(const LinkedHashMap& linkedHashMap)
242 {
243 return _values == linkedHashMap._values;
244 }
245
246 private:
247 typedef std::unordered_map<KeyType, iterator, Hash, Pred> HashMap;
248 typedef typename HashMap::iterator _map_itr;
249 typedef typename HashMap::const_iterator _map_citr;
251 size_t _size;
252 std::list<EntryType> _values;
254 };
255} // namespace Base
256
257#endif // BIMHOMEBASE_LINKEDHASHMAP_H
258
HashMap _map_key2entry
保存 key 到 entry 的 hashmap
Definition LinkedHashMap.h:253
iterator find(const KeyType &key)
在 map 中查找元素,如存在则返回其迭代器,否则返回 end()
Definition LinkedHashMap.h:133
std::list< EntryType > _values
实际存放 entry 的双向链表
Definition LinkedHashMap.h:252
const_iterator find(const KeyType &key) const
find() 的 const 版本
Definition LinkedHashMap.h:154
std::pair< const KeyType, MappedType > EntryType
保存的 entry 的类型
Definition LinkedHashMap.h:43
size_t size() const
返回 entries 的数量
Definition LinkedHashMap.h:216
const_iterator begin() const
获取首元素的 const 迭代器
Definition LinkedHashMap.h:111
size_t _size
entry 的数量/O
Definition LinkedHashMap.h:251
bool empty() const
获取该 map 是否为空
Definition LinkedHashMap.h:223
std::list< EntryType >::iterator iterator
list 中的非 const 迭代器类型
Definition LinkedHashMap.h:44
std::list< EntryType >::const_iterator const_iterator
list 中的 const 迭代器类型
Definition LinkedHashMap.h:45
HashMap::iterator _map_itr
哈希表迭代器类型
Definition LinkedHashMap.h:248
HashMap::const_iterator _map_citr
哈希表const迭代器类型
Definition LinkedHashMap.h:249
iterator insert(const_iterator it, const EntryType &new_entry)
在 map 中指定位置插入一个新 entry
Definition LinkedHashMap.h:80
void erase(const KeyType &key)
移除指定键值的 entry
Definition LinkedHashMap.h:190
void clear()
清空所有 entry
Definition LinkedHashMap.h:204
iterator end()
获取尾元素的迭代器
Definition LinkedHashMap.h:118
const_iterator end() const
获取尾元素的 const 迭代器
Definition LinkedHashMap.h:125
void erase(iterator pos)
移除位于 pos 处的 entry
Definition LinkedHashMap.h:174
std::unordered_map< KeyType, iterator, Hash, Pred > HashMap
用于存储键到迭代器映射的哈希表
Definition LinkedHashMap.h:247
std::pair< iterator, bool > insert(const EntryType &new_entry)
在 map 中插入一个新 entry
Definition LinkedHashMap.h:55
iterator begin()
获取首元素的迭代器
Definition LinkedHashMap.h:104
MappedType & operator[](const KeyType &key)
[] 运算符重载
Definition LinkedHashMap.h:231
bool operator==(const LinkedHashMap &linkedHashMap)
== 运算符重载
Definition LinkedHashMap.h:241
链式哈希映射表
Definition LinkedHashMap.h:40
Definition BaseFigureFactory.h:24