DAWorkbench 0.0.1
DAWorkbench API
载入中...
搜索中...
未找到
da_order_small_map.hpp
1#ifndef DA_ORDER_SMALL_MAP_H
2#define DA_ORDER_SMALL_MAP_H
3#include <qglobal.h>
4#include <vector>
5#include <memory>
6#ifdef QT_VERSION // 说明是qt应用
7#include <QList>
8#include <QMap>
9#endif
10
16#ifdef QT_VERSION // 说明是qt应用
17template< typename KEY, typename VALUE, typename CONTAINTER_KEY = QList< KEY >, typename CONTAINTER_VALUE = QList< VALUE > >
18#else
19template< typename KEY, typename VALUE, typename CONTAINTER_KEY = std::vector< KEY >, typename CONTAINTER_VALUE = std::vector< VALUE > >
20#endif
22{
23public:
26 typedef KEY key_type;
27 typedef VALUE value_type;
29
30 // 等同QMap::clear
31 void clear();
32
33 // 等同QMap::contains
34 bool contains(const KEY& key) const;
35
36 // 等同QMap::count
37 int count(const KEY& key) const;
38
39 // 等同QMap::count
40 int count() const;
41
42 // 等同QMap::empty
43 bool empty() const;
44
45 // 等同QMap::first
46 VALUE& first();
47
48 // 等同QMap::first
49 const VALUE& first() const;
50
51 // 等同QMap::firstKey
52 const KEY& firstKey() const;
53
54 // 等同QMap::isEmpty
55 bool isEmpty() const;
56
57 // 等同QMap::size
58 int size() const;
59
60 // 等同QMap::operator[]
61 VALUE& operator[](const KEY& key);
62
63 class const_iterator;
64
69 {
70 friend class const_iterator;
71 friend class da_order_small_map< KEY, VALUE, CONTAINTER_KEY, CONTAINTER_VALUE >;
72 typename CONTAINTER_KEY::iterator mIteKey;
73 typename CONTAINTER_VALUE::iterator mIteValue;
74
75 public:
76 typedef std::bidirectional_iterator_tag iterator_category;
77 typedef qptrdiff difference_type;
78 typedef VALUE value_type;
79 typedef VALUE* pointer;
80 typedef VALUE& reference;
81 inline iterator()
82 {
83 }
84
85 inline iterator(typename CONTAINTER_KEY::iterator ki, typename CONTAINTER_VALUE::iterator vi)
86 : mIteKey(ki), mIteValue(vi)
87 {
88 }
89
90 inline const KEY& key() const
91 {
92 return (*mIteKey);
93 }
94
95 inline VALUE& value() const
96 {
97 return (*mIteValue);
98 }
99
100 inline VALUE& operator*() const
101 {
102 return (*mIteValue);
103 }
104
105 inline VALUE* operator->() const
106 {
107 return (&(*mIteValue));
108 }
109
110 inline bool operator==(const iterator& o) const
111 {
112 return ((mIteKey == o.mIteKey) && (mIteValue == o.mIteValue));
113 }
114
115 inline bool operator!=(const iterator& o) const
116 {
117 return ((mIteKey != o.mIteKey) || (mIteValue != o.mIteValue));
118 }
119
125 {
126 mIteKey = (++mIteKey);
127 mIteValue = (++mIteValue);
128 return (*this);
129 }
130
136 {
137 iterator r = *this;
138
139 mIteKey = (++mIteKey);
140 mIteValue = (++mIteValue);
141 return (r);
142 }
143
144 inline iterator& operator--()
145 {
146 mIteKey = (--mIteKey);
147 mIteValue = (--mIteValue);
148 return (*this);
149 }
150
151 inline iterator operator--(int)
152 {
153 iterator r = *this;
154
155 mIteKey = (--mIteKey);
156 mIteValue = (--mIteValue);
157 return (r);
158 }
159
160 inline iterator operator+(int j) const
161 {
162 iterator r = *this;
163
164 if (j > 0) {
165 while (j--) {
166 ++r;
167 }
168 } else {
169 while (j++) {
170 --r;
171 }
172 }
173 return (r);
174 }
175
176 inline iterator operator-(int j) const
177 {
178 return (operator+(-j));
179 }
180
181 inline iterator& operator+=(int j)
182 {
183 return (*this = *this + j);
184 }
185
186 inline iterator& operator-=(int j)
187 {
188 return (*this = *this - j);
189 }
190
191 inline bool operator==(const const_iterator& o) const
192 {
193 return ((mIteKey == o.mIteKey) && (mIteValue == o.mIteValue));
194 }
195
196 inline bool operator!=(const const_iterator& o) const
197 {
198 return ((mIteKey != o.mIteKey) || (mIteValue != o.mIteValue));
199 }
200 };
201 friend class iterator;
203 {
204 friend class iterator;
205 friend class da_order_small_map< KEY, VALUE, CONTAINTER_KEY, CONTAINTER_VALUE >;
206 typename CONTAINTER_KEY::const_iterator mIteKey;
207 typename CONTAINTER_VALUE::const_iterator mIteValue;
208
209 public:
210 typedef std::bidirectional_iterator_tag iterator_category;
211 typedef qptrdiff difference_type;
212 typedef VALUE value_type;
213 typedef const VALUE* pointer;
214 typedef const VALUE& reference;
215
216 inline const_iterator()
217 {
218 }
219
220 inline const_iterator(typename CONTAINTER_KEY::const_iterator ki, typename CONTAINTER_VALUE::const_iterator vi)
221 : mIteKey(ki), mIteValue(vi)
222 {
223 }
224
225 inline const_iterator(const iterator& o)
226 {
227 mIteKey = o.mIteKey;
228 mIteValue = o.mIteValue;
229 }
230
231 inline const KEY& key() const
232 {
233 return (*mIteKey);
234 }
235
236 inline const VALUE& value() const
237 {
238 return (*mIteValue);
239 }
240
241 inline const VALUE& operator*() const
242 {
243 return (*mIteValue);
244 }
245
246 inline const VALUE* operator->() const
247 {
248 return (&(*mIteValue));
249 }
250
251 inline bool operator==(const const_iterator& o) const
252 {
253 return ((mIteKey == o.mIteKey) && (mIteValue == o.mIteValue));
254 }
255
256 inline bool operator!=(const const_iterator& o) const
257 {
258 return ((mIteKey != o.mIteKey) || (mIteValue != o.mIteValue));
259 }
260
266 {
267 mIteKey = (++mIteKey);
268 mIteValue = (++mIteValue);
269 return (*this);
270 }
271
277 {
278 const_iterator r = *this;
279
280 mIteKey = (++mIteKey);
281 mIteValue = (++mIteValue);
282 return (r);
283 }
284
285 inline const_iterator& operator--()
286 {
287 mIteKey = (--mIteKey);
288 mIteValue = (--mIteValue);
289 return (*this);
290 }
291
292 inline const_iterator operator--(int)
293 {
294 const_iterator r = *this;
295
296 mIteKey = (--mIteKey);
297 mIteValue = (--mIteValue);
298 return (r);
299 }
300
301 inline const_iterator operator+(int j) const
302 {
303 const_iterator r = *this;
304
305 if (j > 0) {
306 while (j--) {
307 ++r;
308 }
309 } else {
310 while (j++) {
311 --r;
312 }
313 }
314 return (r);
315 }
316
317 inline const_iterator operator-(int j) const
318 {
319 return (operator+(-j));
320 }
321
322 inline const_iterator& operator+=(int j)
323 {
324 return (*this = *this + j);
325 }
326
327 inline const_iterator& operator-=(int j)
328 {
329 return (*this = *this - j);
330 }
331 };
332
333 // STL style
334 inline iterator begin()
335 {
336 return (iterator(mKeys.begin(), mValues.begin()));
337 }
338 inline const_iterator begin() const
339 {
340 return (const_iterator(mKeys.begin(), mValues.begin()));
341 }
342 inline const_iterator constBegin() const
343 {
344 return (const_iterator(mKeys.begin(), mValues.begin()));
345 }
346 inline const_iterator cbegin() const
347 {
348 return (const_iterator(mKeys.begin(), mValues.begin()));
349 }
350 inline iterator end()
351 {
352 return (iterator(mKeys.end(), mValues.end()));
353 }
354 inline const_iterator end() const
355 {
356 return (const_iterator(mKeys.end(), mValues.end()));
357 }
358 inline const_iterator constEnd() const
359 {
360 return (const_iterator(mKeys.end(), mValues.end()));
361 }
362 inline const_iterator cend() const
363 {
364 return (const_iterator(mKeys.end(), mValues.end()));
365 }
366
367 iterator erase(iterator it);
368
369 iterator find(const KEY& key);
370 const_iterator find(const KEY& key) const;
371
372 inline VALUE& orderValue(int order)
373 {
374 return (mValues[ order ]);
375 }
376 inline const VALUE& orderValue(int order) const
377 {
378 return (mValues[ order ]);
379 }
380 inline KEY& orderKey(int order)
381 {
382 return (mKeys[ order ]);
383 }
384 inline const KEY& orderKey(int order) const
385 {
386 return (mKeys[ order ]);
387 }
388 // 遍历k,v
389 const std::pair< KEY, VALUE > orderPair(int order) const
390 {
391 return (std::make_pair< KEY, VALUE >(orderKey(order), orderValue(order)));
392 }
393
394 const VALUE value(const KEY& key, const VALUE& defaultValue = VALUE()) const;
395#ifdef QT_VERSION
396 // 转换为qmap
397 QMap< KEY, VALUE > toMap() const;
398#endif
399private:
400 CONTAINTER_KEY mKeys;
401 CONTAINTER_VALUE mValues;
402};
403
404template< typename KEY, typename VALUE, typename CONTAINTER_KEY, typename CONTAINTER_VALUE >
406{
407}
408
409template< typename KEY, typename VALUE, typename CONTAINTER_KEY, typename CONTAINTER_VALUE >
411{
412}
413
414template< typename KEY, typename VALUE, typename CONTAINTER_KEY, typename CONTAINTER_VALUE >
416{
417 mKeys.clear();
418 mValues.clear();
419}
420
421template< typename KEY, typename VALUE, typename CONTAINTER_KEY, typename CONTAINTER_VALUE >
423{
424 return (mKeys.contains(key));
425}
426
427template< typename KEY, typename VALUE, typename CONTAINTER_KEY, typename CONTAINTER_VALUE >
429{
430 return (mKeys.count(key));
431}
432
433template< typename KEY, typename VALUE, typename CONTAINTER_KEY, typename CONTAINTER_VALUE >
435{
436 return (size());
437}
438
439template< typename KEY, typename VALUE, typename CONTAINTER_KEY, typename CONTAINTER_VALUE >
441{
442 return (mKeys.empty());
443}
444
445template< typename KEY, typename VALUE, typename CONTAINTER_KEY, typename CONTAINTER_VALUE >
447{
448 return (mValues.first());
449}
450
451template< typename KEY, typename VALUE, typename CONTAINTER_KEY, typename CONTAINTER_VALUE >
453{
454 return (mValues.first());
455}
456
457template< typename KEY, typename VALUE, typename CONTAINTER_KEY, typename CONTAINTER_VALUE >
459{
460 return (mKeys.firstKey());
461}
462
463template< typename KEY, typename VALUE, typename CONTAINTER_KEY, typename CONTAINTER_VALUE >
465{
466 return (mKeys.isEmpty());
467}
468
469template< typename KEY, typename VALUE, typename CONTAINTER_KEY, typename CONTAINTER_VALUE >
471{
472 return (mKeys.size());
473}
474
475template< typename KEY, typename VALUE, typename CONTAINTER_KEY, typename CONTAINTER_VALUE >
477{
478 int index = mKeys.indexOf(key);
479
480 if (index < 0) {
481 mKeys.append(key);
482 mValues.append(VALUE());
483 return (mValues.last());
484 }
485 return (mValues[ index ]);
486}
487
488template< typename KEY, typename VALUE, typename CONTAINTER_KEY, typename CONTAINTER_VALUE >
491{
492 typename CONTAINTER_KEY::iterator ki = mKeys.erase(it.mIteKey);
493 typename CONTAINTER_VALUE::iterator vi = mValues.erase(it.mIteValue);
494 return (iterator(ki, vi));
495}
496
497template< typename KEY, typename VALUE, typename CONTAINTER_KEY, typename CONTAINTER_VALUE >
500{
501 iterator i = begin();
502 const_iterator e = end();
503
504 while (i != e) {
505 if (i.key() == key) {
506 return (i);
507 }
508 ++i;
509 }
510 return (e);
511}
512
513template< typename KEY, typename VALUE, typename CONTAINTER_KEY, typename CONTAINTER_VALUE >
516{
517 const_iterator i = begin();
518 const_iterator e = end();
519
520 while (i != e) {
521 if (i.key() == key) {
522 return (i);
523 }
524 ++i;
525 }
526 return (e);
527}
528
529template< typename KEY, typename VALUE, typename CONTAINTER_KEY, typename CONTAINTER_VALUE >
531 const VALUE& defaultValue) const
532{
533 const_iterator i = this->find(key);
534
535 return ((i == this->end()) ? defaultValue : i.value());
536}
537
538#ifdef QT_VERSION
539template< typename KEY, typename VALUE, typename CONTAINTER_KEY, typename CONTAINTER_VALUE >
541{
542 QMap< KEY, VALUE > r;
543 int s = size();
544
545 for (int i = 0; i < s; ++i) {
546 r.insert(orderKey(i), orderValue(i));
547 }
548 return (r);
549}
550#endif
551
552#endif // SASHORTMAP_H
Definition da_order_small_map.hpp:203
const_iterator & operator++()
++i
Definition da_order_small_map.hpp:265
const_iterator operator++(int)
i++
Definition da_order_small_map.hpp:276
迭代器
Definition da_order_small_map.hpp:69
iterator operator++(int)
i++
Definition da_order_small_map.hpp:135
iterator & operator++()
++i
Definition da_order_small_map.hpp:124
一种简化的map,通过两个连续容器来保存key和value,而不是用二叉树,对于少量的map数据有更好的性能
Definition da_order_small_map.hpp:22
da_order_small_map< KEY, VALUE, CONTAINTER_KEY, CONTAINTER_VALUE > _Myt
学stl
Definition da_order_small_map.hpp:28