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;
304 virtual void genericAdd(ZF_IN const void *e) {
305 this->genericAdd(e, zfindexMax());
306 }
307
308 virtual void genericAdd(
309 ZF_IN const void *e
310 , ZF_IN zfindex index
315 virtual void genericSet(
316 ZF_IN zfindex index
317 , ZF_IN const void *e
320 virtual const void *genericGet(ZF_IN zfindex index) const zfpurevirtual;
321};
322ZFOUTPUT_TYPE(ZFCoreArrayBase, {v.objectInfoT(s);})
323
324// ============================================================
341template<typename T_Element>
343public:
347 ZFCoreArray(void) : d(zfnull) {}
352 : d(ref.d)
353 {
354 if(d) {
355 ++(d->refCount);
356 }
357 }
358 virtual ~ZFCoreArray(void) {
359 if(d) {
360 --(d->refCount);
361 if(d->refCount == 0) {
362 zfpoolDelete(d);
363 }
364 }
365 }
367 virtual ZFCoreArrayBase *refNew(void) const {return zfpoolNew(ZFCoreArray<T_Element>, *this);}
369 virtual void *refImpl(void) const {return d;}
374 _ZFP_ZFCoreArrayPrivate<T_Element> *dTmp = d;
375 d = ref.d;
376 if(d) {
377 ++(d->refCount);
378 }
379 if(dTmp) {
380 --(dTmp->refCount);
381 if(dTmp->refCount == 0) {
382 zfpoolDelete(dTmp);
383 }
384 }
385 return *this;
386 }
387
388 zfbool operator == (ZF_IN const ZFCoreArray<T_Element> &ref) const {return (d == ref.d);}
389 inline zfbool operator != (ZF_IN const ZFCoreArray<T_Element> &ref) const {return !this->operator == (ref);}
391 virtual ZFCoreArrayBase &operator = (ZF_IN const ZFCoreArrayBase &ref) {
392 return this->operator = ((const ZFCoreArray<T_Element> &)ref);
393 }
394 virtual zfbool operator == (ZF_IN const ZFCoreArrayBase &ref) const {
395 return this->operator == ((const ZFCoreArray<T_Element> &)ref);
396 }
397 virtual zfbool operator != (ZF_IN const ZFCoreArrayBase &ref) const {
398 return this->operator == ((const ZFCoreArray<T_Element> &)ref);
399 }
401
402public:
407 _ZFP_ZFCoreArrayPrivate<T_Element> *dTmp = d;
408 d = ref.d;
409 ref.d = dTmp;
410 }
411
416 if(d != ref.d) {
417 if(d && d->buf) {
418 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(d->buf, d->buf + d->count);
419 d->count = 0;
420 }
421 if(ref.d) {
422 _capacityRequire(ref.count());
423 _ZFP_ZFCoreArrayW<T_Element>::objCreate(d->buf, d->buf + ref.count(), ref.arrayBuf());
424 d->count = (zfuint)ref.count();
425 }
426 }
427 }
428
431 zfindex objectRetainCount(void) const {return d ? d->refCount : 0;}
438
444 ) const {
445 if(d == ref.d) {
446 return ZFCompareEqual;
447 }
448 if(this->count() != ref.count()) {
450 }
451 for(zfindex i = this->count() - 1; i != zfindexMax(); --i) {
452 if(comparer(this->get(i), ref.get(i)) != ZFCompareEqual) {
454 }
455 }
456 return ZFCompareEqual;
457 }
458
459public:
463 , ZF_IN_OPT zfindex maxCount = zfindexMax()
465 ) const {
466 this->objectInfoOfContentT(ret, maxCount, token, zfnull);
467 }
470 ZF_IN_OPT zfindex maxCount = zfindexMax()
472 ) const {
473 zfstring ret;
474 this->objectInfoOfContentT(ret, maxCount, token, zfnull);
475 return ret;
476 }
477
481 , ZF_IN_OPT zfindex maxCount
482 , ZF_IN_OPT const ZFTokenForContainer &token
484 ) const {
485 zfindex count = 0;
486 ret += token.tokenLeft;
487 for(; count < this->count() && count < maxCount; ++count) {
488 if(count > 0) {
489 ret += token.tokenSeparator;
490 }
491 ret += token.tokenValueLeft;
492 if(infoGetter != zfnull) {
493 infoGetter(ret, this->get(count));
494 }
495 else {
496 zftToStringT(ret, this->get(count));
497 }
498 ret += token.tokenValueRight;
499 }
500 if(count < this->count()) {
501 if(count > 0) {
502 ret += token.tokenSeparator;
503 }
504 ret += token.tokenEtc;
505 }
506 ret += token.tokenRight;
507 }
508
510 ZF_IN_OPT zfindex maxCount
511 , ZF_IN_OPT const ZFTokenForContainer &token
513 ) const {
514 zfstring ret;
515 this->objectInfoOfContentT(ret, maxCount, token, infoGetter);
516 return ret;
517 }
518
519public:
521 virtual void capacity(ZF_IN zfindex newCapacity) {
522 _capacityRequire(newCapacity);
523 }
525 virtual void capacityTrim(void) {
526 zfindex capacity = this->count();
527 _capacityOptimize(capacity);
528 if(capacity != this->capacity()) {
529 _capacityDoChange(capacity);
530 }
531 }
533 virtual zfindex capacity(void) const {
534 return (zfindex)(d ? d->capacity : 0);
535 }
536
537public:
541 void add(ZF_IN T_Element const &e) {
542 _capacityRequire(this->count() + 1);
543 _ZFP_ZFCoreArrayW<T_Element>::objCreate(d->buf + d->count, d->buf + d->count + 1, &e);
544 ++(d->count);
545 }
546
549 void add(
550 ZF_IN T_Element const &e
551 , ZF_IN zfindex index
552 ) {
553 if(index == zfindexMax()) {
554 index = this->count();
555 }
556 else {
557 ZFCoreAssertIndexRange(index, this->count() + 1);
558 }
559 _capacityRequire(this->count() + 1);
560 _ZFP_ZFCoreArrayW<T_Element>::objCreate(d->buf + d->count, d->buf + d->count + 1);
561 T_Element *pos = d->buf + index;
562 _ZFP_ZFCoreArrayW<T_Element>::objMove(pos + 1, pos, this->count() - index);
563 ++(d->count);
564 *pos = e;
565 }
566
570 ZF_IN const T_Element *src
572 ) {
573 if(src == zfnull || count == 0) {
574 return;
575 }
576 if(d == zfnull || src < d->buf || src >= d->buf + d->capacity) {
577 _capacityRequire(this->count() + count);
578 _ZFP_ZFCoreArrayW<T_Element>::objCreate(d->buf + d->count, d->buf + d->count + count, src);
579 d->count += (zfuint)count;
580 }
581 else {
583 tmp.capacity(count);
584 tmp.addFrom(src, count);
585 this->addFrom(tmp.arrayBuf(), count);
586 }
587 }
588
593 if(d != ref.d) {
594 this->addFrom(ref.arrayBuf(), ref.count());
595 }
596 }
597
602 ZF_IN T_Element const &e
604 ) const {
605 if(d) {
606 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
607 if(comparer(*p, e) == ZFCompareEqual) {
608 return (p - d->buf);
609 }
610 }
611 }
612 return zfindexMax();
613 }
614
618 ZF_IN T_Element const &e
620 ) const {
621 if(d && d->buf) {
622 for(T_Element *p = d->buf + d->count - 1; p >= d->buf; --p) {
623 if(comparer(*p, e) == ZFCompareEqual) {
624 return (p - d->buf);
625 }
626 }
627 }
628 return zfindexMax();
629 }
630
633 template<typename T_Another>
635 ZF_IN T_Another const &e
637 ) const {
638 if(d) {
639 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
640 if(comparer(*p, e) == ZFCompareEqual) {
641 return (p - d->buf);
642 }
643 }
644 }
645 return zfindexMax();
646 }
647
650 template<typename T_Another>
652 ZF_IN T_Another const &e
654 ) const {
655 if(d && d->buf) {
656 for(T_Element *p = d->buf + d->count - 1; p >= d->buf; --p) {
657 if(comparer(*p, e) == ZFCompareEqual) {
658 return (p - d->buf);
659 }
660 }
661 }
662 return zfindexMax();
663 }
664
669 ZF_IN T_Element const &e
671 ) {
672 if(d) {
673 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
674 if(comparer(*p, e) == ZFCompareEqual) {
675 this->remove(p - d->buf);
676 return zftrue;
677 }
678 }
679 }
680 return zffalse;
681 }
682
685 template<typename T_Another>
687 ZF_IN T_Another const &e
689 ) {
690 if(d) {
691 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
692 if(comparer(*p, e) == ZFCompareEqual) {
693 this->remove(p - d->buf);
694 return zftrue;
695 }
696 }
697 }
698 return zffalse;
699 }
700
704 ZF_IN T_Element const &e
706 ) {
707 if(d && d->buf) {
708 for(T_Element *p = d->buf + d->count - 1; p >= d->buf; --p) {
709 if(comparer(*p, e) == ZFCompareEqual) {
710 this->remove(p - d->buf);
711 return zftrue;
712 }
713 }
714 }
715 return zffalse;
716 }
717
720 template<typename T_Another>
722 ZF_IN T_Another const &e
724 ) {
725 if(d && d->buf) {
726 for(T_Element *p = d->buf + d->count - 1; p >= d->buf; --p) {
727 if(comparer(*p, e) == ZFCompareEqual) {
728 this->remove(p - d->buf);
729 return zftrue;
730 }
731 }
732 }
733 return zffalse;
734 }
735
739 ZF_IN T_Element const &e
741 ) {
742 zfindex removedCount = 0;
743 if(d) {
744 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
745 if(comparer(*p, e) == ZFCompareEqual) {
746 ++removedCount;
747 this->remove(p - d->buf);
748 --p;
749 }
750 }
751 }
752 return removedCount;
753 }
754
757 template<typename T_Another>
759 ZF_IN T_Another const &e
761 ) {
762 zfindex removedCount = 0;
763 if(d) {
764 for(T_Element *p = d->buf, *pEnd = d->buf + d->count; p < pEnd; ++p) {
765 if(comparer(*p, e) == ZFCompareEqual) {
766 ++removedCount;
767 this->remove(p - d->buf);
768 --p;
769 }
770 }
771 }
772 return removedCount;
773 }
774
776 virtual void remove(ZF_IN zfindex index) {
777 ZFCoreAssertIndexRange(index, this->count());
778 _ZFP_ZFCoreArrayW<T_Element>::objMove(d->buf + index, d->buf + index + 1, this->count() - index - 1);
779 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(d->buf + d->count - 1, d->buf + d->count);
780 --(d->count);
781 }
783 virtual void remove(
784 ZF_IN zfindex index
786 ) {
787 ZFCoreAssertIndexRange(index, this->count());
788 if(count > this->count() - index) {
789 count = this->count() - index;
790 }
791 _ZFP_ZFCoreArrayW<T_Element>::objMove(d->buf + index, d->buf + index + count, this->count() - (index + count));
792 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(d->buf + d->count - count, d->buf + d->count);
793 d->count -= (zfuint)count;
794 }
795
798 T_Element removeAndGet(ZF_IN zfindex index) {
799 T_Element t = this->get(index);
800 this->remove(index);
801 return t;
802 }
803
807 T_Element removeFirstAndGet(void) {
808 T_Element t = this->getFirst();
809 this->removeFirst();
810 return t;
811 }
812
816 T_Element removeLastAndGet(void) {
817 T_Element t = this->getLast();
818 this->removeLast();
819 return t;
820 }
821
823 virtual void move(
824 ZF_IN zfindex fromIndex
825 , ZF_IN zfindex toIndexOrIndexMax
826 ) {
827 ZFCoreAssertIndexRange(fromIndex, this->count());
828 if(toIndexOrIndexMax == zfindexMax()) {
829 toIndexOrIndexMax = this->count() - 1;
830 }
831 else {
832 ZFCoreAssertIndexRange(toIndexOrIndexMax, this->count());
833 }
834 if(fromIndex == toIndexOrIndexMax) {
835 return;
836 }
837 T_Element t = d->buf[fromIndex];
838 if(fromIndex < toIndexOrIndexMax) {
839 _ZFP_ZFCoreArrayW<T_Element>::objMove(d->buf + fromIndex, d->buf + fromIndex + 1, toIndexOrIndexMax - fromIndex);
840 }
841 else {
842 _ZFP_ZFCoreArrayW<T_Element>::objMove(d->buf + toIndexOrIndexMax + 1, d->buf + toIndexOrIndexMax, fromIndex - toIndexOrIndexMax);
843 }
844 d->buf[toIndexOrIndexMax] = t;
845 }
846
847public:
851 void set(
852 ZF_IN zfindex index
853 , ZF_IN T_Element const &e
854 ) {
855 ZFCoreAssertIndexRange(index, this->count());
856 d->buf[index] = e;
857 }
858
859public:
863 T_Element &get(ZF_IN zfindex index) {
864 ZFCoreAssertIndexRange(index, this->count());
865 return d->buf[index];
866 }
867
870 T_Element const &get(ZF_IN zfindex index) const {
871 ZFCoreAssertIndexRange(index, this->count());
872 return d->buf[index];
873 }
874
877 T_Element &operator [] (ZF_IN zfindex index) {
878 ZFCoreAssertIndexRange(index, this->count());
879 return d->buf[index];
880 }
881
884 T_Element const &operator [] (ZF_IN zfindex index) const {
885 ZFCoreAssertIndexRange(index, this->count());
886 return d->buf[index];
887 }
888
891 T_Element const &getFirst(void) const {
892 ZFCoreAssertIndexRange(0, this->count());
893 return *(d->buf);
894 }
895
898 T_Element const &getLast(void) const {
899 ZFCoreAssertIndexRange(0, this->count());
900 return *(d->buf + d->count - 1);
901 }
902
909 T_Element *arrayBuf(void) {return d ? d->buf : zfnull;}
913 const T_Element *arrayBuf(void) const {return d ? d->buf : zfnull;}
914
916 virtual zfindex count(void) const {return (zfindex)(d ? d->count : 0);}
918 virtual zfbool isEmpty(void) const {return (d == zfnull || d->count == 0);}
921 ZF_IN T_Element const &e
923 ) const {
924 return this->find(e, comparer) != zfindexMax();
925 }
926
927public:
929 virtual void sort(
930 ZF_IN_OPT zfindex start = 0
932 ) {
933 this->sort(start, count, ZFComparerDefault);
934 }
936 virtual void sortReversely(
937 ZF_IN_OPT zfindex start = 0
939 ) {
941 }
942
945 void sort(
946 ZF_IN zfindex start
948 , ZF_IN typename ZFComparer<T_Element>::Comparer elementComparer
949 ) {
950 if(!this->isEmpty() && start + 1 < this->count() && count > 1) {
952 d->buf,
953 start,
954 (count > this->count() - start) ? (this->count() - 1) : (start + count - 1),
955 elementComparer);
956 }
957 }
958
962 ZF_IN zfindex start
964 , ZF_IN typename ZFComparer<T_Element>::Comparer elementComparer
965 ) {
966 if(!this->isEmpty() && start + 1 < this->count() && count > 1) {
968 d->buf,
969 start,
970 (count > this->count() - start) ? (this->count() - 1) : (start + count - 1),
971 elementComparer);
972 }
973 }
974
975public:
979 virtual void genericCopyFrom(ZF_IN const ZFCoreArrayBase &ref) {this->copyFrom((const ZFCoreArray<T_Element> &)ref);}
981 virtual zfindex genericFind(ZF_IN const void *e) {return this->find(*(const T_Element *)e);}
983 virtual zfindex genericFindReversely(ZF_IN const void *e) {return this->findReversely(*(const T_Element *)e);}
985 virtual void genericAdd(
986 ZF_IN const void *e
987 , ZF_IN zfindex index
988 ) {this->add(*(const T_Element *)e, index);}
990 virtual void genericAddFrom(ZF_IN const ZFCoreArrayBase &ref) {this->addFrom((const ZFCoreArray<T_Element> &)ref);}
992 virtual void genericSet(
993 ZF_IN zfindex index
994 , ZF_IN const void *e
995 ) {this->set(index, *(const T_Element *)e);}
997 virtual const void *genericGet(ZF_IN zfindex index) const {return &(this->get(index));}
998
999private:
1000 _ZFP_ZFCoreArrayPrivate<T_Element> *d;
1001private:
1002 inline void _capacityOptimize(ZF_IN_OUT zfindex &capacity) {
1003 _ZFP_ZFCoreArrayCapacity<
1004 sizeof(T_Element) <= sizeof(void *)
1005 ? 0
1006 : (sizeof(T_Element) <= 4 * sizeof(void *)
1007 ? 1
1008 : 2
1009 )
1010 >::capacityOptimize(capacity);
1011 }
1012 inline void _capacityRequire(ZF_IN zfindex capacity) {
1013 _capacityOptimize(capacity);
1014 if(capacity > this->capacity()) {
1015 _capacityDoChange(capacity);
1016 }
1017 }
1018 void _capacityDoChange(ZF_IN zfindex capacity) {
1019 if(capacity == 0) {
1020 if(d) {
1021 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(d->buf, d->buf + d->count);
1022 zffree(d->buf);
1023 d->buf = zfnull;
1024 d->capacity = 0;
1025 d->count = 0;
1026 }
1027 }
1028 else {
1029 if(d == zfnull) {
1030 d = zfpoolNew(_ZFP_ZFCoreArrayPrivate<T_Element>);
1031 }
1032
1033 T_Element *oldBuf = d->buf;
1034 zfuint oldCount = d->count;
1035
1036 T_Element *newBuf = (T_Element *)zfmalloc(capacity * sizeof(T_Element));
1037 _ZFP_ZFCoreArrayW<T_Element>::objCreate(newBuf, newBuf + oldCount, oldBuf);
1038
1039 d->buf = newBuf;
1040 d->capacity = (zfuint)capacity;
1041 d->count = oldCount;
1042
1043 _ZFP_ZFCoreArrayW<T_Element>::objDestroy(oldBuf, oldBuf + oldCount);
1044 zffree(oldBuf);
1045 }
1046 }
1047};
1048ZFOUTPUT_TYPE_TEMPLATE(typename T_Element, ZFCoreArray<T_Element>, {v.objectInfoT(s);})
1049
1050template<typename T_Element>
1051zfclassLikePOD _ZFP_ZFCoreArrayCreate {
1052public:
1053 inline _ZFP_ZFCoreArrayCreate<T_Element> &add(ZF_IN T_Element const &v) {
1054 this->v.add(v);
1055 return *this;
1056 }
1057
1058public:
1060};
1061#define _ZFP_ZFCoreArrayCreate_action_expand(value) .add(value)
1062#define _ZFP_ZFCoreArrayCreate_action(CreatorType, values, ...) \
1063 CreatorType() ZFM_FIX_PARAM(_ZFP_ZFCoreArrayCreate_action_expand, ZFM_EMPTY, values, ##__VA_ARGS__) .v
1072#define ZFCoreArrayCreate(ElementType, values, ...) _ZFP_ZFCoreArrayCreate_action(_ZFP_ZFCoreArrayCreate<ElementType>, values, ##__VA_ARGS__)
1073
1075
1076#endif // #ifndef _ZFI_ZFCoreArray_h_
1077
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, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault)
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, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault)
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:304
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 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:342
virtual void capacityTrim(void)
trim current capacity
Definition ZFCoreArray.h:525
ZFCoreArray(void)
main constructor
Definition ZFCoreArray.h:347
virtual void capacity(zfindex newCapacity)
change capacity to hold at least newCapacity
Definition ZFCoreArray.h:521
virtual void genericAddFrom(const ZFCoreArrayBase &ref)
generic version
Definition ZFCoreArray.h:990
virtual zfbool isEmpty(void) const
true if empty
Definition ZFCoreArray.h:918
T_Element const & getLast(void) const
try to get first element, assert fail if empty
Definition ZFCoreArray.h:898
virtual void genericAdd(const void *e, zfindex index)
generic version
Definition ZFCoreArray.h:985
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:758
T_Element & get(zfindex index)
get element's reference at index
Definition ZFCoreArray.h:863
zfindex objectRetainCount(void) const
get retain count
Definition ZFCoreArray.h:431
virtual void objectInfoOfContentT(zfstring &ret, zfindex maxCount=((zfindex) -1), const ZFTokenForContainer &token=_ZFP_ZFTokenForContainerDefault) const
return content info
Definition ZFCoreArray.h:461
void add(T_Element const &e)
add element
Definition ZFCoreArray.h:541
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:703
virtual zfstring objectInfoOfContent(zfindex maxCount=((zfindex) -1), const ZFTokenForContainer &token=_ZFP_ZFTokenForContainerDefault) const
return content info
Definition ZFCoreArray.h:469
virtual zfbool isContain(T_Element const &e, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault) const
true if contains element
Definition ZFCoreArray.h:920
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:721
virtual void genericCopyFrom(const ZFCoreArrayBase &ref)
generic version
Definition ZFCoreArray.h:979
void add(T_Element const &e, zfindex index)
add element at index
Definition ZFCoreArray.h:549
zfindex findReversely(T_Another const &e, typename ZFComparer< T_Element, T_Another >::Comparer comparer) const
find element reversely
Definition ZFCoreArray.h:651
virtual void remove(zfindex index)
remove element at index with count, assert fail if out of range
Definition ZFCoreArray.h:776
T_Element const & get(zfindex index) const
get element's const reference at index
Definition ZFCoreArray.h:870
virtual zfindex genericFindReversely(const void *e)
generic version
Definition ZFCoreArray.h:983
zfindex find(T_Element const &e, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault) const
find element
Definition ZFCoreArray.h:601
void addFrom(const ZFCoreArray< T_Element > &ref)
add from another array
Definition ZFCoreArray.h:592
zfstring objectInfoOfContent(zfindex maxCount, const ZFTokenForContainer &token, typename ZFCoreInfoGetter< T_Element >::InfoGetter infoGetter) const
return content info
Definition ZFCoreArray.h:509
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:686
T_Element removeAndGet(zfindex index)
remove and return the removed value
Definition ZFCoreArray.h:798
T_Element removeLastAndGet(void)
remove last and return the removed value, or assert fail if empty
Definition ZFCoreArray.h:816
virtual zfindex capacity(void) const
get capacity
Definition ZFCoreArray.h:533
T_Element const & getFirst(void) const
try to get first element, assert fail if empty
Definition ZFCoreArray.h:891
virtual const void * genericGet(zfindex index) const
generic version
Definition ZFCoreArray.h:997
virtual void move(zfindex fromIndex, zfindex toIndexOrIndexMax)
move element
Definition ZFCoreArray.h:823
ZFCoreArray(const ZFCoreArray< T_Element > &ref)
construct from another array
Definition ZFCoreArray.h:351
ZFCompareResult objectCompareValue(const ZFCoreArray< T_Element > &ref, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault) const
compare by content
Definition ZFCoreArray.h:441
void set(zfindex index, T_Element const &e)
set element at index, or assert fail if index out of range
Definition ZFCoreArray.h:851
T_Element removeFirstAndGet(void)
remove first and return the removed value, or assert fail if empty
Definition ZFCoreArray.h:807
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:738
void swap(ZFCoreArray< T_Element > &ref)
swap internal data
Definition ZFCoreArray.h:406
virtual void genericSet(zfindex index, const void *e)
generic version
Definition ZFCoreArray.h:992
void objectInfoOfContentT(zfstring &ret, zfindex maxCount, const ZFTokenForContainer &token, typename ZFCoreInfoGetter< T_Element >::InfoGetter infoGetter) const
see objectInfoOfContent
Definition ZFCoreArray.h:479
virtual void remove(zfindex index, zfindex count)
remove element at index with count, assert fail if out of range
Definition ZFCoreArray.h:783
virtual zfindex genericFind(const void *e)
generic version
Definition ZFCoreArray.h:981
void addFrom(const T_Element *src, zfindex count)
add elements, src can be part of this array's buffer
Definition ZFCoreArray.h:569
T_Element * arrayBuf(void)
directly access the array
Definition ZFCoreArray.h:909
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:668
void copyFrom(const ZFCoreArray< T_Element > &ref)
copy all settings and contents from another array
Definition ZFCoreArray.h:415
const T_Element * arrayBuf(void) const
see arrayBuf
Definition ZFCoreArray.h:913
zfindex findReversely(T_Element const &e, typename ZFComparer< T_Element >::Comparer comparer=_ZFP_ZFComparerDefault) const
find element reversely
Definition ZFCoreArray.h:617
virtual void genericSwap(ZFCoreArrayBase &ref)
generic version
Definition ZFCoreArray.h:977
virtual void sort(zfindex start=0, zfindex count=((zfindex) -1))
sort
Definition ZFCoreArray.h:929
virtual ZFCoreArrayBase * refNew(void) const
new reference
Definition ZFCoreArray.h:367
ZFCompareResult objectCompare(const ZFCoreArray< T_Element > &ref) const
compare by instance
Definition ZFCoreArray.h:435
virtual zfindex count(void) const
element count of this array
Definition ZFCoreArray.h:916
void sort(zfindex start, zfindex count, typename ZFComparer< T_Element >::Comparer elementComparer)
sort element
Definition ZFCoreArray.h:945
virtual void sortReversely(zfindex start=0, zfindex count=((zfindex) -1))
sort reversely
Definition ZFCoreArray.h:936
void sortReversely(zfindex start, zfindex count, typename ZFComparer< T_Element >::Comparer elementComparer)
sort element
Definition ZFCoreArray.h:961
virtual void * refImpl(void) const
get the impl
Definition ZFCoreArray.h:369
zfindex find(T_Another const &e, typename ZFComparer< T_Element, T_Another >::Comparer comparer) const
find element
Definition ZFCoreArray.h:634
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