ZFFramework
Loading...
Searching...
No Matches
ZFCoreArray.h
Go to the documentation of this file.
1
5
6#ifndef _ZFI_ZFCoreArray_h_
7#define _ZFI_ZFCoreArray_h_
8
10#include "ZFComparer.h"
11#include "ZFCoreUtilMath.h"
12#include "ZFToken.h"
13
15
16template<typename T_Element, bool isPOD = zftIsPOD<T_Element>::Value>
17zfclassNotPOD _ZFP_ZFCoreArrayW {
18public:
19 static void objCreate(
20 ZF_IN T_Element *p
21 , ZF_IN T_Element *pEnd
22 ) {
23 while(p != pEnd) {
24 zfnewPlacement(p, T_Element);
25 ++p;
26 }
27 }
28 static void objCreate(
29 ZF_IN T_Element *p
30 , ZF_IN T_Element *pEnd
31 , ZF_IN const T_Element *src
32 ) {
33 while(p != pEnd) {
34 zfnewPlacement(p, T_Element, *src);
35 ++p;
36 ++src;
37 }
38 }
39 static void objMove(
40 ZF_IN T_Element *dst
41 , ZF_IN const T_Element *src
42 , ZF_IN zfindex count
43 ) {
44 zfmemmoveObject(dst, src, count);
45 }
46 static void objDestroy(
47 ZF_IN T_Element *p
48 , ZF_IN T_Element *pEnd
49 ) {
50 while(p != pEnd) {
52 ++p;
53 }
54 }
55};
56template<typename T_Element>
57zfclassNotPOD _ZFP_ZFCoreArrayW<T_Element, true> {
58public:
59 static void objCreate(
60 ZF_IN T_Element *p
61 , ZF_IN T_Element *pEnd
62 ) {
63 }
64 static void objCreate(
65 ZF_IN T_Element *p
66 , ZF_IN T_Element *pEnd
67 , ZF_IN const T_Element *src
68 ) {
69 zfmemcpy(p, src, (pEnd - p) * sizeof(T_Element));
70 }
71 static void objMove(
72 ZF_IN T_Element *dst
73 , ZF_IN const T_Element *src
74 , ZF_IN zfindex count
75 ) {
76 zfmemmove(dst, src, count * sizeof(T_Element));
77 }
78 static void objDestroy(
79 ZF_IN T_Element *p
80 , ZF_IN T_Element *pEnd
81 ) {
82 }
83};
84
85template<typename T_Element>
86zffinal zfclassNotPOD _ZFP_ZFCoreArrayPrivate {
87public:
88 zfuint refCount;
89 T_Element *buf;
90 zfuint capacity;
91 zfuint count;
92public:
93 _ZFP_ZFCoreArrayPrivate(void)
94 : refCount(1)
95 , buf(zfnull)
96 , capacity(0)
97 , count(0)
98 {
99 }
100 ~_ZFP_ZFCoreArrayPrivate(void) {
101 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(this->buf, this->buf + this->count);
102 zffree(this->buf);
103 }
104};
105
106template<int sizeFix>
107zfclassNotPOD _ZFP_ZFCoreArrayCapacity {
108public:
109 // for size <= sizeof(void *)
110 static void capacityOptimize(ZF_IN_OUT zfindex &capacity) {
111 if(capacity == 0) {
112 }
113 else if(capacity < 64) {
114 capacity = ((capacity / 16) + 1) * 16;
115 }
116 else if(capacity < 256) {
117 capacity = ((capacity / 64) + 1) * 64;
118 }
119 else {
120 capacity = ((capacity / 256) + 1) * 256;
121 }
122 }
123};
124template<>
125zfclassNotPOD _ZFP_ZFCoreArrayCapacity<1> {
126public:
127 // for size <= 4 * sizeof(void *)
128 static void capacityOptimize(ZF_IN_OUT zfindex &capacity) {
129 if(capacity == 0) {
130 }
131 else if(capacity < 32) {
132 capacity = ((capacity / 8) + 1) * 8;
133 }
134 else if(capacity < 128) {
135 capacity = ((capacity / 32) + 1) * 32;
136 }
137 else {
138 capacity = ((capacity / 64) + 1) * 64;
139 }
140 }
141};
142template<>
143zfclassNotPOD _ZFP_ZFCoreArrayCapacity<2> {
144public:
145 // for size > 4 * sizeof(void *)
146 static void capacityOptimize(ZF_IN_OUT zfindex &capacity) {
147 if(capacity == 0) {
148 }
149 else if(capacity < 32) {
150 capacity = ((capacity / 4) + 1) * 4;
151 }
152 else {
153 capacity = ((capacity / 8) + 1) * 8;
154 }
155 }
156};
157
158// ============================================================
163public:
164 virtual ~ZFCoreArrayBase(void) {}
172 virtual void refDelete(void) {
173 zfpoolDelete(this);
174 }
175
178 virtual void *refImpl(void) const zfpurevirtual;
179
181 virtual void objectInfoT(ZF_IN_OUT zfstring &ret) const {
182 this->objectInfoOfContentT(ret, 10);
183 }
184
185 virtual zfstring objectInfo(void) const {
186 zfstring ret;
187 this->objectInfoT(ret);
188 return ret;
189 }
190
194 , ZF_IN_OPT zfindex maxCount = zfindexMax()
196 ) const zfpurevirtual;
199 ZF_IN_OPT zfindex maxCount = zfindexMax()
201 ) const {
202 zfstring ret;
203 this->objectInfoOfContentT(ret, maxCount, token);
204 return ret;
205 }
206
208 virtual ZFCoreArrayBase &operator = (ZF_IN const ZFCoreArrayBase &ref) zfpurevirtual;
209 virtual zfbool operator == (ZF_IN const ZFCoreArrayBase &ref) const zfpurevirtual;
210 virtual zfbool operator != (ZF_IN const ZFCoreArrayBase &ref) const zfpurevirtual;
212
213public:
219 virtual void capacity(ZF_IN zfindex newCapacity) zfpurevirtual;
225 virtual void capacityTrim(void) zfpurevirtual;
229 virtual zfindex capacity(void) const zfpurevirtual;
230
234 virtual void remove(ZF_IN zfindex index) zfpurevirtual;
238 virtual void remove(
239 ZF_IN zfindex index
245 virtual void removeFirst(void) {
246 this->remove(0);
247 }
248
251 virtual void removeLast(void) {
252 this->remove(this->count() - 1);
253 }
254
257 virtual void removeAll(void) {
258 if(!this->isEmpty()) {
259 this->remove(0, this->count());
260 }
261 }
262
265 virtual void move(
266 ZF_IN zfindex fromIndex
267 , ZF_IN zfindex toIndexOrIndexMax
272 virtual zfindex count(void) const zfpurevirtual;
276 virtual zfbool isEmpty(void) const {
277 return this->count() != 0;
278 }
279
282 virtual void sort(
283 ZF_IN_OPT zfindex start = 0
289 virtual void sortReversely(
290 ZF_IN_OPT zfindex start = 0
293
294public:
300 virtual zfindex genericFind(ZF_IN const void *e) zfpurevirtual;
306 virtual void genericAdd(ZF_IN const void *e) {
307 this->genericAdd(e, zfindexMax());
308 }
309
310 virtual void genericAdd(
311 ZF_IN const void *e
312 , ZF_IN zfindex index
317 virtual void genericSet(
318 ZF_IN zfindex index
319 , ZF_IN const void *e
322 virtual const void *genericGet(ZF_IN zfindex index) const zfpurevirtual;
323};
324ZFOUTPUT_TYPE(ZFCoreArrayBase, {v.objectInfoT(s);})
325
326// ============================================================
343template<typename T_Element>
345public:
349 ZFCoreArray(void) : d(zfnull) {}
354 : d(ref.d)
355 {
356 if(d) {
357 ++(d->refCount);
358 }
359 }
360 virtual ~ZFCoreArray(void) {
361 if(d) {
362 --(d->refCount);
363 if(d->refCount == 0) {
364 zfpoolDelete(d);
365 }
366 }
367 }
369 virtual ZFCoreArrayBase *refNew(void) const {return zfpoolNew(ZFCoreArray<T_Element>, *this);}
371 virtual void *refImpl(void) const {return d;}
376 _ZFP_ZFCoreArrayPrivate<T_Element> *dTmp = d;
377 d = ref.d;
378 if(d) {
379 ++(d->refCount);
380 }
381 if(dTmp) {
382 --(dTmp->refCount);
383 if(dTmp->refCount == 0) {
384 zfpoolDelete(dTmp);
385 }
386 }
387 return *this;
388 }
389
390 zfbool operator == (ZF_IN const ZFCoreArray<T_Element> &ref) const {return (d == ref.d);}
391 inline zfbool operator != (ZF_IN const ZFCoreArray<T_Element> &ref) const {return !this->operator == (ref);}
393 virtual ZFCoreArrayBase &operator = (ZF_IN const ZFCoreArrayBase &ref) {
394 return this->operator = ((const ZFCoreArray<T_Element> &)ref);
395 }
396 virtual zfbool operator == (ZF_IN const ZFCoreArrayBase &ref) const {
397 return this->operator == ((const ZFCoreArray<T_Element> &)ref);
398 }
399 virtual zfbool operator != (ZF_IN const ZFCoreArrayBase &ref) const {
400 return this->operator == ((const ZFCoreArray<T_Element> &)ref);
401 }
403
404public:
409 _ZFP_ZFCoreArrayPrivate<T_Element> *dTmp = d;
410 d = ref.d;
411 ref.d = dTmp;
412 }
413
418 if(d != ref.d) {
419 if(d && d->buf) {
420 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(d->buf, d->buf + d->count);
421 d->count = 0;
422 }
423 if(ref.d) {
424 _capacityRequire(ref.count());
425 _ZFP_ZFCoreArrayW<T_Element>::objCreate(d->buf, d->buf + ref.count(), ref.arrayBuf());
426 d->count = (zfuint)ref.count();
427 }
428 }
429 }
430
433 zfindex objectRetainCount(void) const {return d ? d->refCount : 0;}
440
446 ) const {
447 if(d == ref.d) {
448 return ZFCompareEqual;
449 }
450 if(this->count() != ref.count()) {
452 }
453 for(zfindex i = this->count() - 1; i != zfindexMax(); --i) {
454 if(comparer(this->get(i), ref.get(i)) != ZFCompareEqual) {
456 }
457 }
458 return ZFCompareEqual;
459 }
460
461public:
465 , ZF_IN_OPT zfindex maxCount = zfindexMax()
467 ) const {
468 this->objectInfoOfContentT(ret, maxCount, token, zfnull);
469 }
472 ZF_IN_OPT zfindex maxCount = zfindexMax()
474 ) const {
475 zfstring ret;
476 this->objectInfoOfContentT(ret, maxCount, token, zfnull);
477 return ret;
478 }
479
483 , ZF_IN_OPT zfindex maxCount
484 , ZF_IN_OPT const ZFTokenForContainer &token
486 ) const {
487 zfindex count = 0;
488 ret += token.tokenLeft;
489 for(; count < this->count() && count < maxCount; ++count) {
490 if(count > 0) {
491 ret += token.tokenSeparator;
492 }
493 ret += token.tokenValueLeft;
494 if(infoGetter != zfnull) {
495 infoGetter(ret, this->get(count));
496 }
497 else {
498 zftToStringT(ret, this->get(count));
499 }
500 ret += token.tokenValueRight;
501 }
502 if(count < this->count()) {
503 if(count > 0) {
504 ret += token.tokenSeparator;
505 }
506 ret += token.tokenEtc;
507 }
508 ret += token.tokenRight;
509 }
510
512 ZF_IN_OPT zfindex maxCount
513 , ZF_IN_OPT const ZFTokenForContainer &token
515 ) const {
516 zfstring ret;
517 this->objectInfoOfContentT(ret, maxCount, token, infoGetter);
518 return ret;
519 }
520
521public:
523 virtual void capacity(ZF_IN zfindex newCapacity) {
524 _capacityRequire(newCapacity);
525 }
527 virtual void capacityTrim(void) {
528 zfindex capacity = this->count();
529 _capacityOptimize(capacity);
530 if(capacity != this->capacity()) {
531 _capacityDoChange(capacity);
532 }
533 }
535 virtual zfindex capacity(void) const {
536 return (zfindex)(d ? d->capacity : 0);
537 }
538
539public:
543 void add(ZF_IN T_Element const &e) {
544 _capacityRequire(this->count() + 1);
545 _ZFP_ZFCoreArrayW<T_Element>::objCreate(d->buf + d->count, d->buf + d->count + 1, &e);
546 ++(d->count);
547 }
548
551 void add(
552 ZF_IN T_Element const &e
553 , ZF_IN zfindex index
554 ) {
555 if(index == zfindexMax()) {
556 index = this->count();
557 }
558 else {
559 ZFCoreAssertIndexRange(index, this->count() + 1);
560 }
561 _capacityRequire(this->count() + 1);
562 _ZFP_ZFCoreArrayW<T_Element>::objCreate(d->buf + d->count, d->buf + d->count + 1);
563 T_Element *pos = d->buf + index;
564 _ZFP_ZFCoreArrayW<T_Element>::objMove(pos + 1, pos, this->count() - index);
565 ++(d->count);
566 *pos = e;
567 }
568
572 ZF_IN const T_Element *src
574 ) {
575 if(src == zfnull || count == 0) {
576 return;
577 }
578 if(d == zfnull || src < d->buf || src >= d->buf + d->capacity) {
579 _capacityRequire(this->count() + count);
580 _ZFP_ZFCoreArrayW<T_Element>::objCreate(d->buf + d->count, d->buf + d->count + count, src);
581 d->count += (zfuint)count;
582 }
583 else {
585 tmp.capacity(count);
586 tmp.addFrom(src, count);
587 this->addFrom(tmp.arrayBuf(), count);
588 }
589 }
590
595 if(d != ref.d) {
596 this->addFrom(ref.arrayBuf(), ref.count());
597 }
598 }
599
604 ZF_IN T_Element const &e
606 ) const {
607 if(d) {
608 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
609 if(comparer(*p, e) == ZFCompareEqual) {
610 return (p - d->buf);
611 }
612 }
613 }
614 return zfindexMax();
615 }
616
620 ZF_IN T_Element const &e
622 ) const {
623 if(d && d->buf) {
624 for(T_Element *p = d->buf + d->count - 1; p >= d->buf; --p) {
625 if(comparer(*p, e) == ZFCompareEqual) {
626 return (p - d->buf);
627 }
628 }
629 }
630 return zfindexMax();
631 }
632
635 template<typename T_Another>
637 ZF_IN T_Another const &e
639 ) const {
640 if(d) {
641 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
642 if(comparer(*p, e) == ZFCompareEqual) {
643 return (p - d->buf);
644 }
645 }
646 }
647 return zfindexMax();
648 }
649
652 template<typename T_Another>
654 ZF_IN T_Another const &e
656 ) const {
657 if(d && d->buf) {
658 for(T_Element *p = d->buf + d->count - 1; p >= d->buf; --p) {
659 if(comparer(*p, e) == ZFCompareEqual) {
660 return (p - d->buf);
661 }
662 }
663 }
664 return zfindexMax();
665 }
666
671 ZF_IN T_Element const &e
673 ) {
674 if(d) {
675 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
676 if(comparer(*p, e) == ZFCompareEqual) {
677 this->remove(p - d->buf);
678 return zftrue;
679 }
680 }
681 }
682 return zffalse;
683 }
684
687 template<typename T_Another>
689 ZF_IN T_Another const &e
691 ) {
692 if(d) {
693 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
694 if(comparer(*p, e) == ZFCompareEqual) {
695 this->remove(p - d->buf);
696 return zftrue;
697 }
698 }
699 }
700 return zffalse;
701 }
702
706 ZF_IN T_Element const &e
708 ) {
709 if(d && d->buf) {
710 for(T_Element *p = d->buf + d->count - 1; p >= d->buf; --p) {
711 if(comparer(*p, e) == ZFCompareEqual) {
712 this->remove(p - d->buf);
713 return zftrue;
714 }
715 }
716 }
717 return zffalse;
718 }
719
722 template<typename T_Another>
724 ZF_IN T_Another const &e
726 ) {
727 if(d && d->buf) {
728 for(T_Element *p = d->buf + d->count - 1; p >= d->buf; --p) {
729 if(comparer(*p, e) == ZFCompareEqual) {
730 this->remove(p - d->buf);
731 return zftrue;
732 }
733 }
734 }
735 return zffalse;
736 }
737
741 ZF_IN T_Element const &e
743 ) {
744 zfindex removedCount = 0;
745 if(d) {
746 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
747 if(comparer(*p, e) == ZFCompareEqual) {
748 ++removedCount;
749 this->remove(p - d->buf);
750 --p;
751 }
752 }
753 }
754 return removedCount;
755 }
756
759 template<typename T_Another>
761 ZF_IN T_Another const &e
763 ) {
764 zfindex removedCount = 0;
765 if(d) {
766 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
767 if(comparer(*p, e) == ZFCompareEqual) {
768 ++removedCount;
769 this->remove(p - d->buf);
770 --p;
771 }
772 }
773 }
774 return removedCount;
775 }
776
778 virtual void remove(ZF_IN zfindex index) {
779 ZFCoreAssertIndexRange(index, this->count());
780 _ZFP_ZFCoreArrayW<T_Element>::objMove(d->buf + index, d->buf + index + 1, this->count() - index - 1);
781 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(d->buf + d->count - 1, d->buf + d->count);
782 --(d->count);
783 }
785 virtual void remove(
786 ZF_IN zfindex index
788 ) {
789 ZFCoreAssertIndexRange(index, this->count());
790 if(count > this->count() - index) {
791 count = this->count() - index;
792 }
793 _ZFP_ZFCoreArrayW<T_Element>::objMove(d->buf + index, d->buf + index + count, this->count() - (index + count));
794 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(d->buf + d->count - count, d->buf + d->count);
795 d->count -= (zfuint)count;
796 }
797
800 T_Element removeAndGet(ZF_IN zfindex index) {
801 T_Element t = this->get(index);
802 this->remove(index);
803 return t;
804 }
805
809 T_Element removeFirstAndGet(void) {
810 T_Element t = this->getFirst();
811 this->removeFirst();
812 return t;
813 }
814
818 T_Element removeLastAndGet(void) {
819 T_Element t = this->getLast();
820 this->removeLast();
821 return t;
822 }
823
825 virtual void move(
826 ZF_IN zfindex fromIndex
827 , ZF_IN zfindex toIndexOrIndexMax
828 ) {
829 ZFCoreAssertIndexRange(fromIndex, this->count());
830 if(toIndexOrIndexMax == zfindexMax()) {
831 toIndexOrIndexMax = this->count() - 1;
832 }
833 else {
834 ZFCoreAssertIndexRange(toIndexOrIndexMax, this->count());
835 }
836 if(fromIndex == toIndexOrIndexMax) {
837 return;
838 }
839 T_Element t = d->buf[fromIndex];
840 if(fromIndex < toIndexOrIndexMax) {
841 _ZFP_ZFCoreArrayW<T_Element>::objMove(d->buf + fromIndex, d->buf + fromIndex + 1, toIndexOrIndexMax - fromIndex);
842 }
843 else {
844 _ZFP_ZFCoreArrayW<T_Element>::objMove(d->buf + toIndexOrIndexMax + 1, d->buf + toIndexOrIndexMax, fromIndex - toIndexOrIndexMax);
845 }
846 d->buf[toIndexOrIndexMax] = t;
847 }
848
849public:
853 void set(
854 ZF_IN zfindex index
855 , ZF_IN T_Element const &e
856 ) {
857 ZFCoreAssertIndexRange(index, this->count());
858 d->buf[index] = e;
859 }
860
861public:
865 T_Element &get(ZF_IN zfindex index) {
866 ZFCoreAssertIndexRange(index, this->count());
867 return d->buf[index];
868 }
869
872 T_Element const &get(ZF_IN zfindex index) const {
873 ZFCoreAssertIndexRange(index, this->count());
874 return d->buf[index];
875 }
876
879 T_Element &operator [] (ZF_IN zfindex index) {
880 ZFCoreAssertIndexRange(index, this->count());
881 return d->buf[index];
882 }
883
886 T_Element const &operator [] (ZF_IN zfindex index) const {
887 ZFCoreAssertIndexRange(index, this->count());
888 return d->buf[index];
889 }
890
893 T_Element const &getFirst(void) const {
894 ZFCoreAssertIndexRange(0, this->count());
895 return *(d->buf);
896 }
897
900 T_Element const &getLast(void) const {
901 ZFCoreAssertIndexRange(0, this->count());
902 return *(d->buf + d->count - 1);
903 }
904
911 T_Element *arrayBuf(void) {return d ? d->buf : zfnull;}
915 const T_Element *arrayBuf(void) const {return d ? d->buf : zfnull;}
916
918 virtual zfindex count(void) const {return (zfindex)(d ? d->count : 0);}
920 virtual zfbool isEmpty(void) const {return (d == zfnull || d->count == 0);}
923 ZF_IN T_Element const &e
925 ) const {
926 return this->find(e, comparer) != zfindexMax();
927 }
928
929public:
931 virtual void sort(
932 ZF_IN_OPT zfindex start = 0
934 ) {
935 this->sort(start, count, ZFComparerDefault);
936 }
938 virtual void sortReversely(
939 ZF_IN_OPT zfindex start = 0
941 ) {
943 }
944
947 void sort(
948 ZF_IN zfindex start
950 , ZF_IN typename ZFComparer<T_Element>::Comparer comparer
951 ) {
952 if(!this->isEmpty() && start + 1 < this->count() && count > 1) {
954 d->buf
955 , start
956 , (count > this->count() - start) ? (this->count() - 1) : (start + count - 1)
957 , comparer
958 );
959 }
960 }
961
965 ZF_IN zfindex start
967 , ZF_IN typename ZFComparer<T_Element>::Comparer comparer
968 ) {
969 if(!this->isEmpty() && start + 1 < this->count() && count > 1) {
971 d->buf
972 , start
973 , (count > this->count() - start) ? (this->count() - 1) : (start + count - 1)
974 , comparer
975 );
976 }
977 }
978
979public:
983 virtual void genericCopyFrom(ZF_IN const ZFCoreArrayBase &ref) {this->copyFrom((const ZFCoreArray<T_Element> &)ref);}
985 virtual zfindex genericFind(ZF_IN const void *e) {return this->find(*(const T_Element *)e);}
987 virtual zfindex genericFindReversely(ZF_IN const void *e) {return this->findReversely(*(const T_Element *)e);}
989 virtual zfindex genericRemoveElementAll(ZF_IN const void *e) {return this->removeElementAll(*(const T_Element *)e);}
991 virtual void genericAdd(
992 ZF_IN const void *e
993 , ZF_IN zfindex index
994 ) {this->add(*(const T_Element *)e, index);}
996 virtual void genericAddFrom(ZF_IN const ZFCoreArrayBase &ref) {this->addFrom((const ZFCoreArray<T_Element> &)ref);}
998 virtual void genericSet(
999 ZF_IN zfindex index
1000 , ZF_IN const void *e
1001 ) {this->set(index, *(const T_Element *)e);}
1003 virtual const void *genericGet(ZF_IN zfindex index) const {return &(this->get(index));}
1004
1005private:
1006 _ZFP_ZFCoreArrayPrivate<T_Element> *d;
1007private:
1008 inline void _capacityOptimize(ZF_IN_OUT zfindex &capacity) {
1009 _ZFP_ZFCoreArrayCapacity<
1010 sizeof(T_Element) <= sizeof(void *)
1011 ? 0
1012 : (sizeof(T_Element) <= 4 * sizeof(void *)
1013 ? 1
1014 : 2
1015 )
1016 >::capacityOptimize(capacity);
1017 }
1018 inline void _capacityRequire(ZF_IN zfindex capacity) {
1019 _capacityOptimize(capacity);
1020 if(capacity > this->capacity()) {
1021 _capacityDoChange(capacity);
1022 }
1023 }
1024 void _capacityDoChange(ZF_IN zfindex capacity) {
1025 if(capacity == 0) {
1026 if(d) {
1027 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(d->buf, d->buf + d->count);
1028 zffree(d->buf);
1029 d->buf = zfnull;
1030 d->capacity = 0;
1031 d->count = 0;
1032 }
1033 }
1034 else {
1035 if(d == zfnull) {
1036 d = zfpoolNew(_ZFP_ZFCoreArrayPrivate<T_Element>);
1037 }
1038
1039 T_Element *oldBuf = d->buf;
1040 zfuint oldCount = d->count;
1041
1042 T_Element *newBuf = (T_Element *)zfmalloc(capacity * sizeof(T_Element));
1043 _ZFP_ZFCoreArrayW<T_Element>::objCreate(newBuf, newBuf + oldCount, oldBuf);
1044
1045 d->buf = newBuf;
1046 d->capacity = (zfuint)capacity;
1047 d->count = oldCount;
1048
1049 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(oldBuf, oldBuf + oldCount);
1050 zffree(oldBuf);
1051 }
1052 }
1053};
1054ZFOUTPUT_TYPE_TEMPLATE(typename T_Element, ZFCoreArray<T_Element>, {v.objectInfoT(s);})
1055
1056template<typename T_Element>
1057zfclassLikePOD _ZFP_ZFCoreArrayCreate {
1058public:
1059 inline _ZFP_ZFCoreArrayCreate<T_Element> &add(ZF_IN T_Element const &v) {
1060 this->v.add(v);
1061 return *this;
1062 }
1063
1064public:
1066};
1067#define _ZFP_ZFCoreArrayCreate_action_expand(value) .add(value)
1068#define _ZFP_ZFCoreArrayCreate_action(CreatorType, values, ...) \
1069 CreatorType() ZFM_FIX_PARAM(_ZFP_ZFCoreArrayCreate_action_expand, ZFM_EMPTY, values, ##__VA_ARGS__) .v
1078#define ZFCoreArrayCreate(ElementType, values, ...) _ZFP_ZFCoreArrayCreate_action(_ZFP_ZFCoreArrayCreate<ElementType>, values, ##__VA_ARGS__)
1079
1081
1082#endif // #ifndef _ZFI_ZFCoreArray_h_
1083
common comparer for ZFFramework
#define ZFComparerDefault
default comparer for common types, see ZFComparer
Definition ZFComparer.h:250
#define ZFLIB_ZFCore
used to export symbols
Definition ZFCoreEnvDef.h:30
common log messages for ZFFramework
#define ZFCoreAssertIndexRange(index, range)
log that likes "[file function (line)] index i out of range [0, n)"
Definition ZFCoreLog_CommonLog.h:116
#define zffinal
dummy macro shows that a method or class is designed must not to be overrided
Definition ZFCoreTypeDef_ClassType.h:63
#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 zfnewPlacement(buf, Type,...)
placement new defined for future use
Definition ZFCoreTypeDef_ClassType.h:113
#define zfpurevirtual
dummy macro shows that a method is pure virtual method
Definition ZFCoreTypeDef_ClassType.h:68
#define zfoverride
dummy macro shows that method override parent's method
Definition ZFCoreTypeDef_ClassType.h:58
#define zffree(ptr)
same as free defined for future use, do nothing if ptr is NULL
Definition ZFCoreTypeDef_ClassType.h:104
#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
void * zfmemmove(void *dst, const void *src, zfindex size)
wrapper to memmove
Definition ZFCoreTypeDef_ClassType.h:146
#define zfdeletePlacement(instance)
placement delete (instance->~Type()) defined for future use
Definition ZFCoreTypeDef_ClassType.h:116
#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
T_Element * zfmemmoveObject(T_Element *dst, const T_Element *src, zfindex count)
memmove for common object type, object must support operator =
Definition ZFCoreTypeDef_ClassType.h:156
void * zfmemcpy(void *dst, const void *src, zfindex size)
wrapper to memcpy
Definition ZFCoreTypeDef_ClassType.h:144
#define zfmalloc(size)
same as malloc defined for future use
Definition ZFCoreTypeDef_ClassType.h:90
_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
#define zftrue
bool true type
Definition ZFCoreTypeDef_CoreType.h:107
#define zfindexMax()
(zfindex)-1, indicate a max index value, see zfindex
Definition ZFCoreTypeDef_CoreType.h:159
#define zffalse
bool false type
Definition ZFCoreTypeDef_CoreType.h:111
#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(T_Type, outputAction)
declare your custom type conversion to string, convenient for debug
Definition ZFCoreTypeDef_OtherType.h:221
#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
math utilities for ZFFramework
zfbool zfmSortReversely(T_Holder &holder, zfindex left, zfindex right, T_Comparer const &comparer)
sort with custom comparer in range [left, right], holder must support operator []
Definition ZFCoreUtilMath.h:200
zfbool zfmSort(T_Holder &holder, zfindex left, zfindex right, T_Comparer const &comparer)
sort with custom comparer in range [left, right], holder must support operator []
Definition ZFCoreUtilMath.h:174
#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 ZFTokenForContainerDefault()
see ZFTokenForContainer, modifyable, ZFTokenForContainerTrim by default
Definition ZFToken.h:107
ZFCompareResult(* Comparer)(T_Comparable const &e0, T_Comparable2 const &e1)
see ZFComparer
Definition ZFCoreTypeDef_OtherType.h:130
dummy base for ZFCoreArray
Definition ZFCoreArray.h:162
virtual void objectInfoOfContentT(zfstring &ret, zfindex maxCount=((zfindex) -1), const ZFTokenForContainer &token=_ZFP_ZFTokenForContainerDefault) const =0
return content info
virtual zfstring objectInfo(void) const
return object info
Definition ZFCoreArray.h:185
virtual ZFCoreArrayBase * refNew(void) const =0
new reference
virtual void refDelete(void)
delete reference
Definition ZFCoreArray.h:172
virtual zfindex genericFind(const void *e)=0
generic version
virtual void objectInfoT(zfstring &ret) const
see objectInfo
Definition ZFCoreArray.h:181
virtual void genericCopyFrom(const ZFCoreArrayBase &ref)=0
generic version
virtual void capacityTrim(void)=0
trim current capacity
virtual void genericAdd(const void *e, zfindex index)=0
generic version
virtual void removeFirst(void)
remove first, assert fail if out of range
Definition ZFCoreArray.h:245
virtual void * refImpl(void) const =0
get the impl
virtual void sort(zfindex start=0, zfindex count=((zfindex) -1))=0
sort
virtual void capacity(zfindex newCapacity)=0
change capacity to hold at least newCapacity
virtual void genericSwap(ZFCoreArrayBase &ref)=0
generic version
virtual void genericAdd(const void *e)
generic version
Definition ZFCoreArray.h:306
virtual void sortReversely(zfindex start=0, zfindex count=((zfindex) -1))=0
sort reversely
virtual const void * genericGet(zfindex index) const =0
generic version
virtual zfindex genericRemoveElementAll(const void *e)=0
generic version
virtual void remove(zfindex index)=0
remove element at index with count, assert fail if out of range
virtual void removeLast(void)
remove last, assert fail if out of range
Definition ZFCoreArray.h:251
virtual zfindex genericFindReversely(const void *e)=0
generic version
virtual void genericAddFrom(const ZFCoreArrayBase &ref)=0
generic version
virtual void move(zfindex fromIndex, zfindex toIndexOrIndexMax)=0
move element
virtual zfindex count(void) const =0
element count of this array
virtual zfstring objectInfoOfContent(zfindex maxCount=((zfindex) -1), const ZFTokenForContainer &token=_ZFP_ZFTokenForContainerDefault) const
return content info
Definition ZFCoreArray.h:198
virtual void remove(zfindex index, zfindex count)=0
remove element at index with count, assert fail if out of range
virtual zfbool isEmpty(void) const
true if empty
Definition ZFCoreArray.h:276
virtual void genericSet(zfindex index, const void *e)=0
generic version
virtual void removeAll(void)
remove all content
Definition ZFCoreArray.h:257
virtual zfindex capacity(void) const =0
get capacity
light weight array
Definition ZFCoreArray.h:344
virtual void capacityTrim(void)
trim current capacity
Definition ZFCoreArray.h:527
ZFCoreArray(void)
main constructor
Definition ZFCoreArray.h:349
virtual void capacity(zfindex newCapacity)
change capacity to hold at least newCapacity
Definition ZFCoreArray.h:523
virtual void genericAddFrom(const ZFCoreArrayBase &ref)
generic version
Definition ZFCoreArray.h:996
virtual zfbool isEmpty(void) const
true if empty
Definition ZFCoreArray.h:920
T_Element const & getLast(void) const
try to get first element, assert fail if empty
Definition ZFCoreArray.h:900
virtual void genericAdd(const void *e, zfindex index)
generic version
Definition ZFCoreArray.h:991
zfindex removeElementAll(T_Another const &e, typename ZFComparer< T_Element, T_Another >::Comparer comparer)
remove all matched element, return number of removed element
Definition ZFCoreArray.h:760
T_Element & get(zfindex index)
get element's reference at index
Definition ZFCoreArray.h:865
zfindex objectRetainCount(void) const
get retain count
Definition ZFCoreArray.h:433
virtual void objectInfoOfContentT(zfstring &ret, zfindex maxCount=((zfindex) -1), const ZFTokenForContainer &token=_ZFP_ZFTokenForContainerDefault) const
return content info
Definition ZFCoreArray.h:463
void add(T_Element const &e)
add element
Definition ZFCoreArray.h:543
zfbool removeElementReversely(T_Element const &e, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault)
remove last matched element, return whether the element removed
Definition ZFCoreArray.h:705
virtual zfstring objectInfoOfContent(zfindex maxCount=((zfindex) -1), const ZFTokenForContainer &token=_ZFP_ZFTokenForContainerDefault) const
return content info
Definition ZFCoreArray.h:471
void sortReversely(zfindex start, zfindex count, typename ZFComparer< T_Element >::Comparer comparer)
sort element
Definition ZFCoreArray.h:964
virtual zfbool isContain(T_Element const &e, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault) const
true if contains element
Definition ZFCoreArray.h:922
zfbool removeElementReversely(T_Another const &e, typename ZFComparer< T_Element, T_Another >::Comparer comparer)
remove last matched element, return whether the element removed
Definition ZFCoreArray.h:723
virtual void genericCopyFrom(const ZFCoreArrayBase &ref)
generic version
Definition ZFCoreArray.h:983
void add(T_Element const &e, zfindex index)
add element at index
Definition ZFCoreArray.h:551
zfindex findReversely(T_Another const &e, typename ZFComparer< T_Element, T_Another >::Comparer comparer) const
find element reversely
Definition ZFCoreArray.h:653
virtual void remove(zfindex index)
remove element at index with count, assert fail if out of range
Definition ZFCoreArray.h:778
T_Element const & get(zfindex index) const
get element's const reference at index
Definition ZFCoreArray.h:872
virtual zfindex genericFindReversely(const void *e)
generic version
Definition ZFCoreArray.h:987
zfindex find(T_Element const &e, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault) const
find element
Definition ZFCoreArray.h:603
void addFrom(const ZFCoreArray< T_Element > &ref)
add from another array
Definition ZFCoreArray.h:594
zfstring objectInfoOfContent(zfindex maxCount, const ZFTokenForContainer &token, typename ZFCoreInfoGetter< T_Element >::InfoGetter infoGetter) const
return content info
Definition ZFCoreArray.h:511
zfbool removeElement(T_Another const &e, typename ZFComparer< T_Element, T_Another >::Comparer comparer)
remove first matched element, return whether the element removed
Definition ZFCoreArray.h:688
T_Element removeAndGet(zfindex index)
remove and return the removed value
Definition ZFCoreArray.h:800
T_Element removeLastAndGet(void)
remove last and return the removed value, or assert fail if empty
Definition ZFCoreArray.h:818
virtual zfindex capacity(void) const
get capacity
Definition ZFCoreArray.h:535
T_Element const & getFirst(void) const
try to get first element, assert fail if empty
Definition ZFCoreArray.h:893
virtual const void * genericGet(zfindex index) const
generic version
Definition ZFCoreArray.h:1003
virtual void move(zfindex fromIndex, zfindex toIndexOrIndexMax)
move element
Definition ZFCoreArray.h:825
ZFCoreArray(const ZFCoreArray< T_Element > &ref)
construct from another array
Definition ZFCoreArray.h:353
ZFCompareResult objectCompareValue(const ZFCoreArray< T_Element > &ref, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault) const
compare by content
Definition ZFCoreArray.h:443
void set(zfindex index, T_Element const &e)
set element at index, or assert fail if index out of range
Definition ZFCoreArray.h:853
T_Element removeFirstAndGet(void)
remove first and return the removed value, or assert fail if empty
Definition ZFCoreArray.h:809
zfindex removeElementAll(T_Element const &e, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault)
remove all matched element, return number of removed element
Definition ZFCoreArray.h:740
void swap(ZFCoreArray< T_Element > &ref)
swap internal data
Definition ZFCoreArray.h:408
virtual void genericSet(zfindex index, const void *e)
generic version
Definition ZFCoreArray.h:998
void objectInfoOfContentT(zfstring &ret, zfindex maxCount, const ZFTokenForContainer &token, typename ZFCoreInfoGetter< T_Element >::InfoGetter infoGetter) const
see objectInfoOfContent
Definition ZFCoreArray.h:481
virtual void remove(zfindex index, zfindex count)
remove element at index with count, assert fail if out of range
Definition ZFCoreArray.h:785
void sort(zfindex start, zfindex count, typename ZFComparer< T_Element >::Comparer comparer)
sort element
Definition ZFCoreArray.h:947
virtual zfindex genericFind(const void *e)
generic version
Definition ZFCoreArray.h:985
void addFrom(const T_Element *src, zfindex count)
add elements, src can be part of this array's buffer
Definition ZFCoreArray.h:571
T_Element * arrayBuf(void)
directly access the array
Definition ZFCoreArray.h:911
zfbool removeElement(T_Element const &e, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault)
remove first matched element, return whether the element removed
Definition ZFCoreArray.h:670
void copyFrom(const ZFCoreArray< T_Element > &ref)
copy all settings and contents from another array
Definition ZFCoreArray.h:417
const T_Element * arrayBuf(void) const
see arrayBuf
Definition ZFCoreArray.h:915
zfindex findReversely(T_Element const &e, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault) const
find element reversely
Definition ZFCoreArray.h:619
virtual void genericSwap(ZFCoreArrayBase &ref)
generic version
Definition ZFCoreArray.h:981
virtual void sort(zfindex start=0, zfindex count=((zfindex) -1))
sort
Definition ZFCoreArray.h:931
virtual ZFCoreArrayBase * refNew(void) const
new reference
Definition ZFCoreArray.h:369
ZFCompareResult objectCompare(const ZFCoreArray< T_Element > &ref) const
compare by instance
Definition ZFCoreArray.h:437
virtual zfindex count(void) const
element count of this array
Definition ZFCoreArray.h:918
virtual void sortReversely(zfindex start=0, zfindex count=((zfindex) -1))
sort reversely
Definition ZFCoreArray.h:938
virtual void * refImpl(void) const
get the impl
Definition ZFCoreArray.h:371
zfindex find(T_Another const &e, typename ZFComparer< T_Element, T_Another >::Comparer comparer) const
find element
Definition ZFCoreArray.h:636
virtual zfindex genericRemoveElementAll(const void *e)
generic version
Definition ZFCoreArray.h:989
void(* InfoGetter)(zfstring &ret, T_Type const &v)
proto type for obtain object info, see zftToStringT
Definition ZFCoreTypeDef_OtherType.h:151
util class to hold string tokens
Definition ZFToken.h:17