ZFFramework
Loading...
Searching...
No Matches
ZFCoreOrderMap.h
Go to the documentation of this file.
1
5
6#ifndef _ZFI_ZFCoreOrderMap_h_
7#define _ZFI_ZFCoreOrderMap_h_
8
9#include "ZFCoreMap.h"
10
12
13zfclassNotPOD ZFLIB_ZFCore _ZFP_ZFCoreOrderMap {
14public:
16 public:
17 virtual ~BaseKey(void) {}
18 public:
19 virtual zfidentity implHash(void) const zfpurevirtual;
20 virtual zfbool implEqual(ZF_IN const BaseKey *ref) const zfpurevirtual;
21 virtual void implInfo(ZF_IN_OUT zfstring &ret) const zfpurevirtual;
22 virtual BaseKey *implCopy(void) const zfpurevirtual;
23 virtual void implDestroy(void) zfpurevirtual;
24 };
25 zfclassNotPOD ZFLIB_ZFCore BaseValue {
26 public:
27 virtual ~BaseValue(void) {}
28 public:
29 virtual void implCopy(ZF_IN const BaseValue *ref) zfpurevirtual;
30 virtual zfbool implEqual(ZF_IN const BaseValue *ref) const zfpurevirtual;
31 virtual void implInfo(ZF_IN_OUT zfstring &ret) const zfpurevirtual;
32 virtual BaseValue *implCopy(void) const zfpurevirtual;
33 virtual void implDestroy(void) zfpurevirtual;
34 };
35public:
36 typedef BaseValue *(*Fn_ValueCreate)(void);
37public:
38 zfuint refCount;
39public:
40 static _ZFP_ZFCoreOrderMap *create(void);
41 static void destroy(ZF_IN _ZFP_ZFCoreOrderMap *d);
42 _ZFP_ZFCoreOrderMap(void) : refCount(1) {}
43 virtual ~_ZFP_ZFCoreOrderMap(void) {}
44public:
45 virtual void objectInfoOfContentT(
47 , ZF_IN zfindex maxCount
50 virtual ZFCompareResult objectCompareValue(ZF_IN const _ZFP_ZFCoreOrderMap *ref) zfpurevirtual;
51 virtual void copyFrom(ZF_IN_OUT _ZFP_ZFCoreOrderMap *ref) zfpurevirtual;
52 virtual void addFrom(ZF_IN_OUT _ZFP_ZFCoreOrderMap *ref) zfpurevirtual;
53 virtual zfindex count(void) zfpurevirtual;
54 virtual zfbool isEmpty(void) zfpurevirtual;
55 virtual zfbool isContain(ZF_IN BaseKey *key) zfpurevirtual;
56 virtual void set(ZF_IN BaseKey *key, ZF_IN BaseValue *value) zfpurevirtual;
57 virtual BaseValue *get(ZF_IN BaseKey *key) zfpurevirtual;
58 virtual BaseValue *access(ZF_IN BaseKey *key, ZF_IN Fn_ValueCreate fn_ValueCreate) zfpurevirtual;
59 virtual void remove(ZF_IN BaseKey *key) zfpurevirtual;
60 virtual void removeAll(void) zfpurevirtual;
61public:
62 virtual zfiter iter(void) zfpurevirtual;
63 virtual zfiter iterFind(ZF_IN BaseKey *key) zfpurevirtual;
64 virtual const BaseKey *iterKey(ZF_IN const zfiter &it) zfpurevirtual;
65 virtual BaseValue *iterValue(ZF_IN const zfiter &it) zfpurevirtual;
66 virtual void iterValue(ZF_IN_OUT zfiter &it, ZF_IN BaseValue *value) zfpurevirtual;
67 virtual void iterRemove(ZF_IN_OUT zfiter &it) zfpurevirtual;
68 virtual zfiter iterAdd(ZF_IN BaseKey *key, ZF_IN BaseValue *value) zfpurevirtual;
69 virtual zfiter iterAccess(ZF_IN BaseKey *key, ZF_IN Fn_ValueCreate fn_ValueCreate) zfpurevirtual;
70public:
71 // ============================================================
72 // order map spec
73 virtual void move(ZF_IN_OUT zfiter &from, ZF_IN const zfiter &to) zfpurevirtual;
74};
81template<typename T_Key, typename T_Value, typename T_Hash = ZFCoreMapKeyHash<T_Key>, typename T_Equal = ZFCoreMapKeyEqual<T_Key> >
83protected:
86public:
90 ZFCoreOrderMap(void) : d(zfnull) {}
94 ZFCoreOrderMap(ZF_IN const zfself &ref) : d(ref.d) {
95 if(d) {
96 ++(d->refCount);
97 }
98 }
99
102 ZFCoreOrderMap &operator = (ZF_IN const zfself &ref) {
103 _ZFP_ZFCoreOrderMap *dTmp = d;
104 d = ref.d;
105 if(d) {
106 ++(d->refCount);
107 }
108 if(dTmp && (--(dTmp->refCount)) == 0) {
109 _ZFP_ZFCoreOrderMap::destroy(dTmp);
110 }
111 return *this;
112 }
113
116 zfbool operator == (ZF_IN const zfself &ref) const {return d == ref.d;}
120 zfbool operator != (ZF_IN const zfself &ref) const {return d != ref.d;}
121 ~ZFCoreOrderMap(void) {
122 if(d && (--(d->refCount)) == 0) {
123 _ZFP_ZFCoreOrderMap::destroy(d);
124 }
125 }
126
130 zfself &refPrepare(void) {_dInit(); return *this;}
134 void refDelete(void) {
135 if(d) {
136 _ZFP_ZFCoreOrderMap *dTmp = d;
137 d = zfnull;
138 if(--(dTmp->refCount) == 0) {
139 zfpoolDelete(dTmp);
140 }
141 }
142 }
143
144public:
146 void objectInfoT(ZF_IN_OUT zfstring &ret) const {
147 this->objectInfoOfContentT(ret, 5);
148 }
149
150 zfstring objectInfo(void) const {
151 zfstring ret;
152 this->objectInfoT(ret);
153 return ret;
154 }
155
157 return d == ref.d ? ZFCompareEqual : ZFCompareUncomparable;
158 }
159
161 if(d) {
162 if(ref.d) {
163 return d->objectCompareValue(ref.d);
164 }
165 else {
166 return d->count() == 0 ? ZFCompareEqual : ZFCompareUncomparable;
167 }
168 }
169 else {
170 if(ref.d) {
171 return ref.d->count() == 0 ? ZFCompareEqual : ZFCompareUncomparable;
172 }
173 else {
174 return ZFCompareEqual;
175 }
176 }
177 }
178
179public:
183 , ZF_IN_OPT zfindex maxCount = zfindexMax()
185 ) const {
186 ret += token.tokenLeft;
187 if(d) {
188 d->objectInfoOfContentT(ret, maxCount, token);
189 }
190 ret += token.tokenRight;
191 }
192
194 ZF_IN_OPT zfindex maxCount = zfindexMax()
196 ) const {
197 zfstring ret;
198 this->objectInfoOfContentT(ret, maxCount, token);
199 return ret;
200 }
201
202public:
206 void swap(ZF_IN_OUT zfself &ref) {
207 if(d != ref.d) {
208 _ZFP_ZFCoreOrderMap *dTmp = d;
209 d = ref.d;
210 ref.d = dTmp;
211 }
212 }
213
214public:
218 void copyFrom(ZF_IN const zfself &ref) {
219 if(d != ref.d) {
220 if(d) {
221 if(ref.d) {
222 d->copyFrom(ref.d);
223 }
224 else {
225 d->removeAll();
226 }
227 }
228 else {
229 if(ref.d) {
230 _dInit();
231 d->copyFrom(ref.d);
232 }
233 }
234 }
235 }
236
237public:
241 zfindex count(void) const {
242 return d ? d->count() : 0;
243 }
244
248 zfbool isEmpty(void) const {
249 return !d || d->isEmpty();
250 }
251
255 zfbool isContain(ZF_IN const T_Key &key) const {
256 return d && d->isContain(_KeyCreate(key));
257 }
258
262 void addFrom(ZF_IN const zfself &ref) {
263 if(d != ref.d) {
264 if(d) {
265 if(ref.d) {
266 d->addFrom(ref.d);
267 }
268 }
269 else {
270 if(ref.d) {
271 _dInit();
272 d->addFrom(ref.d);
273 }
274 }
275 }
276 }
277
282 void set(
283 ZF_IN const T_Key &key
284 , ZF_IN const T_Value &value
285 ) {
286 _dInit();
287 d->set(_KeyCreate(key), _ValueCreate(value));
288 }
289
292 const T_Value *get(ZF_IN const T_Key &key) const {
293 if(d) {
294 ImplValue *value = (ImplValue *)d->get(_KeyCreate(key));
295 if(value) {
296 return &(value->v);
297 }
298 }
299 return zfnull;
300 }
301
304 T_Value *get(ZF_IN const T_Key &key) {
305 if(d) {
306 ImplValue *value = (ImplValue *)d->get(_KeyCreate(key));
307 if(value) {
308 return &(value->v);
309 }
310 }
311 return zfnull;
312 }
313
316 T_Value &access(ZF_IN const T_Key &key) {
317 _dInit();
318 return ((ImplValue *)d->access(_KeyCreate(key), _ValueCreate))->v;
319 }
320
323 if(d) {
324 for(zfiter it = d->iter(); it; ++it) {
325 ret.add(((const ImplKey *)d->iterKey(it))->v);
326 }
327 }
328 }
329
332 inline ZFCoreArray<T_Key> allKey(void) const {
334 this->allKeyT(ret);
335 return ret;
336 }
337
340 if(d) {
341 for(zfiter it = d->iter(); it; ++it) {
342 ret.add(((const ImplValue *)d->iterValue(it))->v);
343 }
344 }
345 }
346
349 inline ZFCoreArray<T_Value> allValue(void) const {
351 this->allValueT(ret);
352 return ret;
353 }
354
358 void remove(ZF_IN const T_Key &key) {
359 if(d) {
360 d->remove(_KeyCreate(key));
361 }
362 }
363
367 void removeAll(void) {
368 if(d) {
369 d->removeAll();
370 }
371 }
372
373 // ============================================================
374 // iterator access
375public:
377 zfiter iter(void) const {
378 return d ? d->iter() : zfiter();
379 }
380
382 zfiter iterFind(ZF_IN const T_Key &key) const {
383 return d ? d->iterFind(_KeyCreate(key)) : zfiter();
384 }
385
387 const T_Key &iterKey(ZF_IN const zfiter &it) const {
388 return ((const ImplKey *)d->iterKey(it))->v;
389 }
390
391 const T_Value &iterValue(ZF_IN const zfiter &it) const {
392 return ((const ImplValue *)d->iterValue(it))->v;
393 }
394
396 T_Value &iterValue(ZF_IN const zfiter &it) {
397 return ((ImplValue *)d->iterValue(it))->v;
398 }
399
401 ZF_IN_OUT zfiter &it
402 , ZF_IN const T_Value &value
403 ) {
404 if(d && it) {
405 d->iterValue(it, _ValueCreate(value));
406 }
407 }
408
410 if(d && it) {
411 d->iterRemove(it);
412 }
413 }
414
417 ZF_IN const T_Key &key
418 , ZF_IN const T_Value &value
419 ) {
420 _dInit();
421 return d->iterAdd(_KeyCreate(key), _ValueCreate(value));
422 }
423
424 zfiter iterAccess(ZF_IN const T_Key &key) {
425 _dInit();
426 return d->iterAccess(_KeyCreate(key), _ValueCreate);
427 }
428
429 // ============================================================
430 // order map spec
431public:
436 void move(ZF_IN_OUT zfiter &from, ZF_IN const zfiter &to) {
437 if(d) {
438 d->move(from, to);
439 }
440 }
441
442private:
443 zfclassNotPOD ImplKey : zfextend _ZFP_ZFCoreOrderMap::BaseKey {
444 public:
445 T_Key v;
446 ImplKey(ZF_IN T_Key const &v) : v(v) {}
447 public:
448 virtual zfidentity implHash(void) const {return T_Hash()(v);}
449 virtual zfbool implEqual(ZF_IN const BaseKey *ref) const {return T_Equal()(v, ((ImplKey *)ref)->v);}
450 virtual void implInfo(ZF_IN_OUT zfstring &ret) const {return zftToStringT(ret, v);}
451 virtual BaseKey *implCopy(void) const {return zfpoolNew(ImplKey, v);}
452 virtual void implDestroy(void) {zfpoolDelete(this);}
453 };
454 zfclassNotPOD ImplValue : zfextend _ZFP_ZFCoreOrderMap::BaseValue {
455 public:
456 T_Value v;
457 ImplValue(void) : v() {}
458 ImplValue(ZF_IN T_Value const &v) : v(v) {}
459 public:
460 virtual void implCopy(ZF_IN const BaseValue *ref) {v = ((ImplValue *)ref)->v;}
461 virtual zfbool implEqual(ZF_IN const BaseValue *ref) const {return ZFComparerDefault(v, ((ImplValue *)ref)->v) == ZFCompareEqual;}
462 virtual void implInfo(ZF_IN_OUT zfstring &ret) const {return zftToStringT(ret, v);}
463 virtual BaseValue *implCopy(void) const {return zfpoolNew(ImplValue, v);}
464 virtual void implDestroy(void) {zfpoolDelete(this);}
465 };
466 static _ZFP_ZFCoreOrderMap::BaseKey *_KeyCreate(ZF_IN T_Key const &v) {
467 return zfpoolNew(ImplKey, v);
468 }
469 static _ZFP_ZFCoreOrderMap::BaseValue *_ValueCreate(ZF_IN T_Value const &v) {
470 return zfpoolNew(ImplValue, v);
471 }
472 static _ZFP_ZFCoreOrderMap::BaseValue *_ValueCreate(void) {
473 return zfpoolNew(ImplValue);
474 }
475
476private:
477 inline void _dInit(void) {
478 if(!d) {
479 d = _ZFP_ZFCoreOrderMap::create();
480 }
481 }
482
483private:
484 _ZFP_ZFCoreOrderMap *d;
485};
486ZFOUTPUT_TYPE_TEMPLATE(ZFM_EXPAND(typename T_Key, typename T_Value), ZFM_EXPAND(ZFCoreOrderMap<T_Key, T_Value>), {v.objectInfoT(s);})
487
489
490#endif // #ifndef _ZFI_ZFCoreOrderMap_h_
491
#define ZFComparerDefault
default comparer for common types, see ZFComparer
Definition ZFComparer.h:262
#define ZFLIB_ZFCore
used to export symbols
Definition ZFCoreEnvDef.h:30
core map type for private use only
#define zfclassLikePOD
shows the class is not a POD type, but you may use it like a POD except memset it to 0
Definition ZFCoreTypeDef_ClassType.h:41
#define zfextend
dummy macro shows class inherit from another
Definition ZFCoreTypeDef_ClassType.h:53
#define zfpurevirtual
dummy macro shows that a method is pure virtual method
Definition ZFCoreTypeDef_ClassType.h:68
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:196
#define ZF_IN_OPT
dummy macro that shows the param used as optional input
Definition ZFCoreTypeDef_ClassType.h:200
#define zfclassNotPOD
shows the class is not a POD type, you should not memset it or declare it in stack or copy value by c...
Definition ZFCoreTypeDef_ClassType.h:48
#define ZF_IN_OUT
dummy macro that shows the param used as required input and output
Definition ZFCoreTypeDef_ClassType.h:212
_ZFT_t_zfbool zfbool
bool type
Definition ZFCoreTypeDef_CoreType.h:103
_ZFT_t_zfindex zfindex
similar to size_t, used for index and size only
Definition ZFCoreTypeDef_CoreType.h:154
_zft_zfidentity zfidentity
identity type, ensured at least 32 bit, ensured unsigned
Definition ZFCoreTypeDef_CoreType.h:225
#define zfindexMax()
(zfindex)-1, indicate a max index value, see zfindex
Definition ZFCoreTypeDef_CoreType.h:159
#define zfnull
same as NULL, defined for future use
Definition ZFCoreTypeDef_CoreType.h:88
_ZFT_t_zfuint zfuint
same as unsigned int, see zfindex
Definition ZFCoreTypeDef_CoreType.h:169
#define ZFOUTPUT_TYPE_TEMPLATE(T_typenameList, T_Type, outputAction)
see ZFOUTPUT_TYPE
Definition ZFCoreTypeDef_OtherType.h:262
ZFCompareResult
compare result of two ZFObjects
Definition ZFCoreTypeDef_OtherType.h:28
@ ZFCompareUncomparable
Definition ZFCoreTypeDef_OtherType.h:29
@ ZFCompareEqual
Definition ZFCoreTypeDef_OtherType.h:31
void zftToStringT(zfstring &s, T_Type const &v)
util function to obtain object info
Definition ZFCoreTypeDef_OtherType.h:182
zft_zfstring< zfchar > zfstring
see zft_zfstring
Definition ZFCoreTypeDef_StringType.h:15
#define ZFM_EXPAND(...)
macro to expand a macro
Definition ZFCoreUtilMacro.h:148
#define zfpoolDelete(obj)
see zfnew
Definition ZFMemPool.h:63
#define zfpoolNew(T_Type,...)
see zfnew
Definition ZFMemPool.h:62
#define ZF_NAMESPACE_GLOBAL_BEGIN
begin namespace ZFFramework
Definition ZFNamespace.h:97
#define ZF_NAMESPACE_GLOBAL_END
end namespace ZFFramework
Definition ZFNamespace.h:98
#define ZFTokenForKeyValueContainerDefault()
see ZFTokenForKeyValueContainer, modifyable, ZFTokenForKeyValueContainerTrim by default
Definition ZFToken.h:214
light weight array
Definition ZFCoreArray.h:297
core map type for private use only
Definition ZFCoreOrderMap.h:82
T_Value & iterValue(const zfiter &it)
see zfiter
Definition ZFCoreOrderMap.h:396
void allKeyT(ZFCoreArray< T_Key > &ret) const
see allKey
Definition ZFCoreOrderMap.h:322
void objectInfoT(zfstring &ret) const
see objectInfo
Definition ZFCoreOrderMap.h:146
zfiter iterAdd(const T_Key &key, const T_Value &value)
see zfiter
Definition ZFCoreOrderMap.h:416
zfiter iterFind(const T_Key &key) const
see zfiter
Definition ZFCoreOrderMap.h:382
zfstring objectInfoOfContent(zfindex maxCount=((zfindex) -1), const ZFTokenForKeyValueContainer &token=_ZFP_ZFTokenForKeyValueContainerDefault) const
return contents info
Definition ZFCoreOrderMap.h:193
void remove(const T_Key &key)
remove or do nothing if not exist
Definition ZFCoreOrderMap.h:358
T_Value * get(const T_Key &key)
get value or null if not exist
Definition ZFCoreOrderMap.h:304
zfiter iter(void) const
see zfiter
Definition ZFCoreOrderMap.h:377
const T_Value * get(const T_Key &key) const
get value or null if not exist
Definition ZFCoreOrderMap.h:292
void refDelete(void)
delete reference
Definition ZFCoreOrderMap.h:134
void allValueT(ZFCoreArray< T_Value > &ret) const
see allValue
Definition ZFCoreOrderMap.h:339
zfstring objectInfo(void) const
return object info
Definition ZFCoreOrderMap.h:150
zfself & refPrepare(void)
prepare instance to make it able to be shared between each copy
Definition ZFCoreOrderMap.h:130
zfbool isEmpty(void) const
true if empty
Definition ZFCoreOrderMap.h:248
const T_Value & iterValue(const zfiter &it) const
see zfiter
Definition ZFCoreOrderMap.h:391
zfindex count(void) const
return count
Definition ZFCoreOrderMap.h:241
void addFrom(const zfself &ref)
add elements from ref
Definition ZFCoreOrderMap.h:262
ZFCompareResult objectCompare(zfself const &ref) const
compare by instance
Definition ZFCoreOrderMap.h:156
void set(const T_Key &key, const T_Value &value)
change value or create if not exist, value would be retained by this method
Definition ZFCoreOrderMap.h:282
void copyFrom(const zfself &ref)
copy all contents from ref, remove all before copy
Definition ZFCoreOrderMap.h:218
void move(zfiter &from, const zfiter &to)
move from to position before to, or move to tail if to not valid
Definition ZFCoreOrderMap.h:436
T_Value & access(const T_Key &key)
access value or create if not exist
Definition ZFCoreOrderMap.h:316
zfbool isContain(const T_Key &key) const
true if contains the key
Definition ZFCoreOrderMap.h:255
zfiter iterAccess(const T_Key &key)
see zfiter
Definition ZFCoreOrderMap.h:424
void removeAll(void)
remove all content
Definition ZFCoreOrderMap.h:367
void iterValue(zfiter &it, const T_Value &value)
see zfiter
Definition ZFCoreOrderMap.h:400
void objectInfoOfContentT(zfstring &ret, zfindex maxCount=((zfindex) -1), const ZFTokenForKeyValueContainer &token=_ZFP_ZFTokenForKeyValueContainerDefault) const
Definition ZFCoreOrderMap.h:181
ZFCompareResult objectCompareValue(zfself const &ref) const
compare by instance
Definition ZFCoreOrderMap.h:160
ZFCoreOrderMap(const zfself &ref)
retain the ref, to copy, use copyFrom
Definition ZFCoreOrderMap.h:94
ZFCoreArray< T_Key > allKey(void) const
return a copy of all keys
Definition ZFCoreOrderMap.h:332
void iterRemove(zfiter &it)
see zfiter
Definition ZFCoreOrderMap.h:409
ZFCoreArray< T_Value > allValue(void) const
return a copy of all values
Definition ZFCoreOrderMap.h:349
void swap(zfself &ref)
swap internal data
Definition ZFCoreOrderMap.h:206
ZFCoreOrderMap(void)
construct an empty map
Definition ZFCoreOrderMap.h:90
ZFCoreOrderMap< T_Key, T_Value, T_Hash, T_Equal > zfself
typedef for self
Definition ZFCoreOrderMap.h:85
const T_Key & iterKey(const zfiter &it) const
see zfiter
Definition ZFCoreOrderMap.h:387
util class to hold string tokens
Definition ZFToken.h:113
iterator for ZFFramework
Definition zfiter.h:40