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 zfpurevirtual;
202
204 virtual ZFCoreArrayBase &operator = (ZF_IN const ZFCoreArrayBase &ref) zfpurevirtual;
205 virtual zfbool operator == (ZF_IN const ZFCoreArrayBase &ref) const zfpurevirtual;
206 virtual zfbool operator != (ZF_IN const ZFCoreArrayBase &ref) const zfpurevirtual;
208
209public:
215 virtual void capacity(ZF_IN zfindex newCapacity) zfpurevirtual;
221 virtual void capacityTrim(void) zfpurevirtual;
225 virtual zfindex capacity(void) const zfpurevirtual;
226
230 virtual void remove(ZF_IN zfindex index) zfpurevirtual;
234 virtual void remove(
235 ZF_IN zfindex index
241 virtual void removeFirst(void) zfpurevirtual;
245 virtual void removeLast(void) zfpurevirtual;
249 virtual void removeAll(void) zfpurevirtual;
253 virtual void move(
254 ZF_IN zfindex fromIndex
255 , ZF_IN zfindex toIndexOrIndexMax
260 virtual zfindex count(void) const zfpurevirtual;
264 virtual zfbool isEmpty(void) const zfpurevirtual;
268 virtual void sort(
269 ZF_IN_OPT zfindex start = 0
275 virtual void sortReversely(
276 ZF_IN_OPT zfindex start = 0
279
280public:
286 virtual zfindex genericFind(ZF_IN const void *e) zfpurevirtual;
290 virtual void genericAdd(ZF_IN const void *e) zfpurevirtual;
292 virtual void genericAdd(
293 ZF_IN const void *e
294 , ZF_IN zfindex index
297 virtual void genericAddFrom(
298 ZF_IN const void *e
304 virtual void genericSet(
305 ZF_IN zfindex index
306 , ZF_IN const void *e
309 virtual const void *genericGet(ZF_IN zfindex index) const zfpurevirtual;
310};
312
313// ============================================================
330template<typename T_Element>
332public:
336 ZFCoreArray(void) : d(zfnull) {}
341 : d(ref.d)
342 {
343 if(d) {
344 ++(d->refCount);
345 }
346 }
347 virtual ~ZFCoreArray(void) {
348 if(d) {
349 --(d->refCount);
350 if(d->refCount == 0) {
351 zfpoolDelete(d);
352 }
353 }
354 }
356 virtual ZFCoreArrayBase *refNew(void) const {return zfpoolNew(ZFCoreArray<T_Element>, *this);}
358 virtual void *refImpl(void) const {return d;}
363 _ZFP_ZFCoreArrayPrivate<T_Element> *dTmp = d;
364 d = ref.d;
365 if(d) {
366 ++(d->refCount);
367 }
368 if(dTmp) {
369 --(dTmp->refCount);
370 if(dTmp->refCount == 0) {
371 zfpoolDelete(dTmp);
372 }
373 }
374 return *this;
375 }
376
377 zfbool operator == (ZF_IN const ZFCoreArray<T_Element> &ref) const {return (d == ref.d);}
378 inline zfbool operator != (ZF_IN const ZFCoreArray<T_Element> &ref) const {return !this->operator == (ref);}
380 virtual ZFCoreArrayBase &operator = (ZF_IN const ZFCoreArrayBase &ref) {
381 return this->operator = ((const ZFCoreArray<T_Element> &)ref);
382 }
383 virtual zfbool operator == (ZF_IN const ZFCoreArrayBase &ref) const {
384 return this->operator == ((const ZFCoreArray<T_Element> &)ref);
385 }
386 virtual zfbool operator != (ZF_IN const ZFCoreArrayBase &ref) const {
387 return this->operator == ((const ZFCoreArray<T_Element> &)ref);
388 }
390
391public:
396 _ZFP_ZFCoreArrayPrivate<T_Element> *dTmp = d;
397 d = ref.d;
398 ref.d = dTmp;
399 }
400
405 if(d != ref.d) {
406 if(d && d->buf) {
407 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(d->buf, d->buf + d->count);
408 d->count = 0;
409 }
410 if(ref.d) {
411 _capacityRequire(ref.count());
412 _ZFP_ZFCoreArrayW<T_Element>::objCreate(d->buf, d->buf + ref.count(), ref.arrayBuf());
413 d->count = (zfuint)ref.count();
414 }
415 }
416 }
417
420 zfindex objectRetainCount(void) const {return d ? d->refCount : 0;}
427
433 ) const {
434 if(d == ref.d) {
435 return ZFCompareEqual;
436 }
437 if(this->count() != ref.count()) {
439 }
440 for(zfindex i = this->count() - 1; i != zfindexMax(); --i) {
441 if(comparer(this->get(i), ref.get(i)) != ZFCompareEqual) {
443 }
444 }
445 return ZFCompareEqual;
446 }
447
448public:
452 , ZF_IN_OPT zfindex maxCount = zfindexMax()
454 ) const {
455 this->objectInfoOfContentT(ret, maxCount, token, zfnull);
456 }
459 ZF_IN_OPT zfindex maxCount = zfindexMax()
461 ) const {
462 zfstring ret;
463 this->objectInfoOfContentT(ret, maxCount, token, zfnull);
464 return ret;
465 }
466
470 , ZF_IN_OPT zfindex maxCount
471 , ZF_IN_OPT const ZFTokenForContainer &token
473 ) const {
474 zfindex count = 0;
475 ret += token.tokenLeft;
476 for(; count < this->count() && count < maxCount; ++count) {
477 if(count > 0) {
478 ret += token.tokenSeparator;
479 }
480 ret += token.tokenValueLeft;
481 if(infoGetter != zfnull) {
482 infoGetter(ret, this->get(count));
483 }
484 else {
485 zftToStringT(ret, this->get(count));
486 }
487 ret += token.tokenValueRight;
488 }
489 if(count < this->count()) {
490 if(count > 0) {
491 ret += token.tokenSeparator;
492 }
493 ret += token.tokenEtc;
494 }
495 ret += token.tokenRight;
496 }
497
499 ZF_IN_OPT zfindex maxCount
500 , ZF_IN_OPT const ZFTokenForContainer &token
502 ) const {
503 zfstring ret;
504 this->objectInfoOfContentT(ret, maxCount, token, infoGetter);
505 return ret;
506 }
507
508public:
510 virtual void capacity(ZF_IN zfindex newCapacity) {
511 _capacityRequire(newCapacity);
512 }
514 virtual void capacityTrim(void) {
515 zfindex capacity = this->count();
516 _capacityOptimize(capacity);
517 if(capacity != this->capacity()) {
518 _capacityDoChange(capacity);
519 }
520 }
522 virtual zfindex capacity(void) const {
523 return (zfindex)(d ? d->capacity : 0);
524 }
525
526public:
530 void add(ZF_IN T_Element const &e) {
531 _capacityRequire(this->count() + 1);
532 _ZFP_ZFCoreArrayW<T_Element>::objCreate(d->buf + d->count, d->buf + d->count + 1, &e);
533 ++(d->count);
534 }
535
538 void add(
539 ZF_IN T_Element const &e
540 , ZF_IN zfindex index
541 ) {
542 if(index == zfindexMax()) {
543 index = this->count();
544 }
545 else {
546 ZFCoreAssertIndexRange(index, this->count() + 1);
547 }
548 _capacityRequire(this->count() + 1);
549 _ZFP_ZFCoreArrayW<T_Element>::objCreate(d->buf + d->count, d->buf + d->count + 1);
550 T_Element *pos = d->buf + index;
551 _ZFP_ZFCoreArrayW<T_Element>::objMove(pos + 1, pos, this->count() - index);
552 ++(d->count);
553 *pos = e;
554 }
555
559 ZF_IN const T_Element *src
561 ) {
562 if(src == zfnull || count == 0) {
563 return;
564 }
565 if(d == zfnull || src < d->buf || src >= d->buf + d->capacity) {
566 _capacityRequire(this->count() + count);
567 _ZFP_ZFCoreArrayW<T_Element>::objCreate(d->buf + d->count, d->buf + d->count + count, src);
568 d->count += (zfuint)count;
569 }
570 else {
572 tmp.capacity(count);
573 tmp.addFrom(src, count);
574 this->addFrom(tmp.arrayBuf(), count);
575 }
576 }
577
582 if(d != ref.d) {
583 this->addFrom(ref.arrayBuf(), ref.count());
584 }
585 }
586
591 ZF_IN T_Element const &e
593 ) const {
594 if(d) {
595 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
596 if(comparer(*p, e) == ZFCompareEqual) {
597 return (p - d->buf);
598 }
599 }
600 }
601 return zfindexMax();
602 }
603
607 ZF_IN T_Element const &e
609 ) const {
610 if(d && d->buf) {
611 for(T_Element *p = d->buf + d->count - 1; p >= d->buf; --p) {
612 if(comparer(*p, e) == ZFCompareEqual) {
613 return (p - d->buf);
614 }
615 }
616 }
617 return zfindexMax();
618 }
619
622 template<typename T_Another>
624 ZF_IN T_Another const &e
626 ) const {
627 if(d) {
628 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
629 if(comparer(*p, e) == ZFCompareEqual) {
630 return (p - d->buf);
631 }
632 }
633 }
634 return zfindexMax();
635 }
636
639 template<typename T_Another>
641 ZF_IN T_Another const &e
643 ) const {
644 if(d && d->buf) {
645 for(T_Element *p = d->buf + d->count - 1; p >= d->buf; --p) {
646 if(comparer(*p, e) == ZFCompareEqual) {
647 return (p - d->buf);
648 }
649 }
650 }
651 return zfindexMax();
652 }
653
658 ZF_IN T_Element const &e
660 ) {
661 if(d) {
662 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
663 if(comparer(*p, e) == ZFCompareEqual) {
664 this->remove(p - d->buf);
665 return zftrue;
666 }
667 }
668 }
669 return zffalse;
670 }
671
674 template<typename T_Another>
676 ZF_IN T_Another const &e
678 ) {
679 if(d) {
680 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
681 if(comparer(*p, e) == ZFCompareEqual) {
682 this->remove(p - d->buf);
683 return zftrue;
684 }
685 }
686 }
687 return zffalse;
688 }
689
693 ZF_IN T_Element const &e
695 ) {
696 if(d && d->buf) {
697 for(T_Element *p = d->buf + d->count - 1; p >= d->buf; --p) {
698 if(comparer(*p, e) == ZFCompareEqual) {
699 this->remove(p - d->buf);
700 return zftrue;
701 }
702 }
703 }
704 return zffalse;
705 }
706
709 template<typename T_Another>
711 ZF_IN T_Another const &e
713 ) {
714 if(d && d->buf) {
715 for(T_Element *p = d->buf + d->count - 1; p >= d->buf; --p) {
716 if(comparer(*p, e) == ZFCompareEqual) {
717 this->remove(p - d->buf);
718 return zftrue;
719 }
720 }
721 }
722 return zffalse;
723 }
724
728 ZF_IN T_Element const &e
730 ) {
731 zfindex removedCount = 0;
732 if(d) {
733 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
734 if(comparer(*p, e) == ZFCompareEqual) {
735 ++removedCount;
736 this->remove(p - d->buf);
737 --p;
738 }
739 }
740 }
741 return removedCount;
742 }
743
746 template<typename T_Another>
748 ZF_IN T_Another const &e
750 ) {
751 zfindex removedCount = 0;
752 if(d) {
753 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
754 if(comparer(*p, e) == ZFCompareEqual) {
755 ++removedCount;
756 this->remove(p - d->buf);
757 --p;
758 }
759 }
760 }
761 return removedCount;
762 }
763
765 virtual void remove(ZF_IN zfindex index) {
766 ZFCoreAssertIndexRange(index, this->count());
767 _ZFP_ZFCoreArrayW<T_Element>::objMove(d->buf + index, d->buf + index + 1, this->count() - index - 1);
768 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(d->buf + d->count - 1, d->buf + d->count);
769 --(d->count);
770 }
772 virtual void remove(
773 ZF_IN zfindex index
775 ) {
776 ZFCoreAssertIndexRange(index, this->count());
777 if(count > this->count() - index) {
778 count = this->count() - index;
779 }
780 _ZFP_ZFCoreArrayW<T_Element>::objMove(d->buf + index, d->buf + index + count, this->count() - (index + count));
781 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(d->buf + d->count - count, d->buf + d->count);
782 d->count -= (zfuint)count;
783 }
784
787 T_Element removeAndGet(ZF_IN zfindex index) {
788 T_Element t = this->get(index);
789 this->remove(index);
790 return t;
791 }
793 virtual void removeFirst(void) {
794 this->remove(0);
795 }
796
800 T_Element removeFirstAndGet(void) {
801 T_Element t = this->getFirst();
802 this->removeFirst();
803 return t;
804 }
806 virtual void removeLast(void) {
807 this->remove(this->count() - 1);
808 }
809
813 T_Element removeLastAndGet(void) {
814 T_Element t = this->getLast();
815 this->removeLast();
816 return t;
817 }
819 virtual void removeAll(void) {
820 if(d && d->count != 0) {
821 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(d->buf, d->buf + d->count);
822 d->count = 0;
823 }
824 }
825
827 virtual void move(
828 ZF_IN zfindex fromIndex
829 , ZF_IN zfindex toIndexOrIndexMax
830 ) {
831 ZFCoreAssertIndexRange(fromIndex, this->count());
832 if(toIndexOrIndexMax == zfindexMax()) {
833 toIndexOrIndexMax = this->count() - 1;
834 }
835 else {
836 ZFCoreAssertIndexRange(toIndexOrIndexMax, this->count());
837 }
838 if(fromIndex == toIndexOrIndexMax) {
839 return;
840 }
841 T_Element t = d->buf[fromIndex];
842 if(fromIndex < toIndexOrIndexMax) {
843 _ZFP_ZFCoreArrayW<T_Element>::objMove(d->buf + fromIndex, d->buf + fromIndex + 1, toIndexOrIndexMax - fromIndex);
844 }
845 else {
846 _ZFP_ZFCoreArrayW<T_Element>::objMove(d->buf + toIndexOrIndexMax + 1, d->buf + toIndexOrIndexMax, fromIndex - toIndexOrIndexMax);
847 }
848 d->buf[toIndexOrIndexMax] = t;
849 }
850
851public:
855 void set(
856 ZF_IN zfindex index
857 , ZF_IN T_Element const &e
858 ) {
859 ZFCoreAssertIndexRange(index, this->count());
860 d->buf[index] = e;
861 }
862
863public:
867 T_Element &get(ZF_IN zfindex index) {
868 ZFCoreAssertIndexRange(index, this->count());
869 return d->buf[index];
870 }
871
874 T_Element const &get(ZF_IN zfindex index) const {
875 ZFCoreAssertIndexRange(index, this->count());
876 return d->buf[index];
877 }
878
881 T_Element &operator [] (ZF_IN zfindex index) {
882 ZFCoreAssertIndexRange(index, this->count());
883 return d->buf[index];
884 }
885
888 T_Element const &operator [] (ZF_IN zfindex index) const {
889 ZFCoreAssertIndexRange(index, this->count());
890 return d->buf[index];
891 }
892
895 T_Element const &getFirst(void) const {
896 ZFCoreAssertIndexRange(0, this->count());
897 return *(d->buf);
898 }
899
902 T_Element const &getLast(void) const {
903 ZFCoreAssertIndexRange(0, this->count());
904 return *(d->buf + d->count - 1);
905 }
906
913 T_Element *arrayBuf(void) {return d ? d->buf : zfnull;}
917 const T_Element *arrayBuf(void) const {return d ? d->buf : zfnull;}
918
920 virtual zfindex count(void) const {return (zfindex)(d ? d->count : 0);}
922 virtual zfbool isEmpty(void) const {return (d == zfnull || d->count == 0);}
925 ZF_IN T_Element const &e
927 ) const {
928 return this->find(e, comparer) != zfindexMax();
929 }
930
931public:
933 virtual void sort(
934 ZF_IN_OPT zfindex start = 0
936 ) {
937 this->sort(start, count, ZFComparerDefault);
938 }
940 virtual void sortReversely(
941 ZF_IN_OPT zfindex start = 0
943 ) {
945 }
946
949 void sort(
950 ZF_IN zfindex start
952 , ZF_IN typename ZFComparer<T_Element>::Comparer elementComparer
953 ) {
954 if(!this->isEmpty() && start + 1 < this->count() && count > 1) {
956 d->buf,
957 start,
958 (count > this->count() - start) ? (this->count() - 1) : (start + count - 1),
959 elementComparer);
960 }
961 }
962
966 ZF_IN zfindex start
968 , ZF_IN typename ZFComparer<T_Element>::Comparer elementComparer
969 ) {
970 if(!this->isEmpty() && start + 1 < this->count() && count > 1) {
972 d->buf,
973 start,
974 (count > this->count() - start) ? (this->count() - 1) : (start + count - 1),
975 elementComparer);
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 void genericAdd(ZF_IN const void *e) {this->add(*(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(
997 ZF_IN const void *e
999 ) {this->addFrom((const T_Element *)e, count);}
1001 virtual void genericAddFrom(ZF_IN const ZFCoreArrayBase &ref) {this->addFrom((const ZFCoreArray<T_Element> &)ref);}
1003 virtual void genericSet(
1004 ZF_IN zfindex index
1005 , ZF_IN const void *e
1006 ) {this->set(index, *(const T_Element *)e);}
1008 virtual const void *genericGet(ZF_IN zfindex index) const {return &(this->get(index));}
1009
1010private:
1011 _ZFP_ZFCoreArrayPrivate<T_Element> *d;
1012private:
1013 inline void _capacityOptimize(ZF_IN_OUT zfindex &capacity) {
1014 _ZFP_ZFCoreArrayCapacity<
1015 sizeof(T_Element) <= sizeof(void *)
1016 ? 0
1017 : (sizeof(T_Element) <= 4 * sizeof(void *)
1018 ? 1
1019 : 2
1020 )
1021 >::capacityOptimize(capacity);
1022 }
1023 inline void _capacityRequire(ZF_IN zfindex capacity) {
1024 _capacityOptimize(capacity);
1025 if(capacity > this->capacity()) {
1026 _capacityDoChange(capacity);
1027 }
1028 }
1029 void _capacityDoChange(ZF_IN zfindex capacity) {
1030 if(capacity == 0) {
1031 if(d) {
1032 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(d->buf, d->buf + d->count);
1033 zffree(d->buf);
1034 d->buf = zfnull;
1035 d->capacity = 0;
1036 d->count = 0;
1037 }
1038 }
1039 else {
1040 if(d == zfnull) {
1041 d = zfpoolNew(_ZFP_ZFCoreArrayPrivate<T_Element>);
1042 }
1043
1044 T_Element *oldBuf = d->buf;
1045 zfuint oldCount = d->count;
1046
1047 T_Element *newBuf = (T_Element *)zfmalloc(capacity * sizeof(T_Element));
1048 _ZFP_ZFCoreArrayW<T_Element>::objCreate(newBuf, newBuf + oldCount, oldBuf);
1049
1050 d->buf = newBuf;
1051 d->capacity = (zfuint)capacity;
1052 d->count = oldCount;
1053
1054 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(oldBuf, oldBuf + oldCount);
1055 zffree(oldBuf);
1056 }
1057 }
1058};
1059ZFOUTPUT_TYPE_TEMPLATE(typename T_Element, ZFCoreArray<T_Element>, {v.objectInfoT(s);})
1060
1061template<typename T_Element>
1062zfclassLikePOD _ZFP_ZFCoreArrayCreate {
1063public:
1064 inline _ZFP_ZFCoreArrayCreate<T_Element> &add(ZF_IN T_Element const &v) {
1065 this->v.add(v);
1066 return *this;
1067 }
1068
1069public:
1071};
1072#define _ZFP_ZFCoreArrayCreate_action_expand(value) .add(value)
1073#define _ZFP_ZFCoreArrayCreate_action(CreatorType, values, ...) \
1074 CreatorType() ZFM_FIX_PARAM(_ZFP_ZFCoreArrayCreate_action_expand, ZFM_EMPTY, values, ##__VA_ARGS__) .v
1083#define ZFCoreArrayCreate(ElementType, values, ...) _ZFP_ZFCoreArrayCreate_action(_ZFP_ZFCoreArrayCreate<ElementType>, values, ##__VA_ARGS__)
1084
1086
1087#endif // #ifndef _ZFI_ZFCoreArray_h_
1088
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:94
#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:112
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:180
#define ZF_IN_OPT
dummy macro that shows the param used as optional input
Definition ZFCoreTypeDef_ClassType.h:184
void * zfmemmove(void *dst, const void *src, zfindex size)
wrapper to memmove
Definition ZFCoreTypeDef_ClassType.h:142
#define zfdeletePlacement(instance)
placement delete (instance->~Type()) defined for future use
Definition ZFCoreTypeDef_ClassType.h:97
#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:196
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:152
void * zfmemcpy(void *dst, const void *src, zfindex size)
wrapper to memcpy
Definition ZFCoreTypeDef_ClassType.h:140
#define zfmalloc(size)
same as malloc defined for future use
Definition ZFCoreTypeDef_ClassType.h:100
_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, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault)
sort with custom comparer in range [left, right], holder must support operator []
Definition ZFCoreUtilMath.h:192
zfbool zfmSort(T_Holder &holder, zfindex left, zfindex right, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault)
sort with custom comparer in range [left, right], holder must support operator []
Definition ZFCoreUtilMath.h:166
#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 genericAdd(const void *e)=0
generic version
virtual zfstring objectInfoOfContent(zfindex maxCount=((zfindex) -1), const ZFTokenForContainer &token=_ZFP_ZFTokenForContainerDefault) const =0
return content info
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 zfbool isEmpty(void) const =0
true if empty
virtual void removeAll(void)=0
remove all content
virtual void * refImpl(void) const =0
get the impl
virtual void sort(zfindex start=0, zfindex count=((zfindex) -1))=0
sort
virtual void removeFirst(void)=0
remove first, assert fail if out of range
virtual void capacity(zfindex newCapacity)=0
change capacity to hold at least newCapacity
virtual void genericSwap(ZFCoreArrayBase &ref)=0
generic version
virtual void sortReversely(zfindex start=0, zfindex count=((zfindex) -1))=0
sort reversely
virtual const void * genericGet(zfindex index) const =0
generic version
virtual void genericAddFrom(const void *e, zfindex count)=0
generic version
virtual void remove(zfindex index)=0
remove element at index with count, assert fail if out of range
virtual zfindex genericFindReversely(const void *e)=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 void removeLast(void)=0
remove last, assert fail if out of range
virtual void genericSet(zfindex index, const void *e)=0
generic version
light weight array
Definition ZFCoreArray.h:331
virtual void capacityTrim(void)
trim current capacity
Definition ZFCoreArray.h:514
ZFCoreArray(void)
main constructor
Definition ZFCoreArray.h:336
virtual void removeLast(void)
remove last, assert fail if out of range
Definition ZFCoreArray.h:806
virtual void capacity(zfindex newCapacity)
change capacity to hold at least newCapacity
Definition ZFCoreArray.h:510
virtual void genericAddFrom(const ZFCoreArrayBase &ref)
generic version
Definition ZFCoreArray.h:1001
virtual zfbool isEmpty(void) const
true if empty
Definition ZFCoreArray.h:922
typename ZFFilterBase< T_Public, T_Internal >::CustomFilterCallback const & getLast(void) const
Definition ZFCoreArray.h:902
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:747
typename ZFFilterBase< T_Public, T_Internal >::CustomFilterCallback & get(zfindex index)
Definition ZFCoreArray.h:867
zfindex objectRetainCount(void) const
get retain count
Definition ZFCoreArray.h:420
virtual void objectInfoOfContentT(zfstring &ret, zfindex maxCount=((zfindex) -1), const ZFTokenForContainer &token=_ZFP_ZFTokenForContainerDefault) const
return content info
Definition ZFCoreArray.h:450
void add(T_Element const &e)
add element
Definition ZFCoreArray.h:530
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:692
virtual zfstring objectInfoOfContent(zfindex maxCount=((zfindex) -1), const ZFTokenForContainer &token=_ZFP_ZFTokenForContainerDefault) const
return content info
Definition ZFCoreArray.h:458
virtual zfbool isContain(T_Element const &e, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault) const
true if contains element
Definition ZFCoreArray.h:924
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:710
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:538
zfindex findReversely(T_Another const &e, typename ZFComparer< T_Element, T_Another >::Comparer comparer) const
find element reversely
Definition ZFCoreArray.h:640
virtual void remove(zfindex index)
Definition ZFCoreArray.h:765
T_Element const & get(zfindex index) const
get element's const reference at index
Definition ZFCoreArray.h:874
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:590
void addFrom(const ZFCoreArray< T_Element > &ref)
add from another array
Definition ZFCoreArray.h:581
zfstring objectInfoOfContent(zfindex maxCount, const ZFTokenForContainer &token, typename ZFCoreInfoGetter< T_Element >::InfoGetter infoGetter) const
return content info
Definition ZFCoreArray.h:498
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:675
T_Element removeAndGet(zfindex index)
remove and return the removed value
Definition ZFCoreArray.h:787
T_Element removeLastAndGet(void)
remove last and return the removed value, or assert fail if empty
Definition ZFCoreArray.h:813
virtual zfindex capacity(void) const
get capacity
Definition ZFCoreArray.h:522
typename ZFFilterBase< T_Public, T_Internal >::CustomFilterCallback const & getFirst(void) const
Definition ZFCoreArray.h:895
virtual const void * genericGet(zfindex index) const
generic version
Definition ZFCoreArray.h:1008
virtual void move(zfindex fromIndex, zfindex toIndexOrIndexMax)
move element
Definition ZFCoreArray.h:827
ZFCoreArray(const ZFCoreArray< T_Element > &ref)
construct from another array
Definition ZFCoreArray.h:340
ZFCompareResult objectCompareValue(const ZFCoreArray< T_Element > &ref, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault) const
compare by content
Definition ZFCoreArray.h:430
void set(zfindex index, T_Element const &e)
set element at index, or assert fail if index out of range
Definition ZFCoreArray.h:855
T_Element removeFirstAndGet(void)
remove first and return the removed value, or assert fail if empty
Definition ZFCoreArray.h:800
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:727
void swap(ZFCoreArray< T_Element > &ref)
swap internal data
Definition ZFCoreArray.h:395
virtual void genericSet(zfindex index, const void *e)
generic version
Definition ZFCoreArray.h:1003
void objectInfoOfContentT(zfstring &ret, zfindex maxCount, const ZFTokenForContainer &token, typename ZFCoreInfoGetter< T_Element >::InfoGetter infoGetter) const
see objectInfoOfContent
Definition ZFCoreArray.h:468
virtual void remove(zfindex index, zfindex count)
remove element at index with count, assert fail if out of range
Definition ZFCoreArray.h:772
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:558
T_Element * arrayBuf(void)
directly access the array
Definition ZFCoreArray.h:913
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:657
void copyFrom(const ZFCoreArray< T_Element > &ref)
copy all settings and contents from another array
Definition ZFCoreArray.h:404
virtual void genericAdd(const void *e)
generic version
Definition ZFCoreArray.h:989
const T_Element * arrayBuf(void) const
see arrayBuf
Definition ZFCoreArray.h:917
virtual void genericAddFrom(const void *e, zfindex count)
generic version
Definition ZFCoreArray.h:996
zfindex findReversely(T_Element const &e, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault) const
find element reversely
Definition ZFCoreArray.h:606
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:933
virtual ZFCoreArrayBase * refNew(void) const
new reference
Definition ZFCoreArray.h:356
ZFCompareResult objectCompare(const ZFCoreArray< T_Element > &ref) const
compare by instance
Definition ZFCoreArray.h:424
virtual void removeAll(void)
remove all content
Definition ZFCoreArray.h:819
virtual zfindex count(void) const
Definition ZFCoreArray.h:920
void sort(zfindex start, zfindex count, typename ZFComparer< T_Element >::Comparer elementComparer)
sort element
Definition ZFCoreArray.h:949
virtual void sortReversely(zfindex start=0, zfindex count=((zfindex) -1))
sort reversely
Definition ZFCoreArray.h:940
void sortReversely(zfindex start, zfindex count, typename ZFComparer< T_Element >::Comparer elementComparer)
sort element
Definition ZFCoreArray.h:965
virtual void * refImpl(void) const
get the impl
Definition ZFCoreArray.h:358
zfindex find(T_Another const &e, typename ZFComparer< T_Element, T_Another >::Comparer comparer) const
find element
Definition ZFCoreArray.h:623
virtual void removeFirst(void)
remove first, assert fail if out of range
Definition ZFCoreArray.h:793
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