425 void reduce(RES& res, ::std::function<
void(RES&,
const TYPE&)> fn)
const;
427 void reduce(RES& res, ::std::function<
void(RES&, TYPE&)> fn);
434 CONTAINER *m_Container;
436 CONTAINER * EnsureCapacity(
const int Capacity,
const bool SkipDelete);
437 CONTAINER * Resize(
const int Capacity,
const bool SkipDelete);
443class cArray :
public cList<TYPE, cList_TrivialContainer<TYPE> >{
448template<
class TYPE,
class CONTAINER>
450 m_Container =
nullptr;
455template<
class TYPE,
class CONTAINER>
461template<
class TYPE,
class CONTAINER>
468template<
class TYPE,
class CONTAINER>
475template<
class TYPE,
class CONTAINER>
482template<
class TYPE,
class CONTAINER>
485 SetCount(Count, Fill);
489template<
class TYPE,
class CONTAINER>
495template<
class TYPE,
class CONTAINER>
497 int CurCount = (
nullptr == m_Container) ? 0 : m_Container->Count;
498 return CurCount *
sizeof(TYPE);
502template<
class TYPE,
class CONTAINER>
504 int CurCapacity = (
nullptr == m_Container) ? 0 : m_Container->Capacity;
505 return CurCapacity *
sizeof(TYPE);
509template<
class TYPE,
class CONTAINER>
512 int SrcCount = (
nullptr == Src.m_Container) ? 0 : Src.m_Container->Count;
513 EnsureCapacity(SrcCount,
false);
514 if(m_Container !=
nullptr) {
515 m_Container->Count = SrcCount;
516 for(i = 0; i < m_Container->Count; i++) {
517 m_Container->List[i] = Src.m_Container->List[i];
523template<
class TYPE,
class CONTAINER>
527 CONTAINER *OldContainer = EnsureCapacity(Count,
true);
528 if(m_Container !=
nullptr) {
529 m_Container->Count = Count;
530 for(i = 0; i < m_Container->Count; i++) {
531 m_Container->List[i] = Src[i];
534 if(OldContainer !=
nullptr) {
535 if (!std::is_trivially_copy_constructible<TYPE>()){
536 OldContainer->DestroyElements();
538 CONTAINER::Free(OldContainer);
543template<
class TYPE,
class CONTAINER>
546 Dest.m_Container = m_Container;
547 m_Container =
nullptr;
551template<
class TYPE,
class CONTAINER>
553 const int n = Count();
554 if ( from >= n ) from = n - 1;
else if ( from < 0 ) from = 0;
555 if ( to >= n ) to = n - 1;
else if ( to < 0 ) to = 0;
556 if ( from == to )
return false;
557 const TYPE t = m_Container->List[ from ];
560 m_Container->List + from,
561 m_Container->List + from + 1,
562 (to - from) *
sizeof( TYPE ) );
565 m_Container->List + to + 1,
566 m_Container->List + to,
567 (from - to) *
sizeof( TYPE ) );
569 m_Container->List[ to ] = t;
573template <
class TYPE,
class CONTAINER>
579template<
class TYPE,
class CONTAINER>
581 cAssert((
nullptr == m_Container) || (m_Container != Src->m_Container));
584 m_Container = Src->m_Container;
585 Src->m_Container =
nullptr;
589template<
class TYPE,
class CONTAINER>
595template<
class TYPE,
class CONTAINER>
597 cMath::Swap(m_Container, With->m_Container);
600template <
class TYPE,
class CONTAINER>
601template <
class Reader>
606 BS.Read(ToPtr(), Count() *
sizeof(TYPE));
610template <
class TYPE,
class CONTAINER>
611template <
class Writer>
613 BS.Write(ToPtr(), Count() *
sizeof(TYPE));
617template<
class TYPE,
class CONTAINER>
620 cAssert(m_Container !=
nullptr);
621 cAssert(Index < m_Container->Count);
622 return m_Container->List[Index];
626template<
class TYPE,
class CONTAINER>
629 cAssert(m_Container !=
nullptr);
630 cAssert(Index < m_Container->Count);
631 return m_Container->List[Index];
635template<
class TYPE,
class CONTAINER>
638 cAssert(m_Container !=
nullptr);
639 cAssert(Index < m_Container->Count);
640 return m_Container->List[Index];
644template<
class TYPE,
class CONTAINER>
647 cAssert(m_Container !=
nullptr);
648 cAssert(Index < m_Container->Count);
649 return m_Container->List[Index];
653template<
class TYPE,
class CONTAINER>
655 cAssert(m_Container !=
nullptr);
656 cAssert(m_Container->Count >= 1);
657 return m_Container->List[0];
661template<
class TYPE,
class CONTAINER>
663 cAssert(m_Container !=
nullptr);
664 cAssert(m_Container->Count >= 1);
665 return m_Container->List[0];
669template<
class TYPE,
class CONTAINER>
671 cAssert(m_Container !=
nullptr);
672 cAssert(m_Container->Count >= 1);
673 return m_Container->List[m_Container->Count - 1];
677template<
class TYPE,
class CONTAINER>
679 cAssert(m_Container !=
nullptr);
680 cAssert(m_Container->Count >= 1);
681 return m_Container->List[m_Container->Count - 1];
685template<
class TYPE,
class CONTAINER>
688 cAssert(m_Container !=
nullptr);
689 cAssert(Index < m_Container->Count);
690 m_Container->List[Index] = Value;
694template<
class TYPE,
class CONTAINER>
696 return (
nullptr == m_Container) ? nullptr : m_Container->List;
700template<
class TYPE,
class CONTAINER>
702 return (
nullptr == m_Container) ? nullptr : m_Container->List;
708template<
class TYPE,
class CONTAINER>
712 int CurCount = (
nullptr == m_Container) ? 0 : m_Container->Count;
713 cAssert(Index + Count <= CurCount);
719 for(i = Index; i < Index + Count; i++) {
720 To->Add(m_Container->List[i]);
725template<
class TYPE,
class CONTAINER>
727 if(m_Container !=
nullptr) {
728 m_Container->DestroyElements();
729 CONTAINER::Free(m_Container);
730 m_Container =
nullptr;
734template<
class TYPE,
class CONTAINER>
736 if (Index >= 0 && Index < Count())
return (*
this)[Index];
740template<
class TYPE,
class CONTAINER>
744 if (Index >= n)Add(defvalue, Index - n + 1);
745 (*this)[Index] = value;
750template<
class TYPE,
class CONTAINER>
752 if(m_Container !=
nullptr) {
754 for(i = 0; i < m_Container->Count; i++) {
755 delete m_Container->List[i];
756 m_Container->List[i] =
nullptr;
762template<
class TYPE,
class CONTAINER>
764 if(m_Container !=
nullptr) {
765 Resize(m_Container->Count,
false);
770template<
class TYPE,
class CONTAINER>
772 if(m_Container !=
nullptr) {
773 m_Container->Count = 0;
778template<
class TYPE,
class CONTAINER>
781 static_assert( mod % 8 == 0,
782 "`mod` must be an integral of multiple of 8" );
783 const int ncap = Count() * mod / 8;
788template<
class TYPE,
class CONTAINER>
790 return (
nullptr == m_Container) ? 0 : m_Container->Count;
794template<
class TYPE,
class CONTAINER>
796 return (
nullptr == m_Container) || (m_Container->Count < 1);
800template<
class TYPE,
class CONTAINER>
802 return (
nullptr == m_Container) ? 0 : m_Container->Capacity;
806template<
class TYPE,
class CONTAINER>
808 Resize(Capacity,
false);
814template<
class TYPE,
class CONTAINER>
818 int CurCount = (
nullptr == m_Container) ? 0 : m_Container->Count;
819 CONTAINER *OldContainer = EnsureCapacity(CurCount + Count,
true);
820 for(i = 0; i < Count; i++) {
821 m_Container->List[m_Container->Count] = Value;
822 m_Container->Count++;
824 if(OldContainer !=
nullptr) {
825 if (!std::is_trivially_copy_constructible<TYPE>()){
826 OldContainer->DestroyElements();
828 CONTAINER::Free(OldContainer);
830 return ((
nullptr == m_Container) ? 0 : m_Container->Count) - 1;
833template <
class TYPE,
class CONTAINER>
834template <
class array>
837 int CurCount = (
nullptr == m_Container) ? 0 : m_Container->Count;
838 int SrcCount = Src.Count();
839 EnsureCapacity(CurCount + SrcCount,
false);
840 for (i = 0; i < SrcCount; i++) {
841 m_Container->List[m_Container->Count] = Src[i];
842 m_Container->Count++;
849template<
class TYPE,
class CONTAINER>
853 int CurCount = (
nullptr == m_Container) ? 0 : m_Container->Count;
854 CONTAINER *OldContainer = EnsureCapacity(CurCount + Count,
true);
855 for(i = 0; i < Count; i++) {
856 m_Container->List[m_Container->Count] = Src[i];
857 m_Container->Count++;
859 if(OldContainer !=
nullptr) {
860 if (!std::is_trivially_copy_constructible<TYPE>()){
861 OldContainer->DestroyElements();
863 CONTAINER::Free(OldContainer);
870template<
class TYPE,
class CONTAINER>
872 cAssert(StartIndex >= 0);
874 cAssert(StartIndex + Count <= ((
nullptr == m_Container) ? 0 : m_Container->Count));
878 for(i = StartIndex; i < StartIndex + Count; i++) {
879 if(E(&m_Container->List[i], &Value)) {
887template<
class TYPE,
class CONTAINER>
889 return IndexOf(Value, StartIndex, Count() - StartIndex, E);
892template<
class TYPE,
class CONTAINER>
894 return IndexOf(Value, 0, Count(), E);
901template<
class TYPE,
class CONTAINER>
903 return LastIndexOf(Value, Count() - 1, Count(), E);
906template<
class TYPE,
class CONTAINER>
908 return LastIndexOf(Value, StartIndex, StartIndex + 1, E);
911template<
class TYPE,
class CONTAINER>
913 int CurCount = (
nullptr == m_Container) ? 0 : m_Container->Count;
915 cAssert(StartIndex >= -1);
916 cAssert(StartIndex <= 0);
921 cAssert(StartIndex >= 0);
922 cAssert(StartIndex < CurCount);
924 cAssert(Count <= StartIndex + 1);
928 i0 = StartIndex - Count + 1;
929 for(i = StartIndex; i >= i0; i--) {
930 if(E(&m_Container->List[i], &Value)) {
941template<
class TYPE,
class CONTAINER>
945 cAssert(Index + Count <= ((
nullptr == m_Container) ? 0 : m_Container->Count));
951 r = Index + Count - 1;
954 if(C(&Value, &GetAt(m)) <= 0) {
960 if(C(&Value, &GetAt(l)) == 0) {
968template<
class TYPE,
class CONTAINER>
970 return BinarySearch(0, Count(), Value, C);
975template<
class TYPE,
class CONTAINER>
978 int CurCount = (
nullptr == m_Container) ? 0 : m_Container->Count;
979 for(i = 0; i < CurCount; i++) {
980 if(E(&m_Container->List[i], &Value)) {
988template<
class TYPE,
class CONTAINER>
990 int i = IndexOf(Value, E);
998template<
class TYPE,
class CONTAINER>
1000 const int i = IndexOf( Value, E );
1011template<
class TYPE,
class CONTAINER>
1013 cAssert(Index >= 0);
1014 cAssert(Count >= 0);
1015 cAssert(Index + Count <= ((
nullptr == m_Container) ? 0 : m_Container->Count));
1017 if(m_Container !=
nullptr) {
1019 if (Index + lcount > m_Container->Count){
1020 lcount = m_Container->Count - Index;
1022 m_Container->Count -= lcount;
1023 for(i = Index; i < m_Container->Count; i++) {
1024 m_Container->List[i] = m_Container->List[i + lcount];
1030template<
class TYPE,
class CONTAINER>
1032 cAssert(m_Container !=
nullptr);
1033 cAssert(m_Container->Count >= 1);
1034 m_Container->Count--;
1040template<
class TYPE,
class CONTAINER>
1042 int Index = IndexOf(Value, E);
1053template<
class TYPE,
class CONTAINER>
1055 cAssert(Index >= 0);
1056 int CurCount = (
nullptr == m_Container) ? 0 : m_Container->Count;
1057 cAssert(Index <= CurCount);
1058 cAssert(Count >= 0);
1062 CONTAINER *OldContainer = EnsureCapacity(CurCount + Count,
true);
1063 for(i = CurCount - 1 + Count; i >= Index + Count; i--) {
1064 m_Container->List[i] = m_Container->List[i - Count];
1066 if(m_Container !=
nullptr) {
1067 m_Container->Count += Count;
1068 cAssert(Index + Count <= m_Container->Count);
1070 for(i = 0; i < Count; i++) {
1071 m_Container->List[Index + i] = Value;
1073 if(OldContainer !=
nullptr) {
1074 if (!std::is_trivially_copy_constructible<TYPE>()){
1075 OldContainer->DestroyElements();
1077 CONTAINER::Free(OldContainer);
1084template<
class TYPE,
class CONTAINER>
1086 cAssert(Index >= 0);
1087 int CurCount = (
nullptr == m_Container) ? 0 : m_Container->Count;
1088 cAssert(Index <= CurCount);
1089 int SrcCount = (
nullptr == Src.m_Container) ? 0 : Src.m_Container->Count;
1093 EnsureCapacity(CurCount + SrcCount,
false);
1094 for(i = CurCount - 1 + SrcCount; i >= Index + SrcCount; i--) {
1095 m_Container->List[i] = m_Container->List[i - SrcCount];
1097 if(m_Container !=
nullptr) {
1098 m_Container->Count += SrcCount;
1099 cAssert(Index + SrcCount <= m_Container->Count);
1101 for(i = 0; i < SrcCount; i++) {
1102 m_Container->List[Index + i] = Src.m_Container->List[i];
1109template<
class TYPE,
class CONTAINER>
1111 cAssert(Index >= 0);
1112 int CurCount = (
nullptr == m_Container) ? 0 : m_Container->Count;
1113 cAssert(Index <= CurCount);
1114 cAssert(Count >= 0);
1118 CONTAINER *OldContainer = EnsureCapacity(CurCount + Count,
true);
1119 for(i = CurCount - 1 + Count; i >= Index + Count; i--) {
1120 m_Container->List[i] = m_Container->List[i - Count];
1122 if(m_Container !=
nullptr) {
1123 m_Container->Count += Count;
1124 cAssert(Index + Count <= m_Container->Count);
1126 for(i = 0; i < Count; i++) {
1127 m_Container->List[Index + i] = Src[i];
1129 if(OldContainer !=
nullptr) {
1130 if (!std::is_trivially_copy_constructible<TYPE>()){
1131 OldContainer->DestroyElements();
1133 CONTAINER::Free(OldContainer);
1141template<
class TYPE,
class CONTAINER>
1143 if ( Index >= Count() ) {
1144 Add( Value, Index - Count() + 1 );
1146 return (*
this)[ Index ];
1152template<
class TYPE,
class CONTAINER>
1154 const int i = find( Value );
1155 (i == -1) ? Insert( 0, Value ) : RemoveAt( i, 1 );
1162template<
class TYPE,
class CONTAINER>
1164 cAssert(Index >= 0);
1165 cAssert(Count >= 0);
1166 int CurCount = (
nullptr == m_Container) ? 0 : m_Container->Count;
1167 cAssert(Index + Count <= CurCount);
1173 for(i = 0; i < h; i++) {
1174 cMath::Swap(m_Container->List[Index + i], m_Container->List[Index + Count - 1 - i]);
1179template<
class TYPE,
class CONTAINER>
1181 if(m_Container !=
nullptr) {
1182 Reverse(0, m_Container->Count);
1190template<
class TYPE,
class CONTAINER>
1192 cAssert(Capacity >= 0);
1199 int OldCapacity = (
nullptr == m_Container) ? 0 : m_Container->Capacity;
1200 if(Capacity == OldCapacity) {
1204 CONTAINER *OldContainer = m_Container;
1205 m_Container = CONTAINER::New(Capacity);
1206 m_Container->Count = (
nullptr == OldContainer) ? 0 : OldContainer->Count;
1207 m_Container->Capacity = Capacity;
1209 if(Capacity < m_Container->Count) {
1210 m_Container->Count = Capacity;
1213 if(OldContainer !=
nullptr) {
1214 OldContainer->Copy(m_Container, m_Container->Count);
1216 return OldContainer;
1218 if (!std::is_trivially_copy_constructible<TYPE>()){
1219 OldContainer->DestroyElements();
1221 CONTAINER::Free(OldContainer);
1230template<
class TYPE,
class CONTAINER>
1232 int OldCapacity = (
nullptr == m_Container) ? 0 : m_Container->Capacity;
1233 if(Capacity > OldCapacity) {
1234 int S = Capacity + OldCapacity / 2;
1236 return Resize(S, SkipDelete);
1241template<
class TYPE,
class CONTAINER>
1243 EnsureCapacity(size,
false);
1247template<
class TYPE,
class CONTAINER>
1249 cAssert(Count >= 0);
1250 EnsureCapacity(Count,
false);
1251 if(m_Container !=
nullptr) {
1252 m_Container->Count = Count;
1257template<
class TYPE,
class CONTAINER>
1260 int CurCount = (
nullptr == m_Container) ? 0 : m_Container->Count;
1261 for(i = 0; i < CurCount; i++) {
1262 m_Container->List[i] = Value;
1267template<
class TYPE,
class CONTAINER>
1271 for (
int i = n0; i < _Count; i++)SetAt(i, Value);
1277template<
class TYPE,
class CONTAINER>
1279 cAssert(Index >= 0);
1280 cAssert(Count >= 0);
1281 cAssert(Index + Count <= ((
nullptr == m_Container) ? 0 : m_Container->Count));
1283 int i = 0, j = 0, M = 0;
1289 for(i = M; i < Count; i++) {
1290 K = GetAt(i + Index);
1293 while(j >= 0 && !f) {
1294 if(C(&K, &GetAt(j + Index)) < 0) {
1295 SetAt(j + M + Index, GetAt(j + Index));
1301 SetAt(j + M + Index, K);
1310template<
class TYPE,
class CONTAINER>
1312 cAssert(Index >= 0);
1313 cAssert(Count >= 0);
1314 int CurCount = (
nullptr == m_Container) ? 0 : m_Container->Count;
1315 cAssert(Index + Count <= CurCount);
1317 int i = 0, j = 0, M = 0;
1323 for(i = M; i < Count; i++) {
1324 K = GetAt(i + Index);
1327 while(j >= 0 && !f) {
1328 if(C(K, GetAt(j + Index))) {
1331 SetAt(j + M + Index, GetAt(j + Index));
1335 SetAt(j + M + Index, K);
1342template<
class TYPE,
class CONTAINER>
1344 Sort(0, Count(), C);
1347template<
class TYPE,
class CONTAINER>
1349 Sort(0, Count(), C);
1353template<
class TYPE,
class CONTAINER>
1355 int CurCount = (
nullptr == m_Container) ? 0 : m_Container->Count;
1356 for(
int i = 0; i < CurCount; i++) {
1357 m_Container->List[i] += u;
1362template<
class TYPE,
class CONTAINER>
1364 int CurCount = (
nullptr == m_Container) ? 0 : m_Container->Count;
1365 for(
int i = 0; i < CurCount; i++) {
1366 m_Container->List[i] -= u;
1371template<
class TYPE,
class CONTAINER>
1373 int CurCount = (
nullptr == m_Container) ? 0 : m_Container->Count;
1374 for(
int i = 0; i < CurCount; i++) {
1375 m_Container->List[i] *= u;
1380template<
class TYPE,
class CONTAINER>
1382 int CurCount = (
nullptr == m_Container) ? 0 : m_Container->Count;
1383 for(
int i = 0; i < CurCount; i++) {
1384 m_Container->List[i] /= u;
1389template<
class TYPE,
class CONTAINER>
1391 int CurCount = (
nullptr == m_Container) ? 0 : m_Container->Count;
1392 int OtherCount = (
nullptr == Other.m_Container) ? 0 : Other.m_Container->Count;
1393 if(CurCount != OtherCount) {
1396 for(
int i = 0; i < CurCount; i++) {
1397 if(m_Container->List[i] != Other.m_Container->List[i]) {
1405template<
class TYPE,
class CONTAINER>
1407 return !(*
this == Other);
1410template<
class TYPE,
class CONTAINER>
1412 for(
int i = 0; i < l.Count(); ++i) {
1413 const bool needSeparator = ((i + 1) != l.Count());
1414 out << l[i] << (needSeparator ?
" " :
"");
1419template<
class TYPE,
class CONTAINER>
1421 for (
int i = 0; i < Count(); i++)
if (fn((*
this)[i]))
return true;
1425template<
class TYPE,
class CONTAINER>
1427 for (
int i = 0; i < Count(); i++)
if (fn((*
this)[i]))
return true;
1431template<
class TYPE,
class CONTAINER>
1433 for (
int i = 0; i < Count(); i++)
if (fn((*
this)[i]))
return i;
1437template<
class TYPE,
class CONTAINER>
1439 for (
int i = 0; i < Count(); i++)
if (fn((*
this)[i]))
return i;
1443template<
class TYPE,
class CONTAINER>
1445 for (
int i = 0; i < Count(); i++)
if (!fn((*
this)[i]))
return false;
1449template<
class TYPE,
class CONTAINER>
1451 for (
int i = 0; i < Count(); i++) fn((*
this)[i]);
1454template<
class TYPE,
class CONTAINER>
1456 for (
int i = 0; i < Count(); i++) fn((*
this)[i]);
1459template<
class TYPE,
class CONTAINER>
1462 for (
int i = 0; i < Count(); i++)
if (fn((*
this)[i])) result.Add((*
this)[i]);
1465template<
class TYPE,
class CONTAINER>
1468 for (
int i = 0; i < Count(); i++) fn(res, (*
this)[i]);