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) {}
168 virtual void refPrepare(void) zfpurevirtual;
176 virtual void refDelete(void) {
177 zfpoolDelete(this);
178 }
179
182 virtual void *refImpl(void) const zfpurevirtual;
183
185 virtual void objectInfoT(ZF_IN_OUT zfstring &ret) const {
186 this->objectInfoOfContentT(ret, 10);
187 }
188
189 virtual zfstring objectInfo(void) const {
190 zfstring ret;
191 this->objectInfoT(ret);
192 return ret;
193 }
194
198 , ZF_IN_OPT zfindex maxCount = zfindexMax()
200 ) const zfpurevirtual;
203 ZF_IN_OPT zfindex maxCount = zfindexMax()
205 ) const {
206 zfstring ret;
207 this->objectInfoOfContentT(ret, maxCount, token);
208 return ret;
209 }
210
212 virtual ZFCoreArrayBase &operator = (ZF_IN const ZFCoreArrayBase &ref) zfpurevirtual;
213 virtual zfbool operator == (ZF_IN const ZFCoreArrayBase &ref) const zfpurevirtual;
214 virtual zfbool operator != (ZF_IN const ZFCoreArrayBase &ref) const zfpurevirtual;
216
217public:
223 virtual void capacity(ZF_IN zfindex newCapacity) zfpurevirtual;
229 virtual void capacityTrim(void) zfpurevirtual;
233 virtual zfindex capacity(void) const zfpurevirtual;
234
238 virtual void remove(ZF_IN zfindex index) zfpurevirtual;
242 virtual void remove(
243 ZF_IN zfindex index
249 virtual void removeFirst(void) {
250 this->remove(0);
251 }
252
255 virtual void removeLast(void) {
256 this->remove(this->count() - 1);
257 }
258
261 virtual void removeAll(void) {
262 if(!this->isEmpty()) {
263 this->remove(0, this->count());
264 }
265 }
266
269 virtual void move(
270 ZF_IN zfindex fromIndex
271 , ZF_IN zfindex toIndexOrIndexMax
276 virtual zfindex count(void) const zfpurevirtual;
280 virtual zfbool isEmpty(void) const {
281 return this->count() != 0;
282 }
283
286 virtual void sort(
287 ZF_IN_OPT zfindex start = 0
293 virtual void sortReversely(
294 ZF_IN_OPT zfindex start = 0
297
298public:
304 virtual zfindex genericFind(ZF_IN const void *e) zfpurevirtual;
310 virtual void genericAdd(ZF_IN const void *e) {
311 this->genericAdd(e, zfindexMax());
312 }
313
314 virtual void genericAdd(
315 ZF_IN const void *e
316 , ZF_IN zfindex index
321 virtual void genericSet(
322 ZF_IN zfindex index
323 , ZF_IN const void *e
326 virtual const void *genericGet(ZF_IN zfindex index) const zfpurevirtual;
327};
328ZFOUTPUT_TYPE(ZFCoreArrayBase, {v.objectInfoT(s);})
329
330// ============================================================
347template<typename T_Element>
349public:
353 typedef T_Element ValueType;
354
355public:
359 ZFCoreArray(void) : d(zfnull) {}
364 : d(ref.d)
365 {
366 if(d) {
367 ++(d->refCount);
368 }
369 }
370 virtual ~ZFCoreArray(void) {
371 if(d) {
372 --(d->refCount);
373 if(d->refCount == 0) {
374 zfpoolDelete(d);
375 }
376 }
377 }
379 virtual void refPrepare(void) {if(d == zfnull) {d = zfpoolNew(_ZFP_ZFCoreArrayPrivate<T_Element>);}}
381 virtual ZFCoreArrayBase *refNew(void) const {return zfpoolNew(ZFCoreArray<T_Element>, *this);}
383 virtual void *refImpl(void) const {return d;}
388 _ZFP_ZFCoreArrayPrivate<T_Element> *dTmp = d;
389 d = ref.d;
390 if(d) {
391 ++(d->refCount);
392 }
393 if(dTmp) {
394 --(dTmp->refCount);
395 if(dTmp->refCount == 0) {
396 zfpoolDelete(dTmp);
397 }
398 }
399 return *this;
400 }
401
402 zfbool operator == (ZF_IN const ZFCoreArray<T_Element> &ref) const {return (d == ref.d);}
403 zfbool operator != (ZF_IN const ZFCoreArray<T_Element> &ref) const {return (d != ref.d);}
405 virtual ZFCoreArrayBase &operator = (ZF_IN const ZFCoreArrayBase &ref) {
406 return this->operator = ((const ZFCoreArray<T_Element> &)ref);
407 }
408 virtual zfbool operator == (ZF_IN const ZFCoreArrayBase &ref) const {
409 return this->operator == ((const ZFCoreArray<T_Element> &)ref);
410 }
411 virtual zfbool operator != (ZF_IN const ZFCoreArrayBase &ref) const {
412 return this->operator == ((const ZFCoreArray<T_Element> &)ref);
413 }
415
416public:
421 _ZFP_ZFCoreArrayPrivate<T_Element> *dTmp = d;
422 d = ref.d;
423 ref.d = dTmp;
424 }
425
430 if(d != ref.d) {
431 if(d && d->buf) {
432 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(d->buf, d->buf + d->count);
433 d->count = 0;
434 }
435 if(ref.d) {
436 _capacityRequire(ref.count());
437 _ZFP_ZFCoreArrayW<T_Element>::objCreate(d->buf, d->buf + ref.count(), ref.arrayBuf());
438 d->count = (zfuint)ref.count();
439 }
440 }
441 }
442
445 zfindex objectRetainCount(void) const {return d ? d->refCount : 0;}
452
458 ) const {
459 if(d == ref.d) {
460 return ZFCompareEqual;
461 }
462 if(this->count() != ref.count()) {
464 }
465 for(zfindex i = this->count() - 1; i != zfindexMax(); --i) {
466 if(comparer(this->get(i), ref.get(i)) != ZFCompareEqual) {
468 }
469 }
470 return ZFCompareEqual;
471 }
472
473public:
477 , ZF_IN_OPT zfindex maxCount = zfindexMax()
479 ) const {
480 this->objectInfoOfContentT(ret, maxCount, token, zfnull);
481 }
484 ZF_IN_OPT zfindex maxCount = zfindexMax()
486 ) const {
487 zfstring ret;
488 this->objectInfoOfContentT(ret, maxCount, token, zfnull);
489 return ret;
490 }
491
495 , ZF_IN_OPT zfindex maxCount
496 , ZF_IN_OPT const ZFTokenForContainer &token
498 ) const {
499 zfindex count = 0;
500 ret += token.tokenLeft;
501 for(; count < this->count() && count < maxCount; ++count) {
502 if(count > 0) {
503 ret += token.tokenSeparator;
504 }
505 ret += token.tokenValueLeft;
506 if(infoGetter != zfnull) {
507 infoGetter(ret, this->get(count));
508 }
509 else {
510 zftToStringT(ret, this->get(count));
511 }
512 ret += token.tokenValueRight;
513 }
514 if(count < this->count()) {
515 if(count > 0) {
516 ret += token.tokenSeparator;
517 }
518 ret += token.tokenEtc;
519 }
520 ret += token.tokenRight;
521 }
522
524 ZF_IN_OPT zfindex maxCount
525 , ZF_IN_OPT const ZFTokenForContainer &token
527 ) const {
528 zfstring ret;
529 this->objectInfoOfContentT(ret, maxCount, token, infoGetter);
530 return ret;
531 }
532
533public:
535 virtual void capacity(ZF_IN zfindex newCapacity) {
536 _capacityRequire(newCapacity);
537 }
539 virtual void capacityTrim(void) {
540 zfindex capacity = this->count();
541 _capacityOptimize(capacity);
542 if(capacity != this->capacity()) {
543 _capacityDoChange(capacity);
544 }
545 }
547 virtual zfindex capacity(void) const {
548 return (zfindex)(d ? d->capacity : 0);
549 }
550
551public:
555 void add(ZF_IN T_Element const &e) {
556 _capacityRequire(this->count() + 1);
557 _ZFP_ZFCoreArrayW<T_Element>::objCreate(d->buf + d->count, d->buf + d->count + 1, &e);
558 ++(d->count);
559 }
560
563 void add(
564 ZF_IN T_Element const &e
565 , ZF_IN zfindex index
566 ) {
567 if(index == zfindexMax()) {
568 index = this->count();
569 }
570 else {
571 ZFCoreAssertIndexRange(index, this->count() + 1);
572 }
573 _capacityRequire(this->count() + 1);
574 _ZFP_ZFCoreArrayW<T_Element>::objCreate(d->buf + d->count, d->buf + d->count + 1);
575 T_Element *pos = d->buf + index;
576 _ZFP_ZFCoreArrayW<T_Element>::objMove(pos + 1, pos, this->count() - index);
577 ++(d->count);
578 *pos = e;
579 }
580
584 ZF_IN const T_Element *src
586 ) {
587 if(src == zfnull || count == 0) {
588 return;
589 }
590 if(d == zfnull || src < d->buf || src >= d->buf + d->capacity) {
591 _capacityRequire(this->count() + count);
592 _ZFP_ZFCoreArrayW<T_Element>::objCreate(d->buf + d->count, d->buf + d->count + count, src);
593 d->count += (zfuint)count;
594 }
595 else {
597 tmp.capacity(count);
598 tmp.addFrom(src, count);
599 this->addFrom(tmp.arrayBuf(), count);
600 }
601 }
602
607 if(d != ref.d) {
608 this->addFrom(ref.arrayBuf(), ref.count());
609 }
610 }
611
616 ZF_IN T_Element const &e
618 ) const {
619 if(d) {
620 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
621 if(comparer(*p, e) == ZFCompareEqual) {
622 return (p - d->buf);
623 }
624 }
625 }
626 return zfindexMax();
627 }
628
632 ZF_IN T_Element const &e
634 ) const {
635 if(d && d->buf) {
636 for(T_Element *p = d->buf + d->count - 1; p >= d->buf; --p) {
637 if(comparer(*p, e) == ZFCompareEqual) {
638 return (p - d->buf);
639 }
640 }
641 }
642 return zfindexMax();
643 }
644
647 template<typename T_Another>
649 ZF_IN T_Another const &e
651 ) const {
652 if(d) {
653 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
654 if(comparer(*p, e) == ZFCompareEqual) {
655 return (p - d->buf);
656 }
657 }
658 }
659 return zfindexMax();
660 }
661
664 template<typename T_Another>
666 ZF_IN T_Another const &e
668 ) const {
669 if(d && d->buf) {
670 for(T_Element *p = d->buf + d->count - 1; p >= d->buf; --p) {
671 if(comparer(*p, e) == ZFCompareEqual) {
672 return (p - d->buf);
673 }
674 }
675 }
676 return zfindexMax();
677 }
678
683 ZF_IN T_Element const &e
685 ) {
686 if(d) {
687 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
688 if(comparer(*p, e) == ZFCompareEqual) {
689 this->remove(p - d->buf);
690 return zftrue;
691 }
692 }
693 }
694 return zffalse;
695 }
696
699 template<typename T_Another>
701 ZF_IN T_Another const &e
703 ) {
704 if(d) {
705 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
706 if(comparer(*p, e) == ZFCompareEqual) {
707 this->remove(p - d->buf);
708 return zftrue;
709 }
710 }
711 }
712 return zffalse;
713 }
714
718 ZF_IN T_Element const &e
720 ) {
721 if(d && d->buf) {
722 for(T_Element *p = d->buf + d->count - 1; p >= d->buf; --p) {
723 if(comparer(*p, e) == ZFCompareEqual) {
724 this->remove(p - d->buf);
725 return zftrue;
726 }
727 }
728 }
729 return zffalse;
730 }
731
734 template<typename T_Another>
736 ZF_IN T_Another const &e
738 ) {
739 if(d && d->buf) {
740 for(T_Element *p = d->buf + d->count - 1; p >= d->buf; --p) {
741 if(comparer(*p, e) == ZFCompareEqual) {
742 this->remove(p - d->buf);
743 return zftrue;
744 }
745 }
746 }
747 return zffalse;
748 }
749
753 ZF_IN T_Element const &e
755 ) {
756 zfindex removedCount = 0;
757 if(d) {
758 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
759 if(comparer(*p, e) == ZFCompareEqual) {
760 ++removedCount;
761 this->remove(p - d->buf);
762 --p;
763 }
764 }
765 }
766 return removedCount;
767 }
768
771 template<typename T_Another>
773 ZF_IN T_Another const &e
775 ) {
776 zfindex removedCount = 0;
777 if(d) {
778 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
779 if(comparer(*p, e) == ZFCompareEqual) {
780 ++removedCount;
781 this->remove(p - d->buf);
782 --p;
783 }
784 }
785 }
786 return removedCount;
787 }
788
790 virtual void remove(ZF_IN zfindex index) {
791 ZFCoreAssertIndexRange(index, this->count());
792 _ZFP_ZFCoreArrayW<T_Element>::objMove(d->buf + index, d->buf + index + 1, this->count() - index - 1);
793 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(d->buf + d->count - 1, d->buf + d->count);
794 --(d->count);
795 }
797 virtual void remove(
798 ZF_IN zfindex index
800 ) {
801 ZFCoreAssertIndexRange(index, this->count());
802 if(count > this->count() - index) {
803 count = this->count() - index;
804 }
805 _ZFP_ZFCoreArrayW<T_Element>::objMove(d->buf + index, d->buf + index + count, this->count() - (index + count));
806 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(d->buf + d->count - count, d->buf + d->count);
807 d->count -= (zfuint)count;
808 }
809
812 T_Element removeAndGet(ZF_IN zfindex index) {
813 T_Element t = this->get(index);
814 this->remove(index);
815 return t;
816 }
817
821 T_Element removeFirstAndGet(void) {
822 T_Element t = this->getFirst();
823 this->removeFirst();
824 return t;
825 }
826
830 T_Element removeLastAndGet(void) {
831 T_Element t = this->getLast();
832 this->removeLast();
833 return t;
834 }
835
837 virtual void move(
838 ZF_IN zfindex fromIndex
839 , ZF_IN zfindex toIndexOrIndexMax
840 ) {
841 ZFCoreAssertIndexRange(fromIndex, this->count());
842 if(toIndexOrIndexMax == zfindexMax()) {
843 toIndexOrIndexMax = this->count() - 1;
844 }
845 else {
846 ZFCoreAssertIndexRange(toIndexOrIndexMax, this->count());
847 }
848 if(fromIndex == toIndexOrIndexMax) {
849 return;
850 }
851 T_Element t = d->buf[fromIndex];
852 if(fromIndex < toIndexOrIndexMax) {
853 _ZFP_ZFCoreArrayW<T_Element>::objMove(d->buf + fromIndex, d->buf + fromIndex + 1, toIndexOrIndexMax - fromIndex);
854 }
855 else {
856 _ZFP_ZFCoreArrayW<T_Element>::objMove(d->buf + toIndexOrIndexMax + 1, d->buf + toIndexOrIndexMax, fromIndex - toIndexOrIndexMax);
857 }
858 d->buf[toIndexOrIndexMax] = t;
859 }
860
861public:
865 void set(
866 ZF_IN zfindex index
867 , ZF_IN T_Element const &e
868 ) {
869 ZFCoreAssertIndexRange(index, this->count());
870 d->buf[index] = e;
871 }
872
873public:
877 T_Element &get(ZF_IN zfindex index) {
878 ZFCoreAssertIndexRange(index, this->count());
879 return d->buf[index];
880 }
881
884 T_Element const &get(ZF_IN zfindex index) const {
885 ZFCoreAssertIndexRange(index, this->count());
886 return d->buf[index];
887 }
888
891 T_Element &operator [] (ZF_IN zfindex index) {
892 ZFCoreAssertIndexRange(index, this->count());
893 return d->buf[index];
894 }
895
898 T_Element const &operator [] (ZF_IN zfindex index) const {
899 ZFCoreAssertIndexRange(index, this->count());
900 return d->buf[index];
901 }
902
905 T_Element const &getFirst(void) const {
906 ZFCoreAssertIndexRange(0, this->count());
907 return *(d->buf);
908 }
909
912 T_Element const &getLast(void) const {
913 ZFCoreAssertIndexRange(0, this->count());
914 return *(d->buf + d->count - 1);
915 }
916
923 T_Element *arrayBuf(void) {return d ? d->buf : zfnull;}
927 const T_Element *arrayBuf(void) const {return d ? d->buf : zfnull;}
928
930 virtual zfindex count(void) const {return (zfindex)(d ? d->count : 0);}
932 virtual zfbool isEmpty(void) const {return (d == zfnull || d->count == 0);}
935 ZF_IN T_Element const &e
937 ) const {
938 return this->find(e, comparer) != zfindexMax();
939 }
940
941public:
943 virtual void sort(
944 ZF_IN_OPT zfindex start = 0
946 ) {
947 this->sort(start, count, ZFComparerDefault);
948 }
950 virtual void sortReversely(
951 ZF_IN_OPT zfindex start = 0
953 ) {
955 }
956
959 void sort(
960 ZF_IN zfindex start
962 , ZF_IN typename ZFComparer<T_Element>::Comparer comparer
963 ) {
964 if(!this->isEmpty() && start + 1 < this->count() && count > 1) {
966 d->buf
967 , start
968 , (count > this->count() - start) ? (this->count() - 1) : (start + count - 1)
969 , comparer
970 );
971 }
972 }
973
977 ZF_IN zfindex start
979 , ZF_IN typename ZFComparer<T_Element>::Comparer comparer
980 ) {
981 if(!this->isEmpty() && start + 1 < this->count() && count > 1) {
983 d->buf
984 , start
985 , (count > this->count() - start) ? (this->count() - 1) : (start + count - 1)
986 , comparer
987 );
988 }
989 }
990
991public:
995 virtual void genericCopyFrom(ZF_IN const ZFCoreArrayBase &ref) {this->copyFrom((const ZFCoreArray<T_Element> &)ref);}
997 virtual zfindex genericFind(ZF_IN const void *e) {return this->find(*(const T_Element *)e);}
999 virtual zfindex genericFindReversely(ZF_IN const void *e) {return this->findReversely(*(const T_Element *)e);}
1001 virtual zfindex genericRemoveElementAll(ZF_IN const void *e) {return this->removeElementAll(*(const T_Element *)e);}
1003 virtual void genericAdd(
1004 ZF_IN const void *e
1005 , ZF_IN zfindex index
1006 ) {this->add(*(const T_Element *)e, index);}
1008 virtual void genericAddFrom(ZF_IN const ZFCoreArrayBase &ref) {this->addFrom((const ZFCoreArray<T_Element> &)ref);}
1010 virtual void genericSet(
1011 ZF_IN zfindex index
1012 , ZF_IN const void *e
1013 ) {this->set(index, *(const T_Element *)e);}
1015 virtual const void *genericGet(ZF_IN zfindex index) const {return &(this->get(index));}
1016
1017private:
1018 _ZFP_ZFCoreArrayPrivate<T_Element> *d;
1019private:
1020 inline void _capacityOptimize(ZF_IN_OUT zfindex &capacity) {
1021 _ZFP_ZFCoreArrayCapacity<
1022 sizeof(T_Element) <= sizeof(void *)
1023 ? 0
1024 : (sizeof(T_Element) <= 4 * sizeof(void *)
1025 ? 1
1026 : 2
1027 )
1028 >::capacityOptimize(capacity);
1029 }
1030 inline void _capacityRequire(ZF_IN zfindex capacity) {
1031 _capacityOptimize(capacity);
1032 if(capacity > this->capacity()) {
1033 _capacityDoChange(capacity);
1034 }
1035 }
1036 void _capacityDoChange(ZF_IN zfindex capacity) {
1037 if(capacity == 0) {
1038 if(d) {
1039 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(d->buf, d->buf + d->count);
1040 zffree(d->buf);
1041 d->buf = zfnull;
1042 d->capacity = 0;
1043 d->count = 0;
1044 }
1045 }
1046 else {
1047 if(d == zfnull) {
1048 d = zfpoolNew(_ZFP_ZFCoreArrayPrivate<T_Element>);
1049 }
1050
1051 T_Element *oldBuf = d->buf;
1052 zfuint oldCount = d->count;
1053
1054 T_Element *newBuf = (T_Element *)zfmalloc(capacity * sizeof(T_Element));
1055 _ZFP_ZFCoreArrayW<T_Element>::objCreate(newBuf, newBuf + oldCount, oldBuf);
1056
1057 d->buf = newBuf;
1058 d->capacity = (zfuint)capacity;
1059 d->count = oldCount;
1060
1061 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(oldBuf, oldBuf + oldCount);
1062 zffree(oldBuf);
1063 }
1064 }
1065};
1066ZFOUTPUT_TYPE_TEMPLATE(typename T_Element, ZFCoreArray<T_Element>, {v.objectInfoT(s);})
1067
1068template<typename T_Element>
1069zfclassLikePOD _ZFP_ZFCoreArrayCreate {
1070public:
1071 inline _ZFP_ZFCoreArrayCreate<T_Element> &add(ZF_IN T_Element const &v) {
1072 this->v.add(v);
1073 return *this;
1074 }
1075
1076public:
1078};
1079#define _ZFP_ZFCoreArrayCreate_action_expand(value) .add(value)
1080#define _ZFP_ZFCoreArrayCreate_action(CreatorType, values, ...) \
1081 CreatorType() ZFM_FIX_PARAM(_ZFP_ZFCoreArrayCreate_action_expand, ZFM_EMPTY, values, ##__VA_ARGS__) .v
1090#define ZFCoreArrayCreate(ElementType, values, ...) _ZFP_ZFCoreArrayCreate_action(_ZFP_ZFCoreArrayCreate<ElementType>, values, ##__VA_ARGS__)
1091
1093
1094#endif // #ifndef _ZFI_ZFCoreArray_h_
1095
common comparer for ZFFramework
#define ZFComparerDefault
default comparer for common types, see ZFComparer
Definition ZFComparer.h:262
#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:189
virtual ZFCoreArrayBase * refNew(void) const =0
new reference
virtual void refDelete(void)
delete reference
Definition ZFCoreArray.h:176
virtual zfindex genericFind(const void *e)=0
generic version
virtual void objectInfoT(zfstring &ret) const
see objectInfo
Definition ZFCoreArray.h:185
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:249
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:310
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:255
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 void refPrepare(void)=0
prepare instance to make it able to be shared between each copy
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:202
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:280
virtual void genericSet(zfindex index, const void *e)=0
generic version
virtual void removeAll(void)
remove all content
Definition ZFCoreArray.h:261
virtual zfindex capacity(void) const =0
get capacity
light weight array
Definition ZFCoreArray.h:348
virtual void capacityTrim(void)
trim current capacity
Definition ZFCoreArray.h:539
ZFCoreArray(void)
main constructor
Definition ZFCoreArray.h:359
virtual void capacity(zfindex newCapacity)
change capacity to hold at least newCapacity
Definition ZFCoreArray.h:535
virtual void genericAddFrom(const ZFCoreArrayBase &ref)
generic version
Definition ZFCoreArray.h:1008
virtual zfbool isEmpty(void) const
true if empty
Definition ZFCoreArray.h:932
virtual void refPrepare(void)
prepare instance to make it able to be shared between each copy
Definition ZFCoreArray.h:379
T_Element const & getLast(void) const
try to get first element, assert fail if empty
Definition ZFCoreArray.h:912
virtual void genericAdd(const void *e, zfindex index)
generic version
Definition ZFCoreArray.h:1003
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:772
T_Element & get(zfindex index)
get element's reference at index
Definition ZFCoreArray.h:877
zfindex objectRetainCount(void) const
get retain count
Definition ZFCoreArray.h:445
virtual void objectInfoOfContentT(zfstring &ret, zfindex maxCount=((zfindex) -1), const ZFTokenForContainer &token=_ZFP_ZFTokenForContainerDefault) const
return content info
Definition ZFCoreArray.h:475
void add(T_Element const &e)
add element
Definition ZFCoreArray.h:555
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:717
virtual zfstring objectInfoOfContent(zfindex maxCount=((zfindex) -1), const ZFTokenForContainer &token=_ZFP_ZFTokenForContainerDefault) const
return content info
Definition ZFCoreArray.h:483
void sortReversely(zfindex start, zfindex count, typename ZFComparer< T_Element >::Comparer comparer)
sort element
Definition ZFCoreArray.h:976
virtual zfbool isContain(T_Element const &e, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault) const
true if contains element
Definition ZFCoreArray.h:934
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:735
virtual void genericCopyFrom(const ZFCoreArrayBase &ref)
generic version
Definition ZFCoreArray.h:995
void add(T_Element const &e, zfindex index)
add element at index
Definition ZFCoreArray.h:563
zfindex findReversely(T_Another const &e, typename ZFComparer< T_Element, T_Another >::Comparer comparer) const
find element reversely
Definition ZFCoreArray.h:665
virtual void remove(zfindex index)
remove element at index with count, assert fail if out of range
Definition ZFCoreArray.h:790
T_Element const & get(zfindex index) const
get element's const reference at index
Definition ZFCoreArray.h:884
virtual zfindex genericFindReversely(const void *e)
generic version
Definition ZFCoreArray.h:999
zfindex find(T_Element const &e, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault) const
find element
Definition ZFCoreArray.h:615
void addFrom(const ZFCoreArray< T_Element > &ref)
add from another array
Definition ZFCoreArray.h:606
zfstring objectInfoOfContent(zfindex maxCount, const ZFTokenForContainer &token, typename ZFCoreInfoGetter< T_Element >::InfoGetter infoGetter) const
return content info
Definition ZFCoreArray.h:523
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:700
T_Element removeAndGet(zfindex index)
remove and return the removed value
Definition ZFCoreArray.h:812
T_Element removeLastAndGet(void)
remove last and return the removed value, or assert fail if empty
Definition ZFCoreArray.h:830
virtual zfindex capacity(void) const
get capacity
Definition ZFCoreArray.h:547
T_Element const & getFirst(void) const
try to get first element, assert fail if empty
Definition ZFCoreArray.h:905
virtual const void * genericGet(zfindex index) const
generic version
Definition ZFCoreArray.h:1015
virtual void move(zfindex fromIndex, zfindex toIndexOrIndexMax)
move element
Definition ZFCoreArray.h:837
ZFCoreArray(const ZFCoreArray< T_Element > &ref)
construct from another array
Definition ZFCoreArray.h:363
ZFCompareResult objectCompareValue(const ZFCoreArray< T_Element > &ref, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault) const
compare by content
Definition ZFCoreArray.h:455
void set(zfindex index, T_Element const &e)
set element at index, or assert fail if index out of range
Definition ZFCoreArray.h:865
T_Element removeFirstAndGet(void)
remove first and return the removed value, or assert fail if empty
Definition ZFCoreArray.h:821
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:752
void swap(ZFCoreArray< T_Element > &ref)
swap internal data
Definition ZFCoreArray.h:420
virtual void genericSet(zfindex index, const void *e)
generic version
Definition ZFCoreArray.h:1010
void objectInfoOfContentT(zfstring &ret, zfindex maxCount, const ZFTokenForContainer &token, typename ZFCoreInfoGetter< T_Element >::InfoGetter infoGetter) const
see objectInfoOfContent
Definition ZFCoreArray.h:493
virtual void remove(zfindex index, zfindex count)
remove element at index with count, assert fail if out of range
Definition ZFCoreArray.h:797
void sort(zfindex start, zfindex count, typename ZFComparer< T_Element >::Comparer comparer)
sort element
Definition ZFCoreArray.h:959
virtual zfindex genericFind(const void *e)
generic version
Definition ZFCoreArray.h:997
void addFrom(const T_Element *src, zfindex count)
add elements, src can be part of this array's buffer
Definition ZFCoreArray.h:583
T_Element * arrayBuf(void)
directly access the array
Definition ZFCoreArray.h:923
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:682
void copyFrom(const ZFCoreArray< T_Element > &ref)
copy all settings and contents from another array
Definition ZFCoreArray.h:429
const T_Element * arrayBuf(void) const
see arrayBuf
Definition ZFCoreArray.h:927
zfindex findReversely(T_Element const &e, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault) const
find element reversely
Definition ZFCoreArray.h:631
virtual void genericSwap(ZFCoreArrayBase &ref)
generic version
Definition ZFCoreArray.h:993
virtual void sort(zfindex start=0, zfindex count=((zfindex) -1))
sort
Definition ZFCoreArray.h:943
virtual ZFCoreArrayBase * refNew(void) const
new reference
Definition ZFCoreArray.h:381
ZFCompareResult objectCompare(const ZFCoreArray< T_Element > &ref) const
compare by instance
Definition ZFCoreArray.h:449
virtual zfindex count(void) const
element count of this array
Definition ZFCoreArray.h:930
virtual void sortReversely(zfindex start=0, zfindex count=((zfindex) -1))
sort reversely
Definition ZFCoreArray.h:950
T_Element ValueType
value type
Definition ZFCoreArray.h:353
virtual void * refImpl(void) const
get the impl
Definition ZFCoreArray.h:383
zfindex find(T_Another const &e, typename ZFComparer< T_Element, T_Another >::Comparer comparer) const
find element
Definition ZFCoreArray.h:648
virtual zfindex genericRemoveElementAll(const void *e)
generic version
Definition ZFCoreArray.h:1001
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