123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674 |
- /***************************************************************************
- * E-Com Technology Ltd.
- *
- * ECOMPACS DICOM Network Transport Libraries * Version 0.1 Beta
- ***************************************************************************/
- #pragma warning (disable : 4800)
- // avoid bool (LastAccess);
- //////////////////////////////////////////////////////////////////////
- // Fixed, Fast-Access Array.
- //////////////////////////////////////////////////////////////////////
- template < class DATATYPE > class FixArray
- {
- protected:
- mutable int Top;
- int ArraySize;
- DATATYPE * DataTable;
- public:
- bool bAutoEnlarge;
- public:
- inline FixArray ()
- {
- ArraySize = 0;
- Top = 0;
- DataTable = NULL;
- bAutoEnlarge = false;
- }
- inline FixArray (UINT32 Size)
- {
- ArraySize = Size;
- Top = 0;
- DataTable = new DATATYPE [ ArraySize ];
- bAutoEnlarge = false;
- }
- ~FixArray () { Reset ();}
- inline int GetTop () const { return ( Top ); }
- inline int GetCapacity () const { return ( ArraySize ); }
- inline int GetSize () const { return ( ArraySize ); }
- inline void RemoveAll (void)
- {
- if ( DataTable) delete [] DataTable;
- DataTable = NULL;
- ArraySize = 0;
- Top = 0;
- }
- inline void Reset (void)
- {
- RemoveAll ();
- }
- inline bool IsEmpty (void) const
- {
- return (Top == 0);
- }
- inline void Empty (void)
- {
- Top = 0;
- }
- inline DATATYPE & operator [] (int Index) const
- {
- return (GetAt (Index));
- }
- inline DATATYPE & GetAt (int Index) const
- {
- if (Top <= Index)
- Top = Index + 1;
- if (Top > ArraySize)
- {
- DATATYPE * nul = NULL;
- return (*nul);
- }
- return (DataTable[Index]);
- }
- inline void MemorySet (const DATATYPE & toData)
- {
- for (int Index=0; Index<ArraySize; Index++)
- DataTable[Index] = toData;
- }
- inline int Add (const DATATYPE & Data)
- {
- if (Top >= ArraySize)
- {
- if (bAutoEnlarge)
- Enlarge (GetCapacity () + 1);
- }
- if (Top < ArraySize)
- {
- if (DataTable)
- DataTable [ Top ] = Data;
- ++Top;
- }
- else
- {
- throw ("Out of FixArray range in Add");
- return -1;
- }
- return Top;
- }
- /*
- inline void RemoveAt (int nPos)
- {
- #pragma message ("------------------------------------------")
- #pragma message ("Call FixArray::RemoveAt is NOT recommended")
- #pragma message ("------------------------------------------")
- if (nPos >= ArraySize)
- return;
- if (nPos < 0)
- return;
- for (int Index=nPos; Index<ArraySize-1; Index++)
- {
- DataTable[Index] = DataTable[Index+1];
- }
- Top --;
- }
- */
- inline void SetCapacity (int NewSize)
- {
- Enlarge (NewSize);
- }
- inline int Enlarge (int NewSize)
- {
- if (NewSize <= ArraySize)
- {
- return ArraySize;
- }
- DATATYPE * oldDataTable = DataTable;
- DataTable = new DATATYPE [NewSize];
- if (oldDataTable)
- {
- for (int Index=0; Index<Top; Index++)
- {
- DataTable[Index] = oldDataTable[Index];
- }
- }
- ArraySize = NewSize;
- if (oldDataTable) delete [] oldDataTable;
- return NewSize;
- }
- inline BOOL CopyTo (FixArray <DATATYPE> & array) const
- {
- array.SetCapacity (GetCapacity ());
- for (int Index = 0; Index<GetSize (); Index++)
- {
- array.DataTable[Index] = DataTable[Index];
- }
- array.Top = Top;
- return true;
- }
- inline BOOL CopyTo (Array <DATATYPE> & array) const
- {
- for (int Index = 0; Index<GetSize (); Index++)
- {
- array.Add (DataTable[Index]);
- }
- return true;
- }
- inline int Append (const FixArray <DATATYPE> & from)
- {
- Enlarge (this->GetTop () + from.GetTop ());
- for (int Index=0; Index<from.GetTop (); Index++)
- {
- DATATYPE dt = from[Index];
- Add (dt);
- }
- return GetTop ();
- }
- inline int Append (const Array <DATATYPE> & from)
- {
- Enlarge (this->GetTop () + from.GetTop ());
- for (Array <DATATYPE>::Iterator Iter (from); Iter; Iter++)
- while (Iter)
- {
- DATATYPE value = Iter ();
- Add (value);
- }
- return GetTop ();
- }
- inline void Swap (int ia, int ib)
- {
- DATATYPE & a = GetAt (ia);
- DATATYPE & b = GetAt (ib);
- DATATYPE v;
- v = a;
- a = b;
- b = v;
- }
- void Sort (void)
- {
- if (Top == 0)
- return;
- Sort (0, Top - 1);
- }
- template <class ODT> void Sort (Array <ODT> & ar)
- {
- if (Top == 0)
- return;
- return Sort (0, Top - 1, ar);
- }
- template <class ODT> void Sort (FixArray <ODT> & ar)
- {
- if (Top == 0)
- return;
- return Sort (0, Top - 1, ar);
- }
- inline void HeapSort ()
- {
- HeapSort (ArraySize - 1);
- }
- inline bool IsExist (const DATATYPE & Value) const
- {
- return IndexOf (Value) != -1;
- }
- inline int IndexOf (const DATATYPE & Data) const
- {
- for (int Index=0; Index<Top; Index++)
- {
- if (Data == DataTable[Index])
- return Index;
- }
- return -1;
- }
- template <class ODT> inline int IndexOf (const ODT & Key) const
- {
- for (int Index=0; Index<Top; Index++)
- {
- if (Key == DataTable[Index])
- return Index;
- }
- return -1;
- }
- template <class Compare> inline int IndexOf (const DATATYPE & Key, Compare cf) const
- {
- for (int Index=0; Index<Top; Index++)
- {
- if (cf (Key, DataTable[Index]) == 0)
- return Index;
- }
- return -1;
- }
- template <class ODT, class Compare> inline int IndexOf (const ODT & Key, Compare cf) const
- {
- for (int Index=0; Index<Top; Index++)
- {
- if (cf (Key, DataTable[Index]) == 0)
- return Index;
- }
- return -1;
- }
- inline int LastIndexOf (const DATATYPE & Data) const
- {
- for (int Index=Top-1; Index>=0; Index--)
- {
- if (Data == DataTable[Index])
- return Index;
- }
- return -1;
- }
- inline int IndexOfSorted (const DATATYPE & Data) const
- {
- int Low = 0;
- int High = Top - 1;
- while (Low <= High)
- {
- int Mid = (Low + High) >> 1;
- if (Data == DataTable[Mid]) return Mid;
- if (Data < DataTable[Mid]) High = Mid -1;
- else Low = Mid + 1;
- }
- return -1;
- }
- template <class ODT> inline int IndexOfSorted (const ODT & Key) const
- {
- int Low = 0;
- int High = Top - 1;
- while (Low <= High)
- {
- int Mid = (Low + High) >> 1;
- if (Key == DataTable[Mid]) return Mid;
- if (Key < DataTable[Mid]) High = Mid -1;
- else Low = Mid + 1;
- }
- return -1;
- }
- template <class Compare> inline int IndexOfSorted (const DATATYPE & Key, Compare cf) const
- {
- int Low = 0;
- int High = Top - 1;
- while (Low <= High)
- {
- int Mid = (Low + High) >> 1;
- if (cf (Key, DataTable[Mid]) == 0) return Mid;
- if (cf (Key, DataTable[Mid]) < 0) High = Mid -1;
- else Low = Mid + 1;
- }
- return -1;
- }
- template <class ODT, class Compare> inline int IndexOfSorted (const ODT & Key, Compare cf) const
- {
- int Low = 0;
- int High = Top - 1;
- while (Low <= High)
- {
- int Mid = (Low + High) >> 1;
- if (cf (Key, DataTable[Mid]) == 0) return Mid;
- if (cf (Key, DataTable[Mid]) < 0) High = Mid -1;
- else Low = Mid + 1;
- }
- return -1;
- }
- inline DATATYPE * Find (const DATATYPE & Key) const
- {
- int Index = IndexOf (Key);
- if (Index != -1)
- return &GetAt (Index);
- return NULL;
- }
- template <class ODT> inline DATATYPE * Find (const ODT & Key) const
- {
- int Index = IndexOf (Key);
- if (Index != -1)
- return &GetAt (Index);
- return NULL;
- }
- template <class Compare> inline DATATYPE * Find (const DATATYPE & Key, Compare cf) const
- {
- int Index = IndexOf (Key);
- if (Index != -1)
- return &GetAt (Index);
- return NULL;
- }
- inline DATATYPE * FindSorted (const DATATYPE & Key) const
- {
- int Index = IndexOfSorted (Key);
- if (Index != -1)
- return &GetAt (Index);
- return NULL;
- }
- template <class ODT> inline DATATYPE * FindSorted (const ODT & Key) const
- {
- int Index = IndexOfSorted (Key);
- if (Index != -1)
- return &GetAt (Index);
- return NULL;
- }
- template <class Compare> inline DATATYPE * FindSorted (const DATATYPE & Key, Compare cf) const
- {
- int Index = IndexOfSorted (Key, cf);
- if (Index != -1)
- return &GetAt (Index);
- return NULL;
- }
- template <class ODT, class Compare> inline DATATYPE * FindSorted (const ODT & Key, Compare cf) const
- {
- int Index = IndexOfSorted (Key, cf);
- if (Index != -1)
- return &GetAt (Index);
- return NULL;
- }
- template <class pfnForEach> void ForEach (pfnForEach pf)
- {
- for (int Index=0; Index<Top; Index++)
- {
- DATATYPE & dt = GetAt (Index);
- if (! pf (dt))
- break;
- }
- }
- template <class pfnForEach> void ForEach (pfnForEach pf) const
- {
- for (int Index=0; Index<Top; Index++)
- {
- const DATATYPE & dt = GetAt (Index);
- if (! pf (dt))
- break;
- }
- }
- protected:
- void Sort (int low, int high)
- {
- if (high <= low)
- return;
- if (high - low == 1)
- {
- if (GetAt (high) < GetAt (low))
- Swap (low, high);
- return;
- }
- // 存放中心索引及其值的局部变量及用于扫描的索引
- DATATYPE pivot;
- int scanUp, scanDown;
- int mid;
- mid = (low + high) / 2;
- pivot = GetAt (mid);
- Swap (mid, low);
- scanUp = low + 1; scanDown = high;
- do
- {
- while (scanUp <= scanDown && (*this)[scanUp] <= pivot)
- {
- scanUp ++;
- }
- while (pivot < (*this)[scanDown])
- {
- scanDown --;
- }
- if (scanUp < scanDown)
- {
- Swap (scanUp, scanDown);
- }
- }
- while (scanUp < scanDown);
- (*this)[low] = (*this)[scanDown];
- (*this)[scanDown] = pivot;
- if (low < scanDown - 1)
- Sort (low, scanDown - 1);
- if (scanDown + 1 < high)
- Sort (scanDown + 1, high);
- }
- template <class ODT> void Sort (int low, int high, Array <ODT> & ar);
- template <class ODT> void Sort (int low, int high, FixArray <ODT> & ar);
- // template <class ODT> friend void Array <DATATYPE>::Sort (int low, int high, Array <ODT> & ar);
- // template <class ODT> friend void Array <DATATYPE>::Sort (int low, int high, FixArray <ODT> & ar);
- protected:
- void HeapSort (int n);
- void HeapAdjust (int s, int m);
- };
- template <class DATATYPE> template <class ODT> void FixArray <DATATYPE>::Sort (int low, int high, Array <ODT> & ar)
- {
- if (high - low <= 0)
- return;
- if (high - low == 1)
- {
- if (GetAt (high) < GetAt (low))
- {
- Swap (low, high);
- ar.Swap (low, high);
- }
- return;
- }
- // 存放中心索引及其值的局部变量及用于扫描的索引
- int scanUp, scanDown;
- int mid;
- mid = (low + high) / 2;
- DATATYPE pivot = GetAt (mid);
- ODT __fvalue;
- __fvalue = ar.GetAt (mid);
- Swap (mid, low);
- ar.Swap (mid, low);
- scanUp = low + 1; scanDown = high;
- do
- {
- while (scanUp <= scanDown && (*this)[scanUp] <= pivot)
- {
- scanUp ++;
- }
- while (pivot < (*this)[scanDown])
- {
- scanDown --;
- }
- if (scanUp < scanDown)
- {
- Swap (scanUp, scanDown);
- ar.Swap (scanUp, scanDown);
- }
- }
- while (scanUp < scanDown);
- (*this)[low] = (*this)[scanDown];
- (*this)[scanDown] = pivot;
- {
- ArrayItem < ODT > * fdl;
- fdl = ar.PtrOfItemAt (scanDown);
- ar[low] = fdl->Data;
- fdl->Data = __fvalue;
- }
- if (low < scanDown - 1)
- Sort (low, scanDown - 1, ar);
- if (scanDown + 1 < high)
- Sort (scanDown + 1, high, ar);
- }
- template <class DATATYPE> template <class ODT> void FixArray <DATATYPE>::Sort (int low, int high, FixArray <ODT> & ar)
- {
- if (high - low <= 0)
- return;
- if (high - low == 1)
- {
- if (GetAt (high) < GetAt (low))
- {
- Swap (low, high);
- ar.Swap (low, high);
- }
- return;
- }
- // 存放中心索引及其值的局部变量及用于扫描的索引
- int scanUp, scanDown;
- int mid;
- mid = (low + high) / 2;
- DATATYPE pivot = GetAt (mid);
- ODT __fvalue;
- __fvalue = ar.GetAt (mid);
- Swap (mid, low);
- ar.Swap (mid, low);
- scanUp = low + 1; scanDown = high;
- do
- {
- while (scanUp <= scanDown && (*this)[scanUp] <= pivot)
- {
- scanUp ++;
- }
- while (pivot < (*this)[scanDown])
- {
- scanDown --;
- }
- if (scanUp < scanDown)
- {
- Swap (scanUp, scanDown);
- ar.Swap (scanUp, scanDown);
- }
- }
- while (scanUp < scanDown);
- (*this)[low] = (*this)[scanDown];
- (*this)[scanDown] = pivot;
- {
- ar[low] = ar[scanDown];
- ar[scanDown] = __fvalue;
- }
- if (low < scanDown - 1)
- Sort (low, scanDown - 1, ar);
- if (scanDown + 1 < high)
- Sort (scanDown + 1, high, ar);
- };
- template <class DATATYPE> void FixArray <DATATYPE>::HeapSort (int n)
- {
- for (int i=n/2; i>=0; --i)
- // 把R[1..n]建成大顶堆
- HeapAdjust (i, n);
- for (int i=n; i>=1; --i)
- {
- Swap (0, i);
- // 将堆顶记录和当前未经排序子序列
- // R[1..i]中最后一个记录相互交换
- HeapAdjust (0, i-1);
- // 将R[1..i-1] 重新调整为大顶堆
- }
- }
- template <class DATATYPE> void FixArray <DATATYPE>::HeapAdjust (int s, int m)
- {
- DATATYPE rc = GetAt (s);
- for (int j=2*s; j<=m; j*=2)
- {
- // 沿key较大的孩子结点向下筛选
- if (j < m && GetAt (j) < GetAt (j+1))
- j ++;
- // j为key较大的记录的下标
- if (rc >= GetAt (j))
- break;
- // rc应插入在位置s上
- Swap (s, j); s = j;
- }
- (*this)[s] = rc; // 插入
- }
|