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:
15 typedef _ZFP_ZFCoreMapValue *(*Fn_ValueCreate)(void);
16public:
17 zfuint refCount;
18public:
19 static _ZFP_ZFCoreOrderMap *create(void);
20 static void destroy(ZF_IN _ZFP_ZFCoreOrderMap *d);
21 _ZFP_ZFCoreOrderMap(void) : refCount(1) {}
22 virtual ~_ZFP_ZFCoreOrderMap(void) {}
23public:
24 virtual void objectInfoOfContentT(
26 , ZF_IN zfindex maxCount
29 virtual ZFCompareResult objectCompareValue(ZF_IN const _ZFP_ZFCoreOrderMap *ref) zfpurevirtual;
30 virtual void copyFrom(ZF_IN_OUT _ZFP_ZFCoreOrderMap *ref) zfpurevirtual;
31 virtual void addFrom(ZF_IN_OUT _ZFP_ZFCoreOrderMap *ref) zfpurevirtual;
32 virtual void capacity(ZF_IN zfindex capacity) zfpurevirtual;
33 virtual zfindex count(void) zfpurevirtual;
34 virtual zfbool isEmpty(void) zfpurevirtual;
35 virtual zfbool isContain(ZF_IN _ZFP_ZFCoreMapKey *key) zfpurevirtual;
36 virtual void set(ZF_IN _ZFP_ZFCoreMapKey *key, ZF_IN _ZFP_ZFCoreMapValue *value) zfpurevirtual;
37 virtual _ZFP_ZFCoreMapValue *get(ZF_IN _ZFP_ZFCoreMapKey *key) zfpurevirtual;
38 virtual _ZFP_ZFCoreMapValue *access(ZF_IN _ZFP_ZFCoreMapKey *key, ZF_IN Fn_ValueCreate fn_ValueCreate) zfpurevirtual;
39 virtual void remove(ZF_IN _ZFP_ZFCoreMapKey *key) zfpurevirtual;
40 virtual void removeAll(void) zfpurevirtual;
41public:
42 virtual zfiter iter(void) zfpurevirtual;
43 virtual zfiter iterFind(ZF_IN _ZFP_ZFCoreMapKey *key) zfpurevirtual;
44 virtual const _ZFP_ZFCoreMapKey *iterKey(ZF_IN const zfiter &it) zfpurevirtual;
45 virtual _ZFP_ZFCoreMapValue *iterValue(ZF_IN const zfiter &it) zfpurevirtual;
46 virtual void iterValue(ZF_IN_OUT zfiter &it, ZF_IN _ZFP_ZFCoreMapValue *value) zfpurevirtual;
47 virtual void iterRemove(ZF_IN_OUT zfiter &it) zfpurevirtual;
48 virtual zfiter iterAdd(ZF_IN _ZFP_ZFCoreMapKey *key, ZF_IN _ZFP_ZFCoreMapValue *value) zfpurevirtual;
49 virtual zfbool iterAccess(ZF_OUT zfiter &it, ZF_IN _ZFP_ZFCoreMapKey *key, ZF_IN Fn_ValueCreate fn_ValueCreate) zfpurevirtual;
50public:
51 // ============================================================
52 // order map spec
53 virtual void move(ZF_IN_OUT zfiter &from, ZF_IN const zfiter &to) zfpurevirtual;
54};
61template<typename T_Key, typename T_Value, typename T_Hash = ZFCoreMapKeyHash<T_Key>, typename T_Equal = ZFCoreMapKeyEqual<T_Key> >
63protected:
66public:
70 ZFCoreOrderMap(void) : d(zfnull) {}
74 ZFCoreOrderMap(ZF_IN const zfself &ref) : d(ref.d) {
75 if(d) {
76 ++(d->refCount);
77 }
78 }
79
82 ZFCoreOrderMap &operator = (ZF_IN const zfself &ref) {
83 _ZFP_ZFCoreOrderMap *dTmp = d;
84 d = ref.d;
85 if(d) {
86 ++(d->refCount);
87 }
88 if(dTmp && (--(dTmp->refCount)) == 0) {
89 _ZFP_ZFCoreOrderMap::destroy(dTmp);
90 }
91 return *this;
92 }
93
96 zfbool operator == (ZF_IN const zfself &ref) const {return d == ref.d;}
100 zfbool operator != (ZF_IN const zfself &ref) const {return d != ref.d;}
101 ~ZFCoreOrderMap(void) {
102 if(d && (--(d->refCount)) == 0) {
103 _ZFP_ZFCoreOrderMap::destroy(d);
104 }
105 }
106
110 zfself &refPrepare(void) {_dInit(); return *this;}
114 void refDelete(void) {
115 if(d) {
116 _ZFP_ZFCoreOrderMap *dTmp = d;
117 d = zfnull;
118 if(--(dTmp->refCount) == 0) {
119 zfpoolDelete(dTmp);
120 }
121 }
122 }
123
124public:
126 void objectInfoT(ZF_IN_OUT zfstring &ret) const {
127 this->objectInfoOfContentT(ret, 5);
128 }
129
130 zfstring objectInfo(void) const {
131 zfstring ret;
132 this->objectInfoT(ret);
133 return ret;
134 }
135
137 return d == ref.d ? ZFCompareEqual : ZFCompareUncomparable;
138 }
139
141 if(d) {
142 if(ref.d) {
143 return d->objectCompareValue(ref.d);
144 }
145 else {
146 return d->count() == 0 ? ZFCompareEqual : ZFCompareUncomparable;
147 }
148 }
149 else {
150 if(ref.d) {
151 return ref.d->count() == 0 ? ZFCompareEqual : ZFCompareUncomparable;
152 }
153 else {
154 return ZFCompareEqual;
155 }
156 }
157 }
158
159public:
163 , ZF_IN_OPT zfindex maxCount = zfindexMax()
165 ) const {
166 ret += token.tokenLeft;
167 if(d) {
168 d->objectInfoOfContentT(ret, maxCount, token);
169 }
170 ret += token.tokenRight;
171 }
172
174 ZF_IN_OPT zfindex maxCount = zfindexMax()
176 ) const {
177 zfstring ret;
178 this->objectInfoOfContentT(ret, maxCount, token);
179 return ret;
180 }
181
182public:
186 void swap(ZF_IN_OUT zfself &ref) {
187 if(d != ref.d) {
188 _ZFP_ZFCoreOrderMap *dTmp = d;
189 d = ref.d;
190 ref.d = dTmp;
191 }
192 }
193
194public:
198 void copyFrom(ZF_IN const zfself &ref) {
199 if(d != ref.d) {
200 if(d) {
201 if(ref.d) {
202 d->copyFrom(ref.d);
203 }
204 else {
205 d->removeAll();
206 }
207 }
208 else {
209 if(ref.d) {
210 _dInit();
211 d->copyFrom(ref.d);
212 }
213 }
214 }
215 }
216
217public:
222 _dInit();
223 d->capacity(capacity);
224 }
225
229 zfindex count(void) const {
230 return d ? d->count() : 0;
231 }
232
236 zfbool isEmpty(void) const {
237 return !d || d->isEmpty();
238 }
239
243 zfbool isContain(ZF_IN const T_Key &key) const {
244 return d && d->isContain(_KeyCreate(key));
245 }
246
250 void addFrom(ZF_IN const zfself &ref) {
251 if(d != ref.d) {
252 if(d) {
253 if(ref.d) {
254 d->addFrom(ref.d);
255 }
256 }
257 else {
258 if(ref.d) {
259 _dInit();
260 d->addFrom(ref.d);
261 }
262 }
263 }
264 }
265
270 void set(
271 ZF_IN const T_Key &key
272 , ZF_IN const T_Value &value
273 ) {
274 _dInit();
275 d->set(_KeyCreate(key), _ValueCreate(value));
276 }
277
280 const T_Value *get(ZF_IN const T_Key &key) const {
281 if(d) {
282 ImplValue *value = (ImplValue *)d->get(_KeyCreate(key));
283 if(value) {
284 return &(value->v);
285 }
286 }
287 return zfnull;
288 }
289
292 T_Value *get(ZF_IN const T_Key &key) {
293 if(d) {
294 ImplValue *value = (ImplValue *)d->get(_KeyCreate(key));
295 if(value) {
296 return &(value->v);
297 }
298 }
299 return zfnull;
300 }
301
304 T_Value &access(ZF_IN const T_Key &key) {
305 _dInit();
306 return ((ImplValue *)d->access(_KeyCreate(key), _ValueCreate))->v;
307 }
308
311 if(d) {
312 for(zfiter it = d->iter(); it; ++it) {
313 ret.add(((const ImplKey *)d->iterKey(it))->v);
314 }
315 }
316 }
317
320 inline ZFCoreArray<T_Key> allKey(void) const {
322 this->allKeyT(ret);
323 return ret;
324 }
325
328 if(d) {
329 for(zfiter it = d->iter(); it; ++it) {
330 ret.add(((const ImplValue *)d->iterValue(it))->v);
331 }
332 }
333 }
334
337 inline ZFCoreArray<T_Value> allValue(void) const {
339 this->allValueT(ret);
340 return ret;
341 }
342
346 void remove(ZF_IN const T_Key &key) {
347 if(d) {
348 d->remove(_KeyCreate(key));
349 }
350 }
351
355 void removeAll(void) {
356 if(d) {
357 d->removeAll();
358 }
359 }
360
361 // ============================================================
362 // iterator access
363public:
365 zfiter iter(void) const {
366 return d ? d->iter() : zfiter();
367 }
368
370 zfiter iterFind(ZF_IN const T_Key &key) const {
371 return d ? d->iterFind(_KeyCreate(key)) : zfiter();
372 }
373
375 const T_Key &iterKey(ZF_IN const zfiter &it) const {
376 return ((const ImplKey *)d->iterKey(it))->v;
377 }
378
379 const T_Value &iterValue(ZF_IN const zfiter &it) const {
380 return ((const ImplValue *)d->iterValue(it))->v;
381 }
382
384 T_Value &iterValue(ZF_IN const zfiter &it) {
385 return ((ImplValue *)d->iterValue(it))->v;
386 }
387
389 ZF_IN_OUT zfiter &it
390 , ZF_IN const T_Value &value
391 ) {
392 if(d && it) {
393 d->iterValue(it, _ValueCreate(value));
394 }
395 }
396
398 if(d && it) {
399 d->iterRemove(it);
400 }
401 }
402
405 ZF_IN const T_Key &key
406 , ZF_IN const T_Value &value
407 ) {
408 _dInit();
409 return d->iterAdd(_KeyCreate(key), _ValueCreate(value));
410 }
411
412 zfbool iterAccess(ZF_OUT zfiter &it, ZF_IN const T_Key &key) {
413 _dInit();
414 return d->iterAccess(it, _KeyCreate(key), _ValueCreate);
415 }
416
417 // ============================================================
418 // order map spec
419public:
424 void move(ZF_IN_OUT zfiter &from, ZF_IN const zfiter &to) {
425 if(d) {
426 d->move(from, to);
427 }
428 }
429
430private:
431 zfclassNotPOD ImplKey : zfextend _ZFP_ZFCoreMapKey {
432 public:
433 T_Key v;
434 ImplKey(ZF_IN T_Key const &v) : v(v) {}
435 public:
436 virtual zfidentity implHash(void) const {return T_Hash()(v);}
437 virtual zfbool implEqual(ZF_IN const _ZFP_ZFCoreMapKey *ref) const {return T_Equal()(v, ((ImplKey *)ref)->v);}
438 virtual void implInfo(ZF_IN_OUT zfstring &ret) const {return zftToStringT(ret, v);}
439 virtual _ZFP_ZFCoreMapKey *implCopy(void) const {return zfpoolNew(ImplKey, v);}
440 virtual void implDestroy(void) {zfpoolDelete(this);}
441 };
442 zfclassNotPOD ImplValue : zfextend _ZFP_ZFCoreMapValue {
443 public:
444 T_Value v;
445 ImplValue(void) : v() {}
446 ImplValue(ZF_IN T_Value const &v) : v(v) {}
447 public:
448 virtual void implCopy(ZF_IN const _ZFP_ZFCoreMapValue *ref) {v = ((ImplValue *)ref)->v;}
449 virtual zfbool implEqual(ZF_IN const _ZFP_ZFCoreMapValue *ref) const {return ZFComparerDefault(v, ((ImplValue *)ref)->v) == ZFCompareEqual;}
450 virtual void implInfo(ZF_IN_OUT zfstring &ret) const {return zftToStringT(ret, v);}
451 virtual _ZFP_ZFCoreMapValue *implCopy(void) const {return zfpoolNew(ImplValue, v);}
452 virtual void implDestroy(void) {zfpoolDelete(this);}
453 };
454 static _ZFP_ZFCoreMapKey *_KeyCreate(ZF_IN T_Key const &v) {
455 return zfpoolNew(ImplKey, v);
456 }
457 static _ZFP_ZFCoreMapValue *_ValueCreate(ZF_IN T_Value const &v) {
458 return zfpoolNew(ImplValue, v);
459 }
460 static _ZFP_ZFCoreMapValue *_ValueCreate(void) {
461 return zfpoolNew(ImplValue);
462 }
463
464private:
465 inline void _dInit(void) {
466 if(!d) {
467 d = _ZFP_ZFCoreOrderMap::create();
468 }
469 }
470
471private:
472 _ZFP_ZFCoreOrderMap *d;
473};
474ZFOUTPUT_TYPE_TEMPLATE(ZFM_EXPAND(typename T_Key, typename T_Value, typename T_Hash, typename T_Equal), ZFM_EXPAND(ZFCoreOrderMap<T_Key, T_Value, T_Hash, T_Equal>), {v.objectInfoT(s);})
475
477
478#endif // #ifndef _ZFI_ZFCoreOrderMap_h_
479
#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:198
#define ZF_IN_OPT
dummy macro that shows the param used as optional input
Definition ZFCoreTypeDef_ClassType.h:202
#define ZF_OUT
dummy macro that shows the param used as required output
Definition ZFCoreTypeDef_ClassType.h:206
#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:214
_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: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
#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 ZFCoreOrderMap.h:62
T_Value & iterValue(const zfiter &it)
see zfiter
Definition ZFCoreOrderMap.h:384
void allKeyT(ZFCoreArray< T_Key > &ret) const
see allKey
Definition ZFCoreOrderMap.h:310
void objectInfoT(zfstring &ret) const
see objectInfo
Definition ZFCoreOrderMap.h:126
zfiter iterAdd(const T_Key &key, const T_Value &value)
see zfiter
Definition ZFCoreOrderMap.h:404
void capacity(zfindex capacity)
change capacity
Definition ZFCoreOrderMap.h:221
zfiter iterFind(const T_Key &key) const
see zfiter
Definition ZFCoreOrderMap.h:370
zfstring objectInfoOfContent(zfindex maxCount=((zfindex) -1), const ZFTokenForKeyValueContainer &token=_ZFP_ZFTokenForKeyValueContainerDefault) const
return contents info
Definition ZFCoreOrderMap.h:173
void remove(const T_Key &key)
remove or do nothing if not exist
Definition ZFCoreOrderMap.h:346
T_Value * get(const T_Key &key)
get value or null if not exist
Definition ZFCoreOrderMap.h:292
zfiter iter(void) const
see zfiter
Definition ZFCoreOrderMap.h:365
const T_Value * get(const T_Key &key) const
get value or null if not exist
Definition ZFCoreOrderMap.h:280
void refDelete(void)
delete reference
Definition ZFCoreOrderMap.h:114
void allValueT(ZFCoreArray< T_Value > &ret) const
see allValue
Definition ZFCoreOrderMap.h:327
zfstring objectInfo(void) const
return object info
Definition ZFCoreOrderMap.h:130
zfself & refPrepare(void)
prepare instance to make it able to be shared between each copy
Definition ZFCoreOrderMap.h:110
zfbool isEmpty(void) const
true if empty
Definition ZFCoreOrderMap.h:236
const T_Value & iterValue(const zfiter &it) const
see zfiter
Definition ZFCoreOrderMap.h:379
zfindex count(void) const
return count
Definition ZFCoreOrderMap.h:229
void addFrom(const zfself &ref)
add elements from ref
Definition ZFCoreOrderMap.h:250
ZFCompareResult objectCompare(zfself const &ref) const
compare by instance
Definition ZFCoreOrderMap.h:136
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:270
void copyFrom(const zfself &ref)
copy all contents from ref, remove all before copy
Definition ZFCoreOrderMap.h:198
void move(zfiter &from, const zfiter &to)
move from to position before to, or move to tail if to not valid
Definition ZFCoreOrderMap.h:424
T_Value & access(const T_Key &key)
access value or create if not exist
Definition ZFCoreOrderMap.h:304
zfbool isContain(const T_Key &key) const
true if contains the key
Definition ZFCoreOrderMap.h:243
void removeAll(void)
remove all content
Definition ZFCoreOrderMap.h:355
zfbool iterAccess(zfiter &it, const T_Key &key)
return true if already exist, or false and insert <key, defValue> if not exist
Definition ZFCoreOrderMap.h:412
void iterValue(zfiter &it, const T_Value &value)
see zfiter
Definition ZFCoreOrderMap.h:388
void objectInfoOfContentT(zfstring &ret, zfindex maxCount=((zfindex) -1), const ZFTokenForKeyValueContainer &token=_ZFP_ZFTokenForKeyValueContainerDefault) const
Definition ZFCoreOrderMap.h:161
ZFCompareResult objectCompareValue(zfself const &ref) const
compare by instance
Definition ZFCoreOrderMap.h:140
ZFCoreOrderMap(const zfself &ref)
retain the ref, to copy, use copyFrom
Definition ZFCoreOrderMap.h:74
ZFCoreArray< T_Key > allKey(void) const
return a copy of all keys
Definition ZFCoreOrderMap.h:320
void iterRemove(zfiter &it)
see zfiter
Definition ZFCoreOrderMap.h:397
ZFCoreArray< T_Value > allValue(void) const
return a copy of all values
Definition ZFCoreOrderMap.h:337
void swap(zfself &ref)
swap internal data
Definition ZFCoreOrderMap.h:186
ZFCoreOrderMap(void)
construct an empty map
Definition ZFCoreOrderMap.h:70
ZFCoreOrderMap< T_Key, T_Value, T_Hash, T_Equal > zfself
typedef for self
Definition ZFCoreOrderMap.h:65
const T_Key & iterKey(const zfiter &it) const
see zfiter
Definition ZFCoreOrderMap.h:375
util class to hold string tokens
Definition ZFToken.h:113
iterator for ZFFramework
Definition zfiter.h:40