OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
template.h
Go to the documentation of this file.
1//------------------------------------------------------------------------------88
2// ObjectWindows
3// Copyright (c) 1998 by Yura Bidus
4//
5/// \file
6/// Definition of container classes used and made available by OWL
7//------------------------------------------------------------------------------
8
9#if !defined(OWL_TEMPLATE_H)
10#define OWL_TEMPLATE_H
11
12#include <owl/private/defs.h>
13#if defined(BI_HAS_PRAGMA_ONCE)
14# pragma once
15#endif
16
17#include <owl/defs.h>
18#include <owl/private/wsysinc.h>
19#include <new>
20#include <algorithm>
21
22namespace owl {
23
24#include <owl/preclass.h>
25
26/// \addtogroup utility
27/// @{
28
29//-------------------------------------------------------------------------
30//
31/// Provides class-specific operator new and operator delete that simply call
32/// the global operator new and operator delete. That is, TStandardAllocator
33/// does not provide any specialized behavior. It is used in the non-managed
34/// versions of the parametrized containers.
35//
37{
38public:
39
40 void operator delete(void* ptr);
41 void* operator new(size_t sz);
42
43 void* Alloc(size_t sz);
44 void Free(void* ptr);
45 void* ReAlloc(void* ptr, size_t sz);
46
47};
48
50
51/// @}
52
53inline void TStandardAllocator::operator delete(void* ptr)
54{
55 ::operator delete(ptr);
56}
57
58inline void* TStandardAllocator::operator new(size_t sz)
59{
60 return ::operator new(sz);
61}
62
63inline void* TStandardAllocator::Alloc(size_t sz)
64{
65 return ::malloc(sz);
66}
67
68inline void TStandardAllocator::Free(void* ptr)
69{
70 ::free(ptr);
71}
72
73inline void* TStandardAllocator::ReAlloc(void* ptr, size_t sz)
74{
75 return ::realloc(ptr, sz);
76}
77
78/// \addtogroup utility
79/// @{
80
81//----------------------------------------------------------------------------
82//
83/// Provides class-specific operator new and operator delete that allocate from
84/// local memory.
85//
87{
88public:
89
90 void* operator new(size_t sz);
91 void operator delete(void* ptr);
92
93 void* Alloc(size_t sz);
94 void Free(void* ptr);
95 void* ReAlloc(void* ptr, size_t sz);
96
97};
98
99/// @}
100
101inline void* TLocalAllocator::operator new(size_t sz)
102{
103 return static_cast<void*>(::LocalAlloc(LPTR, sz));
104}
105
106inline void TLocalAllocator::operator delete(void* ptr)
107{
108 ::LocalFree(static_cast<HLOCAL>(ptr));
109}
110
111inline void* TLocalAllocator::Alloc(size_t sz)
112{
113 return static_cast<void*>(::LocalAlloc(LPTR, sz));
114}
115
116inline void TLocalAllocator::Free(void* ptr)
117{
118 ::LocalFree(static_cast<HLOCAL>(ptr));
119}
120
121inline void* TLocalAllocator::ReAlloc(void* ptr, size_t sz)
122{
123 return static_cast<void*>(::LocalReAlloc(static_cast<HLOCAL>(ptr), sz, LPTR));
124}
125
126/// \addtogroup utility
127/// @{
128
129//----------------------------------------------------------------------------
130//
131/// Provides class-specific operator new and operator delete that allocate from
132/// global memory.
133//
135{
136public:
137
138 void* operator new(size_t sz);
139 void operator delete(void* ptr);
140
141 void* Alloc(size_t sz);
142 void Free(void* ptr);
143 void* ReAlloc(void* ptr, size_t sz);
144
145};
146
147/// @}
148
149inline void* TGlobalAllocator::operator new(size_t sz)
150{
151 return static_cast<void*>(::GlobalLock(::GlobalAlloc(GPTR, sz)));
152}
153
154inline void TGlobalAllocator::operator delete(void* ptr)
155{
156 HGLOBAL hMem = static_cast<HGLOBAL>(GlobalHandle(ptr));
157 if (GlobalUnlock(hMem) == 0)
159}
160
161inline void* TGlobalAllocator::Alloc(size_t sz)
162{
163 return static_cast<void*>(::GlobalLock(::GlobalAlloc(GPTR, sz)));
164}
165
166inline void TGlobalAllocator::Free(void* ptr)
167{
168 HGLOBAL hMem = static_cast<HGLOBAL>(GlobalHandle(ptr));
169 if (GlobalUnlock(hMem) == 0)
171}
172
173inline void* TGlobalAllocator::ReAlloc(void* ptr, size_t sz)
174{
175 HGLOBAL hMem = static_cast<HGLOBAL>(GlobalHandle(ptr));
176 if (hMem && GlobalUnlock(hMem) == 0)
177 return static_cast<void*>(::GlobalLock(GlobalReAlloc(hMem, sz, GPTR)));
178 return nullptr;
179}
180
181/// \addtogroup utility
182/// @{
183
184//--------------------------------------------------------------------------
185//
186/// Used internally.
187//
188template <class A>
189class TMBaseMemBlocks;
190
191template <class A>
193{
194public:
195
198
199private:
200
201 void* operator new(size_t, size_t, const A&);
202 void operator delete(void*);
203
204 void operator delete(void* p, size_t, const A&);
205
207 friend class TMBaseMemBlocks<A>;
208
209};
210
211/// @}
212
213template <class A>
217
218template <class A>
219inline void* TMBlockList<A>::operator new(size_t sz, size_t extra, const A&)
220{
221 return A::operator new(sz + extra);
222}
223
224template <class A>
225inline void TMBlockList<A>::operator delete(void* ptr)
226{
227 A::operator delete (ptr);
228}
229
230template <class A>
231inline void TMBlockList<A>::operator delete(void* p, size_t, const A&)
232{
233 A::operator delete (p);
234}
235
236/// \addtogroup utility
237/// @{
238
239//--------------------------------------------------------------------------
240//
241/// Used internally.
242//
244 : public TMBlockList<TStandardAllocator>
245{
246public:
247
249
250};
251
252//--------------------------------------------------------------------------
253//
254/// Used internally.
255//
256template <class A>
258 : public A
259{
260public:
261
262 TMBaseMemBlocks(size_t = 8192);
264
265 char* Block() const { return REINTERPRET_CAST(char*, CurBlock); }
266 unsigned Count() const { return BlockCount; }
267 int AllocBlock(size_t);
268 void FreeTo(unsigned);
269 size_t GetBlockSize() const { return BlockSize; }
270
271private:
272
273 TMBlockList<A>* CurBlock;
274 const size_t BlockSize;
275 unsigned BlockCount;
276
277};
278
279/// @}
280
281template <class A>
283 : CurBlock(0), BlockSize(sz), BlockCount(0)
284{
285 CHECK(sz != 0);
286}
287
288template <class A>
290{
291
292#if !defined( BI_WINDOWS_WEP_BUG )
293
294 FreeTo(0);
295
296#endif
297
298}
299
300template <class A>
302{
303 TMBlockList<A>* temp = new(std::max(sz, BlockSize), *this) TMBlockList<A>(CurBlock);
304 CurBlock = temp + 1;
305 BlockCount++;
306 return 1;
307}
308
309template <class A>
311{
312 PRECONDITION(BlockCount >= term);
313 while (BlockCount > term)
314 {
315 TMBlockList<A>* temp = CurBlock - 1;
316 CurBlock = temp->Next;
317
318 delete temp;
319 BlockCount--;
320 }
321}
322
323/// \addtogroup utility
324/// @{
325
326//--------------------------------------------------------------------------
327//
328/// Used internally.
329//
331 : public TMBaseMemBlocks<TStandardAllocator>
332{
333public:
334
338
339};
340
341//--------------------------------------------------------------------------
342//
343/// Managed memory stack.
344/// Implements mark and release style memory management, using the allocator A.
345//
346template <class A>
347class TMMarker;
348
349template <class A>
351{
352public:
353
354 TMMemStack(size_t = 8192);
355 void* Allocate(size_t);
356
357 friend class TMMarker<A>;
358
359private:
360
362 size_t CurLoc;
363
364};
365
366/// @}
367
368template <class A>
370 : Data(sz), CurLoc(sz)
371{
372 CHECK(sz != 0);
373}
374
375template <class A>
377{
378 sz = std::max(static_cast<size_t>(1), sz);
379 if (sz > Data.GetBlockSize() - CurLoc)
380 {
381 if (Data.AllocBlock(sz) == 0)
382 return nullptr;
383 else
384 CurLoc = 0;
385 }
386 void* temp = Data.Block() + CurLoc;
387 CurLoc += sz;
388 return temp;
389}
390
391/// \addtogroup utility
392/// @{
393
394//--------------------------------------------------------------------------
395//
396/// Provides the mark for TMMemStack.
397//
398template <class A>
400{
401public:
402
404 : Memstk(ms), Blk(ms.Data.Count()), CurLoc(ms.CurLoc)
405 {}
406
408 {
409 const auto ok = Blk < Memstk.Data.Count() || (Blk == Memstk.Data.Count() && CurLoc <= Memstk.CurLoc);
410 WARN(!ok, _T("TMMarker::~TMMarker: Terminating due to failed precondition."));
411 if (!ok) std::terminate();
412
413 Memstk.Data.FreeTo(Blk);
414 Memstk.CurLoc = CurLoc;
415 }
416
417private:
418
419 TMMemStack<A>& Memstk;
420 const unsigned Blk;
421 const size_t CurLoc;
422
423};
424
425//--------------------------------------------------------------------------
426//
427/// Implements mark and release style memory management using the standard allocator.
428//
430 : public TMMemStack<TStandardAllocator>
431{
432public:
434};
435
436//--------------------------------------------------------------------------
437//
438/// Provides the mark for TMemStack.
439//
441 : public TMMarker<TStandardAllocator>
442{
443public:
444
448
449};
450
451//--------------------------------------------------------------------------
452//
453/// Managed single-size block allocator.
454/// Allocates blocks of the size specified in the constructor, using the memory
455/// manager specified by A.
456//
457template <class A>
459{
460public:
461
462 TMMemBlocks(size_t sz, unsigned int count = 100);
463
464 void* Allocate(size_t sz);
465 void Free(void* block);
466
467private:
468
469 void* FreeList;
470 TMMemStack<A> Mem;
471 size_t Size;
472
473};
474
475/// @}
476
477template <class A>
478inline TMMemBlocks<A>::TMMemBlocks(size_t sz, unsigned int count)
479 : FreeList(nullptr), Mem(sz* count), Size(std::max(sz, sizeof(void*)))
480{
481 CHECK(sz != 0 && count != 0);
482}
483
484template <class A>
485inline void* TMMemBlocks<A>::Allocate(size_t sz)
486{
487 PRECONDITION(Size == std::max(sz, sizeof(void *))); InUse(sz);
488 if (FreeList == nullptr)
489 return Mem.Allocate(Size);
490 else
491 {
492 void *temp = FreeList;
493 FreeList = *static_cast<void**>(temp);
494 return temp;
495 }
496}
497
498template <class A>
499inline void TMMemBlocks<A>::Free(void* block)
500{
501 *static_cast<void **>(block) = FreeList;
502 FreeList = block;
503}
504
505/// \addtogroup utility
506/// @{
507
508//--------------------------------------------------------------------------
509/// Single-size block allocator.
510/// Allocates blocks of the size specified in the constructor, using the global operator new
511/// and operator delete.
512//
514 : public TMMemBlocks<TStandardAllocator>
515{
516public:
517
518 TMemBlocks(size_t sz, unsigned n = 100)
520 {}
521
522};
523
524/// @}
525
527
528template <class T> inline T* __owl_construct(void* P)
529{
530 return new(P)T;
531}
532
533template <class T> inline T* __owl_construct(void* P, const T& t)
534{
535 return new(P)T(t);
536}
537
538template <class T> inline void __owl_destruct(T* t)
539{
540 static_cast<void>(t); // Suppress "not in use" warning.
541 t->~T();
542}
543
544//
545// specializations
546// chars
547//
548inline char* __owl_construct(void* p, char c) { *(static_cast<char*>(p)) = c; return static_cast<char*>(p); }
549inline void __owl_destruct(char* /*t*/) {}
550
551//
552// integers
553//
554inline int* __owl_construct(void* p, int c) { *(static_cast<int*>(p)) = c; return static_cast<int*>(p); }
555inline unsigned int* __owl_construct(void* p, unsigned int c) { *(static_cast<unsigned int*>(p)) = c; return static_cast<unsigned int*>(p); }
556inline void __owl_destruct(int* /*t*/) {}
557
558//
559// long
560//
561inline long* __owl_construct(void* p, long c) { *(static_cast<long*>(p)) = c; return static_cast<long*>(p); }
562inline void __owl_destruct(long* /*t*/) {}
563
564/// \addtogroup utility
565/// @{
566
567//--------------------------------------------------------------------------
568// Container classes
569//
570
571//
572/// Iterator for TObjectContainer
573//
574template <class T, class T1>
576{
577public:
578
579 TObjArrayIterator(T1& array) :Vect(array), Cur(0) {}
580
581 operator bool() const { return Cur < static_cast<int>(Vect.Size()); }
582 const T& operator ++(int) { Cur++; return *Vect.Data[Cur - 1]; }
583 const T* operator ++() { return Cur < static_cast<int>(Vect.Size()) ? Vect.Data[++Cur] : 0; }
584 const T& operator *() const { return *Vect.Data[Cur]; }
585 T& operator *() { return *Vect.Data[Cur]; }
586 const T& Current() const { return *Vect.Data[Cur]; }
587 T& Current() { return *Vect.Data[Cur]; }
588 void Restart() { Cur = 0; }
589
590protected:
591
592 T1& Vect;
593 int Cur;
594
595};
596
597//
598/// Iterator for Pointer Container
599//
600template <class T, class T1>
602{
603public:
604
605 TPtrArrayIterator(T1& array) :Vect(array), Cur(0) {}
606
607 operator bool() const { return Cur < static_cast<int>(Vect.Size()); }
608 T operator ++(int) { Cur++; return Vect.Data[Cur - 1]; }
609 const T* operator ++() { return Cur < static_cast<int>(Vect.Size()) ? &Vect.Data[++Cur] : 0; }
610 const T operator *() const { return Vect.Data[Cur]; }
611 T operator *() { return Vect.Data[Cur]; }
612 const T Current() const { return Vect.Data[Cur]; }
613 T Current() { return Vect.Data[Cur]; }
614 void Restart() { Cur = 0; }
615
616protected:
617
618 T1& Vect;
619 int Cur;
620
621};
622
623//
624/// Map node.
625/// Usage:
626/// typedef TMapNode<string,string> TStringMapNode;
627/// typedef TSortedObjectArray<TStringMapNode> TStringMap;
628/// typedef TSortedObjectArray<TStringMapNode>::Iterator TStringMapIterator;
629//
630template <class T1, class T2>
632{
633public:
634
635 TMapNode(T1& name, T2& val) :Name(name), Value(val) {}
640
641 bool operator <(const TMapNode& ms) const { return Name < ms.Name; }
642 bool operator ==(const TMapNode& ms) const { return Name == ms.Name; }
644 {
645 Name = other.Name;
646 Value = other.Value;
647 return *this;
648 }
649
652
653};
654
655//------------------------------------------------------------------------------
656//
657/// Base array class.
658/// Reduces code size.
659//
661{
662public:
663
665
666 uint GetItemsInContainer() const { return Size(); }
667 int LowerBound() const { return 0; }
668 int UpperBound() const { return Size() - 1; }
669 uint ArraySize() const { return Reserved; }
670 bool IsFull() const { return ItemCnt >= Reserved - 1; }
671 bool IsEmpty() const { return Empty(); }
672 uint Size() const { return ItemCnt; }
673 uint Count() const { return ItemCnt; }
674 bool Empty() const { return ItemCnt == 0; }
675
676 //
677 // Support STL naming convention.
678 //
679 uint size() const { return ItemCnt; }
680 bool empty() const { return Empty(); }
681
682 static const int NPOS = -1;
683
684protected:
685
688
689};
690
691
692//------------------------------------------------------------------------------
693//
694/// Holds array pointers to strings.
695//
696template <class T, class A = TStandardAllocator>
698 : public A, public TArrayBase
699{
700public:
701
702 typedef void (*IterFunc)(T&, void*);
703 typedef bool (*CondFunc)(const T&, void*);
704
706 friend class TObjArrayIterator<T, TMObjectArray<T, A> >;
708
709 TMObjectArray(int upper, int lower = 0, int /*delta*/ = 0)
710 : Mem(sizeof(T), upper - lower + 1), Data(0) { Resize(upper); }
716
717 void RemoveEntry(int loc) { Remove(loc); }
718 void SqueezeEntry(unsigned loc) { Remove(loc); }
719 void Grow(int index);
720
721 int Add(const T& t);
722 void AddAt(const T& t, uint index);
723
724 bool DestroyItem(const T& t);
725 bool Destroy(int loc);
726 bool HasMember(const T& t) const;
727 int Find(const T& t) const;
728
730 const T& operator [](int loc) const;
731
732 void ForEach(IterFunc iter, void* args);
733 T* FirstThat(CondFunc cond, void* args) const;
734 T* LastThat(CondFunc cond, void* args) const;
735
736 void Flush();
737
738protected:
739
740 void Resize(int delta);
741 void Remove(int index);
742
743protected:
744
745 T** Data;
747
748};
749
750//------------------------------------------------------------------------------
751
752template <class T>
754 : public TMObjectArray<T, TStandardAllocator>
755{
756public:
757
758 typedef void (*IterFunc)(T&, void*);
759 typedef bool (*CondFunc)(const T&, void*);
760
763 friend Iterator;
764
770
772 {
774 return *this;
775 }
776
777};
778
779//------------------------------------------------------------------------------
780
781template <class T, class A = TStandardAllocator>
783 : public TMObjectArray<T, A>
784{
785public:
786
788 friend class TObjArrayIterator<T, TMSortedObjectArray<T, A> >;
790
791 TMSortedObjectArray(int upper, int lower = 0, int delta = 0)
792 : TMObjectArray<T, A>(upper, lower, delta) {}
796
798
799 int Add(const T& t);
800 bool DestroyItem(const T& t);
801 bool HasMember(const T& t) const;
802 int Find(const T& t) const;
803
804private:
805
806 int AddAt(const T& t, int index) { return 0; } // hide
807 bool DetachItem(const T& t);
808
809};
810
811//------------------------------------------------------------------------------
812
813template <class T>
839
840//------------------------------------------------------------------------------
841//
842/// Stores pointer to object.
843/// \note Jogy: Incorrect, stores simple types without assuming that they are pointer to objects.
844//
845template <class T, class R, class A = TStandardAllocator>
847 : public A, public TArrayBase
848{
849public:
850
851 typedef void (*IterFunc)(R, void*);
852 typedef bool (*CondFunc)(R, void*);
853
855 friend class TPtrArrayIterator<R, TTypedArray<T, R, A> >;
857
859 TTypedArray(int upper, int /*lower*/ = 0, int /*delta*/ = 0)
860 : Data(nullptr)
861 {
862 Resize(upper);
863 }
864
865 ~TTypedArray() { if (Data) A::Free(Data); }
866
869
870 void RemoveEntry(int loc) { Remove(loc); }
871 void SqueezeEntry(unsigned loc) { Remove(loc); }
872 void Grow(int index);
873
874 int Add(R t);
875 void AddAt(R t, uint index);
876 bool DetachItem(R t);
877 bool Detach(int loc);
878 bool DestroyItem(R t);
879 bool Destroy(int loc);
880 bool HasMember(R t) const;
881 int Find(R t) const;
882
884 const T& operator [](int loc) const;
885
886 void ForEach(IterFunc iter, void* args);
887 T* FirstThat(CondFunc cond, void* args) const;
888 T* LastThat(CondFunc cond, void* args) const;
889
890 void Flush();
891
892protected:
893
895
896 void Resize(int delta);
897 void Remove(int index);
898
899};
900
901//------------------------------------------------------------------------------
902
903template <class T, class R, class A = TStandardAllocator>
905 : public TTypedArray<T, R, A>
906{
907public:
908
910 friend class TPtrArrayIterator<R, TSTypedArray<T, R, A> >;
912
913 TSTypedArray() : TTypedArray<T, R, A>() {}
914 TSTypedArray(int upper, int lower = 0, int delta = 0)
915 : TTypedArray<T, R, A>(upper, lower, delta)
916 {}
917
919
922
923 int Add(R t);
924 bool DetachItem(R t);
925 bool DestroyItem(R t);
926 bool HasMember(R t) const;
927 int Find(R t) const;
928
929private:
930
931 int AddAt(R t, int index); // hide
932
933};
934
935//------------------------------------------------------------------------------
936//
937/// Array of pointers of simple types.
938//
939template <class T>
941 : public TTypedArray<T, T, TStandardAllocator>
942{
943public:
944
946 TPtrArray(int upper, int lower = 0, int delta = 0)
948 {}
949
950 TPtrArray(const TPtrArray<T>& array) :TTypedArray<T, T, TStandardAllocator>(array) {}
951
952};
953
954//------------------------------------------------------------------------------
955//
956/// Sorted array of pointers of simple types
957//
958template <class T>
960 : public TSTypedArray<T, T, TStandardAllocator>
961{
962public:
963
965 TSortedPtrArray(int upper, int lower = 0, int delta = 0)
967 {}
968
970
971};
972
973//------------------------------------------------------------------------------
974
975template <class T, class R, class A = TStandardAllocator>
977 : public TTypedArray<T, R, A>
978{
979public:
980
982 friend class TPtrArrayIterator<R, TMIPtrArray<T, R, A> >;
984
985 TMIPtrArray();
986 TMIPtrArray(int upper, int lower = 0, int delta = 0);
987 ~TMIPtrArray();
988
989 TMIPtrArray(const TMIPtrArray<T, R, A>& array);
991
992 bool DestroyItem(R t);
993 bool Destroy(int loc);
994 void Flush(bool del = true);
995
996};
997
998//------------------------------------------------------------------------------
999
1000template <class T>
1002 : public TPtrArray<T>
1003{
1004public:
1005
1007 friend class TPtrArrayIterator<T, TIPtrArray<T> >;
1009
1011 TIPtrArray(int upper, int lower = 0, int delta = 0);
1013
1016
1017 bool DestroyItem(T t);
1018 bool Destroy(int loc);
1019 void Flush(bool del = true);
1020
1021};
1022
1023template <class T>
1025 : public TSortedPtrArray<T>
1026{
1027public:
1028
1030 friend class TPtrArrayIterator<T, TISortedPtrArray<T> >;
1032
1034 TISortedPtrArray(int upper, int lower = 0, int delta = 0);
1035
1037
1040
1041 bool DestroyItem(T t);
1042 bool Destroy(int loc);
1043 void Flush(bool del = true);
1044
1045};
1046
1047//------------------------------------------------------------------------------
1048
1049template <class T>
1051{
1052public:
1053
1054 TBaseNode(const T& data) :Next(0), Prev(0), Data(data) {}
1055
1059
1060};
1061
1062template <class T>
1063class TBaseList;
1064
1065template <class T>
1067{
1068public:
1069
1070 TBaseListIterator(TBaseList<T>& list) : List(list), Cur(list.First) {}
1071
1072 operator bool() const { return Cur != 0; }
1073 const T& operator ++(int) { TBaseNode<T>* tmp = Cur; Cur = Cur->Next; return tmp->Data; }
1074 const T* operator ++() { Cur = Cur->Next; return Cur ? &Cur->Data : 0; } //?????
1075 const T& operator --(int) { TBaseNode<T>* tmp = Cur; Cur = Cur->Prev; return tmp->Data; }
1076 const T* operator --() { Cur = Cur->Prev; return Cur ? &Cur->Data : 0; } //?????
1077 const T& operator *() const { return Cur->Data; }
1078 const T& Current() const { return Cur->Data; }
1079 void Restart() { Cur = List.First; }
1080
1083
1084};
1085
1086//------------------------------------------------------------------------------
1087
1088template <class T>
1090{
1091public:
1092
1093 typedef void (*IterFunc)(T&, void*);
1094 typedef bool (*CondFunc)(const T&, void*);
1095
1097 friend class TBaseListIterator<T>;
1099
1101 TBaseList(int /*upper*/, int lower = 0, int delta = 0);
1103
1104 int LowerBound() const { return 0; }
1105 uint Size() const { return ItemSize; }
1106 int UpperBound() const { return Size() - 1; }
1107 uint ArraySize() const { return Size(); }
1108 bool IsFull() const { return true; }
1109 bool Empty() const { return First == nullptr; }
1110 bool IsEmpty() const { return Empty(); }
1111 uint GetItemsInContainer() const { return Size(); }
1112
1113
1114 void Add(const T& data);
1116 void Push(const T& data);
1117 void PushBack(const T& data);
1118 T Pop();
1119 const T& Top();
1120 const T& Bottom();
1121
1122 void Flush();
1123 bool DetachItem(const T& t);
1124 bool Detach(int loc);
1125 bool HasMember(const T& t) const;
1126 TBaseNode<T>* Find(const T& t) const;
1128 T* FirstThat(CondFunc cond, void* args) const;
1129 T* LastThat(CondFunc cond, void* args) const;
1130
1131protected:
1132
1134 int Add(TBaseNode<T>* node, const T& data);
1137
1138protected:
1139
1142 uint ItemSize; ///< optimization for Size()
1143
1144 static const int NPOS = -1;
1145};
1146
1147//------------------------------------------------------------------------------
1148
1149template <class T, class T1>
1151{
1152public:
1153
1155 : Data(data), Split(split), LoId(0), EqId(0), HiId(0)
1156 {}
1157
1163
1164};
1165
1166template <class T, class T1>
1168{
1169public:
1170
1173
1174 T1* Find(T* s) { return RSearch(Root, s); }
1175 void Insert(T* s, const T1& data) { Root = RInsert(Root, s, data); }
1176 void Flush() { Flush(Root); }
1177
1178protected:
1179
1181 {
1182 if (!p)
1183 return 0;
1184 if (*s < p->Split)
1185 return RSearch(p->LoId, s);
1186 else if (*s > p->Split)
1187 return RSearch(p->HiId, s);
1188 else
1189 {
1190 if (*s == 0)
1191 return &p->Data;
1192 return RSearch(p->EqId, ++s);
1193 }
1194 }
1195
1197 {
1198 if (!p)
1199 p = new TTernaryNode<T, T1>(*s, data);
1200 if (*s < p->Split)
1201 p->LoId = RInsert(p->LoId, s, data);
1202 else if (*s == p->Split)
1203 {
1204 if (*s != 0)
1205 p->EqId = RInsert(p->EqId, ++s, data);
1206 }
1207 else
1208 p->HiId = RInsert(p->HiId, s, data);
1209 ItemCnt++;
1210 return p;
1211 }
1212
1214 {
1215 if (p)
1216 {
1217 Flush(p->LoId);
1218 Flush(p->EqId);
1219 Flush(p->HiId);
1220 delete p;
1221 }
1222 }
1223
1224protected:
1225
1227 uint ItemCnt; /// item count
1228
1229};
1230
1231/// @}
1232
1233#include <owl/posclass.h>
1234
1235//----------------------------------------------------------------------------
1236// Inlines
1237//
1238
1239template<class T, class A> TMObjectArray<T, A>::TMObjectArray(const TMObjectArray& array)
1240 : Data{nullptr}, Mem{sizeof(T), std::max(10u, array.Reserved / 10u)}
1241{
1242 if (array.Reserved)
1243 Resize(array.Reserved);
1244 for (int i = 0; i < static_cast<int>(array.ItemCnt); i++)
1245 {
1246 Data[i] = __owl_construct<T>(Mem.Allocate(sizeof(T)), *array.Data[i]);
1247 }
1248 ItemCnt = array.ItemCnt;
1249}
1250
1251template <class T, class A>
1253{
1254 Flush();
1255 if (Data)
1256 A::Free(Data);
1257}
1258
1259template <class T, class A>
1261{
1262 if (this == &array)
1263 return *this;
1264
1265 Flush();
1266 if (array.ItemCnt >= Reserved)
1267 Resize(array.ItemCnt - Reserved);
1268 for (int i = 0; i < static_cast<int>(array.ItemCnt); i++)
1269 {
1270 Data[i] = __owl_construct<T>(Mem.Allocate(sizeof(T)), *array.Data[i]);
1271 }
1272 ItemCnt = array.ItemCnt;
1273
1274 return *this;
1275}
1276
1277template <class T, class A>
1278inline int TMObjectArray<T, A>::Add(const T& t)
1279{
1280 if (ItemCnt >= Reserved)
1281 Resize(ItemCnt + 1);
1282 Data[ItemCnt] = __owl_construct<T>(Mem.Allocate(sizeof(T)), t);
1283 return ItemCnt++; // if fail -> throw xalloc
1284}
1285
1286template <class T, class A>
1287void TMObjectArray<T, A>::AddAt(const T& t, uint index)
1288{
1289 if (index == Size())
1290 {
1291 Add(t);
1292 return;
1293 }
1294 if (index >= Reserved)
1295 Resize(index + 1); // on error -> throw xalloc
1296 if (ItemCnt >= Reserved)
1297 Resize(ItemCnt + 1); // on error -> throw xalloc
1298 if (index < ItemCnt && ItemCnt)
1299 memmove(&Data[index + 1], &Data[index], sizeof(T*) * (ItemCnt - index));
1300 Data[index] = __owl_construct<T>(Mem.Allocate(sizeof(T)), t);
1301 ItemCnt++;
1302}
1303
1304template <class T, class A>
1306{
1307 const auto index = this->Find(t);
1308 if (index != this->NPOS)
1309 {
1310 T* tmp = Data[index];
1311 Remove(index);
1312 __owl_destruct(tmp);// call destructor
1313 Mem.Free(tmp);
1314 return true;
1315 }
1316 return false;
1317}
1318
1319template <class T, class A>
1321{
1322 if (loc < static_cast<int>(Size()))
1323 {
1324 T* tmp = Data[loc];
1325 Remove(loc);
1326 __owl_destruct(tmp);// call destructor
1327 Mem.Free(tmp);
1328 return true;
1329 }
1330 return false;
1331}
1332
1333template <class T, class A>
1334inline bool TMObjectArray<T, A>::HasMember(const T& t) const
1335{
1336 return this->Find(t) != this->NPOS;
1337}
1338
1339template <class T, class A>
1340int TMObjectArray<T, A>::Find(const T& t) const
1341{
1342 for (int i = 0; i < static_cast<int>(Size()); i++)
1343 if (*Data[i] == t)
1344 return i;
1345 return this->NPOS;
1346}
1347
1348template <class T, class A>
1350{
1351 PRECONDITION(loc < static_cast<int>(Size()));
1352 return *Data[loc];
1353}
1354
1355template <class T, class A>
1356inline const T& TMObjectArray<T, A>::operator [](int loc) const
1357{
1358 PRECONDITION(loc < static_cast<int>(Size()));
1359 return *Data[loc];
1360}
1361
1362template <class T, class A>
1364{
1365 for (int i = 0; i < static_cast<int>(Size()); i++)
1366 (iter)(*Data[i], args);
1367}
1368
1369template <class T, class A>
1371{
1372 for (int i = 0; i < static_cast<int>(Size()); i++)
1373 if ((cond)(*Data[i], args))
1374 return Data[i];
1375 return 0;
1376}
1377
1378template <class T, class A>
1379T* TMObjectArray<T, A>::LastThat(CondFunc cond, void* args) const
1380{
1381 for (int i = static_cast<int>(Size()) - 1; i >= 0; i--)
1382 if ((cond)(*Data[i], args))
1383 return Data[i];
1384 return 0;
1385}
1386
1387template <class T, class A>
1389{
1390 for (int i = 0; i < static_cast<int>(Size()); i++)
1391 {
1392 __owl_destruct(Data[i]);// call destructor
1393 Mem.Free(Data[i]);
1394 }
1395 ItemCnt = 0;
1396}
1397
1398template <class T, class A>
1399inline void TMObjectArray<T, A>::Grow(int index)
1400{
1401 if (static_cast<int>(Reserved) > index)
1402 return;
1403 Resize(index - Reserved);
1404}
1405
1406template <class T, class A>
1408{
1409 if (Data)
1410 {
1411 T** data = static_cast<T**>(A::ReAlloc(static_cast<void*>(Data), (Reserved + delta) * sizeof(T**)));
1412 if (!data)
1413 {
1414 data = static_cast<T**>(A::Alloc((Reserved + delta) * sizeof(T**)));
1415 memcpy(data, Data, Reserved * sizeof(T**));
1416 A::Free(Data);
1417 }
1418 Data = data;
1419 }
1420 else
1421 Data = static_cast<T**>(A::Alloc((Reserved + delta) * sizeof(T**)));
1422
1423 memset(reinterpret_cast<uint8*>(Data) + Reserved * sizeof(T**), 0, delta * sizeof(T**));
1424 Reserved += delta;
1425}
1426
1427template <class T, class A>
1428inline void TMObjectArray<T, A>::Remove(int index)
1429{
1430 memmove(&Data[index], &Data[index + 1], sizeof(T*) * (ItemCnt - index - 1));
1431 ItemCnt--;
1432}
1433
1434//----------------------------------------------------------------------------
1435
1436template <class T, class A>
1440
1441template <class T, class A>
1443{
1444 if (this == &array)
1445 return *this;
1447 return *this;
1448}
1449
1450template <class T, class A>
1452{
1453 if (this->ItemCnt >= this->Reserved)
1454 this->Resize(this->ItemCnt + 1); // on error -> throw xalloc
1455 unsigned loc = this->ItemCnt++;
1456 while (loc > 0 && t < *(this->Data[loc - 1]))
1457 {
1458 this->Data[loc] = this->Data[loc - 1];
1459 loc--;
1460 }
1461 this->Data[loc] = __owl_construct<T>(this->Mem.Allocate(sizeof(T)), t);
1462 return loc;
1463}
1464
1465template <class T, class A>
1467{
1468 const auto index = this->Find(t);
1469 if (index != this->NPOS)
1470 {
1471 T* tmp = this->Data[index];
1472 this->Remove(index);
1473 __owl_destruct(tmp);// call destructor
1474 this->Mem.Free(tmp);
1475 return true;
1476 }
1477 return false;
1478}
1479
1480template <class T, class A>
1481inline bool TMSortedObjectArray<T, A>::HasMember(const T& t) const
1482{
1483 return this->Find(t) != this->NPOS;
1484}
1485
1486template <class T, class A>
1488{
1489 auto lower = 0;
1490 auto upper = static_cast<int>(this->Size()) - 1;
1491 while (lower <= upper)
1492 {
1493 const auto middle = (lower + upper) / 2;
1494 if (*(this->Data[middle]) == t)
1495 return middle;
1496 if (*(this->Data[middle]) < t)
1497 lower = middle + 1;
1498 else
1499 upper = middle - 1;
1500 }
1501 return this->NPOS;
1502}
1503
1504//----------------------------------------------------------------------------
1505
1506template <class T, class R, class A>
1508 : Data(nullptr)
1509{
1510 if (array.Reserved)
1511 Resize(array.Reserved);
1512 for (int i = 0; i < static_cast<int>(array.ItemCnt); i++)
1513 Data[i] = array.Data[i];
1514 ItemCnt = array.ItemCnt;
1515}
1516
1517template <class T, class R, class A>
1519{
1520 if (this == &array)
1521 return *this;
1522
1523 Flush();
1524 if (array.ItemCnt >= Reserved)
1525 Resize(array.ItemCnt - Reserved);
1526 for (int i = 0; i < static_cast<int>(array.ItemCnt); i++)
1527 Data[i] = array.Data[i];
1528 ItemCnt = array.ItemCnt;
1529
1530 return *this;
1531}
1532
1533template <class T, class R, class A>
1535{
1536 if (ItemCnt >= Reserved)
1537 Resize(ItemCnt + 1);
1538 Data[ItemCnt++] = t;
1539 return ItemCnt - 1; // if fail -> throw xalloc
1540}
1541
1542template <class T, class R, class A>
1544{
1545 if (index == Size())
1546 {
1547 Add(t);
1548 return;
1549 }
1550 if (index >= Reserved)
1551 Resize(index + 1); // on error -> throw xalloc
1552 if (ItemCnt >= Reserved)
1553 Resize(ItemCnt + 1); // on error -> throw xalloc
1554 if (index < ItemCnt && ItemCnt)
1555 memmove(&Data[index + 1], &Data[index], sizeof(T) * (ItemCnt - index));
1556 Data[index] = t;
1557 ItemCnt++;
1558}
1559
1560template <class T, class R, class A>
1562{
1563 const auto index = this->Find(t);
1564 if (index != this->NPOS)
1565 {
1566 Remove(index);
1567 return true;
1568 }
1569 return false;
1570}
1571
1572template <class T, class R, class A>
1574{
1575 if (loc < static_cast<int>(Size()))
1576 {
1577 Remove(loc);
1578 return true;
1579 }
1580 return false;
1581}
1582
1583template <class T, class R, class A>
1585{
1586 return DetachItem(t);
1587}
1588
1589template <class T, class R, class A>
1591{
1592 return Detach(loc);
1593}
1594
1595template <class T, class R, class A>
1597{
1598 return this->Find(t) != this->NPOS;
1599}
1600
1601template <class T, class R, class A>
1603{
1604 for (int i = 0; i < static_cast<int>(Size()); i++)
1605 if(Data[i] == t)
1606 return i;
1607 return this->NPOS;
1608}
1609
1610template <class T, class R, class A>
1612{
1613 PRECONDITION(loc < static_cast<int>(Size()));
1614 return Data[loc];
1615}
1616
1617template <class T, class R, class A>
1618inline const T& TTypedArray<T, R, A>::operator [](int loc) const
1619{
1620 PRECONDITION(loc < static_cast<int>(Size()));
1621 return Data[loc];
1622}
1623
1624template <class T, class R, class A>
1626{
1627 for (int i = 0; i < static_cast<int>(Size()); i++)
1628 (iter)(Data[i], args);
1629}
1630
1631template <class T, class R, class A>
1633{
1634 for (int i = 0; i < static_cast<int>(Size()); i++)
1635 if ((cond)(Data[i], args))
1636 return &Data[i];
1637 return 0;
1638}
1639
1640template <class T, class R, class A>
1642{
1643 for (int i = static_cast<int>(Size()) - 1; i >= 0; i--)
1644 if ((cond)(Data[i],args))
1645 return &Data[i];
1646 return 0;
1647}
1648
1649template <class T, class R, class A>
1651{
1652 ItemCnt = 0;
1653}
1654
1655template <class T, class R, class A>
1656inline void TTypedArray<T, R, A>::Grow(int index)
1657{
1658 if (Reserved > static_cast<uint>(index))
1659 return;
1660 Resize(index - Reserved);
1661}
1662
1663template<class T, class R, class A> void
1665{
1666 if (Data)
1667 {
1668 T* data = static_cast<T*>(this->ReAlloc(static_cast<void*>(Data), (Reserved + delta) * sizeof(T)));
1669 if (!data) //no ReAlloc, try Alloc instead
1670 {
1671 data = static_cast<T*>(this->Alloc((Reserved + delta) * sizeof(T)));
1672 if (data)
1673 {
1674 memcpy(data, Data, Reserved * sizeof(T));
1675 A::Free(Data);
1676 Data = data;
1677 }
1678 else
1679 return;
1680 }
1681 else
1682 Data = data;
1683 }
1684 else // if (Data)
1685 Data = static_cast<T*>(this->Alloc((Reserved + delta) * sizeof(T)));
1686
1687 memset(reinterpret_cast<uint8*>(Data) + Reserved * sizeof(T), 0, delta * sizeof(T));
1688 Reserved += delta;
1689}
1690
1691template <class T, class R, class A>
1692inline void TTypedArray<T, R, A>::Remove(int index)
1693{
1694 memmove(&Data[index], &Data[index + 1], sizeof(T) * (ItemCnt - index - 1));
1695 ItemCnt--;
1696}
1697
1698//----------------------------------------------------------------------------
1699
1700template <class T, class R, class A>
1702 : TTypedArray<T, R, A>(array)
1703{}
1704
1705template <class T, class R, class A>
1707{
1708 if (this == &array)
1709 return *this;
1711 return *this;
1712}
1713
1714template <class T, class R, class A>
1716{
1717 if (this->ItemCnt >= this->Reserved)
1718 this->Resize(this->ItemCnt + 1); // on error -> throw xalloc
1719 unsigned loc = this->ItemCnt++;
1720 while (loc > 0 && t < this->Data[loc - 1])
1721 {
1722 this->Data[loc] = this->Data[loc - 1];
1723 loc--;
1724 }
1725 this->Data[loc] = t;
1726 return loc;
1727}
1728
1729template <class T, class R, class A>
1731{
1732 const auto index = this->Find(t);
1733 if (index != this->NPOS)
1734 {
1735 this->Remove(index);
1736 return true;
1737 }
1738 return false;
1739}
1740
1741template <class T, class R, class A>
1743{
1744 return DetachItem(t);
1745}
1746
1747template <class T, class R, class A>
1749{
1750 return this->Find(t) != this->NPOS;
1751}
1752
1753template <class T, class R, class A>
1755{
1756 auto lower = 0;
1757 auto upper = static_cast<int>(this->Size()) - 1;
1758 while (lower <= upper)
1759 {
1760 const auto middle = (lower + upper) / 2;
1761 if (this->Data[middle] == t)
1762 return middle;
1763 if (this->Data[middle] < t)
1764 lower = middle + 1;
1765 else
1766 upper = middle - 1;
1767 }
1768 return this->NPOS;
1769}
1770
1771//----------------------------------------------------------------------------
1772
1773template <class T, class R, class A>
1775 : TTypedArray<T, R, A>(upper, lower, delta)
1776{}
1777
1778template <class T, class R, class A>
1780 : TTypedArray<T, R, A>(array)
1781{}
1782
1783template <class T, class R, class A>
1786
1787template <class T, class R, class A>
1789{
1790 Flush(true);
1791}
1792
1793template <class T, class R, class A>
1795{
1796 if (this == &array)
1797 return *this;
1798 Flush(true);
1800 return *this;
1801}
1802
1803template <class T, class R, class A>
1805{
1806 const auto index = this->Find(t);
1807 if (index != this->NPOS)
1808 {
1809 R tmp = this->Data[index];
1810 this->Remove(index);
1811 delete tmp;// using global delete because we didn't allocate it
1812 return true;
1813 }
1814 return false;
1815}
1816
1817template <class T, class R, class A>
1819{
1820 if (loc < static_cast<int>(this->Size()))
1821 {
1822 R tmp = this->Data[loc];
1823 this->Remove(loc);
1824 delete tmp;// using global delete because we didn't allocate it
1825 return true;
1826 }
1827 return false;
1828}
1829
1830template <class T, class R, class A>
1832{
1833 if (del)
1834 {
1835 for (int i = 0; i < static_cast<int>(this->Size()); i++)
1836 delete this->Data[i];// using global delete because we didn't allocate it
1837 }
1839}
1840
1841//----------------------------------------------------------------------------
1842
1843template <class T>
1845 : TPtrArray<T>(upper, lower, delta)
1846{}
1847
1848template <class T>
1850 : TPtrArray<T>(array)
1851{}
1852
1853template <class T>
1855{
1856 if (this == &array)
1857 return *this;
1858 Flush(true);
1860 return *this;
1861}
1862
1863template <class T>
1866
1867template <class T>
1869{
1870 Flush(true);
1871}
1872
1873template <class T>
1875{
1876 if (del)
1877 {
1878 for (int i = 0; i < static_cast<int>(this->Size()); i++)
1879 delete this->Data[i];// using global delete because we didn't allocate it
1880 }
1882}
1883
1884template <class T>
1886{
1887 const auto index = this->Find(t);
1888 if (index != this->NPOS)
1889 {
1890 T tmp = this->Data[index];
1891 this->Remove(index);
1892 delete tmp;// using global delete because we didn't allocate it
1893 return true;
1894 }
1895 return false;
1896}
1897
1898template <class T>
1900{
1901 if (loc < static_cast<int>(this->Size()))
1902 {
1903 T tmp = this->Data[loc];
1904 this->Remove(loc);
1905 delete tmp;// using global delete because we didn't allocate it
1906 return true;
1907 }
1908 return false;
1909}
1910
1911//----------------------------------------------------------------------------
1912
1913template <class T>
1917
1918template <class T>
1921
1922template <class T>
1924{
1925 Flush(true);
1926}
1927
1928template <class T>
1932
1933template <class T>
1935{
1936 if (this == &array)
1937 return *this;
1938
1939 Flush(true);
1941 return *this;
1942}
1943
1944template <class T>
1946{
1947 if (del)
1948 {
1949 for (int i = 0; i < static_cast<int>(this->Size()); i++)
1950 delete this->Data[i];// using global delete because we didn't allocate it
1951 }
1953}
1954
1955template <class T>
1957{
1958 const auto index = this->Find(t);
1959 if (index != this->NPOS)
1960 {
1961 T tmp = this->Data[index];
1962 this->Remove(index);
1963 delete tmp;// using global delete because we didn't allocate it
1964 return true;
1965 }
1966 return false;
1967}
1968
1969template <class T>
1971{
1972 if (loc < static_cast<int>(this->Size()))
1973 {
1974 T tmp = this->Data[loc];
1975 this->Remove(loc);
1976 delete tmp;// using global delete because we didn't allocate it
1977 return true;
1978 }
1979 return false;
1980}
1981
1982//----------------------------------------------------------------------------
1983
1984template <class T>
1986 : First(nullptr), Last(nullptr), ItemSize(0)
1987{}
1988
1989template <class T>
1990inline TBaseList<T>::TBaseList(int /*upper*/, int /*lower*/, int /*delta*/)
1991 : First(nullptr), Last(nullptr), ItemSize(0)
1992{}
1993
1994template <class T>
1996{
1997 Flush();
1998}
1999
2000template <class T>
2001inline void TBaseList<T>::Add(const T& data)
2002{
2004 Add(node);
2005}
2006
2007template <class T>
2009{
2010 Add(iter ? iter->Cur : 0, new TBaseNode<T>(data));
2011}
2012
2013template <class T>
2014inline void TBaseList<T>::Push(const T& data)
2015{
2016 Add(static_cast<TBaseNode<T>*>(nullptr), data);
2017}
2018
2019template <class T>
2020inline void TBaseList<T>::PushBack(const T& data)
2021{
2022 Add(Last, data);
2023}
2024
2025template <class T>
2027{
2028 TBaseNode<T>* tmp = First;
2029 Remove(tmp);
2030 T data = tmp->Data; delete tmp;
2031 return data;
2032}
2033
2034template <class T>
2035inline const T& TBaseList<T>::Top()
2036{
2037 return First->Data;
2038}
2039
2040template <class T>
2041inline const T& TBaseList<T>::Bottom()
2042{
2043 return Last->Data;
2044}
2045
2046template <class T>
2048{
2049 for (TBaseNode<T>* node = First; node;)
2050 {
2052 node = node->Next;
2053 delete tmp;
2054 }
2055 First = Last = nullptr;
2056 ItemSize = 0;
2057}
2058
2059template <class T>
2061{
2062 TBaseNode<T>* item = this->Find(t);
2063 if (item && Remove(item))
2064 {
2065 delete item;
2066 return true;
2067 }
2068 return false;
2069}
2070
2071template <class T>
2073{
2074 if (loc < static_cast<int>(Size()))
2075 {
2076 int counter = 0;
2077 for (TBaseNode<T>* i = First; i; i = i->Next, counter++)
2078 {
2079 if (counter == loc)
2080 {
2081 Remove(i);
2082 return true;
2083 }
2084 }
2085 }
2086 return false;
2087}
2088
2089template <class T>
2090inline bool TBaseList<T>::HasMember(const T& t) const
2091{
2092 return this->Find(t) != 0;
2093}
2094
2095template <class T>
2097{
2098 for (TBaseNode<T>* i = First; i; i = i->Next)
2099 if (i->Data == t)
2100 return i;
2101 return nullptr;
2102}
2103
2104template <class T>
2105void TBaseList<T>::ForEach(IterFunc iter, void* args)
2106{
2107 for (TBaseNode<T>* i = First; i; i = i->Next)
2108 (iter) (i->Data, args);
2109}
2110
2111template <class T>
2112T* TBaseList<T>::FirstThat(CondFunc cond, void* args) const
2113{
2114 for (TBaseNode<T>* i = First; i; i = i->Next)
2115 if ((cond) (i->Data, args))
2116 return &i->Data;
2117 return nullptr;
2118}
2119
2120template <class T>
2121T* TBaseList<T>::LastThat(CondFunc cond, void* args) const
2122{
2123 for (TBaseNode<T>* i = Last; i; i = i->Prev)
2124 if ((cond) (i->Data, args))
2125 return &i->Data;
2126 return 0;
2127}
2128
2129template <class T>
2131{
2132 if (!node)
2133 return false;
2134 if (node == First)
2135 {
2136 First = node->Next;
2137 node->Next = nullptr;
2138 if (First)
2139 First->Prev = nullptr;
2140 }
2141 else if (node == Last)
2142 {
2143 Last = node->Prev;
2144 node->Prev = nullptr;
2145 if (Last)
2146 Last->Next = nullptr;
2147 }
2148 else
2149 {
2150 node->Prev->Next = node->Next;
2151 node->Next->Prev = node->Prev;
2152 node->Prev = nullptr;
2153 node->Next = nullptr;
2154 }
2155
2156 ItemSize--;
2157 return true;
2158}
2159
2160template <class T>
2162{
2163 return Add(node, new TBaseNode<T>(data));
2164}
2165
2166template <class T>
2168{
2169 if (!node)
2170 return this->NPOS;
2171 if (!First)
2172 {
2173 First = node;
2174 Last = node;
2175 }
2176 else
2177 {
2178 node->Prev = Last;
2179 Last->Next = node;
2180 Last = node;
2181 }
2182 return ++ItemSize;
2183}
2184
2185template <class T>
2187{
2188 if (!node)
2189 return this->NPOS;
2190 if (!First)
2191 {
2192 return Add(node);
2193 }
2194 if (!item)
2195 {
2196 node->Next = First;
2197 First->Prev = node;
2198 First = node;
2199 }
2200 else
2201 {
2202 if (item == Last)
2203 Last = node;
2204 node->Prev = item;
2205 node->Next = item->Next;
2206 item->Next = node;
2207 if (node->Next)
2208 node->Next->Prev = node;
2209 }
2210 return ++ItemSize;
2211}
2212
2213} // OWL namespace
2214
2215//----------------------------------------------------------------------------
2216// Inlined overloads of global new
2217//
2218
2219inline void* operator new(size_t sz, const owl::TStandardAllocator&)
2220{
2221 return ::operator new(sz);
2222}
2223
2224inline void* operator new(size_t sz, const owl::TLocalAllocator&)
2225{
2226 return static_cast<void*>(::LocalAlloc(LPTR, sz));
2227}
2228
2229inline void* operator new(size_t sz, const owl::TGlobalAllocator&)
2230{
2231 return static_cast<void*>(::GlobalLock(::GlobalAlloc(GPTR, sz)));
2232}
2233
2234template <class A>
2235inline void* operator new(size_t sz, owl::TMMemStack<A>& m)
2236{
2237 return m.Allocate(sz);
2238}
2239
2240#endif
#define CHECK(condition)
Definition checks.h:239
#define WARN(condition, message)
Definition checks.h:273
#define PRECONDITION(condition)
Definition checks.h:227
Base array class.
Definition template.h:661
bool IsEmpty() const
Definition template.h:671
uint Size() const
Definition template.h:672
uint Count() const
Definition template.h:673
bool empty() const
Definition template.h:680
int UpperBound() const
Definition template.h:668
uint size() const
Definition template.h:679
uint GetItemsInContainer() const
Definition template.h:666
bool IsFull() const
Definition template.h:670
bool Empty() const
Definition template.h:674
static const int NPOS
Definition template.h:682
uint ArraySize() const
Definition template.h:669
int LowerBound() const
Definition template.h:667
int Add(TBaseNode< T > *node)
Definition template.h:2167
bool IsEmpty() const
Definition template.h:1110
static const int NPOS
Definition template.h:1144
int UpperBound() const
Definition template.h:1106
int Add(TBaseNode< T > *node, const T &data)
Definition template.h:2161
T * FirstThat(CondFunc cond, void *args) const
Definition template.h:2112
uint ArraySize() const
Definition template.h:1107
bool DetachItem(const T &t)
Definition template.h:2060
void(* IterFunc)(T &, void *)
Definition template.h:1093
bool Empty() const
Definition template.h:1109
T * LastThat(CondFunc cond, void *args) const
Definition template.h:2121
void Push(const T &data)
Definition template.h:2014
void ForEach(IterFunc iter, void *args)
Definition template.h:2105
bool IsFull() const
Definition template.h:1108
TBaseNode< T > * Find(const T &t) const
Definition template.h:2096
Iterator iterator
Definition template.h:1098
uint GetItemsInContainer() const
Definition template.h:1111
TBaseListIterator< T > Iterator
Definition template.h:1096
uint Size() const
Definition template.h:1105
bool HasMember(const T &t) const
Definition template.h:2090
TBaseNode< T > * First
Definition template.h:1140
void Add(TBaseListIterator< T > *iter, const T &data)
Definition template.h:2008
bool Detach(int loc)
Definition template.h:2072
uint ItemSize
optimization for Size()
Definition template.h:1142
bool Remove(TBaseNode< T > *t)
Definition template.h:2130
TBaseNode< T > * Last
Definition template.h:1141
void Add(const T &data)
Definition template.h:2001
TBaseList(int, int lower=0, int delta=0)
Definition template.h:1990
int Add(TBaseNode< T > *item, TBaseNode< T > *node)
Definition template.h:2186
const T & Top()
Definition template.h:2035
void PushBack(const T &data)
Definition template.h:2020
const T & Bottom()
Definition template.h:2041
bool(* CondFunc)(const T &, void *)
Definition template.h:1094
int LowerBound() const
Definition template.h:1104
const T & operator*() const
Definition template.h:1077
const T & Current() const
Definition template.h:1078
TBaseList< T > & List
Definition template.h:1081
TBaseNode< T > * Cur
Definition template.h:1082
TBaseListIterator(TBaseList< T > &list)
Definition template.h:1070
const T * operator--()
Definition template.h:1076
const T * operator++()
Definition template.h:1074
Used internally.
Definition template.h:332
TBaseMemBlocks(size_t sz=8192)
Definition template.h:335
TBaseNode(const T &data)
Definition template.h:1054
TBaseNode * Next
Definition template.h:1056
TBaseNode * Prev
Definition template.h:1057
Used internally.
Definition template.h:245
TBlockList(TBlockList *blk)
Definition template.h:248
Provides class-specific operator new and operator delete that allocate from global memory.
Definition template.h:135
void * ReAlloc(void *ptr, size_t sz)
Definition template.h:173
void Free(void *ptr)
Definition template.h:166
void * Alloc(size_t sz)
Definition template.h:161
TPtrArrayIterator< T, TIPtrArray< T > > Iterator
Definition template.h:1006
bool Destroy(int loc)
Definition template.h:1899
TIPtrArray< T > & operator=(const TIPtrArray< T > &array)
Definition template.h:1854
bool DestroyItem(T t)
Definition template.h:1885
Iterator iterator
Definition template.h:1008
TIPtrArray(int upper, int lower=0, int delta=0)
Definition template.h:1844
void Flush(bool del=true)
Definition template.h:1874
TIPtrArray(const TIPtrArray< T > &array)
Definition template.h:1849
bool Destroy(int loc)
Definition template.h:1970
TISortedPtrArray< T > & operator=(const TISortedPtrArray< T > &array)
Definition template.h:1934
void Flush(bool del=true)
Definition template.h:1945
TISortedPtrArray(const TISortedPtrArray< T > &array)
Definition template.h:1929
TISortedPtrArray(int upper, int lower=0, int delta=0)
Definition template.h:1914
TPtrArrayIterator< T, TISortedPtrArray< T > > Iterator
Definition template.h:1029
Provides class-specific operator new and operator delete that allocate from local memory.
Definition template.h:87
void Free(void *ptr)
Definition template.h:116
void * Alloc(size_t sz)
Definition template.h:111
void * ReAlloc(void *ptr, size_t sz)
Definition template.h:121
Used internally.
Definition template.h:259
TMBaseMemBlocks(size_t=8192)
Definition template.h:282
char * Block() const
Definition template.h:265
int AllocBlock(size_t)
Definition template.h:301
unsigned Count() const
Definition template.h:266
void FreeTo(unsigned)
Definition template.h:310
size_t GetBlockSize() const
Definition template.h:269
TMBlockList(TMBlockList *)
Definition template.h:214
TPtrArrayIterator< R, TMIPtrArray< T, R, A > > Iterator
Definition template.h:981
bool Destroy(int loc)
Definition template.h:1818
TMIPtrArray< T, R, A > & operator=(const TMIPtrArray< T, R, A > &array)
Definition template.h:1794
bool DestroyItem(R t)
Definition template.h:1804
Iterator iterator
Definition template.h:983
Managed memory stack.
Definition template.h:400
TMMarker(TMMemStack< A > &ms)
Definition template.h:403
Managed single-size block allocator.
Definition template.h:459
TMMemBlocks(size_t sz, unsigned int count=100)
Definition template.h:478
void Free(void *block)
Definition template.h:499
void * Allocate(size_t sz)
Definition template.h:485
void * Allocate(size_t)
Definition template.h:376
TMMemStack(size_t=8192)
Definition template.h:369
Holds array pointers to strings.
Definition template.h:699
T * FirstThat(CondFunc cond, void *args) const
Definition template.h:1370
void SqueezeEntry(unsigned loc)
Definition template.h:718
void(* IterFunc)(T &, void *)
Definition template.h:702
Iterator iterator
Definition template.h:707
bool Destroy(int loc)
Definition template.h:1320
void Remove(int index)
Definition template.h:1428
bool HasMember(const T &t) const
Definition template.h:1334
int Find(const T &t) const
Definition template.h:1340
bool(* CondFunc)(const T &, void *)
Definition template.h:703
void Grow(int index)
Definition template.h:1399
void Resize(int delta)
Definition template.h:1407
bool DestroyItem(const T &t)
Definition template.h:1305
void RemoveEntry(int loc)
Definition template.h:717
TMObjectArray(const TMObjectArray< T, A > &array)
Definition template.h:1239
TMObjectArray(int upper, int lower=0, int=0)
Definition template.h:709
T * LastThat(CondFunc cond, void *args) const
Definition template.h:1379
void AddAt(const T &t, uint index)
Definition template.h:1287
T & operator[](int loc)
Definition template.h:1349
TMMemBlocks< A > Mem
Definition template.h:746
void ForEach(IterFunc iter, void *args)
Definition template.h:1363
TMMemBlocks< A > & GetAllocator()
Definition template.h:715
int Add(const T &t)
Definition template.h:1278
TMObjectArray< T, A > & operator=(const TMObjectArray< T, A > &array)
Definition template.h:1260
TObjArrayIterator< T, TMObjectArray< T, A > > Iterator
Definition template.h:705
bool DestroyItem(const T &t)
Definition template.h:1466
int Add(const T &t)
Definition template.h:1451
TObjArrayIterator< T, TMSortedObjectArray< T, A > > Iterator
Definition template.h:787
bool HasMember(const T &t) const
Definition template.h:1481
TMSortedObjectArray(int upper, int lower=0, int delta=0)
Definition template.h:791
TMSortedObjectArray(const TMSortedObjectArray< T, A > &array)
Definition template.h:1437
TMSortedObjectArray< T, A > & operator=(const TMSortedObjectArray< T, A > &array)
Definition template.h:1442
int Find(const T &t) const
Definition template.h:1487
Map node.
Definition template.h:632
TMapNode(const TMapNode< T1, T2 > &node)
Definition template.h:637
TMapNode(T1 &name, T2 &val)
Definition template.h:635
bool operator<(const TMapNode &ms) const
Definition template.h:641
bool operator==(const TMapNode &ms) const
Definition template.h:642
TMapNode & operator=(const TMapNode &other)
Definition template.h:643
TMapNode(T1 &name)
Definition template.h:636
Provides the mark for TMemStack.
Definition template.h:442
TMarker(TMMemStack< TStandardAllocator > &ms)
Definition template.h:445
Single-size block allocator.
Definition template.h:515
TMemBlocks(size_t sz, unsigned n=100)
Definition template.h:518
Implements mark and release style memory management using the standard allocator.
Definition template.h:431
TMemStack(size_t sz=8192)
Definition template.h:433
Iterator for TObjectContainer.
Definition template.h:576
TObjArrayIterator(T1 &array)
Definition template.h:579
const T & Current() const
Definition template.h:586
const T & operator*() const
Definition template.h:584
const T * operator++()
Definition template.h:583
TObjectArray(int upper, int lower=0, int delta=0)
Definition template.h:765
TObjArrayIterator< T, TObjectArray< T > > Iterator
Definition template.h:761
TObjectArray< T > & operator=(const TObjectArray< T > &array)
Definition template.h:771
void(* IterFunc)(T &, void *)
Definition template.h:758
Iterator iterator
Definition template.h:762
TObjectArray(const TObjectArray< T > &array)
Definition template.h:769
bool(* CondFunc)(const T &, void *)
Definition template.h:759
Array of pointers of simple types.
Definition template.h:942
TPtrArray(int upper, int lower=0, int delta=0)
Definition template.h:946
TPtrArray(const TPtrArray< T > &array)
Definition template.h:950
Iterator for Pointer Container.
Definition template.h:602
TPtrArrayIterator(T1 &array)
Definition template.h:605
const T operator*() const
Definition template.h:610
const T * operator++()
Definition template.h:609
const T Current() const
Definition template.h:612
TSTypedArray(const TSTypedArray< T, R, A > &array)
Definition template.h:1701
Iterator iterator
Definition template.h:911
bool DetachItem(R t)
Definition template.h:1730
TPtrArrayIterator< R, TSTypedArray< T, R, A > > Iterator
Definition template.h:909
TSTypedArray(int upper, int lower=0, int delta=0)
Definition template.h:914
TSTypedArray< T, R, A > & operator=(const TSTypedArray< T, R, A > &array)
Definition template.h:1706
bool DestroyItem(R t)
Definition template.h:1742
bool HasMember(R t) const
Definition template.h:1748
int Find(R t) const
Definition template.h:1754
TSortedObjectArray< T > & operator=(const TSortedObjectArray< T > &array)
Definition template.h:832
TSortedObjectArray(const TSortedObjectArray< T > &array)
Definition template.h:830
void(* IterFunc)(T &, void *)
Definition template.h:819
TSortedObjectArray(int upper, int lower=0, int delta=0)
Definition template.h:826
bool(* CondFunc)(const T &, void *)
Definition template.h:820
TObjArrayIterator< T, TSortedObjectArray< T > > Iterator
Definition template.h:822
Sorted array of pointers of simple types.
Definition template.h:961
TSortedPtrArray(const TSortedPtrArray< T > &array)
Definition template.h:969
TSortedPtrArray(int upper, int lower=0, int delta=0)
Definition template.h:965
Provides class-specific operator new and operator delete that simply call the global operator new and...
Definition template.h:37
void * ReAlloc(void *ptr, size_t sz)
Definition template.h:73
void Free(void *ptr)
Definition template.h:68
void * Alloc(size_t sz)
Definition template.h:63
TTernaryNode * EqId
Definition template.h:1160
TTernaryNode(T split, const T1 &data)
Definition template.h:1154
TTernaryNode * LoId
Definition template.h:1159
TTernaryNode * HiId
Definition template.h:1161
TTernaryNode< T, T1 > * RInsert(TTernaryNode< T, T1 > *p, T *s, const T1 &data)
Definition template.h:1196
T1 * RSearch(TTernaryNode< T, T1 > *p, T *s)
Definition template.h:1180
TTernaryNode< T, T1 > * Root
Definition template.h:1226
T1 * Find(T *s)
Definition template.h:1174
void Insert(T *s, const T1 &data)
Definition template.h:1175
void Flush(TTernaryNode< T, T1 > *p)
Definition template.h:1213
Stores pointer to object.
Definition template.h:848
T & operator[](int loc)
Definition template.h:1611
bool(* CondFunc)(R, void *)
Definition template.h:852
TTypedArray(int upper, int=0, int=0)
Definition template.h:859
void RemoveEntry(int loc)
Definition template.h:870
void Resize(int delta)
Definition template.h:1664
void ForEach(IterFunc iter, void *args)
Definition template.h:1625
bool Destroy(int loc)
Definition template.h:1590
bool Detach(int loc)
Definition template.h:1573
void Remove(int index)
Definition template.h:1692
bool DestroyItem(R t)
Definition template.h:1584
TTypedArray< T, R, A > & operator=(const TTypedArray< T, R, A > &array)
Definition template.h:1518
void AddAt(R t, uint index)
Definition template.h:1543
int Find(R t) const
Definition template.h:1602
Iterator iterator
Definition template.h:856
bool HasMember(R t) const
Definition template.h:1596
void Grow(int index)
Definition template.h:1656
bool DetachItem(R t)
Definition template.h:1561
T * FirstThat(CondFunc cond, void *args) const
Definition template.h:1632
void(* IterFunc)(R, void *)
Definition template.h:851
void SqueezeEntry(unsigned loc)
Definition template.h:871
TPtrArrayIterator< R, TTypedArray< T, R, A > > Iterator
Definition template.h:854
TTypedArray(const TTypedArray< T, R, A > &array)
Definition template.h:1507
T * LastThat(CondFunc cond, void *args) const
Definition template.h:1641
#define _T(x)
Definition cygwin.h:51
TStandardAllocator TSharedAllocator
Definition template.h:49
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
TMMemBlocks< TStandardAllocator > TStandardBlocks
Definition template.h:526
void InUse(const T &arg)
Handy utility to avoid compiler warnings about unused parameters.
Definition defs.h:299
void __owl_destruct(T *t)
Definition template.h:538
T * __owl_construct(void *P)
Definition template.h:528
unsigned int uint
Definition number.h:25
General definitions used by all ObjectWindows programs.
#define REINTERPRET_CAST(targetType, object)
Definition defs.h:275