/*************************************************************************** * E-Com Technology Ltd. * * ECOMPACS DICOM Network Transport Libraries * Version 0.1 Beta ***************************************************************************/ //#pragma once //#ifndef __ARRAY__ //#define __ARRAY__ #pragma warning (disable : 4800) // avoid bool (LastAccess); template < class DATATYPE > class ArrayIterator; template < class DATATYPE > class ArrayReverseIterator; template < class DATATYPE > class ArrayStepIterator; template < class DATATYPE > class Array; template < class DATATYPE > class ArrayOfPtr; template < class DATATYPE > class FixArray; template < class DATATYPE > class ExclusiveArray; template < class DATATYPE > class ExclusiveArrayOfPtr; /********************************************************************* * * Class Array * *********************************************************************/ template < class DATATYPE > class ArrayItem { public: DATATYPE Data; ArrayItem < DATATYPE > *prev, *next; ArrayItem (ArrayItem < DATATYPE > *p, ArrayItem < DATATYPE > *n) { prev = p; next = n; }; ArrayItem () { prev = NULL; next = NULL; }; }; template < class DATATYPE > class Array { public: typedef DATATYPE _datatype; protected: ArrayItem < DATATYPE > *first; ArrayItem < DATATYPE > *last; BOOL ClearType; int ArraySize; public: Array (const Array & theArray); inline Array () { ArraySize = 0; first = NULL; last = NULL; ClearType = 1; } ~Array () { Reset ();}; inline void Add (const DATATYPE & Value) { InsertLast (Value); } inline void InsertFirst (const DATATYPE & Value) { ArrayItem < DATATYPE > * dl = first; first = new ArrayItem < DATATYPE > ; // chain on new element at head of chain first -> Data = Value; first -> next = dl; if (dl) // array is not empty dl -> prev = first; else // array is empty last = first; ++ ArraySize; } inline void InsertFirst (DATATYPE & Value) { ArrayItem < DATATYPE > * dl = first; first = new ArrayItem < DATATYPE > ; // chain on new element at head of chain first -> Data = Value; first -> next = dl; if (dl) // array is not empty dl -> prev = first; else // array is empty last = first; ++ ArraySize; } inline void InsertLast (const DATATYPE & Value) { ArrayItem < DATATYPE > * dl = last; last = new ArrayItem < DATATYPE > ; // chain on new element at tail of chain last -> Data = Value; last -> prev = dl; if (dl) dl -> next = last; else first = last; ++ ArraySize; } inline void InsertLast (DATATYPE & Value) { ArrayItem < DATATYPE > * dl = last; last = new ArrayItem < DATATYPE > ; // chain on new element at tail of chain last -> Data = Value; last -> prev = dl; if (dl) dl -> next = last; else first = last; ++ ArraySize; } void AddBefore (const DATATYPE &, int Index); void AddAfter (const DATATYPE &, int Index); BOOL RemoveAt (int); inline void Remove (const DATATYPE & dt) { for (Array ::Iterator Iter (*this); Iter; Iter++) { const DATATYPE & mydt = Iter (); if (dt == mydt) { RemoveAt (Iter); return; } } } inline BOOL RemoveFirst (void) { if (! first) return FALSE; ArrayItem < DATATYPE > *dl = first; first = dl -> next; if (first) first -> prev = NULL; else last = NULL; delete dl; -- ArraySize; return TRUE; } inline BOOL RemoveLast (void) { if (! last) return FALSE; ArrayItem < DATATYPE > *dl = last; last = dl -> prev; if (last) last -> next = NULL; else first = NULL; delete dl; -- ArraySize; return TRUE; } inline void RemoveAll (void) { if (! ArraySize) return; if (! ClearType) return; while (first) { ArrayItem < DATATYPE > * pt = first; first = first -> next; delete pt; } first = last = NULL; ArraySize = 0; } inline const Array & operator += (const DATATYPE & Value) { InsertLast (Value); return (*this); } inline DATATYPE & GetAt (int Index) const { ArrayItem < DATATYPE > * dl = PtrOfItemAt (Index); if (dl == NULL) return ((DATATYPE &) *dl); return (dl -> Data); } inline DATATYPE & GetFirst (void) const { return first -> Data; } inline DATATYPE & GetLast (void) const { return last -> Data; } inline DATATYPE & GetNextFirst (void) const { return first -> next -> Data; } inline DATATYPE & GetPrevLast (void) const { return last -> prev -> Data; } BOOL TransferTo (Array & array); int TransferTo (Array & array, DATATYPE & dt); int TransferTo (Array & array, int fromIndex, int NbOfTransfer); int Append (const Array & from); int CopyTo (FixArray & array) const; int CopyTo (Array & array) const; int CopyTo (DATATYPE * pArray, int NbOfElem) const; void SetCapacity (int nNewSize); inline void Reset (void) { RemoveAll (); } inline int GetSize (void) const { return (ArraySize); } inline int GetCapacity (void) const { return ArraySize; } inline BOOL IsEmpty (void) const { return (! GetSize ()); } inline DATATYPE & operator[] (int Index) const { return (GetAt (Index)); } inline void operator = (const Array < DATATYPE > & array) { RemoveAll (); array.CopyTo (*this); } inline bool IsExist (const DATATYPE & Value) const { return IndexOf (Value) != -1; } // find component starting at left, -1 if not found inline int IndexOf (const DATATYPE & Value) const { ArrayIterator < DATATYPE> ArrayIter (*this); while (ArrayIter) { if (ArrayIter () == Value) return ArrayIter.GetLastIndex (); } return -1; } // find component starting at right inline int LastIndexOf (const DATATYPE & Value) const { ArrayReverseIterator < DATATYPE> ArrayIter (*this); while (ArrayIter) { if (ArrayIter () == Value) return ArrayIter.GetLastIndex (); } return -1; } DATATYPE * Find (const DATATYPE & Key) const { for (Iterator Iter (*this); Iter; Iter++) { DATATYPE & value = Iter (); if (value == Key) return &value; } return NULL; } template DATATYPE * Find (const ODT & Key) const { for (Iterator Iter (*this); Iter; Iter++) { DATATYPE & value = Iter (); if (value == Key) return &value; } return NULL; } template DATATYPE * Find (const DATATYPE & Key, Compare cf) const { for (Iterator Iter (*this); Iter; Iter++) { DATATYPE & value = Iter (); if (cf (value, Key)) return &value; } return NULL; } template DATATYPE * Find (const ODT & Key, Compare cf) const { for (Iterator Iter (*this); Iter; Iter++) { DATATYPE & value = Iter (); if (cf (value, Key)) return &value; } return NULL; } inline BOOL Replace (int nPos, const DATATYPE & Value) { if (nPos < 0) return FALSE; if (nPos >= ArraySize) return FALSE; AddAfter (Value, nPos); MoveItemToFirst (nPos); RemoveFirst (); return TRUE; } // Reverse void Reverse (void) { if (ArraySize <= 1) return; ArrayItem < DATATYPE > * pNext = first; ArrayItem < DATATYPE > * pPrev = last; for (int Index=0; Index * dln = pNext->next; ArrayItem < DATATYPE > * dlp = pPrev->prev; Swap (pNext, pPrev); pNext = dln; pPrev = dlp; } } // Exchange the position of nFirst and nLast inline BOOL Swap (int nFirst, int nLast) { if (nFirst < 0) nFirst = 0; if (nFirst >= ArraySize) nFirst = ArraySize -1; if (nLast < 0) nLast = 0; if (nLast >= ArraySize) nLast = ArraySize -1; if (nFirst == nLast) return TRUE; ArrayItem < DATATYPE > * dlFirst; ArrayItem < DATATYPE > * dlLast ; if (nFirst < nLast) { dlFirst = PtrOfItemAt (nFirst); // 2 dlLast = PtrOfItemAt (nLast); // 5 } else { dlFirst = PtrOfItemAt (nLast); // 2 dlLast = PtrOfItemAt (nFirst); // 5 } return Swap (dlFirst, dlLast); } // 把B放到A之前 inline BOOL MoveBBeforeA (int nB, int nA) { if (nA < 0) nA = 0; if (nA >= ArraySize) nA = ArraySize -1; if (nB < 0) nB = 0; if (nB >= ArraySize) nB = ArraySize -1; if (nB == nA) // 把1移到1之前是无效的 return TRUE; if (nA == nB + 1) // 把1移到2之前是无效的, 1本来就在2之前 return TRUE; if (nB == nA + 1) // 相邻的情况,其实是交换 return Swap (nA, nB); ArrayItem < DATATYPE > * dlB; ArrayItem < DATATYPE > * dlA; dlA = PtrOfItemAt (nA); // 2 dlB = PtrOfItemAt (nB); // 5 return MoveBBeforeA (dlB, dlA); } // 把B放到A之后 inline BOOL MoveBAfterA (int nB, int nA) { if (nA < ArraySize-1) return MoveBBeforeA (nB, nA+1); return MoveItemToLast (nB); } // 把指定的项移动到队首 inline BOOL MoveItemToFirst (int nItem) { return MoveBBeforeA (nItem, 0); } // 把指定的项移动到队尾 inline BOOL MoveItemToLast (int nItem) { if (ArraySize == 0) // 空队列不移动 return true; if (ArraySize == 1) // 单项队列也不移动 return true; // 队列至少是2项 if (nItem < 0) nItem = 0; if (nItem >= ArraySize-1) // 最后一项本来就在队尾了 // nItem = ArraySize-1; return true; ArrayItem < DATATYPE > * pItem = PtrOfItemAt (nItem); if (pItem == NULL) return false; if (pItem == first) // so (nItem == 0) first = pItem->next; else pItem->prev->next = pItem->next; // 因为nItem不是最后一项,因此pItem->next不可能为NULL pItem->next->prev = pItem->prev; pItem->next = NULL; pItem->prev = last; last->next = pItem; last = pItem; return true; } // Quick sort inline void Sort (void) { Sort (0, ArraySize - 1); } // Quick sort, associated with ar template void Sort (Array & ar) { return Sort (0, ArraySize - 1, ar); } template void Sort (FixArray & ar) { return Sort (0, ArraySize - 1, ar); } // Quick sort, using the given compare function template void Sort (Compare cf) { return Sort (0, ArraySize - 1, cf); } template void Sort (Array & ar, Compare cf) { return Sort (0, ArraySize - 1, ar, cf); } template void Sort (FixArray & ar, Compare cf) { return Sort (0, ArraySize - 1, ar, cf); } // 带参数的比较 template void Sort (Compare cf, arg a) { return Sort (0, ArraySize - 1, cf, a); } template void Sort (Array & ar, Compare cf, arg a) { return Sort (0, ArraySize - 1, ar, cf, a); } template void Sort (FixArray & ar, Compare cf, arg a) { return Sort (0, ArraySize - 1, ar, cf, a); } protected: void Sort (int low, int high); template void Sort (int low, int high, Array & ar); template void Sort (int low, int high, FixArray & ar); template void Sort (int low, int high, Compare cf); template void Sort (int low, int high, Array & ar, Compare cf); template void Sort (int low, int high, FixArray & ar, Compare cf); template void Sort (int low, int high, Compare cf, arg a); template void Sort (int low, int high, Array & ar, Compare cf, arg a); template void Sort (int low, int high, FixArray & ar, Compare cf, arg a); public: inline void HeapSort () { HeapSort (ArraySize - 1); } protected: void HeapSort (int n); void HeapAdjust (int s, int m); protected: friend class ArrayIterator < DATATYPE > ; friend class ArrayReverseIterator < DATATYPE > ; friend class ArrayStepIterator < DATATYPE > ; public: bool IsSorted () const; template bool IsSorted (Compare cf) const; public: DATATYPE & FindMin (void) const; DATATYPE & FindMax (void) const; template DATATYPE & FindMin (Compare cf) const; template DATATYPE & FindMax (Compare cf) const; public: int IndexOfMin (void) const; int IndexOfMax (void) const; template int IndexOfMin (Compare cf) const; template int IndexOfMax (Compare cf) const; public: inline ArrayItem < DATATYPE > * PtrOfItemAt (int nItem) const { ArrayItem < DATATYPE > * dl; if (nItem >= ArraySize) { return NULL; } // scan the chain to get the element (from head or from tail) if (nItem < ArraySize / 2) { // scan from the head of chain dl = first; ++nItem; while (--nItem > 0) dl = dl -> next; } else { // scan from the tail of chain dl = last; nItem = (ArraySize - nItem); while (--nItem > 0) dl = dl -> prev; } return dl; } inline ArrayItem < DATATYPE > * PtrOfItem (const DATATYPE & dt) const { for (Array ::Iterator Iter (*this); Iter; Iter++) { const DATATYPE & mydt = Iter (); if (dt == mydt) { ArrayItem < DATATYPE > * dl = Iter.m_Next; return dl; } } return NULL; } protected: inline BOOL Swap (ArrayItem < DATATYPE > * dlFirst, ArrayItem < DATATYPE > * dlLast) { if ( (dlFirst == NULL) || (dlLast == NULL) ) return FALSE; ArrayItem < DATATYPE > * PF = dlFirst->prev; // 1 ArrayItem < DATATYPE > * FN = dlFirst->next; // 3 ArrayItem < DATATYPE > * PL = dlLast->prev; // 4 ArrayItem < DATATYPE > * LN = dlLast->next; // 6 bool bNeighbor = (dlFirst->next == dlLast); dlFirst->next = LN; if (LN) LN->prev = dlFirst; else last = dlFirst; if (bNeighbor) { dlLast->next = dlFirst; dlFirst->prev = dlLast; } else { dlLast->next = FN; if (FN) FN->prev = dlLast; if (PL) PL->next = dlFirst; dlFirst->prev = PL; } if (PF) PF->next = dlLast; else first = dlLast; dlLast->prev = PF; return TRUE; } // 把B移动到A之前 inline BOOL MoveBBeforeA (ArrayItem < DATATYPE > * B, ArrayItem < DATATYPE > * A) { if ( (A == NULL) || (B == NULL) ) return FALSE; ArrayItem < DATATYPE > * AP = A->prev; // 2 ArrayItem < DATATYPE > * AN = A->next; // 4 ArrayItem < DATATYPE > * BP = B->prev; // 6 ArrayItem < DATATYPE > * BN = B->next; // 8 bool bNeighbor = (AN == B); A->prev = B; if (AP) { B->next = AP->next; AP->next= B; } else { B->next = first; first = B; } B->prev = AP; if (bNeighbor) { } else { if (AN) AN->prev= A; } if (BP) BP->next = BN; else first = BN; if (BN) BN->prev = BP; else last = BP; return TRUE; } public: /********************************************************************* * Iterator Class *********************************************************************/ class Iterator { protected: const Array < DATATYPE > * m_Array; int m_ArraySize; int m_Index; ArrayItem < DATATYPE > * m_Next; mutable DATATYPE * m_Item; public: Iterator (const Array < DATATYPE > * theArray) { Attach (theArray); } Iterator (const Array < DATATYPE > & theArray) { Attach (& theArray); } Iterator (const Iterator & Iter) { Assign (Iter); } inline Iterator & Assign (const Iterator & Iter) { m_Array = Iter.m_Array; m_ArraySize = Iter.m_ArraySize; m_Index = Iter.m_Index; m_Next = Iter.m_Next; m_Item = Iter.m_Item; return (*this); } inline Iterator & operator = (const Iterator & Iter) { return Assign (Iter); } inline void Attach (const Array < DATATYPE > * theArray) { if (theArray) m_Array = theArray; else m_Array = NULL; Restart (); } inline void Restart () { m_Index = 0; if (m_Array) { m_ArraySize = m_Array -> GetSize (); m_Next = m_Array->first; } else { m_ArraySize = 0; m_Next = NULL; } m_Item = NULL; } inline void GoToFirst (void) { Restart (); } inline void GoToLast (void) { if (m_Array) { m_ArraySize = m_Array -> GetSize (); m_Next = m_Array->last; } else { m_ArraySize = 0; m_Next = NULL; } m_Index = m_ArraySize - 1; m_Item = NULL; } inline operator bool () const { return bool (m_Next); } inline DATATYPE & operator () (void) { return Current (); } inline DATATYPE & operator * (void) { return Current (); } inline DATATYPE & Current (void) { if (m_Next) { m_Item = &(m_Next -> Data); return *m_Item; } // _fatal ("Warning! will return null"); m_Item = NULL; return ((DATATYPE &) *m_Item); } inline const DATATYPE & Current (void) const { if (m_Next) { m_Item = &(m_Next -> Data); return *m_Item; } // _fatal ("Warning! will return null"); m_Item = NULL; return ((const DATATYPE &) *m_Item); } inline int Index (void) const { return m_Index; } virtual inline void Next (void) { if (m_Next) { m_Next = m_Next -> next; m_Index ++; } } inline int Remainder () const { return m_ArraySize - m_Index; } inline void operator ++ () // ++Iter; prevfix { Next (); } inline void operator ++ (int) // Iter ++, postfix { Next (); } virtual inline void Prev (void) { if (m_Next) { m_Next = m_Next -> prev; m_Index --; } } inline void operator -- () // --Iter; prevfix { Prev (); } inline void operator -- (int) // Iter++, postfix { Prev (); } virtual inline void Step (int NumberOfStep) { if (NumberOfStep > 0) { for (int nStep=0; nStep next; m_Index ++; } } else { for (int nStep=0; nStep<-NumberOfStep && m_Next; nStep++) { m_Next = m_Next -> prev; m_Index --; } } } inline Iterator & operator += (int NumberOfStep) { Step (NumberOfStep); return (*this); } inline Iterator & operator -= (int NumberOfStep) { Step (-NumberOfStep); return (*this); } inline bool Find (const DATATYPE & Value) { while (*this) { if (Current () == Value) return true; Next (); } return false; } inline bool ReverseFind (const DATATYPE & Value) { while (*this) { if (Current () == Value) return true; Prev (); } return false; } inline bool operator < (const Iterator & Iter) const { return m_Index < Iter.m_Index; } inline bool operator > (const Iterator & Iter) const { return m_Index > Iter.m_Index; } inline bool operator <= (const Iterator & Iter) const { return m_Index <= Iter.m_Index; } inline bool operator >= (const Iterator & Iter) const { return m_Index >= Iter.m_Index; } inline bool operator == (const Iterator & Iter) const { return m_Index == Iter.m_Index; } inline bool operator < (int i) const { return m_Index < i; } inline bool operator > (int i) const { return m_Index > i; } inline bool operator <= (int i) const { return m_Index <= i; } inline bool operator >= (int i) const { return m_Index >= i; } inline bool operator == (int i) const { return m_Index == i; } inline int GetArraySize (void) const { return (m_ArraySize); } inline const Array * __GetArray () const { return m_Array; } inline const ArrayItem < DATATYPE > * __GetCurrent () const { return m_Next; } inline ArrayItem < DATATYPE > * __GetCurrent () { return m_Next; } inline void __SetArraySize (int size) { m_ArraySize = size; } }; /********************************************************************* * RangeIterator Class *********************************************************************/ class RangeIterator : public Iterator { private: int m_MinIndex; int m_MaxIndex; public: RangeIterator (const Array < DATATYPE > * theArray) : Iterator (theArray) { m_MinIndex = 0; m_MaxIndex = m_ArraySize-1; } RangeIterator (const Array < DATATYPE > & theArray) : Iterator (theArray) { m_MinIndex = 0; m_MaxIndex = m_ArraySize-1; } RangeIterator (const Iterator & Iter) : Iterator (Iter) { m_MinIndex = 0; m_MaxIndex = m_ArraySize-1; } inline void SetRange (int minIndex, int maxIndex) { m_MinIndex = minIndex; m_MaxIndex = maxIndex; if (m_MinIndex < 0) m_MinIndex = 0; if (m_MinIndex >= m_ArraySize) m_MinIndex = m_ArraySize-1; if (m_MaxIndex < 0) m_MaxIndex = 0; if (m_MaxIndex >= m_ArraySize) m_MaxIndex = m_ArraySize-1; if (m_MinIndex > m_MaxIndex) m_MinIndex = m_MaxIndex; Restart (); } inline void Restart () { m_Index = 0; if (m_Array) { m_ArraySize = m_Array -> GetSize (); m_Next = m_Array->first; } else { m_ArraySize = 0; m_Next = NULL; } m_Item = NULL; if (m_MinIndex) (*this) += m_MinIndex; } void GotoIndex (int Index) { m_Index = 0; if (m_Array) { m_ArraySize = m_Array -> GetSize (); m_Next = m_Array->first; } else { m_ArraySize = 0; m_Next = NULL; } m_Item = NULL; if (Index < m_MinIndex) Index = m_MinIndex; if (Index > m_MaxIndex) Index = m_MaxIndex; (*this) += Index; } inline void Next (void) { if (m_Next) { if (m_Index >= m_MaxIndex) { m_Next = NULL; } else { m_Next = m_Next -> next; m_Index ++; } } } virtual inline void Prev (void) { if (m_Next) { if (m_Index < m_MinIndex) { m_Next = NULL; } else { m_Next = m_Next -> prev; m_Index --; } } } inline RangeIterator & operator += (int NumberOfStep) { if (NumberOfStep > 0) { for (int nStep=0; nStep next; m_Index ++; } } else { for (int nStep=0; nStep<-NumberOfStep && m_Next; nStep++) { m_Next = m_Next -> prev; m_Index --; } } return (*this); } inline RangeIterator & operator -= (int NumberOfStep) { return this->operator += (-NumberOfStep); } }; /********************************************************************* * ReverseIterator Class *********************************************************************/ class ReverseIterator { protected: const Array < DATATYPE > * m_Array; int m_ArraySize; int m_Index; ArrayItem < DATATYPE > * m_Next; mutable DATATYPE * m_Item; public: ReverseIterator (const Array < DATATYPE > * theArray) { Attach (theArray); } ReverseIterator (const Array < DATATYPE > & theArray) { Attach (&theArray); } ReverseIterator (const ReverseIterator & Iter) { Assign (Iter); } inline ReverseIterator & Assign (const ReverseIterator & Iter) { m_Array = Iter.m_Array; m_ArraySize = Iter.m_ArraySize; m_Index = Iter.m_Index; m_Next = Iter.m_Next; m_Item = Iter.m_Item; return (*this); } inline ReverseIterator & operator = (const ReverseIterator & Iter) { return Assign (Iter); } inline void Attach (const Array < DATATYPE > * theArray) { if (theArray) m_Array = theArray; else m_Array = NULL; Restart (); } inline void Restart () { if (m_Array) { m_ArraySize = m_Array -> GetSize (); m_Next = m_Array->last; } else { m_ArraySize = 0; m_Next = NULL; } m_Index = m_ArraySize - 1; m_Item = NULL; } inline operator bool () const { return bool (m_Next); } inline DATATYPE & operator () (void) { return Current (); } inline const DATATYPE & operator () (void) const { return Current (); } inline DATATYPE & operator * (void) { return Current (); } inline DATATYPE & Current (void) { if (m_Next) { m_Item = &(m_Next -> Data); return *m_Item; } // _fatal ("Warning! will return null"); m_Item = NULL; return ((DATATYPE &) *m_Item); } inline const DATATYPE & Current (void) const { if (m_Next) { m_Item = &(m_Next -> Data); return *m_Item; } // _fatal ("Warning! will return null"); m_Item = NULL; return ((const DATATYPE &) *m_Item); } inline int Index (void) const { return m_Index; } inline int Remainder () const { return m_Index; } inline DATATYPE & operator ++ () // ++Iter; prevfix { if (m_Next) { m_Next = m_Next -> prev; m_Index --; } return Current (); } inline DATATYPE & operator ++ (int) // Iter ++, postfix { DATATYPE & dt = Current (); if (m_Next) { m_Next = m_Next -> prev; m_Index --; } return dt; } inline DATATYPE & operator -- () // --Iter; prevfix { if (m_Next) { m_Next = m_Next -> next; m_Index ++; } return Current (); } inline DATATYPE & operator -- (int) // Iter++, postfix { DATATYPE & dt = Current (); if (m_Next) { m_Next = m_Next -> next; m_Index ++; } return dt; } inline ReverseIterator & operator += (int NumberOfStep) { if (NumberOfStep > 0) { for (int nStep=0; nStep prev; m_Index --; } } else { for (int nStep=0; nStep<-NumberOfStep && m_Next; nStep++) { m_Next = m_Next -> next; m_Index ++; } } return (*this); } inline ReverseIterator & operator -= (int NumberOfStep) { return this->operator += (-NumberOfStep); } inline bool operator < (const ReverseIterator & Iter) const { return m_Index < Iter.m_Index; } inline bool operator > (const ReverseIterator & Iter) const { return m_Index > Iter.m_Index; } inline bool operator <= (const ReverseIterator & Iter) const { return m_Index <= Iter.m_Index; } inline bool operator >= (const ReverseIterator & Iter) const { return m_Index >= Iter.m_Index; } inline bool operator == (const ReverseIterator & Iter) const { return m_Index == Iter.m_Index; } inline bool operator < (int i) const { return m_Index < i; } inline bool operator > (int i) const { return m_Index > i; } inline bool operator <= (int i) const { return m_Index <= i; } inline bool operator >= (int i) const { return m_Index >= i; } inline bool operator == (int i) const { return m_Index == i; } inline int GetArraySize (void) const { return (m_ArraySize); } inline const Array * __GetArray () const { return m_Array; } inline const ArrayItem < DATATYPE > * __GetCurrent () const { return m_Next; } inline ArrayItem < DATATYPE > * __GetCurrent () { return m_Next; } inline void __SetArraySize (int size) { m_ArraySize = size; } }; /********************************************************************* * RangeReverseIterator Class *********************************************************************/ class RangeReverseIterator : public ReverseIterator { private: int m_MinIndex; int m_MaxIndex; public: RangeReverseIterator (const Array < DATATYPE > * theArray) : ReverseIterator (theArray) { m_MinIndex = 0; m_MaxIndex = m_ArraySize-1; } RangeReverseIterator (const Array < DATATYPE > & theArray) : ReverseIterator (theArray) { m_MinIndex = 0; m_MaxIndex = m_ArraySize-1; } RangeReverseIterator (const ReverseIterator & Iter) : ReverseIterator (Iter) { m_MinIndex = 0; m_MaxIndex = m_ArraySize-1; } inline void SetRange (int minIndex, int maxIndex) { m_MinIndex = minIndex; m_MaxIndex = maxIndex; if (m_MinIndex < 0) m_MinIndex = 0; if (m_MinIndex >= m_ArraySize) m_MinIndex = m_ArraySize-1; if (m_MaxIndex < 0) m_MaxIndex = 0; if (m_MaxIndex >= m_ArraySize) m_MaxIndex = m_ArraySize-1; if (m_MinIndex > m_MaxIndex) m_MinIndex = m_MaxIndex; Restart (); } inline void Restart () { if (m_Array) { m_ArraySize = m_Array -> GetSize (); m_Next = m_Array->last; } else { m_ArraySize = 0; m_Next = NULL; } m_Index = m_ArraySize - 1; m_Item = NULL; if (m_MaxIndex) (*this) += m_ArraySize - m_MaxIndex - 1; } void GotoIndex (int Index) { if (m_Array) { m_ArraySize = m_Array -> GetSize (); m_Next = m_Array->last; } else { m_ArraySize = 0; m_Next = NULL; } m_Index = m_ArraySize - 1; m_Item = NULL; if (Index < m_MinIndex) Index = m_MinIndex; if (Index > m_MaxIndex) Index = m_MaxIndex; (*this) += m_ArraySize - Index - 1; } inline DATATYPE & operator ++ () // ++Iter; prevfix { if (m_Next) { if (m_Index <= m_MinIndex) { m_Next = NULL; } else { m_Next = m_Next -> prev; m_Index --; } } return Current (); } inline DATATYPE & operator ++ (int) // Iter ++, postfix { DATATYPE & dt = Current (); if (m_Next) { if (m_Index <= m_MinIndex) { m_Next = NULL; } else { m_Next = m_Next -> prev; m_Index --; } } return dt; } inline DATATYPE & operator -- () // --Iter; prevfix { if (m_Next) { if (m_Index >= m_MaxIndex) { m_Next = NULL; } else { m_Next = m_Next -> next; m_Index ++; } } return Current (); } inline DATATYPE & operator -- (int) // Iter++, postfix { DATATYPE & dt = Current (); if (m_Next) { if (m_Index >= m_MaxIndex) { m_Next = NULL; } else { m_Next = m_Next -> next; m_Index ++; } } return dt; } inline RangeReverseIterator & operator += (int NumberOfStep) { if (NumberOfStep > 0) { for (int nStep=0; nStep prev; m_Index --; } } else { for (int nStep=0; nStep<-NumberOfStep && m_Next; nStep++) { m_Next = m_Next -> next; m_Index ++; } } return (*this); } inline RangeReverseIterator & operator -= (int NumberOfStep) { return this->operator += (-NumberOfStep); } }; public: void AddBefore (const DATATYPE &, Iterator & Iter); BOOL RemoveAt (Iterator & Iter); void AddBefore (const DATATYPE &, ReverseIterator & Iter); BOOL RemoveAt (ReverseIterator & Iter); public: // 不带参数 template int ForEach (pfnForEach pf) { int NbOfCalled = 0; for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++) { DATATYPE & dt = Iter (); if (! pf (dt)) break; } return NbOfCalled; } template int ForEach (pfnForEach pf) const { int NbOfCalled = 0; for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++) { const DATATYPE & dt = Iter (); if (! pf (dt)) break; } return NbOfCalled; } // 带 1 个参数 template int ForEach (pfnForEach pf, arg e) { int NbOfCalled = 0; for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++) { DATATYPE & dt = Iter (); if (! pf (dt, e)) break; } return NbOfCalled; } template int ForEach (pfnForEach pf, arg e) const { int NbOfCalled = 0; for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++) { const DATATYPE & dt = Iter (); if (! pf (dt, e)) break; } return NbOfCalled; } // 带 2 个参数 template int ForEach (pfnForEach pf, T1 t1, T2 t2) { int NbOfCalled = 0; for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++) { DATATYPE & dt = Iter (); if (! pf (dt, t1, t2)) break; } return NbOfCalled; } template int ForEach (pfnForEach pf, T1 t1, T2 t2) const { int NbOfCalled = 0; for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++) { const DATATYPE & dt = Iter (); if (! pf (dt, t1, t2)) break; } return NbOfCalled; } // 带 3 个参数 template int ForEach (pfnForEach pf, T1 t1, T2 t2, T3 t3) { int NbOfCalled = 0; for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++) { DATATYPE & dt = Iter (); if (! pf (dt, t1, t2, t3)) break; } return NbOfCalled; } template int ForEach (pfnForEach pf, T1 t1, T2 t2, T3 t3) const { int NbOfCalled = 0; for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++) { const DATATYPE & dt = Iter (); if (! pf (dt, t1, t2, t3)) break; } return NbOfCalled; } // 带 4 个参数 template int ForEach (pfnForEach pf, T1 t1, T2 t2, T3 t3, T4 t4) { int NbOfCalled = 0; for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++) { DATATYPE & dt = Iter (); if (! pf (dt, t1, t2, t3, t4)) break; } return NbOfCalled; } template int ForEach (pfnForEach pf, T1 t1, T2 t2, T3 t3, T4 t4) const { int NbOfCalled = 0; for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++) { const DATATYPE & dt = Iter (); if (! pf (dt, t1, t2, t3, t4)) break; } return NbOfCalled; } protected: template void AddBefore (const DATATYPE &, IterType & Iter); template BOOL RemoveAt (IterType & Iter); }; /********************************************************************* * ArrayOfPtr Unit Class * * usage: ArrayOfPtr < ClassType > VarName; * *********************************************************************/ template < class DATATYPE > class ArrayOfPtr : public Array < DATATYPE > { public: inline BOOL RemoveFirst (void) { ArrayItem < DATATYPE > * dl = first; if (dl) delete dl->Data; return Array ::RemoveFirst (); } inline BOOL RemoveLast (void) { ArrayItem < DATATYPE > * dl = last; if (dl) delete dl->Data; return Array ::RemoveLast (); } inline BOOL RemoveAt (int Index) { ArrayItem * dl = PtrOfItemAt (Index); if (dl) delete dl->Data; return Array ::RemoveAt (Index); } inline void RemoveAll (void) { ArrayItem * dl = first; while (dl) { delete dl-> Data; dl = dl->next; } Array ::RemoveAll (); } inline void Reset (void) { RemoveAll (); } ~ArrayOfPtr () { Reset (); } static inline int compare (const DATATYPE & t1, const DATATYPE & t2) { return (*t1>*t2 ? 1 : (*t1 == *t2 ? 0 : -1)); } static inline bool IsEqual (const DATATYPE & t1, const DATATYPE & t2) { return (*t1 == *t2); } DATATYPE * Find (const DATATYPE & Key) const { return Array ::Find (Key, IsEqual); } template DATATYPE * Find (const ODT & Key) const { for (Iterator Iter (*this); Iter; Iter++) { DATATYPE & value = Iter (); if (*value == Key) return &value; } return NULL; } template DATATYPE * Find (const DATATYPE & Key, Compare cf) const { for (Iterator Iter (*this); Iter; Iter++) { DATATYPE & value = Iter (); if (cf (*value, *Key)) return &value; } return NULL; } template DATATYPE * Find (const ODT & Key, Compare cf) const { for (Iterator Iter (*this); Iter; Iter++) { DATATYPE & value = Iter (); if (cf (*value, Key)) return &value; } return NULL; } void Sort (void) { return Array ::Sort (compare); } template void Sort (Array & ar) { return Array ::Sort (ar, compare); } template void Sort (FixArray & ar) { return Array ::Sort (ar, compare); } inline void Remove (const DATATYPE & dt) { for (Array ::Iterator Iter (*this); Iter; Iter++) { const DATATYPE & mydt = Iter (); if (dt == mydt) { RemoveAt (Iter); return; } } } inline BOOL RemoveAt (Iterator & Iter) { return RemoveAt (Iter); } inline BOOL RemoveAt (ReverseIterator & Iter) { return RemoveAt (Iter); } protected: template BOOL RemoveAt (IterType & Iter); }; /********************************************************************* * ArrayIterator Unit Class * * *********************************************************************/ template < class DATATYPE > class ArrayIterator { private: int ArraySize; int Index; DATATYPE * LastItem; ArrayItem < DATATYPE > * LastAccess; public: ArrayIterator (const Array < DATATYPE > * theArray) { if (theArray) { ArraySize = theArray -> GetSize (); LastAccess = (ArrayItem < DATATYPE > *) theArray -> first; } else { ArraySize = 0; LastAccess = NULL; } Index = 0; LastItem = NULL; } ArrayIterator (const Array < DATATYPE > & theArray) { ArraySize = theArray.GetSize (); LastAccess = (ArrayItem < DATATYPE > *) theArray.first; Index = 0; LastItem = NULL; } operator bool () const { return bool (LastAccess); } DATATYPE & operator () (void) { if (LastAccess) { LastItem = &(LastAccess -> Data); LastAccess = LastAccess -> next; Index ++; return *LastItem; } // _fatal ("Warning! will return null"); LastItem = NULL; return ((DATATYPE &) *LastItem); } int GetLastIndex (void) { return (Index-1); } DATATYPE & GetLastItem (void) { return ((DATATYPE &)(*LastItem)); } int GetArraySize (void) const { return (ArraySize); } }; template < class DATATYPE > class ArrayReverseIterator { private: int ArraySize; int Index; DATATYPE * LastItem; ArrayItem < DATATYPE > * LastAccess; public: ArrayReverseIterator (const Array < DATATYPE > * theArray) { if (theArray) { ArraySize = theArray -> GetSize (); LastAccess = (ArrayItem < DATATYPE > *) theArray -> last; Index = ArraySize-1; } else { ArraySize = 0; LastAccess = NULL; Index = 0; } LastItem = NULL; } ArrayReverseIterator (const Array < DATATYPE > & theArray) { ArraySize = theArray.GetSize (); LastAccess = (ArrayItem < DATATYPE > *) theArray.last; Index = ArraySize-1; LastItem = NULL; } operator bool () const { return bool (LastAccess); } DATATYPE & operator () (void) { if (LastAccess) { LastItem = &(LastAccess -> Data); LastAccess = LastAccess -> prev; Index --; return *LastItem; } _fatal ("Warning! will return null"); LastItem = NULL; return ((DATATYPE &) *LastItem); } int GetLastIndex (void) { return (Index+1); } DATATYPE & GetLastItem (void) { return ((DATATYPE &)(*LastItem)); } int GetArraySize (void) const { return (ArraySize); } }; /********************************************************************* * ArrayIterator Unit Class * * *********************************************************************/ template < class DATATYPE > class ArrayStepIterator { private: int ArraySize; int Index; DATATYPE * LastItem; ArrayItem < DATATYPE > * LastAccess; public: ArrayStepIterator (const Array < DATATYPE > * theArray) { if (theArray) { ArraySize = theArray -> GetSize (); LastAccess = (ArrayItem < DATATYPE > *) theArray -> first; } else { ArraySize = 0; LastAccess = NULL; } Index = 0; LastItem = NULL; } ArrayStepIterator (const Array < DATATYPE > & theArray) { ArraySize = theArray.GetSize (); LastAccess = (ArrayItem < DATATYPE > *) theArray.first; Index = 0; LastItem = NULL; } operator bool () const { return bool (LastAccess); } DATATYPE & operator () (void) { if (LastAccess) { LastItem = &(LastAccess -> Data); return *LastItem; } // _fatal ("Warning! will return null"); LastItem = NULL; return ((DATATYPE &) *LastItem); } int GetLastIndex (void) { return Index; } ArrayStepIterator & operator ++ () // Iter ++; postfix { if (LastAccess) { LastAccess = LastAccess -> next; Index ++; } return (*this); } ArrayStepIterator & operator ++ (int) // ++ Iter, prevfix { if (LastAccess) { LastAccess = LastAccess -> next; Index ++; } return (*this); } ArrayStepIterator & operator -- () // Iter --; postfix { if (LastAccess) { LastAccess = LastAccess -> prev; Index --; } return (*this); } ArrayStepIterator & operator -- (int) // -- Iter, prevfix { if (LastAccess) { LastAccess = LastAccess -> prev; Index --; } return (*this); } ArrayStepIterator & operator += (int NumberOfStep) { if (NumberOfStep > 0) { for (int nStep=0; nStep next; Index ++; } } else { for (int nStep=0; nStep<-NumberOfStep && LastAccess; nStep++) { LastAccess = LastAccess -> prev; Index --; } } return (*this); } ArrayStepIterator & operator -= (int NumberOfStep) { return this->operator += (-NumberOfStep); } DATATYPE & GetLastItem (void) { return ((DATATYPE &)(*LastItem)); } int GetArraySize (void) const { return (ArraySize); } }; //#endif