ZFFramework
Loading...
Searching...
No Matches
ZFCoreMap.h
Go to the documentation of this file.
1
5
6#ifndef _ZFI_ZFCoreMap_h_
7#define _ZFI_ZFCoreMap_h_
8
9#include "ZFMemPool.h"
10#include "ZFCoreArray.h"
11#include "zfiter.h"
12#include "ZFToken.h"
13#include "ZFCoreUtilMacro.h"
14
16
17zfclassNotPOD ZFLIB_ZFCore _ZFP_ZFCoreMap {
18public:
20 public:
21 virtual ~BaseKey(void) {}
22 public:
23 virtual zfidentity implHash(void) const zfpurevirtual;
24 virtual zfbool implEqual(ZF_IN const BaseKey *ref) const zfpurevirtual;
25 virtual void implInfo(ZF_IN_OUT zfstring &ret) const zfpurevirtual;
26 virtual BaseKey *implCopy(void) const zfpurevirtual;
27 virtual void implDestroy(void) zfpurevirtual;
28 };
29 zfclassNotPOD ZFLIB_ZFCore BaseValue {
30 public:
31 virtual ~BaseValue(void) {}
32 public:
33 virtual void implCopy(ZF_IN const BaseValue *ref) zfpurevirtual;
34 virtual zfbool implEqual(ZF_IN const BaseValue *ref) const zfpurevirtual;
35 virtual void implInfo(ZF_IN_OUT zfstring &ret) const zfpurevirtual;
36 virtual BaseValue *implCopy(void) const zfpurevirtual;
37 virtual void implDestroy(void) zfpurevirtual;
38 };
39public:
40 typedef BaseValue *(*Fn_ValueCreate)(void);
41public:
42 zfuint refCount;
43public:
44 static _ZFP_ZFCoreMap *create(void);
45 static void destroy(ZF_IN _ZFP_ZFCoreMap *d);
46 _ZFP_ZFCoreMap(void) : refCount(1) {}
47 virtual ~_ZFP_ZFCoreMap(void) {}
48public:
49 virtual void objectInfoOfContentT(
51 , ZF_IN zfindex maxCount
54 virtual ZFCompareResult objectCompareValue(ZF_IN const _ZFP_ZFCoreMap *ref) zfpurevirtual;
55 virtual void copyFrom(ZF_IN_OUT _ZFP_ZFCoreMap *ref) zfpurevirtual;
56 virtual void addFrom(ZF_IN_OUT _ZFP_ZFCoreMap *ref) zfpurevirtual;
57 virtual zfindex count(void) zfpurevirtual;
58 virtual zfbool isEmpty(void) zfpurevirtual;
59 virtual zfbool isContain(ZF_IN BaseKey *key) zfpurevirtual;
60 virtual void set(ZF_IN BaseKey *key, ZF_IN BaseValue *value) zfpurevirtual;
61 virtual BaseValue *get(ZF_IN BaseKey *key) zfpurevirtual;
62 virtual BaseValue *access(ZF_IN BaseKey *key, ZF_IN Fn_ValueCreate fn_ValueCreate) zfpurevirtual;
63 virtual void remove(ZF_IN BaseKey *key) zfpurevirtual;
64 virtual void removeAll(void) zfpurevirtual;
65public:
66 virtual zfiter iter(void) zfpurevirtual;
67 virtual zfiter iterFind(ZF_IN BaseKey *key) zfpurevirtual;
68 virtual const BaseKey *iterKey(ZF_IN const zfiter &it) zfpurevirtual;
69 virtual BaseValue *iterValue(ZF_IN const zfiter &it) zfpurevirtual;
70 virtual void iterValue(ZF_IN_OUT zfiter &it, ZF_IN BaseValue *value) zfpurevirtual;
71 virtual void iterRemove(ZF_IN_OUT zfiter &it) zfpurevirtual;
72 virtual zfiter iterAdd(ZF_IN BaseKey *key, ZF_IN BaseValue *value) zfpurevirtual;
73 virtual zfiter iterAccess(ZF_IN BaseKey *key, ZF_IN Fn_ValueCreate fn_ValueCreate) zfpurevirtual;
74};
75
77template<typename T_Key>
79public:
81 inline zfidentity operator() (ZF_IN T_Key const &key) const {
82 return zfhash(key);
83 }
84};
85
86template<typename T_Key>
88public:
90 inline zfbool operator() (ZF_IN T_Key const &key0, ZF_IN T_Key const &key1) const {
91 return ZFComparerDefault(key0, key1) == ZFCompareEqual;
92 }
93};
94
101template<typename T_Key, typename T_Value, typename T_Hash = ZFCoreMapKeyHash<T_Key>, typename T_Equal = ZFCoreMapKeyEqual<T_Key> >
103protected:
106public:
110 ZFCoreMap(void) : d(zfnull) {}
114 ZFCoreMap(ZF_IN const zfself &ref) : d(ref.d) {
115 if(d) {
116 ++(d->refCount);
117 }
118 }
119
122 ZFCoreMap &operator = (ZF_IN const zfself &ref) {
123 _ZFP_ZFCoreMap *dTmp = d;
124 d = ref.d;
125 if(d) {
126 ++(d->refCount);
127 }
128 if(dTmp && (--(dTmp->refCount)) == 0) {
129 _ZFP_ZFCoreMap::destroy(dTmp);
130 }
131 return *this;
132 }
133
136 zfbool operator == (ZF_IN const zfself &ref) const {return d == ref.d;}
140 zfbool operator != (ZF_IN const zfself &ref) const {return d != ref.d;}
141 ~ZFCoreMap(void) {
142 if(d && (--(d->refCount)) == 0) {
143 _ZFP_ZFCoreMap::destroy(d);
144 }
145 }
146
150 zfself &refPrepare(void) {_dInit(); return *this;}
154 void refDelete(void) {
155 if(d) {
156 _ZFP_ZFCoreMap *dTmp = d;
157 d = zfnull;
158 if(--(dTmp->refCount) == 0) {
159 zfpoolDelete(dTmp);
160 }
161 }
162 }
163
164public:
166 void objectInfoT(ZF_IN_OUT zfstring &ret) const {
167 this->objectInfoOfContentT(ret, 5);
168 }
169
170 zfstring objectInfo(void) const {
171 zfstring ret;
172 this->objectInfoT(ret);
173 return ret;
174 }
175
177 return d == ref.d ? ZFCompareEqual : ZFCompareUncomparable;
178 }
179
181 if(d) {
182 if(ref.d) {
183 return d->objectCompareValue(ref.d);
184 }
185 else {
186 return d->count() == 0 ? ZFCompareEqual : ZFCompareUncomparable;
187 }
188 }
189 else {
190 if(ref.d) {
191 return ref.d->count() == 0 ? ZFCompareEqual : ZFCompareUncomparable;
192 }
193 else {
194 return ZFCompareEqual;
195 }
196 }
197 }
198
199public:
203 , ZF_IN_OPT zfindex maxCount = zfindexMax()
205 ) const {
206 ret += token.tokenLeft;
207 if(d) {
208 d->objectInfoOfContentT(ret, maxCount, token);
209 }
210 ret += token.tokenRight;
211 }
212
214 ZF_IN_OPT zfindex maxCount = zfindexMax()
216 ) const {
217 zfstring ret;
218 this->objectInfoOfContentT(ret, maxCount, token);
219 return ret;
220 }
221
222public:
226 void swap(ZF_IN_OUT zfself &ref) {
227 if(d != ref.d) {
228 _ZFP_ZFCoreMap *dTmp = d;
229 d = ref.d;
230 ref.d = dTmp;
231 }
232 }
233
234public:
238 void copyFrom(ZF_IN const zfself &ref) {
239 if(d != ref.d) {
240 if(d) {
241 if(ref.d) {
242 d->copyFrom(ref.d);
243 }
244 else {
245 d->removeAll();
246 }
247 }
248 else {
249 if(ref.d) {
250 _dInit();
251 d->copyFrom(ref.d);
252 }
253 }
254 }
255 }
256
257public:
261 zfindex count(void) const {
262 return d ? d->count() : 0;
263 }
264
268 zfbool isEmpty(void) const {
269 return !d || d->isEmpty();
270 }
271
275 zfbool isContain(ZF_IN const T_Key &key) const {
276 return d && d->isContain(_KeyCreate(key));
277 }
278
282 void addFrom(ZF_IN const zfself &ref) {
283 if(d != ref.d) {
284 if(d) {
285 if(ref.d) {
286 d->addFrom(ref.d);
287 }
288 }
289 else {
290 if(ref.d) {
291 _dInit();
292 d->addFrom(ref.d);
293 }
294 }
295 }
296 }
297
302 void set(
303 ZF_IN const T_Key &key
304 , ZF_IN const T_Value &value
305 ) {
306 _dInit();
307 d->set(_KeyCreate(key), _ValueCreate(value));
308 }
309
312 const T_Value *get(ZF_IN const T_Key &key) const {
313 if(d) {
314 ImplValue *value = (ImplValue *)d->get(_KeyCreate(key));
315 if(value) {
316 return &(value->v);
317 }
318 }
319 return zfnull;
320 }
321
324 T_Value *get(ZF_IN const T_Key &key) {
325 if(d) {
326 ImplValue *value = (ImplValue *)d->get(_KeyCreate(key));
327 if(value) {
328 return &(value->v);
329 }
330 }
331 return zfnull;
332 }
333
336 T_Value &access(ZF_IN const T_Key &key) {
337 _dInit();
338 return ((ImplValue *)d->access(_KeyCreate(key), _ValueCreate))->v;
339 }
340
343 if(d) {
344 for(zfiter it = d->iter(); it; ++it) {
345 ret.add(((const ImplKey *)d->iterKey(it))->v);
346 }
347 }
348 }
349
352 inline ZFCoreArray<T_Key> allKey(void) const {
354 this->allKeyT(ret);
355 return ret;
356 }
357
360 if(d) {
361 for(zfiter it = d->iter(); it; ++it) {
362 ret.add(((const ImplValue *)d->iterValue(it))->v);
363 }
364 }
365 }
366
369 inline ZFCoreArray<T_Value> allValue(void) const {
371 this->allValueT(ret);
372 return ret;
373 }
374
378 void remove(ZF_IN const T_Key &key) {
379 if(d) {
380 d->remove(_KeyCreate(key));
381 }
382 }
383
387 void removeAll(void) {
388 if(d) {
389 d->removeAll();
390 }
391 }
392
393 // ============================================================
394 // iterator access
395public:
397 zfiter iter(void) const {
398 return d ? d->iter() : zfiter();
399 }
400
402 zfiter iterFind(ZF_IN const T_Key &key) const {
403 return d ? d->iterFind(_KeyCreate(key)) : zfiter();
404 }
405
407 const T_Key &iterKey(ZF_IN const zfiter &it) const {
408 return ((const ImplKey *)d->iterKey(it))->v;
409 }
410
411 const T_Value &iterValue(ZF_IN const zfiter &it) const {
412 return ((const ImplValue *)d->iterValue(it))->v;
413 }
414
416 T_Value &iterValue(ZF_IN const zfiter &it) {
417 return ((ImplValue *)d->iterValue(it))->v;
418 }
419
421 ZF_IN_OUT zfiter &it
422 , ZF_IN const T_Value &value
423 ) {
424 if(d && it) {
425 d->iterValue(it, _ValueCreate(value));
426 }
427 }
428
430 if(d && it) {
431 d->iterRemove(it);
432 }
433 }
434
437 ZF_IN const T_Key &key
438 , ZF_IN const T_Value &value
439 ) {
440 _dInit();
441 return d->iterAdd(_KeyCreate(key), _ValueCreate(value));
442 }
443
444 zfiter iterAccess(ZF_IN const T_Key &key) {
445 _dInit();
446 return d->iterAccess(_KeyCreate(key), _ValueCreate);
447 }
448
449private:
450 zfclassNotPOD ImplKey : zfextend _ZFP_ZFCoreMap::BaseKey {
451 public:
452 T_Key v;
453 ImplKey(ZF_IN T_Key const &v) : v(v) {}
454 public:
455 virtual zfidentity implHash(void) const {return T_Hash()(v);}
456 virtual zfbool implEqual(ZF_IN const BaseKey *ref) const {return T_Equal()(v, ((ImplKey *)ref)->v);}
457 virtual void implInfo(ZF_IN_OUT zfstring &ret) const {return zftToStringT(ret, v);}
458 virtual BaseKey *implCopy(void) const {return zfpoolNew(ImplKey, v);}
459 virtual void implDestroy(void) {zfpoolDelete(this);}
460 };
461 zfclassNotPOD ImplValue : zfextend _ZFP_ZFCoreMap::BaseValue {
462 public:
463 T_Value v;
464 ImplValue(void) : v() {}
465 ImplValue(ZF_IN T_Value const &v) : v(v) {}
466 public:
467 virtual void implCopy(ZF_IN const BaseValue *ref) {v = ((ImplValue *)ref)->v;}
468 virtual zfbool implEqual(ZF_IN const BaseValue *ref) const {return ZFComparerDefault(v, ((ImplValue *)ref)->v) == ZFCompareEqual;}
469 virtual void implInfo(ZF_IN_OUT zfstring &ret) const {return zftToStringT(ret, v);}
470 virtual BaseValue *implCopy(void) const {return zfpoolNew(ImplValue, v);}
471 virtual void implDestroy(void) {zfpoolDelete(this);}
472 };
473 static _ZFP_ZFCoreMap::BaseKey *_KeyCreate(ZF_IN T_Key const &v) {
474 return zfpoolNew(ImplKey, v);
475 }
476 static _ZFP_ZFCoreMap::BaseValue *_ValueCreate(ZF_IN T_Value const &v) {
477 return zfpoolNew(ImplValue, v);
478 }
479 static _ZFP_ZFCoreMap::BaseValue *_ValueCreate(void) {
480 return zfpoolNew(ImplValue);
481 }
482
483private:
484 inline void _dInit(void) {
485 if(!d) {
486 d = _ZFP_ZFCoreMap::create();
487 }
488 }
489
490private:
491 _ZFP_ZFCoreMap *d;
492};
493ZFOUTPUT_TYPE_TEMPLATE(ZFM_EXPAND(typename T_Key, typename T_Value), ZFM_EXPAND(ZFCoreMap<T_Key, T_Value>), {v.objectInfoT(s);})
494
496
497#endif // #ifndef _ZFI_ZFCoreMap_h_
498
#define ZFComparerDefault
default comparer for common types, see ZFComparer
Definition ZFComparer.h:262
light weight array
#define ZFLIB_ZFCore
used to export symbols
Definition ZFCoreEnvDef.h:30
#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
zfidentity zfhash(const T_Type &v)
std::hash wrapper
Definition ZFCoreUtilTemplate.h:518
memory pool
#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
string token util
#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 ZFCoreMap.h:102
void refDelete(void)
delete reference
Definition ZFCoreMap.h:154
void addFrom(const zfself &ref)
add elements from ref
Definition ZFCoreMap.h:282
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 ZFCoreMap.h:302
void copyFrom(const zfself &ref)
copy all contents from ref, remove all before copy
Definition ZFCoreMap.h:238
void removeAll(void)
remove all content
Definition ZFCoreMap.h:387
ZFCompareResult objectCompare(zfself const &ref) const
compare by instance
Definition ZFCoreMap.h:176
T_Value & access(const T_Key &key)
access value or create if not exist
Definition ZFCoreMap.h:336
zfiter iterAdd(const T_Key &key, const T_Value &value)
see zfiter
Definition ZFCoreMap.h:436
void iterRemove(zfiter &it)
see zfiter
Definition ZFCoreMap.h:429
ZFCoreMap(void)
construct an empty map
Definition ZFCoreMap.h:110
zfstring objectInfoOfContent(zfindex maxCount=((zfindex) -1), const ZFTokenForKeyValueContainer &token=_ZFP_ZFTokenForKeyValueContainerDefault) const
return contents info
Definition ZFCoreMap.h:213
ZFCoreArray< T_Key > allKey(void) const
return a copy of all keys
Definition ZFCoreMap.h:352
zfstring objectInfo(void) const
return object info
Definition ZFCoreMap.h:170
void remove(const T_Key &key)
remove or do nothing if not exist
Definition ZFCoreMap.h:378
const T_Value * get(const T_Key &key) const
get value or null if not exist
Definition ZFCoreMap.h:312
T_Value * get(const T_Key &key)
get value or null if not exist
Definition ZFCoreMap.h:324
void iterValue(zfiter &it, const T_Value &value)
see zfiter
Definition ZFCoreMap.h:420
zfindex count(void) const
return count
Definition ZFCoreMap.h:261
zfself & refPrepare(void)
prepare instance to make it able to be shared between each copy
Definition ZFCoreMap.h:150
ZFCoreMap< T_Key, T_Value, T_Hash, T_Equal > zfself
typedef for self
Definition ZFCoreMap.h:105
zfiter iterFind(const T_Key &key) const
see zfiter
Definition ZFCoreMap.h:402
const T_Value & iterValue(const zfiter &it) const
see zfiter
Definition ZFCoreMap.h:411
void objectInfoT(zfstring &ret) const
see objectInfo
Definition ZFCoreMap.h:166
void swap(zfself &ref)
swap internal data
Definition ZFCoreMap.h:226
void allKeyT(ZFCoreArray< T_Key > &ret) const
see allKey
Definition ZFCoreMap.h:342
void allValueT(ZFCoreArray< T_Value > &ret) const
see allValue
Definition ZFCoreMap.h:359
void objectInfoOfContentT(zfstring &ret, zfindex maxCount=((zfindex) -1), const ZFTokenForKeyValueContainer &token=_ZFP_ZFTokenForKeyValueContainerDefault) const
Definition ZFCoreMap.h:201
ZFCoreMap(const zfself &ref)
retain the ref, to copy, use copyFrom
Definition ZFCoreMap.h:114
ZFCompareResult objectCompareValue(zfself const &ref) const
compare by instance
Definition ZFCoreMap.h:180
zfiter iter(void) const
see zfiter
Definition ZFCoreMap.h:397
T_Value & iterValue(const zfiter &it)
see zfiter
Definition ZFCoreMap.h:416
ZFCoreArray< T_Value > allValue(void) const
return a copy of all values
Definition ZFCoreMap.h:369
zfiter iterAccess(const T_Key &key)
see zfiter
Definition ZFCoreMap.h:444
const T_Key & iterKey(const zfiter &it) const
see zfiter
Definition ZFCoreMap.h:407
zfbool isEmpty(void) const
true if empty
Definition ZFCoreMap.h:268
zfbool isContain(const T_Key &key) const
true if contains the key
Definition ZFCoreMap.h:275
default key equal function
Definition ZFCoreMap.h:87
default key hash function
Definition ZFCoreMap.h:78
util class to hold string tokens
Definition ZFToken.h:113
iterator for ZFFramework
Definition zfiter.h:40
iterator in ZFFramework