lists.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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;
  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. begin
  171. Result := 0;
  172. while(Result < FCount) and (Flist^[Result] <> Item) do Result := Result + 1;
  173. If Result = FCount then Result := -1;
  174. end;
  175. procedure TFPList.Insert(Index: Integer; Item: Pointer);
  176. begin
  177. if (Index < 0) or (Index > FCount )then
  178. Error(SlistIndexError, Index);
  179. iF FCount = FCapacity then Self.Expand;
  180. if Index<FCount then
  181. System.Move(Flist^[Index], Flist^[Index+1], (FCount - Index) * SizeOf(Pointer));
  182. FList^[Index] := Item;
  183. FCount := FCount + 1;
  184. end;
  185. function TFPList.Last: Pointer;
  186. begin
  187. { Wouldn't it be better to return nil if the count is zero ?}
  188. If FCount = 0 then
  189. Result := nil
  190. else
  191. Result := Items[FCount - 1];
  192. end;
  193. procedure TFPList.Move(CurIndex, NewIndex: Integer);
  194. var
  195. Temp : Pointer;
  196. begin
  197. if ((CurIndex < 0) or (CurIndex > Count - 1)) then
  198. Error(SListIndexError, CurIndex);
  199. if ((NewIndex < 0) or (NewIndex > Count -1)) then
  200. Error(SlistIndexError, NewIndex);
  201. Temp := FList^[CurIndex];
  202. FList^[CurIndex] := nil;
  203. Self.Delete(CurIndex);
  204. Self.Insert(NewIndex, nil);
  205. FList^[NewIndex] := Temp;
  206. end;
  207. function TFPList.Remove(Item: Pointer): Integer;
  208. begin
  209. Result := IndexOf(Item);
  210. If Result <> -1 then
  211. Self.Delete(Result);
  212. end;
  213. procedure TFPList.Pack;
  214. var
  215. NewCount,
  216. i : integer;
  217. pdest,
  218. psrc : PPointer;
  219. begin
  220. NewCount:=0;
  221. psrc:=@FList^[0];
  222. pdest:=psrc;
  223. For I:=0 To FCount-1 Do
  224. begin
  225. if assigned(psrc^) then
  226. begin
  227. pdest^:=psrc^;
  228. inc(pdest);
  229. inc(NewCount);
  230. end;
  231. inc(psrc);
  232. end;
  233. FCount:=NewCount;
  234. end;
  235. // Needed by Sort method.
  236. Procedure QuickSort(FList: PPointerList; L, R : Longint;
  237. Compare: TListSortCompare);
  238. var
  239. I, J : Longint;
  240. P, Q : Pointer;
  241. begin
  242. repeat
  243. I := L;
  244. J := R;
  245. P := FList^[ (L + R) div 2 ];
  246. repeat
  247. while Compare(P, FList^[i]) > 0 do
  248. I := I + 1;
  249. while Compare(P, FList^[J]) < 0 do
  250. J := J - 1;
  251. If I <= J then
  252. begin
  253. Q := FList^[I];
  254. Flist^[I] := FList^[J];
  255. FList^[J] := Q;
  256. I := I + 1;
  257. J := J - 1;
  258. end;
  259. until I > J;
  260. if L < J then
  261. QuickSort(FList, L, J, Compare);
  262. L := I;
  263. until I >= R;
  264. end;
  265. procedure TFPList.Sort(Compare: TListSortCompare);
  266. begin
  267. if Not Assigned(FList) or (FCount < 2) then exit;
  268. QuickSort(Flist, 0, FCount-1, Compare);
  269. end;
  270. procedure TFPList.ForEachCall(proc2call:TListCallback;arg:pointer);
  271. var
  272. i : integer;
  273. p : pointer;
  274. begin
  275. For I:=0 To Count-1 Do
  276. begin
  277. p:=FList^[i];
  278. if assigned(p) then
  279. proc2call(p,arg);
  280. end;
  281. end;
  282. procedure TFPList.ForEachCall(proc2call:TListStaticCallback;arg:pointer);
  283. var
  284. i : integer;
  285. p : pointer;
  286. begin
  287. For I:=0 To Count-1 Do
  288. begin
  289. p:=FList^[i];
  290. if assigned(p) then
  291. proc2call(p,arg);
  292. end;
  293. end;
  294. procedure TFPList.CopyMove (aList : TFPList);
  295. var r : integer;
  296. begin
  297. Clear;
  298. for r := 0 to aList.count-1 do
  299. Add (aList[r]);
  300. end;
  301. procedure TFPList.MergeMove (aList : TFPList);
  302. var r : integer;
  303. begin
  304. For r := 0 to aList.count-1 do
  305. if self.indexof(aList[r]) < 0 then
  306. self.Add (aList[r]);
  307. end;
  308. procedure TFPList.DoCopy(ListA, ListB : TFPList);
  309. begin
  310. if assigned (ListB) then
  311. CopyMove (ListB)
  312. else
  313. CopyMove (ListA);
  314. end;
  315. procedure TFPList.DoDestUnique(ListA, ListB : TFPList);
  316. procedure MoveElements (src, dest : TFPList);
  317. var r : integer;
  318. begin
  319. self.clear;
  320. for r := 0 to src.count-1 do
  321. if dest.indexof(src[r]) < 0 then
  322. self.Add (src[r]);
  323. end;
  324. var dest : TFPList;
  325. begin
  326. if assigned (ListB) then
  327. MoveElements (ListB, ListA)
  328. else
  329. try
  330. dest := TFPList.Create;
  331. dest.CopyMove (self);
  332. MoveElements (ListA, dest)
  333. finally
  334. dest.Free;
  335. end;
  336. end;
  337. procedure TFPList.DoAnd(ListA, ListB : TFPList);
  338. var r : integer;
  339. begin
  340. if assigned (ListB) then
  341. begin
  342. self.clear;
  343. for r := 0 to ListA.count-1 do
  344. if ListB.indexOf (ListA[r]) >= 0 then
  345. self.Add (ListA[r]);
  346. end
  347. else
  348. begin
  349. for r := self.Count-1 downto 0 do
  350. if ListA.indexof (Self[r]) < 0 then
  351. self.delete (r);
  352. end;
  353. end;
  354. procedure TFPList.DoSrcUnique(ListA, ListB : TFPList);
  355. var r : integer;
  356. begin
  357. if assigned (ListB) then
  358. begin
  359. self.Clear;
  360. for r := 0 to ListA.Count-1 do
  361. if ListB.indexof (ListA[r]) < 0 then
  362. self.Add (ListA[r]);
  363. end
  364. else
  365. begin
  366. for r := self.count-1 downto 0 do
  367. if ListA.indexof (self[r]) >= 0 then
  368. self.delete (r);
  369. end;
  370. end;
  371. procedure TFPList.DoOr(ListA, ListB : TFPList);
  372. begin
  373. if assigned (ListB) then
  374. begin
  375. CopyMove (ListA);
  376. MergeMove (ListB);
  377. end
  378. else
  379. MergeMove (ListA);
  380. end;
  381. procedure TFPList.DoXOr(ListA, ListB : TFPList);
  382. var r : integer;
  383. l : TFPList;
  384. begin
  385. if assigned (ListB) then
  386. begin
  387. self.Clear;
  388. for r := 0 to ListA.count-1 do
  389. if ListB.indexof (ListA[r]) < 0 then
  390. self.Add (ListA[r]);
  391. for r := 0 to ListB.count-1 do
  392. if ListA.indexof (ListB[r]) < 0 then
  393. self.Add (ListB[r]);
  394. end
  395. else
  396. try
  397. l := TFPList.Create;
  398. l.CopyMove (Self);
  399. for r := self.count-1 downto 0 do
  400. if listA.indexof (self[r]) >= 0 then
  401. self.delete (r);
  402. for r := 0 to ListA.count-1 do
  403. if l.indexof (ListA[r]) < 0 then
  404. self.add (ListA[r]);
  405. finally
  406. l.Free;
  407. end;
  408. end;
  409. procedure TFPList.Assign (ListA: TFPList; AOperator: TListAssignOp=laCopy; ListB: TFPList=nil);
  410. begin
  411. case AOperator of
  412. laCopy : DoCopy (ListA, ListB); // replace dest with src
  413. laSrcUnique : DoSrcUnique (ListA, ListB); // replace dest with src that are not in dest
  414. laAnd : DoAnd (ListA, ListB); // remove from dest that are not in src
  415. laDestUnique : DoDestUnique (ListA, ListB); // remove from dest that are in src
  416. laOr : DoOr (ListA, ListB); // add to dest from src and not in dest
  417. laXOr : DoXOr (ListA, ListB); // add to dest from src and not in dest, remove from dest that are in src
  418. end;
  419. end;
  420. {$else}
  421. { generics based implementation of TFPList follows }
  422. procedure TFPList.Assign(Source: TFPList);
  423. begin
  424. inherited Assign(Source);
  425. end;
  426. type
  427. TFPPtrListSortCompare = function(const Item1, Item2: Pointer): Integer;
  428. procedure TFPList.Sort(Compare: TListSortCompare);
  429. begin
  430. inherited Sort(TFPPtrListSortCompare(Compare));
  431. end;
  432. procedure TFPList.ForEachCall(Proc2call: TListCallback; Arg: Pointer);
  433. var
  434. I: integer;
  435. begin
  436. for I:=0 to Count-1 do
  437. proc2call(InternalItems[I],arg);
  438. end;
  439. procedure TFPList.ForEachCall(Proc2call: TListStaticCallback; Arg: Pointer);
  440. var
  441. I: integer;
  442. begin
  443. for I:=0 to Count-1 do
  444. Proc2call(InternalItems[I], Arg);
  445. end;
  446. {$endif}
  447. {****************************************************************************}
  448. {* TListEnumerator *}
  449. {****************************************************************************}
  450. constructor TListEnumerator.Create(AList: TList);
  451. begin
  452. inherited Create;
  453. FList := AList;
  454. FPosition := -1;
  455. end;
  456. function TListEnumerator.GetCurrent: Pointer;
  457. begin
  458. Result := FList[FPosition];
  459. end;
  460. function TListEnumerator.MoveNext: Boolean;
  461. begin
  462. Inc(FPosition);
  463. Result := FPosition < FList.Count;
  464. end;
  465. {****************************************************************************}
  466. {* TList *}
  467. {****************************************************************************}
  468. { TList = class(TObject)
  469. private
  470. FList: TFPList;
  471. }
  472. function TList.Get(Index: Integer): Pointer;
  473. begin
  474. Result := FList.Get(Index);
  475. end;
  476. procedure TList.Grow;
  477. begin
  478. // Only for compatibility with Delphi. Not needed.
  479. end;
  480. procedure TList.Put(Index: Integer; Item: Pointer);
  481. var p : pointer;
  482. begin
  483. p := get(Index);
  484. FList.Put(Index, Item);
  485. if assigned (p) then
  486. Notify (p, lnDeleted);
  487. if assigned (Item) then
  488. Notify (Item, lnAdded);
  489. end;
  490. function TList.Extract(item: Pointer): Pointer;
  491. var c : integer;
  492. begin
  493. c := FList.Count;
  494. Result := FList.Extract(item);
  495. if c <> FList.Count then
  496. Notify (Result, lnExtracted);
  497. end;
  498. procedure TList.Notify(Ptr: Pointer; Action: TListNotification);
  499. begin
  500. end;
  501. function TList.GetCapacity: integer;
  502. begin
  503. Result := FList.Capacity;
  504. end;
  505. procedure TList.SetCapacity(NewCapacity: Integer);
  506. begin
  507. FList.SetCapacity(NewCapacity);
  508. end;
  509. function TList.GetCount: Integer;
  510. begin
  511. Result := FList.Count;
  512. end;
  513. procedure TList.SetCount(NewCount: Integer);
  514. begin
  515. if NewCount < FList.Count then
  516. while FList.Count > NewCount do
  517. Delete(FList.Count - 1)
  518. else
  519. FList.SetCount(NewCount);
  520. end;
  521. constructor TList.Create;
  522. begin
  523. inherited Create;
  524. FList := TFPList.Create;
  525. end;
  526. destructor TList.Destroy;
  527. begin
  528. If (Flist<>Nil) then
  529. Clear;
  530. FreeAndNil(FList);
  531. inherited Destroy;
  532. end;
  533. function TList.Add(Item: Pointer): Integer;
  534. begin
  535. Result := FList.Add(Item);
  536. if Item <> nil then
  537. Notify(Item, lnAdded);
  538. end;
  539. Procedure TList.AddList(AList : TList);
  540. begin
  541. FList.AddList(AList.FList);
  542. end;
  543. procedure TList.Clear;
  544. begin
  545. If Assigned(Flist) then
  546. While (FList.Count>0) do
  547. Delete(Count-1);
  548. end;
  549. procedure TList.Delete(Index: Integer);
  550. var P : pointer;
  551. begin
  552. P:=FList.Get(Index);
  553. FList.Delete(Index);
  554. if assigned(p) then Notify(p, lnDeleted);
  555. end;
  556. class procedure TList.Error(const Msg: string; Data: PtrInt);
  557. begin
  558. Raise EListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame);
  559. end;
  560. procedure TList.Exchange(Index1, Index2: Integer);
  561. begin
  562. FList.Exchange(Index1, Index2);
  563. end;
  564. function TList.Expand: TList;
  565. begin
  566. FList.Expand;
  567. Result:=Self;
  568. end;
  569. function TList.First: Pointer;
  570. begin
  571. Result := FList.First;
  572. end;
  573. function TList.GetEnumerator: TListEnumerator;
  574. begin
  575. Result := TListEnumerator.Create(Self);
  576. end;
  577. function TList.IndexOf(Item: Pointer): Integer;
  578. begin
  579. Result := FList.IndexOf(Item);
  580. end;
  581. procedure TList.Insert(Index: Integer; Item: Pointer);
  582. begin
  583. FList.Insert(Index, Item);
  584. if Item <> nil then
  585. Notify(Item,lnAdded);
  586. end;
  587. function TList.Last: Pointer;
  588. begin
  589. Result := FList.Last;
  590. end;
  591. procedure TList.Move(CurIndex, NewIndex: Integer);
  592. begin
  593. FList.Move(CurIndex, NewIndex);
  594. end;
  595. function TList.Remove(Item: Pointer): Integer;
  596. begin
  597. Result := IndexOf(Item);
  598. if Result <> -1 then
  599. Self.Delete(Result);
  600. end;
  601. procedure TList.Pack;
  602. begin
  603. FList.Pack;
  604. end;
  605. procedure TList.Sort(Compare: TListSortCompare);
  606. begin
  607. FList.Sort(Compare);
  608. end;
  609. procedure TList.CopyMove (aList : TList);
  610. var r : integer;
  611. begin
  612. Clear;
  613. for r := 0 to aList.count-1 do
  614. Add (aList[r]);
  615. end;
  616. procedure TList.MergeMove (aList : TList);
  617. var r : integer;
  618. begin
  619. For r := 0 to aList.count-1 do
  620. if self.indexof(aList[r]) < 0 then
  621. self.Add (aList[r]);
  622. end;
  623. procedure TList.DoCopy(ListA, ListB : TList);
  624. begin
  625. if assigned (ListB) then
  626. CopyMove (ListB)
  627. else
  628. CopyMove (ListA);
  629. end;
  630. procedure TList.DoDestUnique(ListA, ListB : TList);
  631. procedure MoveElements (src, dest : TList);
  632. var r : integer;
  633. begin
  634. self.clear;
  635. for r := 0 to src.count-1 do
  636. if dest.indexof(src[r]) < 0 then
  637. self.Add (src[r]);
  638. end;
  639. var dest : TList;
  640. begin
  641. if assigned (ListB) then
  642. MoveElements (ListB, ListA)
  643. else
  644. try
  645. dest := TList.Create;
  646. dest.CopyMove (self);
  647. MoveElements (ListA, dest)
  648. finally
  649. dest.Free;
  650. end;
  651. end;
  652. procedure TList.DoAnd(ListA, ListB : TList);
  653. var r : integer;
  654. begin
  655. if assigned (ListB) then
  656. begin
  657. self.clear;
  658. for r := 0 to ListA.count-1 do
  659. if ListB.indexOf (ListA[r]) >= 0 then
  660. self.Add (ListA[r]);
  661. end
  662. else
  663. begin
  664. for r := self.Count-1 downto 0 do
  665. if ListA.indexof (Self[r]) < 0 then
  666. self.delete (r);
  667. end;
  668. end;
  669. procedure TList.DoSrcUnique(ListA, ListB : TList);
  670. var r : integer;
  671. begin
  672. if assigned (ListB) then
  673. begin
  674. self.Clear;
  675. for r := 0 to ListA.Count-1 do
  676. if ListB.indexof (ListA[r]) < 0 then
  677. self.Add (ListA[r]);
  678. end
  679. else
  680. begin
  681. for r := self.count-1 downto 0 do
  682. if ListA.indexof (self[r]) >= 0 then
  683. self.delete (r);
  684. end;
  685. end;
  686. procedure TList.DoOr(ListA, ListB : TList);
  687. begin
  688. if assigned (ListB) then
  689. begin
  690. CopyMove (ListA);
  691. MergeMove (ListB);
  692. end
  693. else
  694. MergeMove (ListA);
  695. end;
  696. procedure TList.DoXOr(ListA, ListB : TList);
  697. var r : integer;
  698. l : TList;
  699. begin
  700. if assigned (ListB) then
  701. begin
  702. self.Clear;
  703. for r := 0 to ListA.count-1 do
  704. if ListB.indexof (ListA[r]) < 0 then
  705. self.Add (ListA[r]);
  706. for r := 0 to ListB.count-1 do
  707. if ListA.indexof (ListB[r]) < 0 then
  708. self.Add (ListB[r]);
  709. end
  710. else
  711. try
  712. l := TList.Create;
  713. l.CopyMove (Self);
  714. for r := self.count-1 downto 0 do
  715. if listA.indexof (self[r]) >= 0 then
  716. self.delete (r);
  717. for r := 0 to ListA.count-1 do
  718. if l.indexof (ListA[r]) < 0 then
  719. self.add (ListA[r]);
  720. finally
  721. l.Free;
  722. end;
  723. end;
  724. procedure TList.Assign (ListA: TList; AOperator: TListAssignOp=laCopy; ListB: TList=nil);
  725. begin
  726. case AOperator of
  727. laCopy : DoCopy (ListA, ListB); // replace dest with src
  728. laSrcUnique : DoSrcUnique (ListA, ListB); // replace dest with src that are not in dest
  729. laAnd : DoAnd (ListA, ListB); // remove from dest that are not in src
  730. laDestUnique : DoDestUnique (ListA, ListB); // remove from dest that are in src
  731. laOr : DoOr (ListA, ListB); // add to dest from src and not in dest
  732. laXOr : DoXOr (ListA, ListB); // add to dest from src and not in dest, remove from dest that are in src
  733. end;
  734. end;
  735. function TList.GetList: PPointerList;
  736. begin
  737. Result := PPointerList(FList.List);
  738. end;
  739. {****************************************************************************}
  740. {* TThreadList *}
  741. {****************************************************************************}
  742. constructor TThreadList.Create;
  743. begin
  744. inherited Create;
  745. FDuplicates:=dupIgnore;
  746. InitCriticalSection(FLock);
  747. FList:=TList.Create;
  748. end;
  749. destructor TThreadList.Destroy;
  750. begin
  751. LockList;
  752. try
  753. FList.Free;
  754. inherited Destroy;
  755. finally
  756. UnlockList;
  757. DoneCriticalSection(FLock);
  758. end;
  759. end;
  760. procedure TThreadList.Add(Item: Pointer);
  761. begin
  762. LockList;
  763. try
  764. if (Duplicates=dupAccept) or
  765. // make sure it's not already in the list
  766. (FList.IndexOf(Item)=-1) then
  767. FList.Add(Item)
  768. else if (Duplicates=dupError) then
  769. FList.Error(SDuplicateItem,PtrUInt(Item));
  770. finally
  771. UnlockList;
  772. end;
  773. end;
  774. procedure TThreadList.Clear;
  775. begin
  776. Locklist;
  777. try
  778. FList.Clear;
  779. finally
  780. UnLockList;
  781. end;
  782. end;
  783. function TThreadList.LockList: TList;
  784. begin
  785. Result:=FList;
  786. System.EnterCriticalSection(FLock);
  787. end;
  788. procedure TThreadList.Remove(Item: Pointer);
  789. begin
  790. LockList;
  791. try
  792. FList.Remove(Item);
  793. finally
  794. UnlockList;
  795. end;
  796. end;
  797. procedure TThreadList.UnlockList;
  798. begin
  799. System.LeaveCriticalSection(FLock);
  800. end;