ZFFramework
Loading...
Searching...
No Matches
ZFCoreSet.h
Go to the documentation of this file.
1
5
6#ifndef _ZFI_ZFCoreSet_h_
7#define _ZFI_ZFCoreSet_h_
8
9#include "ZFCoreMap.h"
10
12
13zfclassNotPOD ZFLIB_ZFCore _ZFP_ZFCoreSet {
14public:
15 zfuint refCount;
16public:
17 static _ZFP_ZFCoreSet *create(void);
18 static void destroy(ZF_IN _ZFP_ZFCoreSet *d);
19 _ZFP_ZFCoreSet(void) : refCount(1) {}
20 virtual ~_ZFP_ZFCoreSet(void) {}
21public:
22 virtual void objectInfoOfContentT(
24 , ZF_IN zfindex maxCount
25 , ZF_IN const ZFTokenForContainer &token
27 virtual ZFCompareResult objectCompareValue(ZF_IN const _ZFP_ZFCoreSet *ref) zfpurevirtual;
28 virtual void copyFrom(ZF_IN_OUT _ZFP_ZFCoreSet *ref) zfpurevirtual;
29 virtual void addFrom(ZF_IN_OUT _ZFP_ZFCoreSet *ref) zfpurevirtual;
30 virtual void capacity(ZF_IN zfindex capacity) zfpurevirtual;
31 virtual zfindex count(void) zfpurevirtual;
32 virtual zfbool isEmpty(void) zfpurevirtual;
33 virtual zfbool isContain(ZF_IN _ZFP_ZFCoreMapKey *key) zfpurevirtual;
34 virtual void add(ZF_IN _ZFP_ZFCoreMapKey *key) zfpurevirtual;
35 virtual void remove(ZF_IN _ZFP_ZFCoreMapKey *key) zfpurevirtual;
36 virtual void removeAll(void) zfpurevirtual;
37public:
38 virtual zfiter iter(void) zfpurevirtual;
39 virtual zfiter iterFind(ZF_IN _ZFP_ZFCoreMapKey *key) zfpurevirtual;
40 virtual const _ZFP_ZFCoreMapKey *iterValue(ZF_IN const zfiter &it) zfpurevirtual;
41 virtual void iterRemove(ZF_IN_OUT zfiter &it) zfpurevirtual;
42 virtual zfiter iterAdd(ZF_IN _ZFP_ZFCoreMapKey *key) zfpurevirtual;
43};
50template<typename T_Key, typename T_Hash = ZFCoreMapKeyHash<T_Key>, typename T_Equal = ZFCoreMapKeyEqual<T_Key> >
52protected:
55public:
59 ZFCoreSet(void) : d(zfnull) {}
63 ZFCoreSet(ZF_IN const zfself &ref) : d(ref.d) {
64 if(d) {
65 ++(d->refCount);
66 }
67 }
68
71 ZFCoreSet &operator = (ZF_IN const zfself &ref) {
72 _ZFP_ZFCoreSet *dTmp = d;
73 d = ref.d;
74 if(d) {
75 ++(d->refCount);
76 }
77 if(dTmp && (--(dTmp->refCount)) == 0) {
78 _ZFP_ZFCoreSet::destroy(dTmp);
79 }
80 return *this;
81 }
82
85 zfbool operator == (ZF_IN const zfself &ref) const {return d == ref.d;}
89 zfbool operator != (ZF_IN const zfself &ref) const {return d != ref.d;}
90 ~ZFCoreSet(void) {
91 if(d && (--(d->refCount)) == 0) {
92 _ZFP_ZFCoreSet::destroy(d);
93 }
94 }
95
99 zfself &refPrepare(void) {_dInit(); return *this;}
103 void refDelete(void) {
104 if(d) {
105 _ZFP_ZFCoreSet *dTmp = d;
106 d = zfnull;
107 if(--(dTmp->refCount) == 0) {
108 zfpoolDelete(dTmp);
109 }
110 }
111 }
112
113public:
115 void objectInfoT(ZF_IN_OUT zfstring &ret) const {
116 this->objectInfoOfContentT(ret, 5);
117 }
118
119 zfstring objectInfo(void) const {
120 zfstring ret;
121 this->objectInfoT(ret);
122 return ret;
123 }
124
126 return d == ref.d ? ZFCompareEqual : ZFCompareUncomparable;
127 }
128
130 if(d) {
131 if(ref.d) {
132 return d->objectCompareValue(ref.d);
133 }
134 else {
135 return d->count() == 0 ? ZFCompareEqual : ZFCompareUncomparable;
136 }
137 }
138 else {
139 if(ref.d) {
140 return ref.d->count() == 0 ? ZFCompareEqual : ZFCompareUncomparable;
141 }
142 else {
143 return ZFCompareEqual;
144 }
145 }
146 }
147
148public:
152 , ZF_IN_OPT zfindex maxCount = zfindexMax()
154 ) const {
155 ret += token.tokenLeft;
156 if(d) {
157 d->objectInfoOfContentT(ret, maxCount, token);
158 }
159 ret += token.tokenRight;
160 }
161
163 ZF_IN_OPT zfindex maxCount = zfindexMax()
165 ) const {
166 zfstring ret;
167 this->objectInfoOfContentT(ret, maxCount, token);
168 return ret;
169 }
170
171public:
175 void swap(ZF_IN_OUT zfself &ref) {
176 if(d != ref.d) {
177 _ZFP_ZFCoreSet *dTmp = d;
178 d = ref.d;
179 ref.d = dTmp;
180 }
181 }
182
183public:
187 void copyFrom(ZF_IN const zfself &ref) {
188 if(d != ref.d) {
189 if(d) {
190 if(ref.d) {
191 d->copyFrom(ref.d);
192 }
193 else {
194 d->removeAll();
195 }
196 }
197 else {
198 if(ref.d) {
199 _dInit();
200 d->copyFrom(ref.d);
201 }
202 }
203 }
204 }
205
206public:
211 _dInit();
212 d->capacity(capacity);
213 }
214
218 zfindex count(void) const {
219 return d ? d->count() : 0;
220 }
221
225 zfbool isEmpty(void) const {
226 return !d || d->isEmpty();
227 }
228
232 zfbool isContain(ZF_IN const T_Key &key) const {
233 return d && d->isContain(_KeyCreate(key));
234 }
235
239 void addFrom(ZF_IN const zfself &ref) {
240 if(d != ref.d) {
241 if(d) {
242 if(ref.d) {
243 d->addFrom(ref.d);
244 }
245 }
246 else {
247 if(ref.d) {
248 _dInit();
249 d->addFrom(ref.d);
250 }
251 }
252 }
253 }
254
258 void add(ZF_IN const T_Key &key) {
259 _dInit();
260 d->add(_KeyCreate(key));
261 }
262
266 void remove(ZF_IN const T_Key &key) {
267 if(d) {
268 d->remove(_KeyCreate(key));
269 }
270 }
271
275 void removeAll(void) {
276 if(d) {
277 d->removeAll();
278 }
279 }
280
281 // ============================================================
282 // iterator access
283public:
285 zfiter iter(void) const {
286 return d ? d->iter() : zfiter();
287 }
288
290 zfiter iterFind(ZF_IN const T_Key &key) const {
291 return d ? d->iterFind(_KeyCreate(key)) : zfiter();
292 }
293
294 const T_Key &iterValue(ZF_IN const zfiter &it) const {
295 return ((const ImplKey *)d->iterValue(it))->v;
296 }
297
299 if(d && it) {
300 d->iterRemove(it);
301 }
302 }
303
305 zfiter iterAdd(ZF_IN const T_Key &key) {
306 _dInit();
307 return d->iterAdd(_KeyCreate(key));
308 }
309
310private:
311 zfclassNotPOD ImplKey : zfextend _ZFP_ZFCoreMapKey {
312 public:
313 T_Key v;
314 ImplKey(ZF_IN T_Key const &v) : v(v) {}
315 public:
316 virtual zfidentity implHash(void) const {return T_Hash()(v);}
317 virtual zfbool implEqual(ZF_IN const _ZFP_ZFCoreMapKey *ref) const {return T_Equal()(v, ((ImplKey *)ref)->v);}
318 virtual void implInfo(ZF_IN_OUT zfstring &ret) const {return zftToStringT(ret, v);}
319 virtual _ZFP_ZFCoreMapKey *implCopy(void) const {return zfpoolNew(ImplKey, v);}
320 virtual void implDestroy(void) {zfpoolDelete(this);}
321 };
322 static _ZFP_ZFCoreMapKey *_KeyCreate(ZF_IN T_Key const &v) {
323 return zfpoolNew(ImplKey, v);
324 }
325
326private:
327 inline void _dInit(void) {
328 if(!d) {
329 d = _ZFP_ZFCoreSet::create();
330 }
331 }
332
333private:
334 _ZFP_ZFCoreSet *d;
335};
336ZFOUTPUT_TYPE_TEMPLATE(ZFM_EXPAND(typename T_Key, typename T_Hash, typename T_Equal), ZFM_EXPAND(ZFCoreSet<T_Key, T_Hash, T_Equal>), {v.objectInfoT(s);})
337
339
340#endif // #ifndef _ZFI_ZFCoreSet_h_
341
#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 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 ZFTokenForContainerDefault()
see ZFTokenForContainer, modifyable, ZFTokenForContainerTrim by default
Definition ZFToken.h:107
core set type for private use only
Definition ZFCoreSet.h:51
void swap(zfself &ref)
swap internal data
Definition ZFCoreSet.h:175
void iterRemove(zfiter &it)
see zfiter
Definition ZFCoreSet.h:298
void objectInfoT(zfstring &ret) const
see objectInfo
Definition ZFCoreSet.h:115
zfindex count(void) const
return count
Definition ZFCoreSet.h:218
void objectInfoOfContentT(zfstring &ret, zfindex maxCount=((zfindex) -1), const ZFTokenForContainer &token=_ZFP_ZFTokenForContainerDefault) const
Definition ZFCoreSet.h:150
void removeAll(void)
remove all content
Definition ZFCoreSet.h:275
zfstring objectInfo(void) const
return object info
Definition ZFCoreSet.h:119
zfiter iterFind(const T_Key &key) const
see zfiter
Definition ZFCoreSet.h:290
zfbool isContain(const T_Key &key) const
true if contains the key
Definition ZFCoreSet.h:232
void refDelete(void)
delete reference
Definition ZFCoreSet.h:103
ZFCoreSet(const zfself &ref)
retain the ref, to copy, use copyFrom
Definition ZFCoreSet.h:63
ZFCompareResult objectCompare(zfself const &ref) const
compare by instance
Definition ZFCoreSet.h:125
void copyFrom(const zfself &ref)
copy all contents from ref, remove all before copy
Definition ZFCoreSet.h:187
zfbool isEmpty(void) const
true if empty
Definition ZFCoreSet.h:225
zfstring objectInfoOfContent(zfindex maxCount=((zfindex) -1), const ZFTokenForContainer &token=_ZFP_ZFTokenForContainerDefault) const
return contents info
Definition ZFCoreSet.h:162
void capacity(zfindex capacity)
change capacity
Definition ZFCoreSet.h:210
const T_Key & iterValue(const zfiter &it) const
see zfiter
Definition ZFCoreSet.h:294
ZFCoreSet< T_Key, T_Hash, T_Equal > zfself
typedef for self
Definition ZFCoreSet.h:54
zfself & refPrepare(void)
prepare instance to make it able to be shared between each copy
Definition ZFCoreSet.h:99
zfiter iter(void) const
see zfiter
Definition ZFCoreSet.h:285
void add(const T_Key &key)
add value
Definition ZFCoreSet.h:258
void remove(const T_Key &key)
remove or do nothing if not exist
Definition ZFCoreSet.h:266
zfiter iterAdd(const T_Key &key)
see zfiter
Definition ZFCoreSet.h:305
ZFCoreSet(void)
construct an empty set
Definition ZFCoreSet.h:59
ZFCompareResult objectCompareValue(zfself const &ref) const
compare by instance
Definition ZFCoreSet.h:129
void addFrom(const zfself &ref)
add elements from ref
Definition ZFCoreSet.h:239
util class to hold string tokens
Definition ZFToken.h:17
iterator for ZFFramework
Definition zfiter.h:40