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;
69public:
70 // ============================================================
71 // order map spec
72 virtual void move(ZF_IN_OUT zfiter &from, ZF_IN const zfiter &to) zfpurevirtual;
73};
80template<typename T_Key, typename T_Value, typename T_Hash = ZFCoreMapKeyHash<T_Key>, typename T_Equal = ZFCoreMapKeyEqual<T_Key> >
82protected:
85public:
89 ZFCoreOrderMap(void) : d(zfnull) {}
93 ZFCoreOrderMap(ZF_IN const zfself &ref) : d(ref.d) {
94 if(d) {
95 ++(d->refCount);
96 }
97 }
98
101 ZFCoreOrderMap &operator = (ZF_IN const zfself &ref) {
102 _ZFP_ZFCoreOrderMap *dTmp = d;
103 d = ref.d;
104 if(d) {
105 ++(d->refCount);
106 }
107 if(dTmp && (--(dTmp->refCount)) == 0) {
108 _ZFP_ZFCoreOrderMap::destroy(dTmp);
109 }
110 return *this;
111 }
112
115 zfbool operator == (ZF_IN const zfself &ref) const {return d == ref.d;}
119 zfbool operator != (ZF_IN const zfself &ref) const {return d != ref.d;}
120 ~ZFCoreOrderMap(void) {
121 if(d && (--(d->refCount)) == 0) {
122 _ZFP_ZFCoreOrderMap::destroy(d);
123 }
124 }
125
129 zfself &refPrepare(void) {_dInit(); return *this;}
133 void refDelete(void) {
134 if(d) {
135 _ZFP_ZFCoreOrderMap *dTmp = d;
136 d = zfnull;
137 if(--(dTmp->refCount) == 0) {
138 zfpoolDelete(dTmp);
139 }
140 }
141 }
142
143public:
145 void objectInfoT(ZF_IN_OUT zfstring &ret) const {
146 this->objectInfoOfContentT(ret, 5);
147 }
148
149 zfstring objectInfo(void) const {
150 zfstring ret;
151 this->objectInfoT(ret);
152 return ret;
153 }
154
156 return d == ref.d ? ZFCompareEqual : ZFCompareUncomparable;
157 }
158
160 if(d) {
161 if(ref.d) {
162 return d->objectCompareValue(ref.d);
163 }
164 else {
165 return d->count() == 0 ? ZFCompareEqual : ZFCompareUncomparable;
166 }
167 }
168 else {
169 if(ref.d) {
170 return ref.d->count() == 0 ? ZFCompareEqual : ZFCompareUncomparable;
171 }
172 else {
173 return ZFCompareEqual;
174 }
175 }
176 }
177
178public:
182 , ZF_IN_OPT zfindex maxCount = zfindexMax()
184 ) const {
185 ret += token.tokenLeft;
186 if(d) {
187 d->objectInfoOfContentT(ret, maxCount, token);
188 }
189 ret += token.tokenRight;
190 }
191
193 ZF_IN_OPT zfindex maxCount = zfindexMax()
195 ) const {
196 zfstring ret;
197 this->objectInfoOfContentT(ret, maxCount, token);
198 return ret;
199 }
200
201public:
205 void swap(ZF_IN_OUT zfself &ref) {
206 if(d != ref.d) {
207 _ZFP_ZFCoreOrderMap *dTmp = d;
208 d = ref.d;
209 ref.d = dTmp;
210 }
211 }
212
213public:
217 void copyFrom(ZF_IN const zfself &ref) {
218 if(d != ref.d) {
219 if(d) {
220 if(ref.d) {
221 d->copyFrom(ref.d);
222 }
223 else {
224 d->removeAll();
225 }
226 }
227 else {
228 if(ref.d) {
229 _dInit();
230 d->copyFrom(ref.d);
231 }
232 }
233 }
234 }
235
236public:
240 zfindex count(void) const {
241 return d ? d->count() : 0;
242 }
243
247 zfbool isEmpty(void) const {
248 return !d || d->isEmpty();
249 }
250
254 zfbool isContain(ZF_IN const T_Key &key) const {
255 return d && d->isContain(_KeyCreate(key));
256 }
257
261 void addFrom(ZF_IN const zfself &ref) {
262 if(d != ref.d) {
263 if(d) {
264 if(ref.d) {
265 d->addFrom(ref.d);
266 }
267 }
268 else {
269 if(ref.d) {
270 _dInit();
271 d->addFrom(ref.d);
272 }
273 }
274 }
275 }
276
281 void set(
282 ZF_IN const T_Key &key
283 , ZF_IN const T_Value &value
284 ) {
285 _dInit();
286 d->set(_KeyCreate(key), _ValueCreate(value));
287 }
288
291 const T_Value *get(ZF_IN const T_Key &key) const {
292 if(d) {
293 ImplValue *value = (ImplValue *)d->get(_KeyCreate(key));
294 if(value) {
295 return &(value->v);
296 }
297 }
298 return zfnull;
299 }
300
303 T_Value *get(ZF_IN const T_Key &key) {
304 if(d) {
305 ImplValue *value = (ImplValue *)d->get(_KeyCreate(key));
306 if(value) {
307 return &(value->v);
308 }
309 }
310 return zfnull;
311 }
312
315 T_Value &access(ZF_IN const T_Key &key) {
316 _dInit();
317 return ((ImplValue *)d->access(_KeyCreate(key), _ValueCreate))->v;
318 }
319
322 if(d) {
323 for(zfiter it = d->iter(); it; ++it) {
324 ret.add(((const ImplKey *)d->iterKey(it))->v);
325 }
326 }
327 }
328
331 inline ZFCoreArray<T_Key> allKey(void) const {
333 this->allKeyT(ret);
334 return ret;
335 }
336
339 if(d) {
340 for(zfiter it = d->iter(); it; ++it) {
341 ret.add(((const ImplValue *)d->iterValue(it))->v);
342 }
343 }
344 }
345
348 inline ZFCoreArray<T_Value> allValue(void) const {
350 this->allValueT(ret);
351 return ret;
352 }
353
357 void remove(ZF_IN const T_Key &key) {
358 if(d) {
359 d->remove(_KeyCreate(key));
360 }
361 }
362
366 void removeAll(void) {
367 if(d) {
368 d->removeAll();
369 }
370 }
371
372 // ============================================================
373 // iterator access
374public:
376 zfiter iter(void) const {
377 return d ? d->iter() : zfiter();
378 }
379
381 zfiter iterFind(ZF_IN const T_Key &key) const {
382 return d ? d->iterFind(_KeyCreate(key)) : zfiter();
383 }
384
386 const T_Key &iterKey(ZF_IN const zfiter &it) const {
387 return ((const ImplKey *)d->iterKey(it))->v;
388 }
389
390 const T_Value &iterValue(ZF_IN const zfiter &it) const {
391 return ((const ImplValue *)d->iterValue(it))->v;
392 }
393
395 T_Value &iterValue(ZF_IN const zfiter &it) {
396 return ((ImplValue *)d->iterValue(it))->v;
397 }
398
400 ZF_IN_OUT zfiter &it
401 , ZF_IN const T_Value &value
402 ) {
403 if(d && it) {
404 d->iterValue(it, _ValueCreate(value));
405 }
406 }
407
409 if(d && it) {
410 d->iterRemove(it);
411 }
412 }
413
416 ZF_IN const T_Key &key
417 , ZF_IN const T_Value &value
418 ) {
419 _dInit();
420 return d->iterAdd(_KeyCreate(key), _ValueCreate(value));
421 }
422
423 // ============================================================
424 // order map spec
425public:
430 void move(ZF_IN_OUT zfiter &from, ZF_IN const zfiter &to) {
431 if(d) {
432 d->move(from, to);
433 }
434 }
435
436private:
437 zfclassNotPOD ImplKey : zfextend _ZFP_ZFCoreOrderMap::BaseKey {
438 public:
439 T_Key v;
440 ImplKey(ZF_IN T_Key const &v) : v(v) {}
441 public:
442 virtual zfidentity implHash(void) const {return T_Hash()(v);}
443 virtual zfbool implEqual(ZF_IN const BaseKey *ref) const {return T_Equal()(v, ((ImplKey *)ref)->v);}
444 virtual void implInfo(ZF_IN_OUT zfstring &ret) const {return zftToStringT(ret, v);}
445 virtual BaseKey *implCopy(void) const {return zfpoolNew(ImplKey, v);}
446 virtual void implDestroy(void) {zfpoolDelete(this);}
447 };
448 zfclassNotPOD ImplValue : zfextend _ZFP_ZFCoreOrderMap::BaseValue {
449 public:
450 T_Value v;
451 ImplValue(void) : v() {}
452 ImplValue(ZF_IN T_Value const &v) : v(v) {}
453 public:
454 virtual void implCopy(ZF_IN const BaseValue *ref) {v = ((ImplValue *)ref)->v;}
455 virtual zfbool implEqual(ZF_IN const BaseValue *ref) const {return ZFComparerDefault(v, ((ImplValue *)ref)->v) == ZFCompareEqual;}
456 virtual void implInfo(ZF_IN_OUT zfstring &ret) const {return zftToStringT(ret, v);}
457 virtual BaseValue *implCopy(void) const {return zfpoolNew(ImplValue, v);}
458 virtual void implDestroy(void) {zfpoolDelete(this);}
459 };
460 static _ZFP_ZFCoreOrderMap::BaseKey *_KeyCreate(ZF_IN T_Key const &v) {
461 return zfpoolNew(ImplKey, v);
462 }
463 static _ZFP_ZFCoreOrderMap::BaseValue *_ValueCreate(ZF_IN T_Value const &v) {
464 return zfpoolNew(ImplValue, v);
465 }
466 static _ZFP_ZFCoreOrderMap::BaseValue *_ValueCreate(void) {
467 return zfpoolNew(ImplValue);
468 }
469
470private:
471 inline void _dInit(void) {
472 if(!d) {
473 d = _ZFP_ZFCoreOrderMap::create();
474 }
475 }
476
477private:
478 _ZFP_ZFCoreOrderMap *d;
479};
480ZFOUTPUT_TYPE_TEMPLATE(ZFM_EXPAND(typename T_Key, typename T_Value), ZFM_EXPAND(ZFCoreOrderMap<T_Key, T_Value>), {v.objectInfoT(s);})
481
483
484#endif // #ifndef _ZFI_ZFCoreOrderMap_h_
485
#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:81
#define zfpoolNew(T_Type,...)
see zfnew
Definition ZFMemPool.h:80
#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:292
core map type for private use only
Definition ZFCoreOrderMap.h:81
T_Value & iterValue(const zfiter &it)
see zfiter
Definition ZFCoreOrderMap.h:395
void allKeyT(ZFCoreArray< T_Key > &ret) const
see allKey
Definition ZFCoreOrderMap.h:321
void objectInfoT(zfstring &ret) const
see objectInfo
Definition ZFCoreOrderMap.h:145
zfiter iterAdd(const T_Key &key, const T_Value &value)
see zfiter
Definition ZFCoreOrderMap.h:415
zfiter iterFind(const T_Key &key) const
see zfiter
Definition ZFCoreOrderMap.h:381
zfstring objectInfoOfContent(zfindex maxCount=((zfindex) -1), const ZFTokenForKeyValueContainer &token=_ZFP_ZFTokenForKeyValueContainerDefault) const
return contents info
Definition ZFCoreOrderMap.h:192
void remove(const T_Key &key)
remove or do nothing if not exist
Definition ZFCoreOrderMap.h:357
T_Value * get(const T_Key &key)
get value or null if not exist
Definition ZFCoreOrderMap.h:303
zfiter iter(void) const
see zfiter
Definition ZFCoreOrderMap.h:376
const T_Value * get(const T_Key &key) const
get value or null if not exist
Definition ZFCoreOrderMap.h:291
void refDelete(void)
delete reference
Definition ZFCoreOrderMap.h:133
void allValueT(ZFCoreArray< T_Value > &ret) const
see allValue
Definition ZFCoreOrderMap.h:338
zfstring objectInfo(void) const
return object info
Definition ZFCoreOrderMap.h:149
zfself & refPrepare(void)
prepare instance to make it able to be shared between each copy
Definition ZFCoreOrderMap.h:129
zfbool isEmpty(void) const
true if empty
Definition ZFCoreOrderMap.h:247
const T_Value & iterValue(const zfiter &it) const
see zfiter
Definition ZFCoreOrderMap.h:390
zfindex count(void) const
return count
Definition ZFCoreOrderMap.h:240
void addFrom(const zfself &ref)
add elements from ref
Definition ZFCoreOrderMap.h:261
ZFCompareResult objectCompare(zfself const &ref) const
compare by instance
Definition ZFCoreOrderMap.h:155
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:281
void copyFrom(const zfself &ref)
copy all contents from ref, remove all before copy
Definition ZFCoreOrderMap.h:217
void move(zfiter &from, const zfiter &to)
move from to position before to, or move to tail if to not valid
Definition ZFCoreOrderMap.h:430
T_Value & access(const T_Key &key)
access value or create if not exist
Definition ZFCoreOrderMap.h:315
zfbool isContain(const T_Key &key) const
true if contains the key
Definition ZFCoreOrderMap.h:254
void removeAll(void)
remove all content
Definition ZFCoreOrderMap.h:366
void iterValue(zfiter &it, const T_Value &value)
see zfiter
Definition ZFCoreOrderMap.h:399
void objectInfoOfContentT(zfstring &ret, zfindex maxCount=((zfindex) -1), const ZFTokenForKeyValueContainer &token=_ZFP_ZFTokenForKeyValueContainerDefault) const
Definition ZFCoreOrderMap.h:180
ZFCompareResult objectCompareValue(zfself const &ref) const
compare by instance
Definition ZFCoreOrderMap.h:159
ZFCoreOrderMap(const zfself &ref)
retain the ref, to copy, use copyFrom
Definition ZFCoreOrderMap.h:93
ZFCoreArray< T_Key > allKey(void) const
return a copy of all keys
Definition ZFCoreOrderMap.h:331
void iterRemove(zfiter &it)
see zfiter
Definition ZFCoreOrderMap.h:408
ZFCoreArray< T_Value > allValue(void) const
return a copy of all values
Definition ZFCoreOrderMap.h:348
void swap(zfself &ref)
swap internal data
Definition ZFCoreOrderMap.h:205
ZFCoreOrderMap(void)
construct an empty map
Definition ZFCoreOrderMap.h:89
ZFCoreOrderMap< T_Key, T_Value, T_Hash, T_Equal > zfself
typedef for self
Definition ZFCoreOrderMap.h:84
const T_Key & iterKey(const zfiter &it) const
see zfiter
Definition ZFCoreOrderMap.h:386
util class to hold string tokens
Definition ZFToken.h:113
iterator for ZFFramework
Definition zfiter.h:40