array.tli 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  1. /***************************************************************************
  2. * E-Com Technology Ltd.
  3. *
  4. * ECOMPACS DICOM Network Transport Libraries * Version 0.1 Beta
  5. ***************************************************************************/
  6. /*********************************************************************
  7. * Class Array
  8. *********************************************************************/
  9. template < class DATATYPE >
  10. Array < DATATYPE >::Array (const Array & theArray)
  11. {
  12. ArraySize = 0;
  13. first = NULL;
  14. last = NULL;
  15. ClearType = 1;
  16. theArray.CopyTo (*this);
  17. }
  18. template <class DATATYPE>
  19. void Array<DATATYPE>::AddBefore (const DATATYPE & Value, int Index)
  20. {
  21. if (Index >= ArraySize)
  22. {
  23. Add (Value) ;
  24. return;
  25. }
  26. ArrayItem<DATATYPE> *dl;
  27. // scan the chain to get the element (from head or from tail)
  28. if (Index < ArraySize / 2)
  29. { // scan from the head of chain
  30. dl = first;
  31. ++Index;
  32. while (--Index > 0) dl = dl->next;
  33. }
  34. else
  35. { // scan from the tail of chain
  36. dl = last;
  37. Index = (ArraySize - Index);
  38. while (--Index > 0) dl = dl->prev;
  39. }
  40. ArrayItem<DATATYPE> *ndl;
  41. ndl = new ArrayItem<DATATYPE>;
  42. // relink chain before the element to be added
  43. ndl -> Data = Value;
  44. ndl -> next = dl;
  45. ndl -> prev = dl -> prev;
  46. dl -> prev = ndl;
  47. if (ndl->prev)
  48. ndl->prev->next = ndl;
  49. else
  50. first = ndl;
  51. ++ ArraySize;
  52. }
  53. template <class DATATYPE>
  54. void Array<DATATYPE>::AddAfter (const DATATYPE & Value, int Index)
  55. {
  56. if (Index < 0)
  57. {
  58. InsertFirst (Value);
  59. return;
  60. }
  61. AddBefore (Value, Index+1);
  62. }
  63. template <class DATATYPE>
  64. template <class IterType>
  65. void Array<DATATYPE>::AddBefore (const DATATYPE & Value, IterType & Iter)
  66. {
  67. if (Iter.__GetArray () != this) // not the same array
  68. return;
  69. ArrayItem<DATATYPE> * dl = Iter.__GetCurrent ();
  70. if (! dl)
  71. {
  72. Add (Value);
  73. if (Iter.GetArraySize () == 0) // Iter point to an empty array
  74. Iter.Restart ();
  75. return;
  76. }
  77. ArrayItem<DATATYPE> *ndl;
  78. ndl = new ArrayItem <DATATYPE>;
  79. // relink chain before the element to be added
  80. ndl -> Data = Value;
  81. ndl -> next = dl;
  82. ndl -> prev = dl -> prev;
  83. dl -> prev = ndl;
  84. if (ndl->prev)
  85. ndl->prev->next = ndl;
  86. else
  87. first = ndl;
  88. ++ ArraySize;
  89. Iter.__SetArraySize (ArraySize);
  90. Iter --;
  91. }
  92. template < class DATATYPE >
  93. BOOL Array < DATATYPE >::RemoveAt (int Index)
  94. {
  95. if (Index >= ArraySize) return (FALSE);
  96. ArrayItem < DATATYPE > * dl;
  97. // scan the chain to get the element (from head or from tail)
  98. if (Index < ArraySize / 2)
  99. { // scan from the head of chain
  100. dl = first;
  101. ++ Index;
  102. while (--Index > 0) dl = dl->next;
  103. }
  104. else
  105. { // scan from the tail of chain
  106. dl = last;
  107. Index = (ArraySize - Index);
  108. while (--Index > 0) dl = dl->prev;
  109. }
  110. // relink chain around element to be deleted
  111. if (dl->prev)
  112. dl->prev->next = dl->next;
  113. else
  114. first = dl->next;
  115. if (dl->next)
  116. dl->next->prev = dl->prev;
  117. else
  118. last = dl->prev;
  119. delete dl;
  120. --ArraySize;
  121. return (TRUE);
  122. }
  123. template < class DATATYPE >
  124. template < class IterType >
  125. BOOL Array < DATATYPE >::RemoveAt (IterType & Iter)
  126. {
  127. if (Iter.__GetArray () != this) // not the same array
  128. return false;
  129. ArrayItem < DATATYPE > * dl = Iter.__GetCurrent ();
  130. if (! dl)
  131. return false;
  132. Iter ++;
  133. // relink chain around element to be deleted
  134. if (dl->prev)
  135. dl->prev->next = dl->next;
  136. else
  137. first = dl->next;
  138. if (dl->next)
  139. dl->next->prev = dl->prev;
  140. else
  141. last = dl->prev;
  142. delete dl;
  143. -- ArraySize;
  144. Iter.__SetArraySize (ArraySize);
  145. return (TRUE);
  146. }
  147. template <class DATATYPE>
  148. void Array<DATATYPE>::AddBefore (const DATATYPE & Value, Iterator & Iter)
  149. {
  150. AddBefore <Iterator> (Value, Iter);
  151. }
  152. template < class DATATYPE >
  153. BOOL Array < DATATYPE >::RemoveAt (Iterator & Iter)
  154. {
  155. return RemoveAt <Iterator> (Iter);
  156. }
  157. template <class DATATYPE>
  158. void Array<DATATYPE>::AddBefore (const DATATYPE & Value, ReverseIterator & Iter)
  159. {
  160. AddBefore <ReverseIterator> (Value, Iter);
  161. }
  162. template <class DATATYPE>
  163. BOOL Array<DATATYPE>::RemoveAt (ReverseIterator & rIter)
  164. {
  165. return RemoveAt <ReverseIterator> (rIter);
  166. }
  167. template <class DATATYPE>
  168. BOOL Array <DATATYPE>::TransferTo (Array & theArray)
  169. {
  170. if (! first)
  171. return FALSE;
  172. if (ClearType != theArray.ClearType)
  173. return FALSE;
  174. first->prev = theArray.last;
  175. if (theArray.last)
  176. theArray.last->next = first;
  177. else
  178. theArray.first = first;
  179. theArray.last = last;
  180. theArray.ArraySize += ArraySize;
  181. ArraySize = 0;
  182. first = NULL;
  183. last = NULL;
  184. return TRUE;
  185. }
  186. template <class DATATYPE>
  187. int Array <DATATYPE>::TransferTo (Array & theArray, DATATYPE & dt)
  188. {
  189. if (! first)
  190. return 0;
  191. if (ClearType != theArray.ClearType)
  192. return 0;
  193. for (Iterator Iter (*this); Iter; Iter++)
  194. {
  195. DATATYPE & mydt = Iter ();
  196. if (dt == mydt)
  197. {
  198. RemoveAt (Iter);
  199. theArray.Add (dt);
  200. return 1;
  201. }
  202. }
  203. return 0;
  204. }
  205. /*
  206. template <class DATATYPE>
  207. int Array <DATATYPE>::TransferTo (Array & theArray, DATATYPE dt)
  208. {
  209. if (! first)
  210. return 0;
  211. if (ClearType != theArray.ClearType)
  212. return 0;
  213. for (Iterator Iter (*this); Iter; Iter++)
  214. {
  215. DATATYPE & mydt = Iter ();
  216. if (dt == mydt)
  217. {
  218. RemoveAt (Iter);
  219. theArray.Add (dt);
  220. return 1;
  221. }
  222. }
  223. return 0;
  224. }
  225. */
  226. template <class DATATYPE>
  227. int Array <DATATYPE>::TransferTo (Array & theArray, int fromIndex, int NbOfTransfer)
  228. {
  229. if (! first)
  230. return 0;
  231. if (ClearType != theArray.ClearType)
  232. return 0;
  233. Iterator Iter (*this); Iter += fromIndex;
  234. for (int Index = 0; Iter && Index < NbOfTransfer; Index++)
  235. {
  236. DATATYPE & dt = Iter ();
  237. theArray.Add (dt);
  238. if (! RemoveAt (Iter))
  239. Iter ++;
  240. }
  241. return Index;
  242. }
  243. template <class DATATYPE>
  244. int Array <DATATYPE>::CopyTo (FixArray <DATATYPE> & theArray) const
  245. {
  246. if (theArray.GetCapacity () < this->GetSize ())
  247. return 0;
  248. for (Array <DATATYPE>::Iterator Iter (*this); Iter; Iter++)
  249. {
  250. theArray.Add (Iter ());
  251. }
  252. return GetSize ();
  253. }
  254. template <class DATATYPE>
  255. int Array <DATATYPE>::CopyTo (Array <DATATYPE> & theArray) const
  256. {
  257. for (Array <DATATYPE>::Iterator Iter (*this); Iter; Iter++)
  258. {
  259. theArray.Add (Iter ());
  260. }
  261. return GetSize ();
  262. }
  263. template <class DATATYPE>
  264. int Array <DATATYPE>::CopyTo (DATATYPE * pArray, int NbOfElem) const
  265. {
  266. if (NbOfElem == 0)
  267. return 0;
  268. int size = GetSize ();
  269. if (NbOfElem > size)
  270. NbOfElem = size;
  271. if (NbOfElem == 0)
  272. return NbOfElem;
  273. if (! pArray)
  274. return 0;
  275. if (NbOfElem == 1)
  276. {
  277. *pArray = first->Data;
  278. return NbOfElem;
  279. }
  280. if (NbOfElem == 2)
  281. {
  282. *pArray = first->Data;
  283. pArray++;
  284. *pArray = first->next->Data;
  285. return NbOfElem;
  286. }
  287. if (NbOfElem == 3)
  288. {
  289. ArrayItem < DATATYPE > * dl = first;
  290. *pArray = dl->Data;
  291. pArray++; dl = dl->next;
  292. *pArray = dl->Data;
  293. pArray++; dl = dl->next;
  294. *pArray = dl->Data;
  295. return NbOfElem;
  296. }
  297. if (NbOfElem == 4)
  298. {
  299. ArrayItem < DATATYPE > * dl = first;
  300. *pArray = dl->Data;
  301. pArray++; dl = dl->next;
  302. *pArray = dl->Data;
  303. pArray++; dl = dl->next;
  304. *pArray = dl->Data;
  305. pArray++; dl = dl->next;
  306. *pArray = dl->Data;
  307. return NbOfElem;
  308. }
  309. Array <DATATYPE>::Iterator Iter (*this);
  310. for (int Index = 0; Index<NbOfElem; Index++, Iter++)
  311. // 因为前面已经确定NbOfElem <= size;因此这里不用再判定Iter是否已经到尾了
  312. {
  313. *pArray++ = Iter ();
  314. }
  315. return NbOfElem;
  316. }
  317. template <class DATATYPE>
  318. int Array <DATATYPE>::Append (const Array <DATATYPE> & from)
  319. {
  320. if (ClearType != from.ClearType)
  321. return -1;
  322. int Size = GetSize ();
  323. ArrayIterator <DATATYPE> Iter (from);
  324. while (Iter)
  325. {
  326. DATATYPE value = Iter ();
  327. Add (value);
  328. }
  329. return Size;
  330. }
  331. template <class DATATYPE>
  332. void Array <DATATYPE>::SetCapacity (int nNewSize)
  333. {
  334. if (nNewSize == 0)
  335. {
  336. RemoveAll ();
  337. }
  338. else
  339. {
  340. // it fits
  341. if (nNewSize > ArraySize)
  342. {
  343. // initialize the new elements
  344. for (int Index=0; Index<nNewSize-ArraySize; Index++)
  345. {
  346. DATATYPE value;
  347. Add (value);
  348. }
  349. }
  350. else if (ArraySize > nNewSize)
  351. {
  352. // destroy the old elements
  353. for (int Index=0; Index<ArraySize-nNewSize; Index++)
  354. {
  355. RemoveLast ();
  356. }
  357. }
  358. }
  359. }
  360. //////////////////////////////////////////////////////////////////////
  361. // Quick Sort
  362. //////////////////////////////////////////////////////////////////////
  363. template <class DATATYPE>
  364. void Array <DATATYPE>::Sort (int low, int high)
  365. {
  366. if (high - low <= 0)
  367. return;
  368. if (high - low == 1)
  369. {
  370. if (GetAt (high) < GetAt (low))
  371. Swap (low, high);
  372. return;
  373. }
  374. // 存放中心索引及其值的局部变量及用于扫描的索引
  375. DATATYPE pivot;
  376. int scanUp, scanDown;
  377. int mid;
  378. mid = (low + high) / 2;
  379. pivot = GetAt (mid);
  380. Swap (mid, low);
  381. scanUp = low + 1; scanDown = high;
  382. do
  383. {
  384. ArrayItem < DATATYPE > * dl;
  385. dl = PtrOfItemAt (scanUp);
  386. while (scanUp <= scanDown && dl->Data <= pivot)
  387. {
  388. scanUp ++;
  389. dl = dl->next;
  390. }
  391. dl = PtrOfItemAt (scanDown);
  392. while (pivot < dl->Data)
  393. {
  394. scanDown --;
  395. dl = dl->prev;
  396. }
  397. if (scanUp < scanDown)
  398. {
  399. Swap (scanUp, scanDown);
  400. }
  401. }
  402. while (scanUp < scanDown);
  403. ArrayItem < DATATYPE > * dl = PtrOfItemAt (scanDown);
  404. (*this)[low] = dl->Data;
  405. dl->Data = pivot;
  406. if (low < scanDown - 1)
  407. Sort (low, scanDown - 1);
  408. if (scanDown + 1 < high)
  409. Sort (scanDown + 1, high);
  410. }
  411. template <class DATATYPE>
  412. template <class ODT>
  413. void Array <DATATYPE>::Sort (int low, int high, Array <ODT> & ar)
  414. {
  415. if (high - low <= 0)
  416. return;
  417. if (high - low == 1)
  418. {
  419. if (GetAt (high) < GetAt (low))
  420. {
  421. Swap (low, high);
  422. ar.Swap (low, high);
  423. }
  424. return;
  425. }
  426. // 存放中心索引及其值的局部变量及用于扫描的索引
  427. int scanUp, scanDown;
  428. int mid;
  429. mid = (low + high) / 2;
  430. DATATYPE pivot = GetAt (mid);
  431. ODT __fvalue;
  432. __fvalue = ar.GetAt (mid);
  433. Swap (mid, low);
  434. ar.Swap (mid, low);
  435. scanUp = low + 1; scanDown = high;
  436. do
  437. {
  438. ArrayItem < DATATYPE > * dl;
  439. dl = PtrOfItemAt (scanUp);
  440. while (scanUp <= scanDown && dl->Data <= pivot)
  441. {
  442. scanUp ++;
  443. dl = dl->next;
  444. }
  445. dl = PtrOfItemAt (scanDown);
  446. while (pivot < dl->Data)
  447. {
  448. scanDown --;
  449. dl = dl->prev;
  450. }
  451. if (scanUp < scanDown)
  452. {
  453. Swap (scanUp, scanDown);
  454. ar.Swap (scanUp, scanDown);
  455. }
  456. }
  457. while (scanUp < scanDown);
  458. ArrayItem < DATATYPE > * dl;
  459. dl = PtrOfItemAt (scanDown);
  460. (*this)[low] = dl->Data;
  461. dl->Data = pivot;
  462. {
  463. ArrayItem < ODT > * fdl;
  464. fdl = ar.PtrOfItemAt (scanDown);
  465. ar[low] = fdl->Data;
  466. fdl->Data = __fvalue;
  467. }
  468. if (low < scanDown - 1)
  469. Sort (low, scanDown - 1, ar);
  470. if (scanDown + 1 < high)
  471. Sort (scanDown + 1, high, ar);
  472. }
  473. template <class DATATYPE>
  474. template <class ODT>
  475. void Array <DATATYPE>::Sort (int low, int high, FixArray <ODT> & ar)
  476. {
  477. if (high - low <= 0)
  478. return;
  479. if (high - low == 1)
  480. {
  481. if (GetAt (high) < GetAt (low))
  482. {
  483. Swap (low, high);
  484. ar.Swap (low, high);
  485. }
  486. return;
  487. }
  488. // 存放中心索引及其值的局部变量及用于扫描的索引
  489. int scanUp, scanDown;
  490. int mid;
  491. mid = (low + high) / 2;
  492. DATATYPE pivot = GetAt (mid);
  493. ODT __fvalue;
  494. __fvalue = ar.GetAt (mid);
  495. Swap (mid, low);
  496. ar.Swap (mid, low);
  497. scanUp = low + 1; scanDown = high;
  498. do
  499. {
  500. ArrayItem < DATATYPE > * dl;
  501. dl = PtrOfItemAt (scanUp);
  502. while (scanUp <= scanDown && dl->Data <= pivot)
  503. {
  504. scanUp ++;
  505. dl = dl->next;
  506. }
  507. dl = PtrOfItemAt (scanDown);
  508. while (pivot < dl->Data)
  509. {
  510. scanDown --;
  511. dl = dl->prev;
  512. }
  513. if (scanUp < scanDown)
  514. {
  515. Swap (scanUp, scanDown);
  516. ar.Swap (scanUp, scanDown);
  517. }
  518. }
  519. while (scanUp < scanDown);
  520. ArrayItem < DATATYPE > * dl;
  521. dl = PtrOfItemAt (scanDown);
  522. (*this)[low] = dl->Data;
  523. dl->Data = pivot;
  524. {
  525. ar[low] = ar[scanDown];
  526. ar[scanDown] = __fvalue;
  527. }
  528. if (low < scanDown - 1)
  529. Sort (low, scanDown - 1, ar);
  530. if (scanDown + 1 < high)
  531. Sort (scanDown + 1, high, ar);
  532. }
  533. //////////////////////////////////////////////////////////////////////
  534. // Sort using the give compare function
  535. //////////////////////////////////////////////////////////////////////
  536. template <class DATATYPE>
  537. template <class Compare>
  538. void Array <DATATYPE>::Sort (int low, int high, Compare cf)
  539. {
  540. if (high - low <= 0)
  541. return;
  542. if (high - low == 1)
  543. {
  544. if (cf (GetAt (high), GetAt (low)) < 0)
  545. Swap (low, high);
  546. return;
  547. }
  548. // 存放中心索引及其值的局部变量及用于扫描的索引
  549. DATATYPE pivot;
  550. int scanUp, scanDown;
  551. int mid;
  552. mid = (low + high) / 2;
  553. pivot = GetAt (mid);
  554. Swap (mid, low);
  555. scanUp = low + 1; scanDown = high;
  556. do
  557. {
  558. ArrayItem < DATATYPE > * dl;
  559. dl = PtrOfItemAt (scanUp);
  560. while (scanUp <= scanDown && cf (dl->Data, pivot) <= 0)
  561. {
  562. scanUp ++;
  563. dl = dl->next;
  564. }
  565. dl = PtrOfItemAt (scanDown);
  566. while (cf (pivot, dl->Data) < 0)
  567. {
  568. scanDown --;
  569. dl = dl->prev;
  570. }
  571. if (scanUp < scanDown)
  572. {
  573. Swap (scanUp, scanDown);
  574. }
  575. }
  576. while (scanUp < scanDown);
  577. ArrayItem < DATATYPE > * dl= PtrOfItemAt (scanDown);
  578. (*this)[low] = dl->Data;
  579. dl->Data = pivot;
  580. if (low < scanDown - 1)
  581. Sort (low, scanDown - 1, cf);
  582. if (scanDown + 1 < high)
  583. Sort (scanDown + 1, high, cf);
  584. }
  585. template <class DATATYPE>
  586. template <class ODT, class Compare>
  587. void Array <DATATYPE>::Sort (int low, int high, Array <ODT> & ar, Compare cf)
  588. {
  589. if (high - low <= 0)
  590. return;
  591. if (high - low == 1)
  592. {
  593. if (cf (GetAt (high), GetAt (low)) < 0)
  594. {
  595. Swap (low, high);
  596. ar.Swap (low, high);
  597. }
  598. return;
  599. }
  600. // 存放中心索引及其值的局部变量及用于扫描的索引
  601. int scanUp, scanDown;
  602. int mid;
  603. mid = (low + high) / 2;
  604. DATATYPE pivot = GetAt (mid);
  605. ODT __fvalue;
  606. __fvalue = ar.GetAt (mid);
  607. Swap (mid, low);
  608. ar.Swap (mid, low);
  609. scanUp = low + 1; scanDown = high;
  610. do
  611. {
  612. ArrayItem < DATATYPE > * dl;
  613. dl = PtrOfItemAt (scanUp);
  614. while (scanUp <= scanDown && cf (dl->Data, pivot) <= 0)
  615. {
  616. scanUp ++;
  617. dl = dl->next;
  618. }
  619. dl = PtrOfItemAt (scanDown);
  620. while (cf (pivot, dl->Data) < 0)
  621. {
  622. scanDown --;
  623. dl = dl->prev;
  624. }
  625. if (scanUp < scanDown)
  626. {
  627. Swap (scanUp, scanDown);
  628. ar.Swap (scanUp, scanDown);
  629. }
  630. }
  631. while (scanUp < scanDown);
  632. ArrayItem < DATATYPE > * dl;
  633. dl = PtrOfItemAt (scanDown);
  634. (*this)[low] = dl->Data;
  635. dl->Data = pivot;
  636. {
  637. ArrayItem < ODT > * fdl;
  638. fdl = ar.PtrOfItemAt (scanDown);
  639. ar[low] = fdl->Data;
  640. fdl->Data = __fvalue;
  641. }
  642. if (low < scanDown - 1)
  643. Sort (low, scanDown - 1, ar, cf);
  644. if (scanDown + 1 < high)
  645. Sort (scanDown + 1, high, ar, cf);
  646. }
  647. template <class DATATYPE>
  648. template <class ODT, class Compare>
  649. void Array <DATATYPE>::Sort (int low, int high, FixArray <ODT> & ar, Compare cf)
  650. {
  651. if (high - low <= 0)
  652. return;
  653. if (high - low == 1)
  654. {
  655. if (cf (GetAt (high), GetAt (low)) < 0)
  656. {
  657. Swap (low, high);
  658. ar.Swap (low, high);
  659. }
  660. return;
  661. }
  662. // 存放中心索引及其值的局部变量及用于扫描的索引
  663. int scanUp, scanDown;
  664. int mid;
  665. mid = (low + high) / 2;
  666. DATATYPE pivot = GetAt (mid);
  667. ODT __fvalue;
  668. __fvalue = ar.GetAt (mid);
  669. Swap (mid, low);
  670. ar.Swap (mid, low);
  671. scanUp = low + 1; scanDown = high;
  672. do
  673. {
  674. ArrayItem < DATATYPE > * dl;
  675. dl = PtrOfItemAt (scanUp);
  676. while (scanUp <= scanDown && cf (dl->Data, pivot) <= 0)
  677. {
  678. scanUp ++;
  679. dl = dl->next;
  680. }
  681. dl = PtrOfItemAt (scanDown);
  682. while (cf (pivot, dl->Data) < 0)
  683. {
  684. scanDown --;
  685. dl = dl->prev;
  686. }
  687. if (scanUp < scanDown)
  688. {
  689. Swap (scanUp, scanDown);
  690. ar.Swap (scanUp, scanDown);
  691. }
  692. }
  693. while (scanUp < scanDown);
  694. ArrayItem < DATATYPE > * dl;
  695. dl = PtrOfItemAt (scanDown);
  696. (*this)[low] = dl->Data;
  697. dl->Data = pivot;
  698. {
  699. ar[low] = ar[scanDown];
  700. ar[scanDown] = __fvalue;
  701. }
  702. if (low < scanDown - 1)
  703. Sort (low, scanDown - 1, ar, cf);
  704. if (scanDown + 1 < high)
  705. Sort (scanDown + 1, high, ar, cf);
  706. }
  707. //////////////////////////////////////////////////////////////////////
  708. // Sort using the give compare function, with arg
  709. //////////////////////////////////////////////////////////////////////
  710. template <class DATATYPE>
  711. template <class Compare, typename arg>
  712. void Array <DATATYPE>::Sort (int low, int high, Compare cf, arg a)
  713. {
  714. if (high - low <= 0)
  715. return;
  716. if (high - low == 1)
  717. {
  718. if (cf (GetAt (high), GetAt (low), a) < 0)
  719. Swap (low, high);
  720. return;
  721. }
  722. // 存放中心索引及其值的局部变量及用于扫描的索引
  723. DATATYPE pivot;
  724. int scanUp, scanDown;
  725. int mid;
  726. mid = (low + high) / 2;
  727. pivot = GetAt (mid);
  728. Swap (mid, low);
  729. scanUp = low + 1; scanDown = high;
  730. do
  731. {
  732. ArrayItem < DATATYPE > * dl;
  733. dl = PtrOfItemAt (scanUp);
  734. while (scanUp <= scanDown && cf (dl->Data, pivot, a) <= 0)
  735. {
  736. scanUp ++;
  737. dl = dl->next;
  738. }
  739. dl = PtrOfItemAt (scanDown);
  740. while (cf (pivot, dl->Data, a) < 0)
  741. {
  742. scanDown --;
  743. dl = dl->prev;
  744. }
  745. if (scanUp < scanDown)
  746. {
  747. Swap (scanUp, scanDown);
  748. }
  749. }
  750. while (scanUp < scanDown);
  751. ArrayItem < DATATYPE > * dl= PtrOfItemAt (scanDown);
  752. (*this)[low] = dl->Data;
  753. dl->Data = pivot;
  754. if (low < scanDown - 1)
  755. Sort (low, scanDown - 1, cf, a);
  756. if (scanDown + 1 < high)
  757. Sort (scanDown + 1, high, cf, a);
  758. }
  759. template <class DATATYPE>
  760. template <class ODT, class Compare, typename arg>
  761. void Array <DATATYPE>::Sort (int low, int high, Array <ODT> & ar, Compare cf, arg a)
  762. {
  763. if (high - low <= 0)
  764. return;
  765. if (high - low == 1)
  766. {
  767. if (cf (GetAt (high), GetAt (low), a) < 0)
  768. {
  769. Swap (low, high);
  770. ar.Swap (low, high);
  771. }
  772. return;
  773. }
  774. // 存放中心索引及其值的局部变量及用于扫描的索引
  775. int scanUp, scanDown;
  776. int mid;
  777. mid = (low + high) / 2;
  778. DATATYPE pivot = GetAt (mid);
  779. ODT __fvalue;
  780. __fvalue = ar.GetAt (mid);
  781. Swap (mid, low);
  782. ar.Swap (mid, low);
  783. scanUp = low + 1; scanDown = high;
  784. do
  785. {
  786. ArrayItem < DATATYPE > * dl;
  787. dl = PtrOfItemAt (scanUp);
  788. while (scanUp <= scanDown && cf (dl->Data, pivot, a) <= 0)
  789. {
  790. scanUp ++;
  791. dl = dl->next;
  792. }
  793. dl = PtrOfItemAt (scanDown);
  794. while (cf (pivot, dl->Data, a) < 0)
  795. {
  796. scanDown --;
  797. dl = dl->prev;
  798. }
  799. if (scanUp < scanDown)
  800. {
  801. Swap (scanUp, scanDown);
  802. ar.Swap (scanUp, scanDown);
  803. }
  804. }
  805. while (scanUp < scanDown);
  806. ArrayItem < DATATYPE > * dl;
  807. dl = PtrOfItemAt (scanDown);
  808. (*this)[low] = dl->Data;
  809. dl->Data = pivot;
  810. {
  811. ArrayItem < ODT > * fdl;
  812. fdl = ar.PtrOfItemAt (scanDown);
  813. ar[low] = fdl->Data;
  814. fdl->Data = __fvalue;
  815. }
  816. if (low < scanDown - 1)
  817. Sort (low, scanDown - 1, ar, cf, a);
  818. if (scanDown + 1 < high)
  819. Sort (scanDown + 1, high, ar, cf, a);
  820. }
  821. template <class DATATYPE>
  822. template <class ODT, class Compare, typename arg>
  823. void Array <DATATYPE>::Sort (int low, int high, FixArray <ODT> & ar, Compare cf, arg a)
  824. {
  825. if (high - low <= 0)
  826. return;
  827. if (high - low == 1)
  828. {
  829. if (cf (GetAt (high), GetAt (low), a) < 0)
  830. {
  831. Swap (low, high);
  832. ar.Swap (low, high);
  833. }
  834. return;
  835. }
  836. // 存放中心索引及其值的局部变量及用于扫描的索引
  837. int scanUp, scanDown;
  838. int mid;
  839. mid = (low + high) / 2;
  840. DATATYPE pivot = GetAt (mid);
  841. ODT __fvalue;
  842. __fvalue = ar.GetAt (mid);
  843. Swap (mid, low);
  844. ar.Swap (mid, low);
  845. scanUp = low + 1; scanDown = high;
  846. do
  847. {
  848. ArrayItem < DATATYPE > * dl;
  849. dl = PtrOfItemAt (scanUp);
  850. while (scanUp <= scanDown && cf (dl->Data, pivot, a) <= 0)
  851. {
  852. scanUp ++;
  853. dl = dl->next;
  854. }
  855. dl = PtrOfItemAt (scanDown);
  856. while (cf (pivot, dl->Data, a) < 0)
  857. {
  858. scanDown --;
  859. dl = dl->prev;
  860. }
  861. if (scanUp < scanDown)
  862. {
  863. Swap (scanUp, scanDown);
  864. ar.Swap (scanUp, scanDown);
  865. }
  866. }
  867. while (scanUp < scanDown);
  868. ArrayItem < DATATYPE > * dl;
  869. dl = PtrOfItemAt (scanDown);
  870. (*this)[low] = dl->Data;
  871. dl->Data = pivot;
  872. {
  873. ar[low] = ar[scanDown];
  874. ar[scanDown] = __fvalue;
  875. }
  876. if (low < scanDown - 1)
  877. Sort (low, scanDown - 1, ar, cf, a);
  878. if (scanDown + 1 < high)
  879. Sort (scanDown + 1, high, ar, cf, a);
  880. }
  881. //////////////////////////////////////////////////////////////////////
  882. // 对记录序列 R[0..n]进行堆排序。
  883. //////////////////////////////////////////////////////////////////////
  884. template <class DATATYPE>
  885. void Array <DATATYPE>::HeapSort (int n)
  886. {
  887. for (int i=n/2; i>=0; --i)
  888. // 把R[1..n]建成大顶堆
  889. HeapAdjust (i, n);
  890. for (int i=n; i>=1; --i)
  891. {
  892. Swap (0, i);
  893. // 将堆顶记录和当前未经排序子序列
  894. // R[1..i]中最后一个记录相互交换
  895. HeapAdjust (0, i-1);
  896. // 将R[1..i-1] 重新调整为大顶堆
  897. }
  898. }
  899. // 已知R[s..m]中记录的关键字除R[s].key之
  900. // 外均满足堆的定义,本函数调整R[s] 的关
  901. // 键字,使R[s..m]成为一个大顶堆(对其中
  902. // 记录的关键字而言)
  903. template <class DATATYPE>
  904. void Array <DATATYPE>::HeapAdjust (int s, int m)
  905. {
  906. DATATYPE rc = GetAt (s);
  907. for (int j=2*s; j<=m; j*=2)
  908. {
  909. ArrayItem < DATATYPE > * dl;
  910. dl = PtrOfItemAt (j);
  911. // 沿key较大的孩子结点向下筛选
  912. if (j < m && dl->Data < dl->next->Data)
  913. {
  914. j ++;
  915. dl = dl->next;
  916. }
  917. // j为key较大的记录的下标
  918. if (rc >= dl->Data)
  919. break;
  920. // rc应插入在位置s上
  921. Swap (s, j); s = j;
  922. }
  923. (*this)[s] = rc; // 插入
  924. }
  925. //////////////////////////////////////////////////////////////////////
  926. // IsSorted
  927. //////////////////////////////////////////////////////////////////////
  928. template <class DATATYPE>
  929. bool Array <DATATYPE>::IsSorted () const
  930. {
  931. ArrayItem < DATATYPE > * prev = first;
  932. ArrayItem < DATATYPE > * next = prev;
  933. if (! next)
  934. return true;
  935. while (next->next)
  936. {
  937. prev = next;
  938. next = next->next;
  939. if (prev->Data > next->Data)
  940. return false;
  941. }
  942. return true;
  943. }
  944. template <class DATATYPE>
  945. template <class Compare>
  946. bool Array <DATATYPE>::IsSorted (Compare cf) const
  947. {
  948. ArrayItem < DATATYPE > * prev = first;
  949. ArrayItem < DATATYPE > * next = prev;
  950. if (! next)
  951. return true;
  952. while (next->next)
  953. {
  954. prev = next;
  955. next = next->next;
  956. if (cf (prev->Data, next->Data) > 0)
  957. return false;
  958. }
  959. return true;
  960. }
  961. //////////////////////////////////////////////////////////////////////
  962. // FindMin, FindMax
  963. //////////////////////////////////////////////////////////////////////
  964. template <class DATATYPE>
  965. DATATYPE & Array <DATATYPE>::FindMin (void) const
  966. {
  967. ArrayItem < DATATYPE > * pMin = first;
  968. if (! pMin)
  969. return pMin -> Data;
  970. ArrayItem < DATATYPE > * next = first->next;
  971. while (next)
  972. {
  973. if (next->Data < pMin->Data)
  974. pMin = next;
  975. next = next->next;
  976. }
  977. return pMin -> Data;
  978. }
  979. template <class DATATYPE>
  980. DATATYPE & Array <DATATYPE>::FindMax (void) const
  981. {
  982. ArrayItem < DATATYPE > * pMax = first;
  983. if (! pMax)
  984. return pMax -> Data;
  985. ArrayItem < DATATYPE > * next = first->next;
  986. while (next)
  987. {
  988. if (next->Data > pMax->Data)
  989. pMax = next;
  990. next = next->next;
  991. }
  992. return pMax -> Data;
  993. }
  994. template <class DATATYPE>
  995. template <class Compare>
  996. DATATYPE & Array <DATATYPE>::FindMin (Compare cf) const
  997. {
  998. ArrayItem < DATATYPE > * pMin = first;
  999. if (! pMin)
  1000. return pMin -> Data;
  1001. ArrayItem < DATATYPE > * next = first->next;
  1002. while (next)
  1003. {
  1004. if (cf (next->Data, pMin->Data) < 0)
  1005. pMin = next;
  1006. next = next->next;
  1007. }
  1008. return pMin -> Data;
  1009. }
  1010. template <class DATATYPE>
  1011. template <class Compare>
  1012. DATATYPE & Array <DATATYPE>::FindMax (Compare cf) const
  1013. {
  1014. ArrayItem < DATATYPE > * pMax = first;
  1015. if (! pMax)
  1016. return pMax -> Data;
  1017. ArrayItem < DATATYPE > * next = first->next;
  1018. while (next)
  1019. {
  1020. if (cf (next->Data, pMax->Data) > 0)
  1021. pMax = next;
  1022. next = next->next;
  1023. }
  1024. return pMax -> Data;
  1025. }
  1026. //////////////////////////////////////////////////////////////////////
  1027. // IndexOfMin, IndexOfMax
  1028. //////////////////////////////////////////////////////////////////////
  1029. template <class DATATYPE>
  1030. int Array <DATATYPE>::IndexOfMin (void) const
  1031. {
  1032. ArrayItem < DATATYPE > * pMin = first;
  1033. if (! pMin)
  1034. return -1;
  1035. int Index = 1;
  1036. int LastIndex = 0;
  1037. ArrayItem < DATATYPE > * next = first->next;
  1038. while (next)
  1039. {
  1040. if (next->Data < pMin->Data)
  1041. {
  1042. pMin = next;
  1043. LastIndex = Index;
  1044. }
  1045. next = next->next;
  1046. Index ++;
  1047. }
  1048. return LastIndex;
  1049. }
  1050. template <class DATATYPE>
  1051. int Array <DATATYPE>::IndexOfMax (void) const
  1052. {
  1053. ArrayItem < DATATYPE > * pMax = first;
  1054. if (! pMax)
  1055. return -1;
  1056. int Index = 1;
  1057. int LastIndex = 0;
  1058. ArrayItem < DATATYPE > * next = first->next;
  1059. while (next)
  1060. {
  1061. if (next->Data > pMax->Data)
  1062. {
  1063. pMax = next;
  1064. LastIndex = Index;
  1065. }
  1066. next = next->next;
  1067. Index ++;
  1068. }
  1069. return LastIndex;
  1070. }
  1071. template <class DATATYPE>
  1072. template <class Compare>
  1073. int Array <DATATYPE>::IndexOfMin (Compare cf) const
  1074. {
  1075. ArrayItem < DATATYPE > * pMin = first;
  1076. if (! pMin)
  1077. return -1;
  1078. int Index = 1;
  1079. int LastIndex = 0;
  1080. ArrayItem < DATATYPE > * next = first->next;
  1081. while (next)
  1082. {
  1083. if (cf (next->Data, pMin->Data) < 0)
  1084. {
  1085. pMin = next;
  1086. LastIndex = Index;
  1087. }
  1088. next = next->next;
  1089. Index ++;
  1090. }
  1091. return LastIndex;
  1092. }
  1093. template <class DATATYPE>
  1094. template <class Compare>
  1095. int Array <DATATYPE>::IndexOfMax (Compare cf) const
  1096. {
  1097. ArrayItem < DATATYPE > * pMax = first;
  1098. if (! pMax)
  1099. return -1;
  1100. int Index = 1;
  1101. int LastIndex = 0;
  1102. ArrayItem < DATATYPE > * next = first->next;
  1103. while (next)
  1104. {
  1105. if (cf (next->Data, pMax->Data) > 0)
  1106. {
  1107. pMax = next;
  1108. LastIndex = Index;
  1109. }
  1110. next = next->next;
  1111. Index ++;
  1112. }
  1113. return LastIndex;
  1114. }
  1115. /*********************************************************************
  1116. * Class ArrayOfPtr
  1117. *********************************************************************/
  1118. template < class DATATYPE >
  1119. template < class IterType >
  1120. BOOL ArrayOfPtr < DATATYPE >::RemoveAt (IterType & Iter)
  1121. {
  1122. if (Iter.__GetArray () != this) // not the same array
  1123. return false;
  1124. ArrayItem < DATATYPE > * dl = Iter.__GetCurrent ();
  1125. if (! dl)
  1126. return false;
  1127. delete dl->Data;
  1128. return Array <DATATYPE>::RemoveAt (Iter);
  1129. }