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