array.tlh 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274
  1. /***************************************************************************
  2. * E-Com Technology Ltd.
  3. *
  4. * ECOMPACS DICOM Network Transport Libraries * Version 0.1 Beta
  5. ***************************************************************************/
  6. //#pragma once
  7. //#ifndef __ARRAY__
  8. //#define __ARRAY__
  9. #pragma warning (disable : 4800)
  10. // avoid bool (LastAccess);
  11. template < class DATATYPE > class ArrayIterator;
  12. template < class DATATYPE > class ArrayReverseIterator;
  13. template < class DATATYPE > class ArrayStepIterator;
  14. template < class DATATYPE > class Array;
  15. template < class DATATYPE > class ArrayOfPtr;
  16. template < class DATATYPE > class FixArray;
  17. template < class DATATYPE > class ExclusiveArray;
  18. template < class DATATYPE > class ExclusiveArrayOfPtr;
  19. /*********************************************************************
  20. *
  21. * Class Array
  22. *
  23. *********************************************************************/
  24. template < class DATATYPE > class ArrayItem
  25. {
  26. public:
  27. DATATYPE Data;
  28. ArrayItem < DATATYPE > *prev, *next;
  29. ArrayItem (ArrayItem < DATATYPE > *p, ArrayItem < DATATYPE > *n)
  30. {
  31. prev = p;
  32. next = n;
  33. };
  34. ArrayItem ()
  35. {
  36. prev = NULL;
  37. next = NULL;
  38. };
  39. };
  40. template < class DATATYPE > class Array
  41. {
  42. public:
  43. typedef DATATYPE _datatype;
  44. protected:
  45. ArrayItem < DATATYPE > *first;
  46. ArrayItem < DATATYPE > *last;
  47. BOOL ClearType;
  48. int ArraySize;
  49. public:
  50. Array (const Array & theArray);
  51. inline Array ()
  52. {
  53. ArraySize = 0;
  54. first = NULL;
  55. last = NULL;
  56. ClearType = 1;
  57. }
  58. ~Array () { Reset ();};
  59. inline void Add (const DATATYPE & Value)
  60. {
  61. InsertLast (Value);
  62. }
  63. inline void InsertFirst (const DATATYPE & Value)
  64. {
  65. ArrayItem < DATATYPE > * dl = first;
  66. first = new ArrayItem < DATATYPE > ;
  67. // chain on new element at head of chain
  68. first -> Data = Value;
  69. first -> next = dl;
  70. if (dl) // array is not empty
  71. dl -> prev = first;
  72. else // array is empty
  73. last = first;
  74. ++ ArraySize;
  75. }
  76. inline void InsertFirst (DATATYPE & Value)
  77. {
  78. ArrayItem < DATATYPE > * dl = first;
  79. first = new ArrayItem < DATATYPE > ;
  80. // chain on new element at head of chain
  81. first -> Data = Value;
  82. first -> next = dl;
  83. if (dl) // array is not empty
  84. dl -> prev = first;
  85. else // array is empty
  86. last = first;
  87. ++ ArraySize;
  88. }
  89. inline void InsertLast (const DATATYPE & Value)
  90. {
  91. ArrayItem < DATATYPE > * dl = last;
  92. last = new ArrayItem < DATATYPE > ;
  93. // chain on new element at tail of chain
  94. last -> Data = Value;
  95. last -> prev = dl;
  96. if (dl)
  97. dl -> next = last;
  98. else
  99. first = last;
  100. ++ ArraySize;
  101. }
  102. inline void InsertLast (DATATYPE & Value)
  103. {
  104. ArrayItem < DATATYPE > * dl = last;
  105. last = new ArrayItem < DATATYPE > ;
  106. // chain on new element at tail of chain
  107. last -> Data = Value;
  108. last -> prev = dl;
  109. if (dl)
  110. dl -> next = last;
  111. else
  112. first = last;
  113. ++ ArraySize;
  114. }
  115. void AddBefore (const DATATYPE &, int Index);
  116. void AddAfter (const DATATYPE &, int Index);
  117. BOOL RemoveAt (int);
  118. inline void Remove (const DATATYPE & dt)
  119. {
  120. for (Array <DATATYPE>::Iterator Iter (*this); Iter; Iter++)
  121. {
  122. const DATATYPE & mydt = Iter ();
  123. if (dt == mydt)
  124. {
  125. RemoveAt (Iter);
  126. return;
  127. }
  128. }
  129. }
  130. inline BOOL RemoveFirst (void)
  131. {
  132. if (! first) return FALSE;
  133. ArrayItem < DATATYPE > *dl = first;
  134. first = dl -> next;
  135. if (first)
  136. first -> prev = NULL;
  137. else
  138. last = NULL;
  139. delete dl;
  140. -- ArraySize;
  141. return TRUE;
  142. }
  143. inline BOOL RemoveLast (void)
  144. {
  145. if (! last) return FALSE;
  146. ArrayItem < DATATYPE > *dl = last;
  147. last = dl -> prev;
  148. if (last)
  149. last -> next = NULL;
  150. else
  151. first = NULL;
  152. delete dl;
  153. -- ArraySize;
  154. return TRUE;
  155. }
  156. inline void RemoveAll (void)
  157. {
  158. if (! ArraySize) return;
  159. if (! ClearType) return;
  160. while (first)
  161. {
  162. ArrayItem < DATATYPE > * pt = first;
  163. first = first -> next;
  164. delete pt;
  165. }
  166. first = last = NULL;
  167. ArraySize = 0;
  168. }
  169. inline const Array <DATATYPE> & operator += (const DATATYPE & Value)
  170. {
  171. InsertLast (Value);
  172. return (*this);
  173. }
  174. inline DATATYPE & GetAt (int Index) const
  175. {
  176. ArrayItem < DATATYPE > * dl = PtrOfItemAt (Index);
  177. if (dl == NULL)
  178. return ((DATATYPE &) *dl);
  179. return (dl -> Data);
  180. }
  181. inline DATATYPE & GetFirst (void) const { return first -> Data; }
  182. inline DATATYPE & GetLast (void) const { return last -> Data; }
  183. inline DATATYPE & GetNextFirst (void) const { return first -> next -> Data; }
  184. inline DATATYPE & GetPrevLast (void) const { return last -> prev -> Data; }
  185. BOOL TransferTo (Array <DATATYPE> & array);
  186. int TransferTo (Array <DATATYPE> & array, DATATYPE & dt);
  187. int TransferTo (Array <DATATYPE> & array, int fromIndex, int NbOfTransfer);
  188. int Append (const Array <DATATYPE> & from);
  189. int CopyTo (FixArray <DATATYPE> & array) const;
  190. int CopyTo (Array <DATATYPE> & array) const;
  191. int CopyTo (DATATYPE * pArray, int NbOfElem) const;
  192. void SetCapacity (int nNewSize);
  193. inline void Reset (void)
  194. {
  195. RemoveAll ();
  196. }
  197. inline int GetSize (void) const
  198. {
  199. return (ArraySize);
  200. }
  201. inline int GetCapacity (void) const
  202. {
  203. return ArraySize;
  204. }
  205. inline BOOL IsEmpty (void) const
  206. {
  207. return (! GetSize ());
  208. }
  209. inline DATATYPE & operator[] (int Index) const
  210. {
  211. return (GetAt (Index));
  212. }
  213. inline void operator = (const Array < DATATYPE > & array)
  214. {
  215. RemoveAll ();
  216. array.CopyTo (*this);
  217. }
  218. inline bool IsExist (const DATATYPE & Value) const
  219. {
  220. return IndexOf (Value) != -1;
  221. }
  222. // find component starting at left, -1 if not found
  223. inline int IndexOf (const DATATYPE & Value) const
  224. {
  225. ArrayIterator < DATATYPE> ArrayIter (*this);
  226. while (ArrayIter)
  227. {
  228. if (ArrayIter () == Value)
  229. return ArrayIter.GetLastIndex ();
  230. }
  231. return -1;
  232. }
  233. // find component starting at right
  234. inline int LastIndexOf (const DATATYPE & Value) const
  235. {
  236. ArrayReverseIterator < DATATYPE> ArrayIter (*this);
  237. while (ArrayIter)
  238. {
  239. if (ArrayIter () == Value)
  240. return ArrayIter.GetLastIndex ();
  241. }
  242. return -1;
  243. }
  244. DATATYPE * Find (const DATATYPE & Key) const
  245. {
  246. for (Iterator Iter (*this); Iter; Iter++)
  247. {
  248. DATATYPE & value = Iter ();
  249. if (value == Key)
  250. return &value;
  251. }
  252. return NULL;
  253. }
  254. template <class ODT> DATATYPE * Find (const ODT & Key) const
  255. {
  256. for (Iterator Iter (*this); Iter; Iter++)
  257. {
  258. DATATYPE & value = Iter ();
  259. if (value == Key)
  260. return &value;
  261. }
  262. return NULL;
  263. }
  264. template <class Compare> DATATYPE * Find (const DATATYPE & Key, Compare cf) const
  265. {
  266. for (Iterator Iter (*this); Iter; Iter++)
  267. {
  268. DATATYPE & value = Iter ();
  269. if (cf (value, Key))
  270. return &value;
  271. }
  272. return NULL;
  273. }
  274. template <class ODT, class Compare> DATATYPE * Find (const ODT & Key, Compare cf) const
  275. {
  276. for (Iterator Iter (*this); Iter; Iter++)
  277. {
  278. DATATYPE & value = Iter ();
  279. if (cf (value, Key))
  280. return &value;
  281. }
  282. return NULL;
  283. }
  284. inline BOOL Replace (int nPos, const DATATYPE & Value)
  285. {
  286. if (nPos < 0)
  287. return FALSE;
  288. if (nPos >= ArraySize)
  289. return FALSE;
  290. AddAfter (Value, nPos);
  291. MoveItemToFirst (nPos);
  292. RemoveFirst ();
  293. return TRUE;
  294. }
  295. // Reverse
  296. void Reverse (void)
  297. {
  298. if (ArraySize <= 1)
  299. return;
  300. ArrayItem < DATATYPE > * pNext = first;
  301. ArrayItem < DATATYPE > * pPrev = last;
  302. for (int Index=0; Index<ArraySize/2; Index++)
  303. {
  304. ArrayItem < DATATYPE > * dln = pNext->next;
  305. ArrayItem < DATATYPE > * dlp = pPrev->prev;
  306. Swap (pNext, pPrev);
  307. pNext = dln;
  308. pPrev = dlp;
  309. }
  310. }
  311. // Exchange the position of nFirst and nLast
  312. inline BOOL Swap (int nFirst, int nLast)
  313. {
  314. if (nFirst < 0)
  315. nFirst = 0;
  316. if (nFirst >= ArraySize)
  317. nFirst = ArraySize -1;
  318. if (nLast < 0)
  319. nLast = 0;
  320. if (nLast >= ArraySize)
  321. nLast = ArraySize -1;
  322. if (nFirst == nLast)
  323. return TRUE;
  324. ArrayItem < DATATYPE > * dlFirst;
  325. ArrayItem < DATATYPE > * dlLast ;
  326. if (nFirst < nLast)
  327. {
  328. dlFirst = PtrOfItemAt (nFirst); // 2
  329. dlLast = PtrOfItemAt (nLast); // 5
  330. }
  331. else
  332. {
  333. dlFirst = PtrOfItemAt (nLast); // 2
  334. dlLast = PtrOfItemAt (nFirst); // 5
  335. }
  336. return Swap (dlFirst, dlLast);
  337. }
  338. // 把B放到A之前
  339. inline BOOL MoveBBeforeA (int nB, int nA)
  340. {
  341. if (nA < 0)
  342. nA = 0;
  343. if (nA >= ArraySize)
  344. nA = ArraySize -1;
  345. if (nB < 0)
  346. nB = 0;
  347. if (nB >= ArraySize)
  348. nB = ArraySize -1;
  349. if (nB == nA) // 把1移到1之前是无效的
  350. return TRUE;
  351. if (nA == nB + 1) // 把1移到2之前是无效的, 1本来就在2之前
  352. return TRUE;
  353. if (nB == nA + 1) // 相邻的情况,其实是交换
  354. return Swap (nA, nB);
  355. ArrayItem < DATATYPE > * dlB;
  356. ArrayItem < DATATYPE > * dlA;
  357. dlA = PtrOfItemAt (nA); // 2
  358. dlB = PtrOfItemAt (nB); // 5
  359. return MoveBBeforeA (dlB, dlA);
  360. }
  361. // 把B放到A之后
  362. inline BOOL MoveBAfterA (int nB, int nA)
  363. {
  364. if (nA < ArraySize-1)
  365. return MoveBBeforeA (nB, nA+1);
  366. return MoveItemToLast (nB);
  367. }
  368. // 把指定的项移动到队首
  369. inline BOOL MoveItemToFirst (int nItem)
  370. {
  371. return MoveBBeforeA (nItem, 0);
  372. }
  373. // 把指定的项移动到队尾
  374. inline BOOL MoveItemToLast (int nItem)
  375. {
  376. if (ArraySize == 0) // 空队列不移动
  377. return true;
  378. if (ArraySize == 1) // 单项队列也不移动
  379. return true;
  380. // 队列至少是2项
  381. if (nItem < 0)
  382. nItem = 0;
  383. if (nItem >= ArraySize-1) // 最后一项本来就在队尾了
  384. // nItem = ArraySize-1;
  385. return true;
  386. ArrayItem < DATATYPE > * pItem = PtrOfItemAt (nItem);
  387. if (pItem == NULL)
  388. return false;
  389. if (pItem == first) // so (nItem == 0)
  390. first = pItem->next;
  391. else
  392. pItem->prev->next = pItem->next;
  393. // 因为nItem不是最后一项,因此pItem->next不可能为NULL
  394. pItem->next->prev = pItem->prev;
  395. pItem->next = NULL;
  396. pItem->prev = last;
  397. last->next = pItem;
  398. last = pItem;
  399. return true;
  400. }
  401. // Quick sort
  402. inline void Sort (void)
  403. {
  404. Sort (0, ArraySize - 1);
  405. }
  406. // Quick sort, associated with ar
  407. template <class ODT> void Sort (Array <ODT> & ar)
  408. {
  409. return Sort (0, ArraySize - 1, ar);
  410. }
  411. template <class ODT> void Sort (FixArray <ODT> & ar)
  412. {
  413. return Sort (0, ArraySize - 1, ar);
  414. }
  415. // Quick sort, using the given compare function
  416. template <class Compare> void Sort (Compare cf)
  417. {
  418. return Sort (0, ArraySize - 1, cf);
  419. }
  420. template <class ODT, class Compare> void Sort (Array <ODT> & ar, Compare cf)
  421. {
  422. return Sort (0, ArraySize - 1, ar, cf);
  423. }
  424. template <class ODT, class Compare> void Sort (FixArray <ODT> & ar, Compare cf)
  425. {
  426. return Sort (0, ArraySize - 1, ar, cf);
  427. }
  428. // 带参数的比较
  429. template <class Compare, typename arg> void Sort (Compare cf, arg a)
  430. {
  431. return Sort (0, ArraySize - 1, cf, a);
  432. }
  433. template <class ODT, class Compare, typename arg> void Sort (Array <ODT> & ar, Compare cf, arg a)
  434. {
  435. return Sort (0, ArraySize - 1, ar, cf, a);
  436. }
  437. template <class ODT, class Compare, typename arg> void Sort (FixArray <ODT> & ar, Compare cf, arg a)
  438. {
  439. return Sort (0, ArraySize - 1, ar, cf, a);
  440. }
  441. protected:
  442. void Sort (int low, int high);
  443. template <class ODT> void Sort (int low, int high, Array <ODT> & ar);
  444. template <class ODT> void Sort (int low, int high, FixArray <ODT> & ar);
  445. template <class Compare> void Sort (int low, int high, Compare cf);
  446. template <class ODT, class Compare> void Sort (int low, int high, Array <ODT> & ar, Compare cf);
  447. template <class ODT, class Compare> void Sort (int low, int high, FixArray <ODT> & ar, Compare cf);
  448. template <class Compare, typename arg> void Sort (int low, int high, Compare cf, arg a);
  449. template <class ODT, class Compare, typename arg> void Sort (int low, int high, Array <ODT> & ar, Compare cf, arg a);
  450. template <class ODT, class Compare, typename arg> void Sort (int low, int high, FixArray <ODT> & ar, Compare cf, arg a);
  451. public:
  452. inline void HeapSort ()
  453. {
  454. HeapSort (ArraySize - 1);
  455. }
  456. protected:
  457. void HeapSort (int n);
  458. void HeapAdjust (int s, int m);
  459. protected:
  460. friend class ArrayIterator < DATATYPE > ;
  461. friend class ArrayReverseIterator < DATATYPE > ;
  462. friend class ArrayStepIterator < DATATYPE > ;
  463. public:
  464. bool IsSorted () const;
  465. template <class Compare> bool IsSorted (Compare cf) const;
  466. public:
  467. DATATYPE & FindMin (void) const;
  468. DATATYPE & FindMax (void) const;
  469. template <class Compare> DATATYPE & FindMin (Compare cf) const;
  470. template <class Compare> DATATYPE & FindMax (Compare cf) const;
  471. public:
  472. int IndexOfMin (void) const;
  473. int IndexOfMax (void) const;
  474. template <class Compare> int IndexOfMin (Compare cf) const;
  475. template <class Compare> int IndexOfMax (Compare cf) const;
  476. public:
  477. inline ArrayItem < DATATYPE > * PtrOfItemAt (int nItem) const
  478. {
  479. ArrayItem < DATATYPE > * dl;
  480. if (nItem >= ArraySize)
  481. {
  482. return NULL;
  483. }
  484. // scan the chain to get the element (from head or from tail)
  485. if (nItem < ArraySize / 2)
  486. { // scan from the head of chain
  487. dl = first;
  488. ++nItem;
  489. while (--nItem > 0) dl = dl -> next;
  490. }
  491. else
  492. { // scan from the tail of chain
  493. dl = last;
  494. nItem = (ArraySize - nItem);
  495. while (--nItem > 0) dl = dl -> prev;
  496. }
  497. return dl;
  498. }
  499. inline ArrayItem < DATATYPE > * PtrOfItem (const DATATYPE & dt) const
  500. {
  501. for (Array <DATATYPE>::Iterator Iter (*this); Iter; Iter++)
  502. {
  503. const DATATYPE & mydt = Iter ();
  504. if (dt == mydt)
  505. {
  506. ArrayItem < DATATYPE > * dl = Iter.m_Next;
  507. return dl;
  508. }
  509. }
  510. return NULL;
  511. }
  512. protected:
  513. inline BOOL Swap (ArrayItem < DATATYPE > * dlFirst, ArrayItem < DATATYPE > * dlLast)
  514. {
  515. if ( (dlFirst == NULL) || (dlLast == NULL) )
  516. return FALSE;
  517. ArrayItem < DATATYPE > * PF = dlFirst->prev; // 1
  518. ArrayItem < DATATYPE > * FN = dlFirst->next; // 3
  519. ArrayItem < DATATYPE > * PL = dlLast->prev; // 4
  520. ArrayItem < DATATYPE > * LN = dlLast->next; // 6
  521. bool bNeighbor = (dlFirst->next == dlLast);
  522. dlFirst->next = LN;
  523. if (LN)
  524. LN->prev = dlFirst;
  525. else
  526. last = dlFirst;
  527. if (bNeighbor)
  528. {
  529. dlLast->next = dlFirst;
  530. dlFirst->prev = dlLast;
  531. }
  532. else
  533. {
  534. dlLast->next = FN;
  535. if (FN)
  536. FN->prev = dlLast;
  537. if (PL)
  538. PL->next = dlFirst;
  539. dlFirst->prev = PL;
  540. }
  541. if (PF)
  542. PF->next = dlLast;
  543. else
  544. first = dlLast;
  545. dlLast->prev = PF;
  546. return TRUE;
  547. }
  548. // 把B移动到A之前
  549. inline BOOL MoveBBeforeA (ArrayItem < DATATYPE > * B, ArrayItem < DATATYPE > * A)
  550. {
  551. if ( (A == NULL) || (B == NULL) )
  552. return FALSE;
  553. ArrayItem < DATATYPE > * AP = A->prev; // 2
  554. ArrayItem < DATATYPE > * AN = A->next; // 4
  555. ArrayItem < DATATYPE > * BP = B->prev; // 6
  556. ArrayItem < DATATYPE > * BN = B->next; // 8
  557. bool bNeighbor = (AN == B);
  558. A->prev = B;
  559. if (AP)
  560. {
  561. B->next = AP->next;
  562. AP->next= B;
  563. }
  564. else
  565. {
  566. B->next = first;
  567. first = B;
  568. }
  569. B->prev = AP;
  570. if (bNeighbor)
  571. {
  572. }
  573. else
  574. {
  575. if (AN)
  576. AN->prev= A;
  577. }
  578. if (BP)
  579. BP->next = BN;
  580. else
  581. first = BN;
  582. if (BN)
  583. BN->prev = BP;
  584. else
  585. last = BP;
  586. return TRUE;
  587. }
  588. public:
  589. /*********************************************************************
  590. * Iterator Class
  591. *********************************************************************/
  592. class Iterator
  593. {
  594. protected:
  595. const Array < DATATYPE > * m_Array;
  596. int m_ArraySize;
  597. int m_Index;
  598. ArrayItem < DATATYPE > * m_Next;
  599. mutable DATATYPE * m_Item;
  600. public:
  601. Iterator (const Array < DATATYPE > * theArray)
  602. {
  603. Attach (theArray);
  604. }
  605. Iterator (const Array < DATATYPE > & theArray)
  606. {
  607. Attach (& theArray);
  608. }
  609. Iterator (const Iterator & Iter)
  610. {
  611. Assign (Iter);
  612. }
  613. inline Iterator & Assign (const Iterator & Iter)
  614. {
  615. m_Array = Iter.m_Array;
  616. m_ArraySize = Iter.m_ArraySize;
  617. m_Index = Iter.m_Index;
  618. m_Next = Iter.m_Next;
  619. m_Item = Iter.m_Item;
  620. return (*this);
  621. }
  622. inline Iterator & operator = (const Iterator & Iter)
  623. {
  624. return Assign (Iter);
  625. }
  626. inline void Attach (const Array < DATATYPE > * theArray)
  627. {
  628. if (theArray)
  629. m_Array = theArray;
  630. else
  631. m_Array = NULL;
  632. Restart ();
  633. }
  634. inline void Restart ()
  635. {
  636. m_Index = 0;
  637. if (m_Array)
  638. {
  639. m_ArraySize = m_Array -> GetSize ();
  640. m_Next = m_Array->first;
  641. }
  642. else
  643. {
  644. m_ArraySize = 0;
  645. m_Next = NULL;
  646. }
  647. m_Item = NULL;
  648. }
  649. inline void GoToFirst (void)
  650. {
  651. Restart ();
  652. }
  653. inline void GoToLast (void)
  654. {
  655. if (m_Array)
  656. {
  657. m_ArraySize = m_Array -> GetSize ();
  658. m_Next = m_Array->last;
  659. }
  660. else
  661. {
  662. m_ArraySize = 0;
  663. m_Next = NULL;
  664. }
  665. m_Index = m_ArraySize - 1;
  666. m_Item = NULL;
  667. }
  668. inline operator bool () const
  669. {
  670. return bool (m_Next);
  671. }
  672. inline DATATYPE & operator () (void)
  673. {
  674. return Current ();
  675. }
  676. inline DATATYPE & operator * (void)
  677. {
  678. return Current ();
  679. }
  680. inline DATATYPE & Current (void)
  681. {
  682. if (m_Next)
  683. {
  684. m_Item = &(m_Next -> Data);
  685. return *m_Item;
  686. }
  687. // _fatal ("Warning! will return null");
  688. m_Item = NULL;
  689. return ((DATATYPE &) *m_Item);
  690. }
  691. inline const DATATYPE & Current (void) const
  692. {
  693. if (m_Next)
  694. {
  695. m_Item = &(m_Next -> Data);
  696. return *m_Item;
  697. }
  698. // _fatal ("Warning! will return null");
  699. m_Item = NULL;
  700. return ((const DATATYPE &) *m_Item);
  701. }
  702. inline int Index (void) const
  703. {
  704. return m_Index;
  705. }
  706. virtual inline void Next (void)
  707. {
  708. if (m_Next)
  709. {
  710. m_Next = m_Next -> next;
  711. m_Index ++;
  712. }
  713. }
  714. inline int Remainder () const
  715. {
  716. return m_ArraySize - m_Index;
  717. }
  718. inline void operator ++ () // ++Iter; prevfix
  719. {
  720. Next ();
  721. }
  722. inline void operator ++ (int) // Iter ++, postfix
  723. {
  724. Next ();
  725. }
  726. virtual inline void Prev (void)
  727. {
  728. if (m_Next)
  729. {
  730. m_Next = m_Next -> prev;
  731. m_Index --;
  732. }
  733. }
  734. inline void operator -- () // --Iter; prevfix
  735. {
  736. Prev ();
  737. }
  738. inline void operator -- (int) // Iter++, postfix
  739. {
  740. Prev ();
  741. }
  742. virtual inline void Step (int NumberOfStep)
  743. {
  744. if (NumberOfStep > 0)
  745. {
  746. for (int nStep=0; nStep<NumberOfStep && m_Next; nStep++)
  747. {
  748. m_Next = m_Next -> next;
  749. m_Index ++;
  750. }
  751. }
  752. else
  753. {
  754. for (int nStep=0; nStep<-NumberOfStep && m_Next; nStep++)
  755. {
  756. m_Next = m_Next -> prev;
  757. m_Index --;
  758. }
  759. }
  760. }
  761. inline Iterator & operator += (int NumberOfStep)
  762. {
  763. Step (NumberOfStep);
  764. return (*this);
  765. }
  766. inline Iterator & operator -= (int NumberOfStep)
  767. {
  768. Step (-NumberOfStep);
  769. return (*this);
  770. }
  771. inline bool Find (const DATATYPE & Value)
  772. {
  773. while (*this)
  774. {
  775. if (Current () == Value)
  776. return true;
  777. Next ();
  778. }
  779. return false;
  780. }
  781. inline bool ReverseFind (const DATATYPE & Value)
  782. {
  783. while (*this)
  784. {
  785. if (Current () == Value)
  786. return true;
  787. Prev ();
  788. }
  789. return false;
  790. }
  791. inline bool operator < (const Iterator & Iter) const
  792. {
  793. return m_Index < Iter.m_Index;
  794. }
  795. inline bool operator > (const Iterator & Iter) const
  796. {
  797. return m_Index > Iter.m_Index;
  798. }
  799. inline bool operator <= (const Iterator & Iter) const
  800. {
  801. return m_Index <= Iter.m_Index;
  802. }
  803. inline bool operator >= (const Iterator & Iter) const
  804. {
  805. return m_Index >= Iter.m_Index;
  806. }
  807. inline bool operator == (const Iterator & Iter) const
  808. {
  809. return m_Index == Iter.m_Index;
  810. }
  811. inline bool operator < (int i) const
  812. {
  813. return m_Index < i;
  814. }
  815. inline bool operator > (int i) const
  816. {
  817. return m_Index > i;
  818. }
  819. inline bool operator <= (int i) const
  820. {
  821. return m_Index <= i;
  822. }
  823. inline bool operator >= (int i) const
  824. {
  825. return m_Index >= i;
  826. }
  827. inline bool operator == (int i) const
  828. {
  829. return m_Index == i;
  830. }
  831. inline int GetArraySize (void) const
  832. {
  833. return (m_ArraySize);
  834. }
  835. inline const Array <DATATYPE> * __GetArray () const
  836. {
  837. return m_Array;
  838. }
  839. inline const ArrayItem < DATATYPE > * __GetCurrent () const
  840. {
  841. return m_Next;
  842. }
  843. inline ArrayItem < DATATYPE > * __GetCurrent ()
  844. {
  845. return m_Next;
  846. }
  847. inline void __SetArraySize (int size)
  848. {
  849. m_ArraySize = size;
  850. }
  851. };
  852. /*********************************************************************
  853. * RangeIterator Class
  854. *********************************************************************/
  855. class RangeIterator : public Iterator
  856. {
  857. private:
  858. int m_MinIndex;
  859. int m_MaxIndex;
  860. public:
  861. RangeIterator (const Array < DATATYPE > * theArray) : Iterator (theArray)
  862. {
  863. m_MinIndex = 0;
  864. m_MaxIndex = m_ArraySize-1;
  865. }
  866. RangeIterator (const Array < DATATYPE > & theArray) : Iterator (theArray)
  867. {
  868. m_MinIndex = 0;
  869. m_MaxIndex = m_ArraySize-1;
  870. }
  871. RangeIterator (const Iterator & Iter) : Iterator (Iter)
  872. {
  873. m_MinIndex = 0;
  874. m_MaxIndex = m_ArraySize-1;
  875. }
  876. inline void SetRange (int minIndex, int maxIndex)
  877. {
  878. m_MinIndex = minIndex;
  879. m_MaxIndex = maxIndex;
  880. if (m_MinIndex < 0)
  881. m_MinIndex = 0;
  882. if (m_MinIndex >= m_ArraySize)
  883. m_MinIndex = m_ArraySize-1;
  884. if (m_MaxIndex < 0)
  885. m_MaxIndex = 0;
  886. if (m_MaxIndex >= m_ArraySize)
  887. m_MaxIndex = m_ArraySize-1;
  888. if (m_MinIndex > m_MaxIndex)
  889. m_MinIndex = m_MaxIndex;
  890. Restart ();
  891. }
  892. inline void Restart ()
  893. {
  894. m_Index = 0;
  895. if (m_Array)
  896. {
  897. m_ArraySize = m_Array -> GetSize ();
  898. m_Next = m_Array->first;
  899. }
  900. else
  901. {
  902. m_ArraySize = 0;
  903. m_Next = NULL;
  904. }
  905. m_Item = NULL;
  906. if (m_MinIndex)
  907. (*this) += m_MinIndex;
  908. }
  909. void GotoIndex (int Index)
  910. {
  911. m_Index = 0;
  912. if (m_Array)
  913. {
  914. m_ArraySize = m_Array -> GetSize ();
  915. m_Next = m_Array->first;
  916. }
  917. else
  918. {
  919. m_ArraySize = 0;
  920. m_Next = NULL;
  921. }
  922. m_Item = NULL;
  923. if (Index < m_MinIndex)
  924. Index = m_MinIndex;
  925. if (Index > m_MaxIndex)
  926. Index = m_MaxIndex;
  927. (*this) += Index;
  928. }
  929. inline void Next (void)
  930. {
  931. if (m_Next)
  932. {
  933. if (m_Index >= m_MaxIndex)
  934. {
  935. m_Next = NULL;
  936. }
  937. else
  938. {
  939. m_Next = m_Next -> next;
  940. m_Index ++;
  941. }
  942. }
  943. }
  944. virtual inline void Prev (void)
  945. {
  946. if (m_Next)
  947. {
  948. if (m_Index < m_MinIndex)
  949. {
  950. m_Next = NULL;
  951. }
  952. else
  953. {
  954. m_Next = m_Next -> prev;
  955. m_Index --;
  956. }
  957. }
  958. }
  959. inline RangeIterator & operator += (int NumberOfStep)
  960. {
  961. if (NumberOfStep > 0)
  962. {
  963. for (int nStep=0; nStep<NumberOfStep && m_Next; nStep++)
  964. {
  965. m_Next = m_Next -> next;
  966. m_Index ++;
  967. }
  968. }
  969. else
  970. {
  971. for (int nStep=0; nStep<-NumberOfStep && m_Next; nStep++)
  972. {
  973. m_Next = m_Next -> prev;
  974. m_Index --;
  975. }
  976. }
  977. return (*this);
  978. }
  979. inline RangeIterator & operator -= (int NumberOfStep)
  980. {
  981. return this->operator += (-NumberOfStep);
  982. }
  983. };
  984. /*********************************************************************
  985. * ReverseIterator Class
  986. *********************************************************************/
  987. class ReverseIterator
  988. {
  989. protected:
  990. const Array < DATATYPE > * m_Array;
  991. int m_ArraySize;
  992. int m_Index;
  993. ArrayItem < DATATYPE > * m_Next;
  994. mutable DATATYPE * m_Item;
  995. public:
  996. ReverseIterator (const Array < DATATYPE > * theArray)
  997. {
  998. Attach (theArray);
  999. }
  1000. ReverseIterator (const Array < DATATYPE > & theArray)
  1001. {
  1002. Attach (&theArray);
  1003. }
  1004. ReverseIterator (const ReverseIterator & Iter)
  1005. {
  1006. Assign (Iter);
  1007. }
  1008. inline ReverseIterator & Assign (const ReverseIterator & Iter)
  1009. {
  1010. m_Array = Iter.m_Array;
  1011. m_ArraySize = Iter.m_ArraySize;
  1012. m_Index = Iter.m_Index;
  1013. m_Next = Iter.m_Next;
  1014. m_Item = Iter.m_Item;
  1015. return (*this);
  1016. }
  1017. inline ReverseIterator & operator = (const ReverseIterator & Iter)
  1018. {
  1019. return Assign (Iter);
  1020. }
  1021. inline void Attach (const Array < DATATYPE > * theArray)
  1022. {
  1023. if (theArray)
  1024. m_Array = theArray;
  1025. else
  1026. m_Array = NULL;
  1027. Restart ();
  1028. }
  1029. inline void Restart ()
  1030. {
  1031. if (m_Array)
  1032. {
  1033. m_ArraySize = m_Array -> GetSize ();
  1034. m_Next = m_Array->last;
  1035. }
  1036. else
  1037. {
  1038. m_ArraySize = 0;
  1039. m_Next = NULL;
  1040. }
  1041. m_Index = m_ArraySize - 1;
  1042. m_Item = NULL;
  1043. }
  1044. inline operator bool () const
  1045. {
  1046. return bool (m_Next);
  1047. }
  1048. inline DATATYPE & operator () (void)
  1049. {
  1050. return Current ();
  1051. }
  1052. inline const DATATYPE & operator () (void) const
  1053. {
  1054. return Current ();
  1055. }
  1056. inline DATATYPE & operator * (void)
  1057. {
  1058. return Current ();
  1059. }
  1060. inline DATATYPE & Current (void)
  1061. {
  1062. if (m_Next)
  1063. {
  1064. m_Item = &(m_Next -> Data);
  1065. return *m_Item;
  1066. }
  1067. // _fatal ("Warning! will return null");
  1068. m_Item = NULL;
  1069. return ((DATATYPE &) *m_Item);
  1070. }
  1071. inline const DATATYPE & Current (void) const
  1072. {
  1073. if (m_Next)
  1074. {
  1075. m_Item = &(m_Next -> Data);
  1076. return *m_Item;
  1077. }
  1078. // _fatal ("Warning! will return null");
  1079. m_Item = NULL;
  1080. return ((const DATATYPE &) *m_Item);
  1081. }
  1082. inline int Index (void) const
  1083. {
  1084. return m_Index;
  1085. }
  1086. inline int Remainder () const
  1087. {
  1088. return m_Index;
  1089. }
  1090. inline DATATYPE & operator ++ () // ++Iter; prevfix
  1091. {
  1092. if (m_Next)
  1093. {
  1094. m_Next = m_Next -> prev;
  1095. m_Index --;
  1096. }
  1097. return Current ();
  1098. }
  1099. inline DATATYPE & operator ++ (int) // Iter ++, postfix
  1100. {
  1101. DATATYPE & dt = Current ();
  1102. if (m_Next)
  1103. {
  1104. m_Next = m_Next -> prev;
  1105. m_Index --;
  1106. }
  1107. return dt;
  1108. }
  1109. inline DATATYPE & operator -- () // --Iter; prevfix
  1110. {
  1111. if (m_Next)
  1112. {
  1113. m_Next = m_Next -> next;
  1114. m_Index ++;
  1115. }
  1116. return Current ();
  1117. }
  1118. inline DATATYPE & operator -- (int) // Iter++, postfix
  1119. {
  1120. DATATYPE & dt = Current ();
  1121. if (m_Next)
  1122. {
  1123. m_Next = m_Next -> next;
  1124. m_Index ++;
  1125. }
  1126. return dt;
  1127. }
  1128. inline ReverseIterator & operator += (int NumberOfStep)
  1129. {
  1130. if (NumberOfStep > 0)
  1131. {
  1132. for (int nStep=0; nStep<NumberOfStep && m_Next; nStep++)
  1133. {
  1134. m_Next = m_Next -> prev;
  1135. m_Index --;
  1136. }
  1137. }
  1138. else
  1139. {
  1140. for (int nStep=0; nStep<-NumberOfStep && m_Next; nStep++)
  1141. {
  1142. m_Next = m_Next -> next;
  1143. m_Index ++;
  1144. }
  1145. }
  1146. return (*this);
  1147. }
  1148. inline ReverseIterator & operator -= (int NumberOfStep)
  1149. {
  1150. return this->operator += (-NumberOfStep);
  1151. }
  1152. inline bool operator < (const ReverseIterator & Iter) const
  1153. {
  1154. return m_Index < Iter.m_Index;
  1155. }
  1156. inline bool operator > (const ReverseIterator & Iter) const
  1157. {
  1158. return m_Index > Iter.m_Index;
  1159. }
  1160. inline bool operator <= (const ReverseIterator & Iter) const
  1161. {
  1162. return m_Index <= Iter.m_Index;
  1163. }
  1164. inline bool operator >= (const ReverseIterator & Iter) const
  1165. {
  1166. return m_Index >= Iter.m_Index;
  1167. }
  1168. inline bool operator == (const ReverseIterator & Iter) const
  1169. {
  1170. return m_Index == Iter.m_Index;
  1171. }
  1172. inline bool operator < (int i) const
  1173. {
  1174. return m_Index < i;
  1175. }
  1176. inline bool operator > (int i) const
  1177. {
  1178. return m_Index > i;
  1179. }
  1180. inline bool operator <= (int i) const
  1181. {
  1182. return m_Index <= i;
  1183. }
  1184. inline bool operator >= (int i) const
  1185. {
  1186. return m_Index >= i;
  1187. }
  1188. inline bool operator == (int i) const
  1189. {
  1190. return m_Index == i;
  1191. }
  1192. inline int GetArraySize (void) const
  1193. {
  1194. return (m_ArraySize);
  1195. }
  1196. inline const Array <DATATYPE> * __GetArray () const
  1197. {
  1198. return m_Array;
  1199. }
  1200. inline const ArrayItem < DATATYPE > * __GetCurrent () const
  1201. {
  1202. return m_Next;
  1203. }
  1204. inline ArrayItem < DATATYPE > * __GetCurrent ()
  1205. {
  1206. return m_Next;
  1207. }
  1208. inline void __SetArraySize (int size)
  1209. {
  1210. m_ArraySize = size;
  1211. }
  1212. };
  1213. /*********************************************************************
  1214. * RangeReverseIterator Class
  1215. *********************************************************************/
  1216. class RangeReverseIterator : public ReverseIterator
  1217. {
  1218. private:
  1219. int m_MinIndex;
  1220. int m_MaxIndex;
  1221. public:
  1222. RangeReverseIterator (const Array < DATATYPE > * theArray) : ReverseIterator (theArray)
  1223. {
  1224. m_MinIndex = 0;
  1225. m_MaxIndex = m_ArraySize-1;
  1226. }
  1227. RangeReverseIterator (const Array < DATATYPE > & theArray) : ReverseIterator (theArray)
  1228. {
  1229. m_MinIndex = 0;
  1230. m_MaxIndex = m_ArraySize-1;
  1231. }
  1232. RangeReverseIterator (const ReverseIterator & Iter) : ReverseIterator (Iter)
  1233. {
  1234. m_MinIndex = 0;
  1235. m_MaxIndex = m_ArraySize-1;
  1236. }
  1237. inline void SetRange (int minIndex, int maxIndex)
  1238. {
  1239. m_MinIndex = minIndex;
  1240. m_MaxIndex = maxIndex;
  1241. if (m_MinIndex < 0)
  1242. m_MinIndex = 0;
  1243. if (m_MinIndex >= m_ArraySize)
  1244. m_MinIndex = m_ArraySize-1;
  1245. if (m_MaxIndex < 0)
  1246. m_MaxIndex = 0;
  1247. if (m_MaxIndex >= m_ArraySize)
  1248. m_MaxIndex = m_ArraySize-1;
  1249. if (m_MinIndex > m_MaxIndex)
  1250. m_MinIndex = m_MaxIndex;
  1251. Restart ();
  1252. }
  1253. inline void Restart ()
  1254. {
  1255. if (m_Array)
  1256. {
  1257. m_ArraySize = m_Array -> GetSize ();
  1258. m_Next = m_Array->last;
  1259. }
  1260. else
  1261. {
  1262. m_ArraySize = 0;
  1263. m_Next = NULL;
  1264. }
  1265. m_Index = m_ArraySize - 1;
  1266. m_Item = NULL;
  1267. if (m_MaxIndex)
  1268. (*this) += m_ArraySize - m_MaxIndex - 1;
  1269. }
  1270. void GotoIndex (int Index)
  1271. {
  1272. if (m_Array)
  1273. {
  1274. m_ArraySize = m_Array -> GetSize ();
  1275. m_Next = m_Array->last;
  1276. }
  1277. else
  1278. {
  1279. m_ArraySize = 0;
  1280. m_Next = NULL;
  1281. }
  1282. m_Index = m_ArraySize - 1;
  1283. m_Item = NULL;
  1284. if (Index < m_MinIndex)
  1285. Index = m_MinIndex;
  1286. if (Index > m_MaxIndex)
  1287. Index = m_MaxIndex;
  1288. (*this) += m_ArraySize - Index - 1;
  1289. }
  1290. inline DATATYPE & operator ++ () // ++Iter; prevfix
  1291. {
  1292. if (m_Next)
  1293. {
  1294. if (m_Index <= m_MinIndex)
  1295. {
  1296. m_Next = NULL;
  1297. }
  1298. else
  1299. {
  1300. m_Next = m_Next -> prev;
  1301. m_Index --;
  1302. }
  1303. }
  1304. return Current ();
  1305. }
  1306. inline DATATYPE & operator ++ (int) // Iter ++, postfix
  1307. {
  1308. DATATYPE & dt = Current ();
  1309. if (m_Next)
  1310. {
  1311. if (m_Index <= m_MinIndex)
  1312. {
  1313. m_Next = NULL;
  1314. }
  1315. else
  1316. {
  1317. m_Next = m_Next -> prev;
  1318. m_Index --;
  1319. }
  1320. }
  1321. return dt;
  1322. }
  1323. inline DATATYPE & operator -- () // --Iter; prevfix
  1324. {
  1325. if (m_Next)
  1326. {
  1327. if (m_Index >= m_MaxIndex)
  1328. {
  1329. m_Next = NULL;
  1330. }
  1331. else
  1332. {
  1333. m_Next = m_Next -> next;
  1334. m_Index ++;
  1335. }
  1336. }
  1337. return Current ();
  1338. }
  1339. inline DATATYPE & operator -- (int) // Iter++, postfix
  1340. {
  1341. DATATYPE & dt = Current ();
  1342. if (m_Next)
  1343. {
  1344. if (m_Index >= m_MaxIndex)
  1345. {
  1346. m_Next = NULL;
  1347. }
  1348. else
  1349. {
  1350. m_Next = m_Next -> next;
  1351. m_Index ++;
  1352. }
  1353. }
  1354. return dt;
  1355. }
  1356. inline RangeReverseIterator & operator += (int NumberOfStep)
  1357. {
  1358. if (NumberOfStep > 0)
  1359. {
  1360. for (int nStep=0; nStep<NumberOfStep && m_Next; nStep++)
  1361. {
  1362. m_Next = m_Next -> prev;
  1363. m_Index --;
  1364. }
  1365. }
  1366. else
  1367. {
  1368. for (int nStep=0; nStep<-NumberOfStep && m_Next; nStep++)
  1369. {
  1370. m_Next = m_Next -> next;
  1371. m_Index ++;
  1372. }
  1373. }
  1374. return (*this);
  1375. }
  1376. inline RangeReverseIterator & operator -= (int NumberOfStep)
  1377. {
  1378. return this->operator += (-NumberOfStep);
  1379. }
  1380. };
  1381. public:
  1382. void AddBefore (const DATATYPE &, Iterator & Iter);
  1383. BOOL RemoveAt (Iterator & Iter);
  1384. void AddBefore (const DATATYPE &, ReverseIterator & Iter);
  1385. BOOL RemoveAt (ReverseIterator & Iter);
  1386. public:
  1387. // 不带参数
  1388. template <class pfnForEach>
  1389. int ForEach (pfnForEach pf)
  1390. {
  1391. int NbOfCalled = 0;
  1392. for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++)
  1393. {
  1394. DATATYPE & dt = Iter ();
  1395. if (! pf (dt))
  1396. break;
  1397. }
  1398. return NbOfCalled;
  1399. }
  1400. template <class pfnForEach>
  1401. int ForEach (pfnForEach pf) const
  1402. {
  1403. int NbOfCalled = 0;
  1404. for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++)
  1405. {
  1406. const DATATYPE & dt = Iter ();
  1407. if (! pf (dt))
  1408. break;
  1409. }
  1410. return NbOfCalled;
  1411. }
  1412. // 带 1 个参数
  1413. template <class pfnForEach, typename arg>
  1414. int ForEach (pfnForEach pf, arg e)
  1415. {
  1416. int NbOfCalled = 0;
  1417. for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++)
  1418. {
  1419. DATATYPE & dt = Iter ();
  1420. if (! pf (dt, e))
  1421. break;
  1422. }
  1423. return NbOfCalled;
  1424. }
  1425. template <class pfnForEach, typename arg>
  1426. int ForEach (pfnForEach pf, arg e) const
  1427. {
  1428. int NbOfCalled = 0;
  1429. for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++)
  1430. {
  1431. const DATATYPE & dt = Iter ();
  1432. if (! pf (dt, e))
  1433. break;
  1434. }
  1435. return NbOfCalled;
  1436. }
  1437. // 带 2 个参数
  1438. template <class pfnForEach, typename T1, typename T2>
  1439. int ForEach (pfnForEach pf, T1 t1, T2 t2)
  1440. {
  1441. int NbOfCalled = 0;
  1442. for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++)
  1443. {
  1444. DATATYPE & dt = Iter ();
  1445. if (! pf (dt, t1, t2))
  1446. break;
  1447. }
  1448. return NbOfCalled;
  1449. }
  1450. template <class pfnForEach, typename T1, typename T2>
  1451. int ForEach (pfnForEach pf, T1 t1, T2 t2) const
  1452. {
  1453. int NbOfCalled = 0;
  1454. for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++)
  1455. {
  1456. const DATATYPE & dt = Iter ();
  1457. if (! pf (dt, t1, t2))
  1458. break;
  1459. }
  1460. return NbOfCalled;
  1461. }
  1462. // 带 3 个参数
  1463. template <class pfnForEach, typename T1, typename T2, typename T3>
  1464. int ForEach (pfnForEach pf, T1 t1, T2 t2, T3 t3)
  1465. {
  1466. int NbOfCalled = 0;
  1467. for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++)
  1468. {
  1469. DATATYPE & dt = Iter ();
  1470. if (! pf (dt, t1, t2, t3))
  1471. break;
  1472. }
  1473. return NbOfCalled;
  1474. }
  1475. template <class pfnForEach, typename T1, typename T2, typename T3>
  1476. int ForEach (pfnForEach pf, T1 t1, T2 t2, T3 t3) const
  1477. {
  1478. int NbOfCalled = 0;
  1479. for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++)
  1480. {
  1481. const DATATYPE & dt = Iter ();
  1482. if (! pf (dt, t1, t2, t3))
  1483. break;
  1484. }
  1485. return NbOfCalled;
  1486. }
  1487. // 带 4 个参数
  1488. template <class pfnForEach, typename T1, typename T2, typename T3, typename T4>
  1489. int ForEach (pfnForEach pf, T1 t1, T2 t2, T3 t3, T4 t4)
  1490. {
  1491. int NbOfCalled = 0;
  1492. for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++)
  1493. {
  1494. DATATYPE & dt = Iter ();
  1495. if (! pf (dt, t1, t2, t3, t4))
  1496. break;
  1497. }
  1498. return NbOfCalled;
  1499. }
  1500. template <class pfnForEach, typename T1, typename T2, typename T3, typename T4>
  1501. int ForEach (pfnForEach pf, T1 t1, T2 t2, T3 t3, T4 t4) const
  1502. {
  1503. int NbOfCalled = 0;
  1504. for (Iterator Iter (*this); Iter; Iter++, NbOfCalled ++)
  1505. {
  1506. const DATATYPE & dt = Iter ();
  1507. if (! pf (dt, t1, t2, t3, t4))
  1508. break;
  1509. }
  1510. return NbOfCalled;
  1511. }
  1512. protected:
  1513. template <class IterType> void AddBefore (const DATATYPE &, IterType & Iter);
  1514. template <class IterType> BOOL RemoveAt (IterType & Iter);
  1515. };
  1516. /*********************************************************************
  1517. * ArrayOfPtr Unit Class
  1518. *
  1519. * usage: ArrayOfPtr < ClassType > VarName;
  1520. *
  1521. *********************************************************************/
  1522. template < class DATATYPE > class ArrayOfPtr : public Array < DATATYPE >
  1523. {
  1524. public:
  1525. inline BOOL RemoveFirst (void)
  1526. {
  1527. ArrayItem < DATATYPE > * dl = first;
  1528. if (dl)
  1529. delete dl->Data;
  1530. return Array <DATATYPE>::RemoveFirst ();
  1531. }
  1532. inline BOOL RemoveLast (void)
  1533. {
  1534. ArrayItem < DATATYPE > * dl = last;
  1535. if (dl)
  1536. delete dl->Data;
  1537. return Array <DATATYPE>::RemoveLast ();
  1538. }
  1539. inline BOOL RemoveAt (int Index)
  1540. {
  1541. ArrayItem <DATATYPE> * dl = PtrOfItemAt (Index);
  1542. if (dl)
  1543. delete dl->Data;
  1544. return Array <DATATYPE>::RemoveAt (Index);
  1545. }
  1546. inline void RemoveAll (void)
  1547. {
  1548. ArrayItem <DATATYPE> * dl = first;
  1549. while (dl)
  1550. {
  1551. delete dl-> Data;
  1552. dl = dl->next;
  1553. }
  1554. Array <DATATYPE>::RemoveAll ();
  1555. }
  1556. inline void Reset (void)
  1557. {
  1558. RemoveAll ();
  1559. }
  1560. ~ArrayOfPtr () { Reset (); }
  1561. static inline int compare (const DATATYPE & t1, const DATATYPE & t2)
  1562. {
  1563. return (*t1>*t2 ? 1 : (*t1 == *t2 ? 0 : -1));
  1564. }
  1565. static inline bool IsEqual (const DATATYPE & t1, const DATATYPE & t2)
  1566. {
  1567. return (*t1 == *t2);
  1568. }
  1569. DATATYPE * Find (const DATATYPE & Key) const
  1570. {
  1571. return Array <DATATYPE>::Find (Key, IsEqual);
  1572. }
  1573. template <class ODT> DATATYPE * Find (const ODT & Key) const
  1574. {
  1575. for (Iterator Iter (*this); Iter; Iter++)
  1576. {
  1577. DATATYPE & value = Iter ();
  1578. if (*value == Key)
  1579. return &value;
  1580. }
  1581. return NULL;
  1582. }
  1583. template <class Compare> DATATYPE * Find (const DATATYPE & Key, Compare cf) const
  1584. {
  1585. for (Iterator Iter (*this); Iter; Iter++)
  1586. {
  1587. DATATYPE & value = Iter ();
  1588. if (cf (*value, *Key))
  1589. return &value;
  1590. }
  1591. return NULL;
  1592. }
  1593. template <class ODT, class Compare> DATATYPE * Find (const ODT & Key, Compare cf) const
  1594. {
  1595. for (Iterator Iter (*this); Iter; Iter++)
  1596. {
  1597. DATATYPE & value = Iter ();
  1598. if (cf (*value, Key))
  1599. return &value;
  1600. }
  1601. return NULL;
  1602. }
  1603. void Sort (void)
  1604. {
  1605. return Array <DATATYPE>::Sort (compare);
  1606. }
  1607. template <class ODT> void Sort (Array <ODT> & ar)
  1608. {
  1609. return Array <DATATYPE>::Sort (ar, compare);
  1610. }
  1611. template <class ODT> void Sort (FixArray <ODT> & ar)
  1612. {
  1613. return Array <DATATYPE>::Sort (ar, compare);
  1614. }
  1615. inline void Remove (const DATATYPE & dt)
  1616. {
  1617. for (Array <DATATYPE>::Iterator Iter (*this); Iter; Iter++)
  1618. {
  1619. const DATATYPE & mydt = Iter ();
  1620. if (dt == mydt)
  1621. {
  1622. RemoveAt (Iter);
  1623. return;
  1624. }
  1625. }
  1626. }
  1627. inline BOOL RemoveAt (Iterator & Iter)
  1628. {
  1629. return RemoveAt <Iterator> (Iter);
  1630. }
  1631. inline BOOL RemoveAt (ReverseIterator & Iter)
  1632. {
  1633. return RemoveAt <ReverseIterator> (Iter);
  1634. }
  1635. protected:
  1636. template <class IterType> BOOL RemoveAt (IterType & Iter);
  1637. };
  1638. /*********************************************************************
  1639. * ArrayIterator Unit Class
  1640. *
  1641. *
  1642. *********************************************************************/
  1643. template < class DATATYPE > class ArrayIterator
  1644. {
  1645. private:
  1646. int ArraySize;
  1647. int Index;
  1648. DATATYPE * LastItem;
  1649. ArrayItem < DATATYPE > * LastAccess;
  1650. public:
  1651. ArrayIterator (const Array < DATATYPE > * theArray)
  1652. {
  1653. if (theArray)
  1654. {
  1655. ArraySize = theArray -> GetSize ();
  1656. LastAccess = (ArrayItem < DATATYPE > *) theArray -> first;
  1657. }
  1658. else
  1659. {
  1660. ArraySize = 0;
  1661. LastAccess = NULL;
  1662. }
  1663. Index = 0;
  1664. LastItem = NULL;
  1665. }
  1666. ArrayIterator (const Array < DATATYPE > & theArray)
  1667. {
  1668. ArraySize = theArray.GetSize ();
  1669. LastAccess = (ArrayItem < DATATYPE > *) theArray.first;
  1670. Index = 0;
  1671. LastItem = NULL;
  1672. }
  1673. operator bool () const
  1674. {
  1675. return bool (LastAccess);
  1676. }
  1677. DATATYPE & operator () (void)
  1678. {
  1679. if (LastAccess)
  1680. {
  1681. LastItem = &(LastAccess -> Data);
  1682. LastAccess = LastAccess -> next;
  1683. Index ++;
  1684. return *LastItem;
  1685. }
  1686. // _fatal ("Warning! will return null");
  1687. LastItem = NULL;
  1688. return ((DATATYPE &) *LastItem);
  1689. }
  1690. int GetLastIndex (void)
  1691. {
  1692. return (Index-1);
  1693. }
  1694. DATATYPE & GetLastItem (void)
  1695. {
  1696. return ((DATATYPE &)(*LastItem));
  1697. }
  1698. int GetArraySize (void) const
  1699. {
  1700. return (ArraySize);
  1701. }
  1702. };
  1703. template < class DATATYPE > class ArrayReverseIterator
  1704. {
  1705. private:
  1706. int ArraySize;
  1707. int Index;
  1708. DATATYPE * LastItem;
  1709. ArrayItem < DATATYPE > * LastAccess;
  1710. public:
  1711. ArrayReverseIterator (const Array < DATATYPE > * theArray)
  1712. {
  1713. if (theArray)
  1714. {
  1715. ArraySize = theArray -> GetSize ();
  1716. LastAccess = (ArrayItem < DATATYPE > *) theArray -> last;
  1717. Index = ArraySize-1;
  1718. }
  1719. else
  1720. {
  1721. ArraySize = 0;
  1722. LastAccess = NULL;
  1723. Index = 0;
  1724. }
  1725. LastItem = NULL;
  1726. }
  1727. ArrayReverseIterator (const Array < DATATYPE > & theArray)
  1728. {
  1729. ArraySize = theArray.GetSize ();
  1730. LastAccess = (ArrayItem < DATATYPE > *) theArray.last;
  1731. Index = ArraySize-1;
  1732. LastItem = NULL;
  1733. }
  1734. operator bool () const
  1735. {
  1736. return bool (LastAccess);
  1737. }
  1738. DATATYPE & operator () (void)
  1739. {
  1740. if (LastAccess)
  1741. {
  1742. LastItem = &(LastAccess -> Data);
  1743. LastAccess = LastAccess -> prev;
  1744. Index --;
  1745. return *LastItem;
  1746. }
  1747. _fatal ("Warning! will return null");
  1748. LastItem = NULL;
  1749. return ((DATATYPE &) *LastItem);
  1750. }
  1751. int GetLastIndex (void)
  1752. {
  1753. return (Index+1);
  1754. }
  1755. DATATYPE & GetLastItem (void)
  1756. {
  1757. return ((DATATYPE &)(*LastItem));
  1758. }
  1759. int GetArraySize (void) const
  1760. {
  1761. return (ArraySize);
  1762. }
  1763. };
  1764. /*********************************************************************
  1765. * ArrayIterator Unit Class
  1766. *
  1767. *
  1768. *********************************************************************/
  1769. template < class DATATYPE > class ArrayStepIterator
  1770. {
  1771. private:
  1772. int ArraySize;
  1773. int Index;
  1774. DATATYPE * LastItem;
  1775. ArrayItem < DATATYPE > * LastAccess;
  1776. public:
  1777. ArrayStepIterator (const Array < DATATYPE > * theArray)
  1778. {
  1779. if (theArray)
  1780. {
  1781. ArraySize = theArray -> GetSize ();
  1782. LastAccess = (ArrayItem < DATATYPE > *) theArray -> first;
  1783. }
  1784. else
  1785. {
  1786. ArraySize = 0;
  1787. LastAccess = NULL;
  1788. }
  1789. Index = 0;
  1790. LastItem = NULL;
  1791. }
  1792. ArrayStepIterator (const Array < DATATYPE > & theArray)
  1793. {
  1794. ArraySize = theArray.GetSize ();
  1795. LastAccess = (ArrayItem < DATATYPE > *) theArray.first;
  1796. Index = 0;
  1797. LastItem = NULL;
  1798. }
  1799. operator bool () const
  1800. {
  1801. return bool (LastAccess);
  1802. }
  1803. DATATYPE & operator () (void)
  1804. {
  1805. if (LastAccess)
  1806. {
  1807. LastItem = &(LastAccess -> Data);
  1808. return *LastItem;
  1809. }
  1810. // _fatal ("Warning! will return null");
  1811. LastItem = NULL;
  1812. return ((DATATYPE &) *LastItem);
  1813. }
  1814. int GetLastIndex (void)
  1815. {
  1816. return Index;
  1817. }
  1818. ArrayStepIterator & operator ++ () // Iter ++; postfix
  1819. {
  1820. if (LastAccess)
  1821. {
  1822. LastAccess = LastAccess -> next;
  1823. Index ++;
  1824. }
  1825. return (*this);
  1826. }
  1827. ArrayStepIterator & operator ++ (int) // ++ Iter, prevfix
  1828. {
  1829. if (LastAccess)
  1830. {
  1831. LastAccess = LastAccess -> next;
  1832. Index ++;
  1833. }
  1834. return (*this);
  1835. }
  1836. ArrayStepIterator & operator -- () // Iter --; postfix
  1837. {
  1838. if (LastAccess)
  1839. {
  1840. LastAccess = LastAccess -> prev;
  1841. Index --;
  1842. }
  1843. return (*this);
  1844. }
  1845. ArrayStepIterator & operator -- (int) // -- Iter, prevfix
  1846. {
  1847. if (LastAccess)
  1848. {
  1849. LastAccess = LastAccess -> prev;
  1850. Index --;
  1851. }
  1852. return (*this);
  1853. }
  1854. ArrayStepIterator & operator += (int NumberOfStep)
  1855. {
  1856. if (NumberOfStep > 0)
  1857. {
  1858. for (int nStep=0; nStep<NumberOfStep && LastAccess; nStep++)
  1859. {
  1860. LastAccess = LastAccess -> next;
  1861. Index ++;
  1862. }
  1863. }
  1864. else
  1865. {
  1866. for (int nStep=0; nStep<-NumberOfStep && LastAccess; nStep++)
  1867. {
  1868. LastAccess = LastAccess -> prev;
  1869. Index --;
  1870. }
  1871. }
  1872. return (*this);
  1873. }
  1874. ArrayStepIterator & operator -= (int NumberOfStep)
  1875. {
  1876. return this->operator += (-NumberOfStep);
  1877. }
  1878. DATATYPE & GetLastItem (void)
  1879. {
  1880. return ((DATATYPE &)(*LastItem));
  1881. }
  1882. int GetArraySize (void) const
  1883. {
  1884. return (ArraySize);
  1885. }
  1886. };
  1887. //#endif