lists.inc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. {
  2. This file is part of the Free Pascal Run Time Library (rtl)
  3. Copyright (c) 1999-2005 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {$if defined(VER2_0) or not defined(FPC_TESTGENERICS)}
  11. {****************************************************************************}
  12. {* TFPListEnumerator *}
  13. {****************************************************************************}
  14. constructor TFPListEnumerator.Create(AList: TFPList);
  15. begin
  16. inherited Create;
  17. FList := AList;
  18. FPosition := -1;
  19. end;
  20. function TFPListEnumerator.GetCurrent: Pointer;
  21. begin
  22. Result := FList[FPosition];
  23. end;
  24. function TFPListEnumerator.MoveNext: Boolean;
  25. begin
  26. Inc(FPosition);
  27. Result := FPosition < FList.Count;
  28. end;
  29. {****************************************************************************}
  30. {* TFPList *}
  31. {****************************************************************************}
  32. Const
  33. // Ratio of Pointer and Word Size.
  34. WordRatio = SizeOf(Pointer) Div SizeOf(Word);
  35. procedure TFPList.RaiseIndexError(Index : Integer);
  36. begin
  37. Error(SListIndexError, Index);
  38. end;
  39. function TFPList.Get(Index: Integer): Pointer;
  40. begin
  41. If (Index < 0) or (Index >= FCount) then
  42. RaiseIndexError(Index);
  43. Result:=FList^[Index];
  44. end;
  45. procedure TFPList.Put(Index: Integer; Item: Pointer);
  46. begin
  47. if (Index < 0) or (Index >= FCount) then
  48. RaiseIndexError(Index);
  49. Flist^[Index] := Item;
  50. end;
  51. function TFPList.Extract(Item: Pointer): Pointer;
  52. var
  53. i : Integer;
  54. begin
  55. i := IndexOf(item);
  56. if i >= 0 then
  57. begin
  58. Result := item;
  59. Delete(i);
  60. end
  61. else
  62. result := nil;
  63. end;
  64. procedure TFPList.SetCapacity(NewCapacity: Integer);
  65. begin
  66. If (NewCapacity < FCount) or (NewCapacity > MaxListSize) then
  67. Error (SListCapacityError, NewCapacity);
  68. if NewCapacity = FCapacity then
  69. exit;
  70. ReallocMem(FList, SizeOf(Pointer)*NewCapacity);
  71. FCapacity := NewCapacity;
  72. end;
  73. procedure TFPList.SetCount(NewCount: Integer);
  74. begin
  75. if (NewCount < 0) or (NewCount > MaxListSize)then
  76. Error(SListCountError, NewCount);
  77. If NewCount > FCount then
  78. begin
  79. If NewCount > FCapacity then
  80. SetCapacity(NewCount);
  81. If FCount < NewCount then
  82. FillWord(Flist^[FCount], (NewCount-FCount) * WordRatio, 0);
  83. end;
  84. FCount := Newcount;
  85. end;
  86. destructor TFPList.Destroy;
  87. begin
  88. Self.Clear;
  89. inherited Destroy;
  90. end;
  91. Procedure TFPList.AddList(AList : TFPList);
  92. Var
  93. I : Integer;
  94. begin
  95. If (Capacity<Count+AList.Count) then
  96. Capacity:=Count+AList.Count;
  97. For I:=0 to AList.Count-1 do
  98. Add(AList[i]);
  99. end;
  100. function TFPList.Add(Item: Pointer): Integer;
  101. begin
  102. if FCount = FCapacity then
  103. Self.Expand;
  104. FList^[FCount] := Item;
  105. Result := FCount;
  106. FCount := FCount + 1;
  107. end;
  108. procedure TFPList.Clear;
  109. begin
  110. if Assigned(FList) then
  111. begin
  112. SetCount(0);
  113. SetCapacity(0);
  114. FList := nil;
  115. end;
  116. end;
  117. procedure TFPList.Delete(Index: Integer);
  118. begin
  119. If (Index<0) or (Index>=FCount) then
  120. Error (SListIndexError, Index);
  121. FCount := FCount-1;
  122. System.Move (FList^[Index+1], FList^[Index], (FCount - Index) * SizeOf(Pointer));
  123. // Shrink the list if appropriate
  124. if (FCapacity > 256) and (FCount < FCapacity shr 2) then
  125. begin
  126. FCapacity := FCapacity shr 1;
  127. ReallocMem(FList, SizeOf(Pointer) * FCapacity);
  128. end;
  129. end;
  130. class procedure TFPList.Error(const Msg: string; Data: PtrInt);
  131. begin
  132. Raise EListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame);
  133. end;
  134. procedure TFPList.Exchange(Index1, Index2: Integer);
  135. var
  136. Temp : Pointer;
  137. begin
  138. If ((Index1 >= FCount) or (Index1 < 0)) then
  139. Error(SListIndexError, Index1);
  140. If ((Index2 >= FCount) or (Index2 < 0)) then
  141. Error(SListIndexError, Index2);
  142. Temp := FList^[Index1];
  143. FList^[Index1] := FList^[Index2];
  144. FList^[Index2] := Temp;
  145. end;
  146. function TFPList.Expand: TFPList;
  147. var
  148. IncSize : Longint;
  149. begin
  150. if FCount < FCapacity then exit(self);
  151. IncSize := 4;
  152. if FCapacity > 3 then IncSize := IncSize + 4;
  153. if FCapacity > 8 then IncSize := IncSize+8;
  154. if FCapacity > 127 then Inc(IncSize, FCapacity shr 2);
  155. SetCapacity(FCapacity + IncSize);
  156. Result := Self;
  157. end;
  158. function TFPList.First: Pointer;
  159. begin
  160. If FCount = 0 then
  161. Result := Nil
  162. else
  163. Result := Items[0];
  164. end;
  165. function TFPList.GetEnumerator: TFPListEnumerator;
  166. begin
  167. Result := TFPListEnumerator.Create(Self);
  168. end;
  169. function TFPList.IndexOf(Item: Pointer): Integer;
  170. Var
  171. C : Integer;
  172. begin
  173. Result:=0;
  174. C:=Count;
  175. while (Result<C) and (Flist^[Result]<>Item) do
  176. Inc(Result);
  177. If Result>=C then
  178. Result:=-1;
  179. end;
  180. function TFPList.IndexOfItem(Item: Pointer; Direction: TDirection): Integer;
  181. Var
  182. C : Integer;
  183. begin
  184. if Direction=fromBeginning then
  185. Result:=IndexOf(Item)
  186. else
  187. begin
  188. Result:=Count-1;
  189. while (Result >=0) and (Flist^[Result]<>Item) do
  190. Result:=Result - 1;
  191. end;
  192. end;
  193. procedure TFPList.Insert(Index: Integer; Item: Pointer);
  194. begin
  195. if (Index < 0) or (Index > FCount )then
  196. Error(SlistIndexError, Index);
  197. iF FCount = FCapacity then Self.Expand;
  198. if Index<FCount then
  199. System.Move(Flist^[Index], Flist^[Index+1], (FCount - Index) * SizeOf(Pointer));
  200. FList^[Index] := Item;
  201. FCount := FCount + 1;
  202. end;
  203. function TFPList.Last: Pointer;
  204. begin
  205. { Wouldn't it be better to return nil if the count is zero ?}
  206. If FCount = 0 then
  207. Result := nil
  208. else
  209. Result := Items[FCount - 1];
  210. end;
  211. procedure TFPList.Move(CurIndex, NewIndex: Integer);
  212. var
  213. Temp : Pointer;
  214. begin
  215. if ((CurIndex < 0) or (CurIndex > Count - 1)) then
  216. Error(SListIndexError, CurIndex);
  217. if ((NewIndex < 0) or (NewIndex > Count -1)) then
  218. Error(SlistIndexError, NewIndex);
  219. Temp := FList^[CurIndex];
  220. if NewIndex > CurIndex then
  221. System.Move(FList^[CurIndex+1], FList^[CurIndex], (NewIndex - CurIndex) * SizeOf(Pointer))
  222. else
  223. System.Move(FList^[NewIndex], FList^[NewIndex+1], (CurIndex - NewIndex) * SizeOf(Pointer));
  224. FList^[NewIndex] := Temp;
  225. end;
  226. function TFPList.Remove(Item: Pointer): Integer;
  227. begin
  228. Result := IndexOf(Item);
  229. If Result <> -1 then
  230. Self.Delete(Result);
  231. end;
  232. procedure TFPList.Pack;
  233. var
  234. NewCount,
  235. i : integer;
  236. pdest,
  237. psrc : PPointer;
  238. begin
  239. NewCount:=0;
  240. psrc:=@FList^[0];
  241. pdest:=psrc;
  242. For I:=0 To FCount-1 Do
  243. begin
  244. if assigned(psrc^) then
  245. begin
  246. pdest^:=psrc^;
  247. inc(pdest);
  248. inc(NewCount);
  249. end;
  250. inc(psrc);
  251. end;
  252. FCount:=NewCount;
  253. end;
  254. // Needed by Sort method.
  255. Procedure QuickSort(FList: PPointerList; L, R : Longint;
  256. Compare: TListSortCompare);
  257. var
  258. I, J : Longint;
  259. P, Q : Pointer;
  260. begin
  261. repeat
  262. I := L;
  263. J := R;
  264. P := FList^[ (L + R) div 2 ];
  265. repeat
  266. while Compare(P, FList^[i]) > 0 do
  267. I := I + 1;
  268. while Compare(P, FList^[J]) < 0 do
  269. J := J - 1;
  270. If I <= J then
  271. begin
  272. Q := FList^[I];
  273. Flist^[I] := FList^[J];
  274. FList^[J] := Q;
  275. I := I + 1;
  276. J := J - 1;
  277. end;
  278. until I > J;
  279. // sort the smaller range recursively
  280. // sort the bigger range via the loop
  281. // Reasons: memory usage is O(log(n)) instead of O(n) and loop is faster than recursion
  282. if J - L < R - I then
  283. begin
  284. if L < J then
  285. QuickSort(FList, L, J, Compare);
  286. L := I;
  287. end
  288. else
  289. begin
  290. if I < R then
  291. QuickSort(FList, I, R, Compare);
  292. R := J;
  293. end;
  294. until L >= R;
  295. end;
  296. procedure TFPList.Sort(Compare: TListSortCompare);
  297. begin
  298. if Not Assigned(FList) or (FCount < 2) then exit;
  299. QuickSort(Flist, 0, FCount-1, Compare);
  300. end;
  301. procedure TFPList.ForEachCall(proc2call:TListCallback;arg:pointer);
  302. var
  303. i : integer;
  304. p : pointer;
  305. begin
  306. For I:=0 To Count-1 Do
  307. begin
  308. p:=FList^[i];
  309. if assigned(p) then
  310. proc2call(p,arg);
  311. end;
  312. end;
  313. procedure TFPList.ForEachCall(proc2call:TListStaticCallback;arg:pointer);
  314. var
  315. i : integer;
  316. p : pointer;
  317. begin
  318. For I:=0 To Count-1 Do
  319. begin
  320. p:=FList^[i];
  321. if assigned(p) then
  322. proc2call(p,arg);
  323. end;
  324. end;
  325. procedure TFPList.CopyMove (aList : TFPList);
  326. var r : integer;
  327. begin
  328. Clear;
  329. for r := 0 to aList.count-1 do
  330. Add (aList[r]);
  331. end;
  332. procedure TFPList.MergeMove (aList : TFPList);
  333. var r : integer;
  334. begin
  335. For r := 0 to aList.count-1 do
  336. if self.indexof(aList[r]) < 0 then
  337. self.Add (aList[r]);
  338. end;
  339. procedure TFPList.DoCopy(ListA, ListB : TFPList);
  340. begin
  341. if assigned (ListB) then
  342. CopyMove (ListB)
  343. else
  344. CopyMove (ListA);
  345. end;
  346. procedure TFPList.DoDestUnique(ListA, ListB : TFPList);
  347. procedure MoveElements (src, dest : TFPList);
  348. var r : integer;
  349. begin
  350. self.clear;
  351. for r := 0 to src.count-1 do
  352. if dest.indexof(src[r]) < 0 then
  353. self.Add (src[r]);
  354. end;
  355. var dest : TFPList;
  356. begin
  357. if assigned (ListB) then
  358. MoveElements (ListB, ListA)
  359. else
  360. try
  361. dest := TFPList.Create;
  362. dest.CopyMove (self);
  363. MoveElements (ListA, dest)
  364. finally
  365. dest.Free;
  366. end;
  367. end;
  368. procedure TFPList.DoAnd(ListA, ListB : TFPList);
  369. var r : integer;
  370. begin
  371. if assigned (ListB) then
  372. begin
  373. self.clear;
  374. for r := 0 to ListA.count-1 do
  375. if ListB.indexOf (ListA[r]) >= 0 then
  376. self.Add (ListA[r]);
  377. end
  378. else
  379. begin
  380. for r := self.Count-1 downto 0 do
  381. if ListA.indexof (Self[r]) < 0 then
  382. self.delete (r);
  383. end;
  384. end;
  385. procedure TFPList.DoSrcUnique(ListA, ListB : TFPList);
  386. var r : integer;
  387. begin
  388. if assigned (ListB) then
  389. begin
  390. self.Clear;
  391. for r := 0 to ListA.Count-1 do
  392. if ListB.indexof (ListA[r]) < 0 then
  393. self.Add (ListA[r]);
  394. end
  395. else
  396. begin
  397. for r := self.count-1 downto 0 do
  398. if ListA.indexof (self[r]) >= 0 then
  399. self.delete (r);
  400. end;
  401. end;
  402. procedure TFPList.DoOr(ListA, ListB : TFPList);
  403. begin
  404. if assigned (ListB) then
  405. begin
  406. CopyMove (ListA);
  407. MergeMove (ListB);
  408. end
  409. else
  410. MergeMove (ListA);
  411. end;
  412. procedure TFPList.DoXOr(ListA, ListB : TFPList);
  413. var r : integer;
  414. l : TFPList;
  415. begin
  416. if assigned (ListB) then
  417. begin
  418. self.Clear;
  419. for r := 0 to ListA.count-1 do
  420. if ListB.indexof (ListA[r]) < 0 then
  421. self.Add (ListA[r]);
  422. for r := 0 to ListB.count-1 do
  423. if ListA.indexof (ListB[r]) < 0 then
  424. self.Add (ListB[r]);
  425. end
  426. else
  427. try
  428. l := TFPList.Create;
  429. l.CopyMove (Self);
  430. for r := self.count-1 downto 0 do
  431. if listA.indexof (self[r]) >= 0 then
  432. self.delete (r);
  433. for r := 0 to ListA.count-1 do
  434. if l.indexof (ListA[r]) < 0 then
  435. self.add (ListA[r]);
  436. finally
  437. l.Free;
  438. end;
  439. end;
  440. procedure TFPList.Assign (ListA: TFPList; AOperator: TListAssignOp=laCopy; ListB: TFPList=nil);
  441. begin
  442. case AOperator of
  443. laCopy : DoCopy (ListA, ListB); // replace dest with src
  444. laSrcUnique : DoSrcUnique (ListA, ListB); // replace dest with src that are not in dest
  445. laAnd : DoAnd (ListA, ListB); // remove from dest that are not in src
  446. laDestUnique : DoDestUnique (ListA, ListB); // remove from dest that are in src
  447. laOr : DoOr (ListA, ListB); // add to dest from src and not in dest
  448. laXOr : DoXOr (ListA, ListB); // add to dest from src and not in dest, remove from dest that are in src
  449. end;
  450. end;
  451. {$else}
  452. { generics based implementation of TFPList follows }
  453. procedure TFPList.Assign(Source: TFPList);
  454. begin
  455. inherited Assign(Source);
  456. end;
  457. type
  458. TFPPtrListSortCompare = function(const Item1, Item2: Pointer): Integer;
  459. procedure TFPList.Sort(Compare: TListSortCompare);
  460. begin
  461. inherited Sort(TFPPtrListSortCompare(Compare));
  462. end;
  463. procedure TFPList.ForEachCall(Proc2call: TListCallback; Arg: Pointer);
  464. var
  465. I: integer;
  466. begin
  467. for I:=0 to Count-1 do
  468. proc2call(InternalItems[I],arg);
  469. end;
  470. procedure TFPList.ForEachCall(Proc2call: TListStaticCallback; Arg: Pointer);
  471. var
  472. I: integer;
  473. begin
  474. for I:=0 to Count-1 do
  475. Proc2call(InternalItems[I], Arg);
  476. end;
  477. {$endif}
  478. {****************************************************************************}
  479. {* TListEnumerator *}
  480. {****************************************************************************}
  481. constructor TListEnumerator.Create(AList: TList);
  482. begin
  483. inherited Create;
  484. FList := AList;
  485. FPosition := -1;
  486. end;
  487. function TListEnumerator.GetCurrent: Pointer;
  488. begin
  489. Result := FList[FPosition];
  490. end;
  491. function TListEnumerator.MoveNext: Boolean;
  492. begin
  493. Inc(FPosition);
  494. Result := FPosition < FList.Count;
  495. end;
  496. {****************************************************************************}
  497. {* TList *}
  498. {****************************************************************************}
  499. { TList = class(TObject)
  500. private
  501. FList: TFPList;
  502. }
  503. function TList.Get(Index: Integer): Pointer;
  504. begin
  505. Result := FList.Get(Index);
  506. end;
  507. procedure TList.Grow;
  508. begin
  509. // Only for compatibility with Delphi. Not needed.
  510. end;
  511. procedure TList.Put(Index: Integer; Item: Pointer);
  512. var p : pointer;
  513. begin
  514. p := get(Index);
  515. FList.Put(Index, Item);
  516. if assigned (p) then
  517. Notify (p, lnDeleted);
  518. if assigned (Item) then
  519. Notify (Item, lnAdded);
  520. end;
  521. function TList.Extract(item: Pointer): Pointer;
  522. var c : integer;
  523. begin
  524. c := FList.Count;
  525. Result := FList.Extract(item);
  526. if c <> FList.Count then
  527. Notify (Result, lnExtracted);
  528. end;
  529. procedure TList.Notify(Ptr: Pointer; Action: TListNotification);
  530. begin
  531. end;
  532. function TList.GetCapacity: integer;
  533. begin
  534. Result := FList.Capacity;
  535. end;
  536. procedure TList.SetCapacity(NewCapacity: Integer);
  537. begin
  538. FList.SetCapacity(NewCapacity);
  539. end;
  540. function TList.GetCount: Integer;
  541. begin
  542. Result := FList.Count;
  543. end;
  544. procedure TList.SetCount(NewCount: Integer);
  545. begin
  546. if NewCount < FList.Count then
  547. while FList.Count > NewCount do
  548. Delete(FList.Count - 1)
  549. else
  550. FList.SetCount(NewCount);
  551. end;
  552. constructor TList.Create;
  553. begin
  554. inherited Create;
  555. FList := TFPList.Create;
  556. end;
  557. destructor TList.Destroy;
  558. begin
  559. If (Flist<>Nil) then
  560. Clear;
  561. FreeAndNil(FList);
  562. inherited Destroy;
  563. end;
  564. function TList.Add(Item: Pointer): Integer;
  565. begin
  566. Result := FList.Add(Item);
  567. if Item <> nil then
  568. Notify(Item, lnAdded);
  569. end;
  570. Procedure TList.AddList(AList : TList);
  571. var
  572. I: Integer;
  573. begin
  574. { this only does FList.AddList(AList.FList), avoiding notifications }
  575. FList.AddList(AList.FList);
  576. { make lnAdded notifications }
  577. for I := 0 to AList.Count - 1 do
  578. if AList[I] <> nil then
  579. Notify(AList[I], lnAdded);
  580. end;
  581. procedure TList.Clear;
  582. begin
  583. If Assigned(Flist) then
  584. While (FList.Count>0) do
  585. Delete(Count-1);
  586. end;
  587. procedure TList.Delete(Index: Integer);
  588. var P : pointer;
  589. begin
  590. P:=FList.Get(Index);
  591. FList.Delete(Index);
  592. if assigned(p) then Notify(p, lnDeleted);
  593. end;
  594. class procedure TList.Error(const Msg: string; Data: PtrInt);
  595. begin
  596. Raise EListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame);
  597. end;
  598. procedure TList.Exchange(Index1, Index2: Integer);
  599. begin
  600. FList.Exchange(Index1, Index2);
  601. end;
  602. function TList.Expand: TList;
  603. begin
  604. FList.Expand;
  605. Result:=Self;
  606. end;
  607. function TList.First: Pointer;
  608. begin
  609. Result := FList.First;
  610. end;
  611. function TList.GetEnumerator: TListEnumerator;
  612. begin
  613. Result := TListEnumerator.Create(Self);
  614. end;
  615. function TList.IndexOf(Item: Pointer): Integer;
  616. begin
  617. Result := FList.IndexOf(Item);
  618. end;
  619. procedure TList.Insert(Index: Integer; Item: Pointer);
  620. begin
  621. FList.Insert(Index, Item);
  622. if Item <> nil then
  623. Notify(Item,lnAdded);
  624. end;
  625. function TList.Last: Pointer;
  626. begin
  627. Result := FList.Last;
  628. end;
  629. procedure TList.Move(CurIndex, NewIndex: Integer);
  630. begin
  631. FList.Move(CurIndex, NewIndex);
  632. end;
  633. function TList.Remove(Item: Pointer): Integer;
  634. begin
  635. Result := IndexOf(Item);
  636. if Result <> -1 then
  637. Self.Delete(Result);
  638. end;
  639. procedure TList.Pack;
  640. begin
  641. FList.Pack;
  642. end;
  643. procedure TList.Sort(Compare: TListSortCompare);
  644. begin
  645. FList.Sort(Compare);
  646. end;
  647. procedure TList.CopyMove (aList : TList);
  648. var r : integer;
  649. begin
  650. Clear;
  651. for r := 0 to aList.count-1 do
  652. Add (aList[r]);
  653. end;
  654. procedure TList.MergeMove (aList : TList);
  655. var r : integer;
  656. begin
  657. For r := 0 to aList.count-1 do
  658. if self.indexof(aList[r]) < 0 then
  659. self.Add (aList[r]);
  660. end;
  661. procedure TList.DoCopy(ListA, ListB : TList);
  662. begin
  663. if assigned (ListB) then
  664. CopyMove (ListB)
  665. else
  666. CopyMove (ListA);
  667. end;
  668. procedure TList.DoDestUnique(ListA, ListB : TList);
  669. procedure MoveElements (src, dest : TList);
  670. var r : integer;
  671. begin
  672. self.clear;
  673. for r := 0 to src.count-1 do
  674. if dest.indexof(src[r]) < 0 then
  675. self.Add (src[r]);
  676. end;
  677. var dest : TList;
  678. begin
  679. if assigned (ListB) then
  680. MoveElements (ListB, ListA)
  681. else
  682. try
  683. dest := TList.Create;
  684. dest.CopyMove (self);
  685. MoveElements (ListA, dest)
  686. finally
  687. dest.Free;
  688. end;
  689. end;
  690. procedure TList.DoAnd(ListA, ListB : TList);
  691. var r : integer;
  692. begin
  693. if assigned (ListB) then
  694. begin
  695. self.clear;
  696. for r := 0 to ListA.count-1 do
  697. if ListB.indexOf (ListA[r]) >= 0 then
  698. self.Add (ListA[r]);
  699. end
  700. else
  701. begin
  702. for r := self.Count-1 downto 0 do
  703. if ListA.indexof (Self[r]) < 0 then
  704. self.delete (r);
  705. end;
  706. end;
  707. procedure TList.DoSrcUnique(ListA, ListB : TList);
  708. var r : integer;
  709. begin
  710. if assigned (ListB) then
  711. begin
  712. self.Clear;
  713. for r := 0 to ListA.Count-1 do
  714. if ListB.indexof (ListA[r]) < 0 then
  715. self.Add (ListA[r]);
  716. end
  717. else
  718. begin
  719. for r := self.count-1 downto 0 do
  720. if ListA.indexof (self[r]) >= 0 then
  721. self.delete (r);
  722. end;
  723. end;
  724. procedure TList.DoOr(ListA, ListB : TList);
  725. begin
  726. if assigned (ListB) then
  727. begin
  728. CopyMove (ListA);
  729. MergeMove (ListB);
  730. end
  731. else
  732. MergeMove (ListA);
  733. end;
  734. procedure TList.DoXOr(ListA, ListB : TList);
  735. var r : integer;
  736. l : TList;
  737. begin
  738. if assigned (ListB) then
  739. begin
  740. self.Clear;
  741. for r := 0 to ListA.count-1 do
  742. if ListB.indexof (ListA[r]) < 0 then
  743. self.Add (ListA[r]);
  744. for r := 0 to ListB.count-1 do
  745. if ListA.indexof (ListB[r]) < 0 then
  746. self.Add (ListB[r]);
  747. end
  748. else
  749. try
  750. l := TList.Create;
  751. l.CopyMove (Self);
  752. for r := self.count-1 downto 0 do
  753. if listA.indexof (self[r]) >= 0 then
  754. self.delete (r);
  755. for r := 0 to ListA.count-1 do
  756. if l.indexof (ListA[r]) < 0 then
  757. self.add (ListA[r]);
  758. finally
  759. l.Free;
  760. end;
  761. end;
  762. procedure TList.Assign (ListA: TList; AOperator: TListAssignOp=laCopy; ListB: TList=nil);
  763. begin
  764. case AOperator of
  765. laCopy : DoCopy (ListA, ListB); // replace dest with src
  766. laSrcUnique : DoSrcUnique (ListA, ListB); // replace dest with src that are not in dest
  767. laAnd : DoAnd (ListA, ListB); // remove from dest that are not in src
  768. laDestUnique : DoDestUnique (ListA, ListB); // remove from dest that are in src
  769. laOr : DoOr (ListA, ListB); // add to dest from src and not in dest
  770. laXOr : DoXOr (ListA, ListB); // add to dest from src and not in dest, remove from dest that are in src
  771. end;
  772. end;
  773. function TList.GetList: PPointerList;
  774. begin
  775. Result := PPointerList(FList.List);
  776. end;
  777. {****************************************************************************}
  778. {* TThreadList *}
  779. {****************************************************************************}
  780. constructor TThreadList.Create;
  781. begin
  782. inherited Create;
  783. FDuplicates:=dupIgnore;
  784. InitCriticalSection(FLock);
  785. FList:=TList.Create;
  786. end;
  787. destructor TThreadList.Destroy;
  788. begin
  789. LockList;
  790. try
  791. FList.Free;
  792. inherited Destroy;
  793. finally
  794. UnlockList;
  795. DoneCriticalSection(FLock);
  796. end;
  797. end;
  798. procedure TThreadList.Add(Item: Pointer);
  799. begin
  800. LockList;
  801. try
  802. if (Duplicates=dupAccept) or
  803. // make sure it's not already in the list
  804. (FList.IndexOf(Item)=-1) then
  805. FList.Add(Item)
  806. else if (Duplicates=dupError) then
  807. FList.Error(SDuplicateItem,PtrUInt(Item));
  808. finally
  809. UnlockList;
  810. end;
  811. end;
  812. procedure TThreadList.Clear;
  813. begin
  814. Locklist;
  815. try
  816. FList.Clear;
  817. finally
  818. UnLockList;
  819. end;
  820. end;
  821. function TThreadList.LockList: TList;
  822. begin
  823. Result:=FList;
  824. System.EnterCriticalSection(FLock);
  825. end;
  826. procedure TThreadList.Remove(Item: Pointer);
  827. begin
  828. LockList;
  829. try
  830. FList.Remove(Item);
  831. finally
  832. UnlockList;
  833. end;
  834. end;
  835. procedure TThreadList.UnlockList;
  836. begin
  837. System.LeaveCriticalSection(FLock);
  838. end;