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};
74
76template<typename T_Key>
78public:
80 inline zfidentity operator() (ZF_IN T_Key const &key) const {
81 return zfhash(key);
82 }
83};
84
85template<typename T_Key>
87public:
89 inline zfbool operator() (ZF_IN T_Key const &key0, ZF_IN T_Key const &key1) const {
90 return ZFComparerDefault(key0, key1) == ZFCompareEqual;
91 }
92};
93
100template<typename T_Key, typename T_Value, typename T_Hash = ZFCoreMapKeyHash<T_Key>, typename T_Equal = ZFCoreMapKeyEqual<T_Key> >
102protected:
105public:
109 ZFCoreMap(void) : d(zfnull) {}
113 ZFCoreMap(ZF_IN const zfself &ref) : d(ref.d) {
114 if(d) {
115 ++(d->refCount);
116 }
117 }
118
121 ZFCoreMap &operator = (ZF_IN const zfself &ref) {
122 _ZFP_ZFCoreMap *dTmp = d;
123 d = ref.d;
124 if(d) {
125 ++(d->refCount);
126 }
127 if(dTmp && (--(dTmp->refCount)) == 0) {
128 _ZFP_ZFCoreMap::destroy(dTmp);
129 }
130 return *this;
131 }
132
135 zfbool operator == (ZF_IN const zfself &ref) const {return d == ref.d;}
139 zfbool operator != (ZF_IN const zfself &ref) const {return d != ref.d;}
140 ~ZFCoreMap(void) {
141 if(d && (--(d->refCount)) == 0) {
142 _ZFP_ZFCoreMap::destroy(d);
143 }
144 }
145
149 zfself &refPrepare(void) {_dInit(); return *this;}
153 void refDelete(void) {
154 if(d) {
155 _ZFP_ZFCoreMap *dTmp = d;
156 d = zfnull;
157 if(--(dTmp->refCount) == 0) {
158 zfpoolDelete(dTmp);
159 }
160 }
161 }
162
163public:
165 void objectInfoT(ZF_IN_OUT zfstring &ret) const {
166 this->objectInfoOfContentT(ret, 5);
167 }
168
169 zfstring objectInfo(void) const {
170 zfstring ret;
171 this->objectInfoT(ret);
172 return ret;
173 }
174
176 return d == ref.d ? ZFCompareEqual : ZFCompareUncomparable;
177 }
178
180 if(d) {
181 if(ref.d) {
182 return d->objectCompareValue(ref.d);
183 }
184 else {
185 return d->count() == 0 ? ZFCompareEqual : ZFCompareUncomparable;
186 }
187 }
188 else {
189 if(ref.d) {
190 return ref.d->count() == 0 ? ZFCompareEqual : ZFCompareUncomparable;
191 }
192 else {
193 return ZFCompareEqual;
194 }
195 }
196 }
197
198public:
202 , ZF_IN_OPT zfindex maxCount = zfindexMax()
204 ) const {
205 ret += token.tokenLeft;
206 if(d) {
207 d->objectInfoOfContentT(ret, maxCount, token);
208 }
209 ret += token.tokenRight;
210 }
211
213 ZF_IN_OPT zfindex maxCount = zfindexMax()
215 ) const {
216 zfstring ret;
217 this->objectInfoOfContentT(ret, maxCount, token);
218 return ret;
219 }
220
221public:
225 void swap(ZF_IN_OUT zfself &ref) {
226 if(d != ref.d) {
227 _ZFP_ZFCoreMap *dTmp = d;
228 d = ref.d;
229 ref.d = dTmp;
230 }
231 }
232
233public:
237 void copyFrom(ZF_IN const zfself &ref) {
238 if(d != ref.d) {
239 if(d) {
240 if(ref.d) {
241 d->copyFrom(ref.d);
242 }
243 else {
244 d->removeAll();
245 }
246 }
247 else {
248 if(ref.d) {
249 _dInit();
250 d->copyFrom(ref.d);
251 }
252 }
253 }
254 }
255
256public:
260 zfindex count(void) const {
261 return d ? d->count() : 0;
262 }
263
267 zfbool isEmpty(void) const {
268 return !d || d->isEmpty();
269 }
270
274 zfbool isContain(ZF_IN const T_Key &key) const {
275 return d && d->isContain(_KeyCreate(key));
276 }
277
281 void addFrom(ZF_IN const zfself &ref) {
282 if(d != ref.d) {
283 if(d) {
284 if(ref.d) {
285 d->addFrom(ref.d);
286 }
287 }
288 else {
289 if(ref.d) {
290 _dInit();
291 d->addFrom(ref.d);
292 }
293 }
294 }
295 }
296
301 void set(
302 ZF_IN const T_Key &key
303 , ZF_IN const T_Value &value
304 ) {
305 _dInit();
306 d->set(_KeyCreate(key), _ValueCreate(value));
307 }
308
311 const T_Value *get(ZF_IN const T_Key &key) const {
312 if(d) {
313 ImplValue *value = (ImplValue *)d->get(_KeyCreate(key));
314 if(value) {
315 return &(value->v);
316 }
317 }
318 return zfnull;
319 }
320
323 T_Value *get(ZF_IN const T_Key &key) {
324 if(d) {
325 ImplValue *value = (ImplValue *)d->get(_KeyCreate(key));
326 if(value) {
327 return &(value->v);
328 }
329 }
330 return zfnull;
331 }
332
335 T_Value &access(ZF_IN const T_Key &key) {
336 _dInit();
337 return ((ImplValue *)d->access(_KeyCreate(key), _ValueCreate))->v;
338 }
339
342 if(d) {
343 for(zfiter it = d->iter(); it; ++it) {
344 ret.add(((const ImplKey *)d->iterKey(it))->v);
345 }
346 }
347 }
348
351 inline ZFCoreArray<T_Key> allKey(void) const {
353 this->allKeyT(ret);
354 return ret;
355 }
356
359 if(d) {
360 for(zfiter it = d->iter(); it; ++it) {
361 ret.add(((const ImplValue *)d->iterValue(it))->v);
362 }
363 }
364 }
365
368 inline ZFCoreArray<T_Value> allValue(void) const {
370 this->allValueT(ret);
371 return ret;
372 }
373
377 void remove(ZF_IN const T_Key &key) {
378 if(d) {
379 d->remove(_KeyCreate(key));
380 }
381 }
382
386 void removeAll(void) {
387 if(d) {
388 d->removeAll();
389 }
390 }
391
392 // ============================================================
393 // iterator access
394public:
396 zfiter iter(void) const {
397 return d ? d->iter() : zfiter();
398 }
399
401 zfiter iterFind(ZF_IN const T_Key &key) const {
402 return d ? d->iterFind(_KeyCreate(key)) : zfiter();
403 }
404
406 const T_Key &iterKey(ZF_IN const zfiter &it) const {
407 return ((const ImplKey *)d->iterKey(it))->v;
408 }
409
410 const T_Value &iterValue(ZF_IN const zfiter &it) const {
411 return ((const ImplValue *)d->iterValue(it))->v;
412 }
413
415 T_Value &iterValue(ZF_IN const zfiter &it) {
416 return ((ImplValue *)d->iterValue(it))->v;
417 }
418
420 ZF_IN_OUT zfiter &it
421 , ZF_IN const T_Value &value
422 ) {
423 if(d && it) {
424 d->iterValue(it, _ValueCreate(value));
425 }
426 }
427
429 if(d && it) {
430 d->iterRemove(it);
431 }
432 }
433
436 ZF_IN const T_Key &key
437 , ZF_IN const T_Value &value
438 ) {
439 _dInit();
440 return d->iterAdd(_KeyCreate(key), _ValueCreate(value));
441 }
442
443private:
444 zfclassNotPOD ImplKey : zfextend _ZFP_ZFCoreMap::BaseKey {
445 public:
446 T_Key v;
447 ImplKey(ZF_IN T_Key const &v) : v(v) {}
448 public:
449 virtual zfidentity implHash(void) const {return T_Hash()(v);}
450 virtual zfbool implEqual(ZF_IN const BaseKey *ref) const {return T_Equal()(v, ((ImplKey *)ref)->v);}
451 virtual void implInfo(ZF_IN_OUT zfstring &ret) const {return zftToStringT(ret, v);}
452 virtual BaseKey *implCopy(void) const {return zfpoolNew(ImplKey, v);}
453 virtual void implDestroy(void) {zfpoolDelete(this);}
454 };
455 zfclassNotPOD ImplValue : zfextend _ZFP_ZFCoreMap::BaseValue {
456 public:
457 T_Value v;
458 ImplValue(void) : v() {}
459 ImplValue(ZF_IN T_Value const &v) : v(v) {}
460 public:
461 virtual void implCopy(ZF_IN const BaseValue *ref) {v = ((ImplValue *)ref)->v;}
462 virtual zfbool implEqual(ZF_IN const BaseValue *ref) const {return ZFComparerDefault(v, ((ImplValue *)ref)->v) == ZFCompareEqual;}
463 virtual void implInfo(ZF_IN_OUT zfstring &ret) const {return zftToStringT(ret, v);}
464 virtual BaseValue *implCopy(void) const {return zfpoolNew(ImplValue, v);}
465 virtual void implDestroy(void) {zfpoolDelete(this);}
466 };
467 static _ZFP_ZFCoreMap::BaseKey *_KeyCreate(ZF_IN T_Key const &v) {
468 return zfpoolNew(ImplKey, v);
469 }
470 static _ZFP_ZFCoreMap::BaseValue *_ValueCreate(ZF_IN T_Value const &v) {
471 return zfpoolNew(ImplValue, v);
472 }
473 static _ZFP_ZFCoreMap::BaseValue *_ValueCreate(void) {
474 return zfpoolNew(ImplValue);
475 }
476
477private:
478 inline void _dInit(void) {
479 if(!d) {
480 d = _ZFP_ZFCoreMap::create();
481 }
482 }
483
484private:
485 _ZFP_ZFCoreMap *d;
486};
487ZFOUTPUT_TYPE_TEMPLATE(ZFM_EXPAND(typename T_Key, typename T_Value), ZFM_EXPAND(ZFCoreMap<T_Key, T_Value>), {v.objectInfoT(s);})
488
490
491#endif // #ifndef _ZFI_ZFCoreMap_h_
492
#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: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
string token util
#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 ZFCoreMap.h:101
void refDelete(void)
delete reference
Definition ZFCoreMap.h:153
void addFrom(const zfself &ref)
add elements from ref
Definition ZFCoreMap.h:281
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:301
void copyFrom(const zfself &ref)
copy all contents from ref, remove all before copy
Definition ZFCoreMap.h:237
void removeAll(void)
remove all content
Definition ZFCoreMap.h:386
ZFCompareResult objectCompare(zfself const &ref) const
compare by instance
Definition ZFCoreMap.h:175
T_Value & access(const T_Key &key)
access value or create if not exist
Definition ZFCoreMap.h:335
zfiter iterAdd(const T_Key &key, const T_Value &value)
see zfiter
Definition ZFCoreMap.h:435
void iterRemove(zfiter &it)
see zfiter
Definition ZFCoreMap.h:428
ZFCoreMap(void)
construct an empty map
Definition ZFCoreMap.h:109
zfstring objectInfoOfContent(zfindex maxCount=((zfindex) -1), const ZFTokenForKeyValueContainer &token=_ZFP_ZFTokenForKeyValueContainerDefault) const
return contents info
Definition ZFCoreMap.h:212
ZFCoreArray< T_Key > allKey(void) const
return a copy of all keys
Definition ZFCoreMap.h:351
zfstring objectInfo(void) const
return object info
Definition ZFCoreMap.h:169
void remove(const T_Key &key)
remove or do nothing if not exist
Definition ZFCoreMap.h:377
const T_Value * get(const T_Key &key) const
get value or null if not exist
Definition ZFCoreMap.h:311
T_Value * get(const T_Key &key)
get value or null if not exist
Definition ZFCoreMap.h:323
void iterValue(zfiter &it, const T_Value &value)
see zfiter
Definition ZFCoreMap.h:419
zfindex count(void) const
return count
Definition ZFCoreMap.h:260
zfself & refPrepare(void)
prepare instance to make it able to be shared between each copy
Definition ZFCoreMap.h:149
ZFCoreMap< T_Key, T_Value, T_Hash, T_Equal > zfself
typedef for self
Definition ZFCoreMap.h:104
zfiter iterFind(const T_Key &key) const
see zfiter
Definition ZFCoreMap.h:401
const T_Value & iterValue(const zfiter &it) const
see zfiter
Definition ZFCoreMap.h:410
void objectInfoT(zfstring &ret) const
see objectInfo
Definition ZFCoreMap.h:165
void swap(zfself &ref)
swap internal data
Definition ZFCoreMap.h:225
void allKeyT(ZFCoreArray< T_Key > &ret) const
see allKey
Definition ZFCoreMap.h:341
void allValueT(ZFCoreArray< T_Value > &ret) const
see allValue
Definition ZFCoreMap.h:358
void objectInfoOfContentT(zfstring &ret, zfindex maxCount=((zfindex) -1), const ZFTokenForKeyValueContainer &token=_ZFP_ZFTokenForKeyValueContainerDefault) const
Definition ZFCoreMap.h:200
ZFCoreMap(const zfself &ref)
retain the ref, to copy, use copyFrom
Definition ZFCoreMap.h:113
ZFCompareResult objectCompareValue(zfself const &ref) const
compare by instance
Definition ZFCoreMap.h:179
zfiter iter(void) const
see zfiter
Definition ZFCoreMap.h:396
T_Value & iterValue(const zfiter &it)
see zfiter
Definition ZFCoreMap.h:415
ZFCoreArray< T_Value > allValue(void) const
return a copy of all values
Definition ZFCoreMap.h:368
const T_Key & iterKey(const zfiter &it) const
see zfiter
Definition ZFCoreMap.h:406
zfbool isEmpty(void) const
true if empty
Definition ZFCoreMap.h:267
zfbool isContain(const T_Key &key) const
true if contains the key
Definition ZFCoreMap.h:274
default key equal function
Definition ZFCoreMap.h:86
default key hash function
Definition ZFCoreMap.h:77
util class to hold string tokens
Definition ZFToken.h:113
iterator for ZFFramework
Definition zfiter.h:40
iterator in ZFFramework