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> >
82public:
86 ZFCoreOrderMap(void) : d(zfnull) {}
91 if(d) {
92 ++(d->refCount);
93 }
94 }
95
99 _ZFP_ZFCoreOrderMap *dTmp = d;
100 d = ref.d;
101 if(d) {
102 ++(d->refCount);
103 }
104 if(dTmp && (--(dTmp->refCount)) == 0) {
105 _ZFP_ZFCoreOrderMap::destroy(dTmp);
106 }
107 return *this;
108 }
109
112 zfbool operator == (ZF_IN const ZFCoreOrderMap<T_Key, T_Value> &ref) const {return d == ref.d;}
116 zfbool operator != (ZF_IN const ZFCoreOrderMap<T_Key, T_Value> &ref) const {return d != ref.d;}
117 ~ZFCoreOrderMap(void) {
118 if(d && (--(d->refCount)) == 0) {
119 _ZFP_ZFCoreOrderMap::destroy(d);
120 }
121 }
122
126 void refPrepare(void) {_dInit();}
130 void refDelete(void) {
131 if(d) {
132 _ZFP_ZFCoreOrderMap *dTmp = d;
133 d = zfnull;
134 if(--(dTmp->refCount) == 0) {
135 zfpoolDelete(dTmp);
136 }
137 }
138 }
139
140public:
142 void objectInfoT(ZF_IN_OUT zfstring &ret) const {
143 this->objectInfoOfContentT(ret, 5);
144 }
145
146 zfstring objectInfo(void) const {
147 zfstring ret;
148 this->objectInfoT(ret);
149 return ret;
150 }
151
155
157 if(d) {
158 if(ref.d) {
159 return d->objectCompareValue(ref.d);
160 }
161 else {
162 return d->count() == 0 ? ZFCompareEqual : ZFCompareUncomparable;
163 }
164 }
165 else {
166 if(ref.d) {
167 return ref.d->count() == 0 ? ZFCompareEqual : ZFCompareUncomparable;
168 }
169 else {
170 return ZFCompareEqual;
171 }
172 }
173 }
174
175public:
179 , ZF_IN_OPT zfindex maxCount = zfindexMax()
181 ) const {
182 ret += token.tokenLeft;
183 if(d) {
184 d->objectInfoOfContentT(ret, maxCount, token);
185 }
186 ret += token.tokenRight;
187 }
188
190 ZF_IN_OPT zfindex maxCount = zfindexMax()
192 ) const {
193 zfstring ret;
194 this->objectInfoOfContentT(ret, maxCount, token);
195 return ret;
196 }
197
198public:
203 if(d != ref.d) {
204 _ZFP_ZFCoreOrderMap *dTmp = d;
205 d = ref.d;
206 ref.d = dTmp;
207 }
208 }
209
210public:
215 if(d != ref.d) {
216 if(d) {
217 if(ref.d) {
218 d->copyFrom(ref.d);
219 }
220 else {
221 d->removeAll();
222 }
223 }
224 else {
225 if(ref.d) {
226 _dInit();
227 d->copyFrom(ref.d);
228 }
229 }
230 }
231 }
232
233public:
237 zfindex count(void) const {
238 return d ? d->count() : 0;
239 }
240
244 zfbool isEmpty(void) const {
245 return !d || d->isEmpty();
246 }
247
251 zfbool isContain(ZF_IN const T_Key &key) const {
252 return d && d->isContain(_KeyCreate(key));
253 }
254
259 if(d != ref.d) {
260 if(d) {
261 if(ref.d) {
262 d->addFrom(ref.d);
263 }
264 }
265 else {
266 if(ref.d) {
267 _dInit();
268 d->addFrom(ref.d);
269 }
270 }
271 }
272 }
273
278 void set(
279 ZF_IN const T_Key &key
280 , ZF_IN const T_Value &value
281 ) {
282 _dInit();
283 d->set(_KeyCreate(key), _ValueCreate(value));
284 }
285
288 const T_Value *get(ZF_IN const T_Key &key) const {
289 if(d) {
290 ImplValue *value = (ImplValue *)d->get(_KeyCreate(key));
291 if(value) {
292 return &(value->v);
293 }
294 }
295 return zfnull;
296 }
297
300 T_Value *get(ZF_IN const T_Key &key) {
301 if(d) {
302 ImplValue *value = (ImplValue *)d->get(_KeyCreate(key));
303 if(value) {
304 return &(value->v);
305 }
306 }
307 return zfnull;
308 }
309
312 T_Value &access(ZF_IN const T_Key &key) {
313 _dInit();
314 return ((ImplValue *)d->access(_KeyCreate(key), _ValueCreate))->v;
315 }
316
319 if(d) {
320 for(zfiter it = d->iter(); it; ++it) {
321 ret.add(((const ImplKey *)d->iterKey(it))->v);
322 }
323 }
324 }
325
328 inline ZFCoreArray<T_Key> allKey(void) const {
330 this->allKeyT(ret);
331 return ret;
332 }
333
336 if(d) {
337 for(zfiter it = d->iter(); it; ++it) {
338 ret.add(((const ImplValue *)d->iterValue(it))->v);
339 }
340 }
341 }
342
345 inline ZFCoreArray<T_Value> allValue(void) const {
347 this->allValueT(ret);
348 return ret;
349 }
350
354 void remove(ZF_IN const T_Key &key) {
355 if(d) {
356 d->remove(_KeyCreate(key));
357 }
358 }
359
363 void removeAll(void) {
364 if(d) {
365 d->removeAll();
366 }
367 }
368
369 // ============================================================
370 // iterator access
371public:
373 zfiter iter(void) const {
374 return d ? d->iter() : zfiter();
375 }
376
378 zfiter iterFind(ZF_IN const T_Key &key) const {
379 return d ? d->iterFind(_KeyCreate(key)) : zfiter();
380 }
381
383 const T_Key &iterKey(ZF_IN const zfiter &it) const {
384 return ((const ImplKey *)d->iterKey(it))->v;
385 }
386
387 const T_Value &iterValue(ZF_IN const zfiter &it) const {
388 return ((const ImplValue *)d->iterValue(it))->v;
389 }
390
392 T_Value &iterValue(ZF_IN const zfiter &it) {
393 return ((ImplValue *)d->iterValue(it))->v;
394 }
395
397 ZF_IN_OUT zfiter &it
398 , ZF_IN const T_Value &value
399 ) {
400 if(d && it) {
401 d->iterValue(it, _ValueCreate(value));
402 }
403 }
404
406 if(d && it) {
407 d->iterRemove(it);
408 }
409 }
410
413 ZF_IN const T_Key &key
414 , ZF_IN const T_Value &value
415 ) {
416 _dInit();
417 return d->iterAdd(_KeyCreate(key), _ValueCreate(value));
418 }
419
420 // ============================================================
421 // order map spec
422public:
427 void move(ZF_IN_OUT zfiter &from, ZF_IN const zfiter &to) {
428 if(d) {
429 d->move(from, to);
430 }
431 }
432
433private:
434 zfclassNotPOD ImplKey : zfextend _ZFP_ZFCoreOrderMap::BaseKey {
435 public:
436 T_Key v;
437 ImplKey(ZF_IN T_Key const &v) : v(v) {}
438 public:
439 virtual zfidentity implHash(void) const {return T_Hash()(v);}
440 virtual zfbool implEqual(ZF_IN const BaseKey *ref) const {return T_Equal()(v, ((ImplKey *)ref)->v);}
441 virtual void implInfo(ZF_IN_OUT zfstring &ret) const {return zftToStringT(ret, v);}
442 virtual BaseKey *implCopy(void) const {return zfpoolNew(ImplKey, v);}
443 virtual void implDestroy(void) {zfpoolDelete(this);}
444 };
445 zfclassNotPOD ImplValue : zfextend _ZFP_ZFCoreOrderMap::BaseValue {
446 public:
447 T_Value v;
448 ImplValue(void) : v() {}
449 ImplValue(ZF_IN T_Value const &v) : v(v) {}
450 public:
451 virtual void implCopy(ZF_IN const BaseValue *ref) {v = ((ImplValue *)ref)->v;}
452 virtual zfbool implEqual(ZF_IN const BaseValue *ref) const {return ZFComparerDefault(v, ((ImplValue *)ref)->v) == ZFCompareEqual;}
453 virtual void implInfo(ZF_IN_OUT zfstring &ret) const {return zftToStringT(ret, v);}
454 virtual BaseValue *implCopy(void) const {return zfpoolNew(ImplValue, v);}
455 virtual void implDestroy(void) {zfpoolDelete(this);}
456 };
457 static _ZFP_ZFCoreOrderMap::BaseKey *_KeyCreate(ZF_IN T_Key const &v) {
458 return zfpoolNew(ImplKey, v);
459 }
460 static _ZFP_ZFCoreOrderMap::BaseValue *_ValueCreate(ZF_IN T_Value const &v) {
461 return zfpoolNew(ImplValue, v);
462 }
463 static _ZFP_ZFCoreOrderMap::BaseValue *_ValueCreate(void) {
464 return zfpoolNew(ImplValue);
465 }
466
467private:
468 inline void _dInit(void) {
469 if(!d) {
470 d = _ZFP_ZFCoreOrderMap::create();
471 }
472 }
473
474private:
475 _ZFP_ZFCoreOrderMap *d;
476};
477ZFOUTPUT_TYPE_TEMPLATE(ZFM_EXPAND(typename T_Key, typename T_Value), ZFM_EXPAND(ZFCoreOrderMap<T_Key, T_Value>), {v.objectInfoT(s);})
478
480
481#endif // #ifndef _ZFI_ZFCoreOrderMap_h_
482
#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: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
#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
#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 ZFCoreOrderMap.h:81
T_Value & iterValue(const zfiter &it)
see zfiter
Definition ZFCoreOrderMap.h:392
ZFCoreOrderMap(const ZFCoreOrderMap< T_Key, T_Value > &ref)
retain the ref, to copy, use copyFrom
Definition ZFCoreOrderMap.h:90
void allKeyT(ZFCoreArray< T_Key > &ret) const
see allKey
Definition ZFCoreOrderMap.h:318
void objectInfoT(zfstring &ret) const
see objectInfo
Definition ZFCoreOrderMap.h:142
ZFCompareResult objectCompare(ZFCoreOrderMap< T_Key, T_Value > const &ref) const
compare by instance
Definition ZFCoreOrderMap.h:152
zfiter iterAdd(const T_Key &key, const T_Value &value)
see zfiter
Definition ZFCoreOrderMap.h:412
void refPrepare(void)
prepare instance to make it able to be shared between each copy
Definition ZFCoreOrderMap.h:126
zfiter iterFind(const T_Key &key) const
see zfiter
Definition ZFCoreOrderMap.h:378
zfstring objectInfoOfContent(zfindex maxCount=((zfindex) -1), const ZFTokenForKeyValueContainer &token=_ZFP_ZFTokenForKeyValueContainerDefault) const
return contents info
Definition ZFCoreOrderMap.h:189
void remove(const T_Key &key)
remove or do nothing if not exist
Definition ZFCoreOrderMap.h:354
T_Value * get(const T_Key &key)
get value or null if not exist
Definition ZFCoreOrderMap.h:300
zfiter iter(void) const
see zfiter
Definition ZFCoreOrderMap.h:373
const T_Value * get(const T_Key &key) const
get value or null if not exist
Definition ZFCoreOrderMap.h:288
void refDelete(void)
delete reference
Definition ZFCoreOrderMap.h:130
void allValueT(ZFCoreArray< T_Value > &ret) const
see allValue
Definition ZFCoreOrderMap.h:335
zfstring objectInfo(void) const
return object info
Definition ZFCoreOrderMap.h:146
zfbool isEmpty(void) const
true if empty
Definition ZFCoreOrderMap.h:244
const T_Value & iterValue(const zfiter &it) const
see zfiter
Definition ZFCoreOrderMap.h:387
zfindex count(void) const
return count
Definition ZFCoreOrderMap.h:237
void addFrom(const ZFCoreOrderMap< T_Key, T_Value > &ref)
add elements from ref
Definition ZFCoreOrderMap.h:258
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:278
void move(zfiter &from, const zfiter &to)
move from to position before to, or move to tail if to not valid
Definition ZFCoreOrderMap.h:427
T_Value & access(const T_Key &key)
access value or create if not exist
Definition ZFCoreOrderMap.h:312
void swap(ZFCoreOrderMap< T_Key, T_Value > &ref)
swap internal data
Definition ZFCoreOrderMap.h:202
zfbool isContain(const T_Key &key) const
true if contains the key
Definition ZFCoreOrderMap.h:251
void removeAll(void)
remove all content
Definition ZFCoreOrderMap.h:363
ZFCompareResult objectCompareValue(ZFCoreOrderMap< T_Key, T_Value > const &ref) const
compare by instance
Definition ZFCoreOrderMap.h:156
void iterValue(zfiter &it, const T_Value &value)
see zfiter
Definition ZFCoreOrderMap.h:396
void objectInfoOfContentT(zfstring &ret, zfindex maxCount=((zfindex) -1), const ZFTokenForKeyValueContainer &token=_ZFP_ZFTokenForKeyValueContainerDefault) const
see objectInfoOfContent
Definition ZFCoreOrderMap.h:177
ZFCoreArray< T_Key > allKey(void) const
return a copy of all keys
Definition ZFCoreOrderMap.h:328
void iterRemove(zfiter &it)
see zfiter
Definition ZFCoreOrderMap.h:405
ZFCoreArray< T_Value > allValue(void) const
return a copy of all values
Definition ZFCoreOrderMap.h:345
void copyFrom(const ZFCoreOrderMap< T_Key, T_Value > &ref)
copy all contents from ref, remove all before copy
Definition ZFCoreOrderMap.h:214
ZFCoreOrderMap(void)
construct an empty map
Definition ZFCoreOrderMap.h:86
const T_Key & iterKey(const zfiter &it) const
see zfiter
Definition ZFCoreOrderMap.h:383
util class to hold string tokens
Definition ZFToken.h:113
iterator for ZFFramework
Definition zfiter.h:40