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> >
102public:
106 ZFCoreMap(void) : d(zfnull) {}
111 if(d) {
112 ++(d->refCount);
113 }
114 }
115
118 ZFCoreMap &operator = (ZF_IN const ZFCoreMap<T_Key, T_Value> &ref) {
119 _ZFP_ZFCoreMap *dTmp = d;
120 d = ref.d;
121 if(d) {
122 ++(d->refCount);
123 }
124 if(dTmp && (--(dTmp->refCount)) == 0) {
125 _ZFP_ZFCoreMap::destroy(dTmp);
126 }
127 return *this;
128 }
129
132 zfbool operator == (ZF_IN const ZFCoreMap<T_Key, T_Value> &ref) const {return d == ref.d;}
136 zfbool operator != (ZF_IN const ZFCoreMap<T_Key, T_Value> &ref) const {return d != ref.d;}
137 ~ZFCoreMap(void) {
138 if(d && (--(d->refCount)) == 0) {
139 _ZFP_ZFCoreMap::destroy(d);
140 }
141 }
142
146 void refPrepare(void) {_dInit();}
150 void refDelete(void) {
151 if(d) {
152 _ZFP_ZFCoreMap *dTmp = d;
153 d = zfnull;
154 if(--(dTmp->refCount) == 0) {
155 zfpoolDelete(dTmp);
156 }
157 }
158 }
159
160public:
162 void objectInfoT(ZF_IN_OUT zfstring &ret) const {
163 this->objectInfoOfContentT(ret, 5);
164 }
165
166 zfstring objectInfo(void) const {
167 zfstring ret;
168 this->objectInfoT(ret);
169 return ret;
170 }
171
175
177 if(d) {
178 if(ref.d) {
179 return d->objectCompareValue(ref.d);
180 }
181 else {
182 return d->count() == 0 ? ZFCompareEqual : ZFCompareUncomparable;
183 }
184 }
185 else {
186 if(ref.d) {
187 return ref.d->count() == 0 ? ZFCompareEqual : ZFCompareUncomparable;
188 }
189 else {
190 return ZFCompareEqual;
191 }
192 }
193 }
194
195public:
199 , ZF_IN_OPT zfindex maxCount = zfindexMax()
201 ) const {
202 ret += token.tokenLeft;
203 if(d) {
204 d->objectInfoOfContentT(ret, maxCount, token);
205 }
206 ret += token.tokenRight;
207 }
208
210 ZF_IN_OPT zfindex maxCount = zfindexMax()
212 ) const {
213 zfstring ret;
214 this->objectInfoOfContentT(ret, maxCount, token);
215 return ret;
216 }
217
218public:
223 if(d != ref.d) {
224 _ZFP_ZFCoreMap *dTmp = d;
225 d = ref.d;
226 ref.d = dTmp;
227 }
228 }
229
230public:
235 if(d != ref.d) {
236 if(d) {
237 if(ref.d) {
238 d->copyFrom(ref.d);
239 }
240 else {
241 d->removeAll();
242 }
243 }
244 else {
245 if(ref.d) {
246 _dInit();
247 d->copyFrom(ref.d);
248 }
249 }
250 }
251 }
252
253public:
257 zfindex count(void) const {
258 return d ? d->count() : 0;
259 }
260
264 zfbool isEmpty(void) const {
265 return !d || d->isEmpty();
266 }
267
271 zfbool isContain(ZF_IN const T_Key &key) const {
272 return d && d->isContain(_KeyCreate(key));
273 }
274
279 if(d != ref.d) {
280 if(d) {
281 if(ref.d) {
282 d->addFrom(ref.d);
283 }
284 }
285 else {
286 if(ref.d) {
287 _dInit();
288 d->addFrom(ref.d);
289 }
290 }
291 }
292 }
293
298 void set(
299 ZF_IN const T_Key &key
300 , ZF_IN const T_Value &value
301 ) {
302 _dInit();
303 d->set(_KeyCreate(key), _ValueCreate(value));
304 }
305
308 const T_Value *get(ZF_IN const T_Key &key) const {
309 if(d) {
310 ImplValue *value = (ImplValue *)d->get(_KeyCreate(key));
311 if(value) {
312 return &(value->v);
313 }
314 }
315 return zfnull;
316 }
317
320 T_Value *get(ZF_IN const T_Key &key) {
321 if(d) {
322 ImplValue *value = (ImplValue *)d->get(_KeyCreate(key));
323 if(value) {
324 return &(value->v);
325 }
326 }
327 return zfnull;
328 }
329
332 T_Value &access(ZF_IN const T_Key &key) {
333 _dInit();
334 return ((ImplValue *)d->access(_KeyCreate(key), _ValueCreate))->v;
335 }
336
339 if(d) {
340 for(zfiter it = d->iter(); it; ++it) {
341 ret.add(((const ImplKey *)d->iterKey(it))->v);
342 }
343 }
344 }
345
348 inline ZFCoreArray<T_Key> allKey(void) const {
350 this->allKeyT(ret);
351 return ret;
352 }
353
356 if(d) {
357 for(zfiter it = d->iter(); it; ++it) {
358 ret.add(((const ImplValue *)d->iterValue(it))->v);
359 }
360 }
361 }
362
365 inline ZFCoreArray<T_Value> allValue(void) const {
367 this->allValueT(ret);
368 return ret;
369 }
370
374 void remove(ZF_IN const T_Key &key) {
375 if(d) {
376 d->remove(_KeyCreate(key));
377 }
378 }
379
383 void removeAll(void) {
384 if(d) {
385 d->removeAll();
386 }
387 }
388
389 // ============================================================
390 // iterator access
391public:
393 zfiter iter(void) const {
394 return d ? d->iter() : zfiter();
395 }
396
398 zfiter iterFind(ZF_IN const T_Key &key) const {
399 return d ? d->iterFind(_KeyCreate(key)) : zfiter();
400 }
401
403 const T_Key &iterKey(ZF_IN const zfiter &it) const {
404 return ((const ImplKey *)d->iterKey(it))->v;
405 }
406
407 const T_Value &iterValue(ZF_IN const zfiter &it) const {
408 return ((const ImplValue *)d->iterValue(it))->v;
409 }
410
412 T_Value &iterValue(ZF_IN const zfiter &it) {
413 return ((ImplValue *)d->iterValue(it))->v;
414 }
415
417 ZF_IN_OUT zfiter &it
418 , ZF_IN const T_Value &value
419 ) {
420 if(d && it) {
421 d->iterValue(it, _ValueCreate(value));
422 }
423 }
424
426 if(d && it) {
427 d->iterRemove(it);
428 }
429 }
430
433 ZF_IN const T_Key &key
434 , ZF_IN const T_Value &value
435 ) {
436 _dInit();
437 return d->iterAdd(_KeyCreate(key), _ValueCreate(value));
438 }
439
440private:
441 zfclassNotPOD ImplKey : zfextend _ZFP_ZFCoreMap::BaseKey {
442 public:
443 T_Key v;
444 ImplKey(ZF_IN T_Key const &v) : v(v) {}
445 public:
446 virtual zfidentity implHash(void) const {return T_Hash()(v);}
447 virtual zfbool implEqual(ZF_IN const BaseKey *ref) const {return T_Equal()(v, ((ImplKey *)ref)->v);}
448 virtual void implInfo(ZF_IN_OUT zfstring &ret) const {return zftToStringT(ret, v);}
449 virtual BaseKey *implCopy(void) const {return zfpoolNew(ImplKey, v);}
450 virtual void implDestroy(void) {zfpoolDelete(this);}
451 };
452 zfclassNotPOD ImplValue : zfextend _ZFP_ZFCoreMap::BaseValue {
453 public:
454 T_Value v;
455 ImplValue(void) : v() {}
456 ImplValue(ZF_IN T_Value const &v) : v(v) {}
457 public:
458 virtual void implCopy(ZF_IN const BaseValue *ref) {v = ((ImplValue *)ref)->v;}
459 virtual zfbool implEqual(ZF_IN const BaseValue *ref) const {return ZFComparerDefault(v, ((ImplValue *)ref)->v) == ZFCompareEqual;}
460 virtual void implInfo(ZF_IN_OUT zfstring &ret) const {return zftToStringT(ret, v);}
461 virtual BaseValue *implCopy(void) const {return zfpoolNew(ImplValue, v);}
462 virtual void implDestroy(void) {zfpoolDelete(this);}
463 };
464 static _ZFP_ZFCoreMap::BaseKey *_KeyCreate(ZF_IN T_Key const &v) {
465 return zfpoolNew(ImplKey, v);
466 }
467 static _ZFP_ZFCoreMap::BaseValue *_ValueCreate(ZF_IN T_Value const &v) {
468 return zfpoolNew(ImplValue, v);
469 }
470 static _ZFP_ZFCoreMap::BaseValue *_ValueCreate(void) {
471 return zfpoolNew(ImplValue);
472 }
473
474private:
475 inline void _dInit(void) {
476 if(!d) {
477 d = _ZFP_ZFCoreMap::create();
478 }
479 }
480
481private:
482 _ZFP_ZFCoreMap *d;
483};
484ZFOUTPUT_TYPE_TEMPLATE(ZFM_EXPAND(typename T_Key, typename T_Value), ZFM_EXPAND(ZFCoreMap<T_Key, T_Value>), {v.objectInfoT(s);})
485
487
488#endif // #ifndef _ZFI_ZFCoreMap_h_
489
#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:184
#define ZF_IN_OPT
dummy macro that shows the param used as optional input
Definition ZFCoreTypeDef_ClassType.h:188
#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:200
_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 zfpoolNew
Definition ZFMemPool.h:38
#define zfpoolNew(T_Type,...)
internal use only, for allocating internal types for performance
Definition ZFMemPool.h:37
#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:348
core map type for private use only
Definition ZFCoreMap.h:101
void refDelete(void)
delete reference
Definition ZFCoreMap.h:150
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:298
void swap(ZFCoreMap< T_Key, T_Value > &ref)
swap internal data
Definition ZFCoreMap.h:222
void removeAll(void)
remove all content
Definition ZFCoreMap.h:383
ZFCompareResult objectCompareValue(ZFCoreMap< T_Key, T_Value > 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:332
zfiter iterAdd(const T_Key &key, const T_Value &value)
see zfiter
Definition ZFCoreMap.h:432
ZFCompareResult objectCompare(ZFCoreMap< T_Key, T_Value > const &ref) const
compare by instance
Definition ZFCoreMap.h:172
void iterRemove(zfiter &it)
see zfiter
Definition ZFCoreMap.h:425
void copyFrom(const ZFCoreMap< T_Key, T_Value > &ref)
copy all contents from ref, remove all before copy
Definition ZFCoreMap.h:234
ZFCoreMap(void)
construct an empty map
Definition ZFCoreMap.h:106
zfstring objectInfoOfContent(zfindex maxCount=((zfindex) -1), const ZFTokenForKeyValueContainer &token=_ZFP_ZFTokenForKeyValueContainerDefault) const
return contents info
Definition ZFCoreMap.h:209
ZFCoreArray< T_Key > allKey(void) const
return a copy of all keys
Definition ZFCoreMap.h:348
zfstring objectInfo(void) const
return object info
Definition ZFCoreMap.h:166
void refPrepare(void)
prepare instance to make it able to be shared between each copy
Definition ZFCoreMap.h:146
void remove(const T_Key &key)
remove or do nothing if not exist
Definition ZFCoreMap.h:374
const T_Value * get(const T_Key &key) const
get value or null if not exist
Definition ZFCoreMap.h:308
T_Value * get(const T_Key &key)
get value or null if not exist
Definition ZFCoreMap.h:320
void iterValue(zfiter &it, const T_Value &value)
see zfiter
Definition ZFCoreMap.h:416
ZFCoreMap(const ZFCoreMap< T_Key, T_Value > &ref)
retain the ref, to copy, use copyFrom
Definition ZFCoreMap.h:110
zfindex count(void) const
return count
Definition ZFCoreMap.h:257
zfiter iterFind(const T_Key &key) const
see zfiter
Definition ZFCoreMap.h:398
void addFrom(const ZFCoreMap< T_Key, T_Value > &ref)
add elements from ref
Definition ZFCoreMap.h:278
const T_Value & iterValue(const zfiter &it) const
see zfiter
Definition ZFCoreMap.h:407
void objectInfoT(zfstring &ret) const
see objectInfo
Definition ZFCoreMap.h:162
void allKeyT(ZFCoreArray< T_Key > &ret) const
see allKey
Definition ZFCoreMap.h:338
void allValueT(ZFCoreArray< T_Value > &ret) const
see allValue
Definition ZFCoreMap.h:355
void objectInfoOfContentT(zfstring &ret, zfindex maxCount=((zfindex) -1), const ZFTokenForKeyValueContainer &token=_ZFP_ZFTokenForKeyValueContainerDefault) const
see objectInfoOfContent
Definition ZFCoreMap.h:197
zfiter iter(void) const
see zfiter
Definition ZFCoreMap.h:393
T_Value & iterValue(const zfiter &it)
see zfiter
Definition ZFCoreMap.h:412
ZFCoreArray< T_Value > allValue(void) const
return a copy of all values
Definition ZFCoreMap.h:365
const T_Key & iterKey(const zfiter &it) const
see zfiter
Definition ZFCoreMap.h:403
zfbool isEmpty(void) const
true if empty
Definition ZFCoreMap.h:264
zfbool isContain(const T_Key &key) const
true if contains the key
Definition ZFCoreMap.h:271
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