lists.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. if Assigned(FObservers) then
  532. Case ACtion of
  533. lnAdded : FPONotifyObservers(Self,ooAddItem,Ptr);
  534. lnExtracted : FPONotifyObservers(Self,ooDeleteItem,Ptr);
  535. lnDeleted : FPONotifyObservers(Self,ooDeleteItem,Ptr);
  536. end;
  537. end;
  538. function TList.GetCapacity: integer;
  539. begin
  540. Result := FList.Capacity;
  541. end;
  542. procedure TList.SetCapacity(NewCapacity: Integer);
  543. begin
  544. FList.SetCapacity(NewCapacity);
  545. end;
  546. function TList.GetCount: Integer;
  547. begin
  548. Result := FList.Count;
  549. end;
  550. procedure TList.SetCount(NewCount: Integer);
  551. begin
  552. if NewCount < FList.Count then
  553. while FList.Count > NewCount do
  554. Delete(FList.Count - 1)
  555. else
  556. FList.SetCount(NewCount);
  557. end;
  558. constructor TList.Create;
  559. begin
  560. inherited Create;
  561. FList := TFPList.Create;
  562. end;
  563. destructor TList.Destroy;
  564. begin
  565. if Assigned(Flist) then
  566. Clear;
  567. If Assigned(FObservers) then
  568. begin
  569. FPONotifyObservers(Self,ooFree,Nil);
  570. FreeAndNil(FObservers);
  571. end;
  572. FreeAndNil(FList);
  573. inherited Destroy;
  574. end;
  575. procedure TList.FPOAttachObserver(AObserver: TObject);
  576. Var
  577. I : IFPObserver;
  578. begin
  579. If Not AObserver.GetInterface(SGUIDObserver,I) then
  580. Raise EObserver.CreateFmt(SErrNotObserver,[AObserver.ClassName]);
  581. If not Assigned(FObservers) then
  582. FObservers:=TFPList.Create;
  583. FObservers.Add(AObserver);
  584. end;
  585. procedure TList.FPODetachObserver(AObserver: TObject);
  586. Var
  587. I : IFPObserver;
  588. begin
  589. If Not AObserver.GetInterface(SGUIDObserver,I) then
  590. Raise EObserver.CreateFmt(SErrNotObserver,[AObserver.ClassName]);
  591. If Assigned(FObservers) then
  592. begin
  593. FObservers.Remove(AObserver);
  594. If (FObservers.Count=0) then
  595. FreeAndNil(FObservers);
  596. end;
  597. end;
  598. procedure TList.FPONotifyObservers(ASender: TObject;
  599. AOperation: TFPObservedOperation; Data : Pointer);
  600. Var
  601. O : TObject;
  602. I : Integer;
  603. Obs : IFPObserver;
  604. begin
  605. If Assigned(FObservers) then
  606. For I:=FObservers.Count-1 downto 0 do
  607. begin
  608. O:=TObject(FObservers[i]);
  609. If O.GetInterface(SGUIDObserver,Obs) then
  610. Obs.FPOObservedChanged(Self,AOperation,Data);
  611. end;
  612. end;
  613. function TList.Add(Item: Pointer): Integer;
  614. begin
  615. Result := FList.Add(Item);
  616. if Item <> nil then
  617. Notify(Item, lnAdded);
  618. end;
  619. Procedure TList.AddList(AList : TList);
  620. var
  621. I: Integer;
  622. begin
  623. { this only does FList.AddList(AList.FList), avoiding notifications }
  624. FList.AddList(AList.FList);
  625. { make lnAdded notifications }
  626. for I := 0 to AList.Count - 1 do
  627. if AList[I] <> nil then
  628. Notify(AList[I], lnAdded);
  629. end;
  630. procedure TList.Clear;
  631. begin
  632. While (FList.Count>0) do
  633. Delete(Count-1);
  634. end;
  635. procedure TList.Delete(Index: Integer);
  636. var P : pointer;
  637. begin
  638. P:=FList.Get(Index);
  639. FList.Delete(Index);
  640. if assigned(p) then
  641. Notify(p, lnDeleted);
  642. end;
  643. class procedure TList.Error(const Msg: string; Data: PtrInt);
  644. begin
  645. Raise EListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame);
  646. end;
  647. procedure TList.Exchange(Index1, Index2: Integer);
  648. begin
  649. FList.Exchange(Index1, Index2);
  650. FPONotifyObservers(Self,ooChange,Nil);
  651. end;
  652. function TList.Expand: TList;
  653. begin
  654. FList.Expand;
  655. Result:=Self;
  656. end;
  657. function TList.First: Pointer;
  658. begin
  659. Result := FList.First;
  660. end;
  661. function TList.GetEnumerator: TListEnumerator;
  662. begin
  663. Result := TListEnumerator.Create(Self);
  664. end;
  665. function TList.IndexOf(Item: Pointer): Integer;
  666. begin
  667. Result := FList.IndexOf(Item);
  668. end;
  669. procedure TList.Insert(Index: Integer; Item: Pointer);
  670. begin
  671. FList.Insert(Index, Item);
  672. if Item <> nil then
  673. Notify(Item,lnAdded);
  674. end;
  675. function TList.Last: Pointer;
  676. begin
  677. Result := FList.Last;
  678. end;
  679. procedure TList.Move(CurIndex, NewIndex: Integer);
  680. begin
  681. FList.Move(CurIndex, NewIndex);
  682. end;
  683. function TList.Remove(Item: Pointer): Integer;
  684. begin
  685. Result := IndexOf(Item);
  686. if Result <> -1 then
  687. Self.Delete(Result);
  688. end;
  689. procedure TList.Pack;
  690. begin
  691. FList.Pack;
  692. end;
  693. procedure TList.Sort(Compare: TListSortCompare);
  694. begin
  695. FList.Sort(Compare);
  696. end;
  697. procedure TList.CopyMove (aList : TList);
  698. var r : integer;
  699. begin
  700. Clear;
  701. for r := 0 to aList.count-1 do
  702. Add (aList[r]);
  703. end;
  704. procedure TList.MergeMove (aList : TList);
  705. var r : integer;
  706. begin
  707. For r := 0 to aList.count-1 do
  708. if self.indexof(aList[r]) < 0 then
  709. self.Add (aList[r]);
  710. end;
  711. procedure TList.DoCopy(ListA, ListB : TList);
  712. begin
  713. if assigned (ListB) then
  714. CopyMove (ListB)
  715. else
  716. CopyMove (ListA);
  717. end;
  718. procedure TList.DoDestUnique(ListA, ListB : TList);
  719. procedure MoveElements (src, dest : TList);
  720. var r : integer;
  721. begin
  722. self.clear;
  723. for r := 0 to src.count-1 do
  724. if dest.indexof(src[r]) < 0 then
  725. self.Add (src[r]);
  726. end;
  727. var dest : TList;
  728. begin
  729. if assigned (ListB) then
  730. MoveElements (ListB, ListA)
  731. else
  732. try
  733. dest := TList.Create;
  734. dest.CopyMove (self);
  735. MoveElements (ListA, dest)
  736. finally
  737. dest.Free;
  738. end;
  739. end;
  740. procedure TList.DoAnd(ListA, ListB : TList);
  741. var r : integer;
  742. begin
  743. if assigned (ListB) then
  744. begin
  745. self.clear;
  746. for r := 0 to ListA.count-1 do
  747. if ListB.indexOf (ListA[r]) >= 0 then
  748. self.Add (ListA[r]);
  749. end
  750. else
  751. begin
  752. for r := self.Count-1 downto 0 do
  753. if ListA.indexof (Self[r]) < 0 then
  754. self.delete (r);
  755. end;
  756. end;
  757. procedure TList.DoSrcUnique(ListA, ListB : TList);
  758. var r : integer;
  759. begin
  760. if assigned (ListB) then
  761. begin
  762. self.Clear;
  763. for r := 0 to ListA.Count-1 do
  764. if ListB.indexof (ListA[r]) < 0 then
  765. self.Add (ListA[r]);
  766. end
  767. else
  768. begin
  769. for r := self.count-1 downto 0 do
  770. if ListA.indexof (self[r]) >= 0 then
  771. self.delete (r);
  772. end;
  773. end;
  774. procedure TList.DoOr(ListA, ListB : TList);
  775. begin
  776. if assigned (ListB) then
  777. begin
  778. CopyMove (ListA);
  779. MergeMove (ListB);
  780. end
  781. else
  782. MergeMove (ListA);
  783. end;
  784. procedure TList.DoXOr(ListA, ListB : TList);
  785. var r : integer;
  786. l : TList;
  787. begin
  788. if assigned (ListB) then
  789. begin
  790. self.Clear;
  791. for r := 0 to ListA.count-1 do
  792. if ListB.indexof (ListA[r]) < 0 then
  793. self.Add (ListA[r]);
  794. for r := 0 to ListB.count-1 do
  795. if ListA.indexof (ListB[r]) < 0 then
  796. self.Add (ListB[r]);
  797. end
  798. else
  799. try
  800. l := TList.Create;
  801. l.CopyMove (Self);
  802. for r := self.count-1 downto 0 do
  803. if listA.indexof (self[r]) >= 0 then
  804. self.delete (r);
  805. for r := 0 to ListA.count-1 do
  806. if l.indexof (ListA[r]) < 0 then
  807. self.add (ListA[r]);
  808. finally
  809. l.Free;
  810. end;
  811. end;
  812. procedure TList.Assign (ListA: TList; AOperator: TListAssignOp=laCopy; ListB: TList=nil);
  813. begin
  814. case AOperator of
  815. laCopy : DoCopy (ListA, ListB); // replace dest with src
  816. laSrcUnique : DoSrcUnique (ListA, ListB); // replace dest with src that are not in dest
  817. laAnd : DoAnd (ListA, ListB); // remove from dest that are not in src
  818. laDestUnique : DoDestUnique (ListA, ListB); // remove from dest that are in src
  819. laOr : DoOr (ListA, ListB); // add to dest from src and not in dest
  820. laXOr : DoXOr (ListA, ListB); // add to dest from src and not in dest, remove from dest that are in src
  821. end;
  822. end;
  823. function TList.GetList: PPointerList;
  824. begin
  825. Result := PPointerList(FList.List);
  826. end;
  827. {****************************************************************************}
  828. {* TThreadList *}
  829. {****************************************************************************}
  830. constructor TThreadList.Create;
  831. begin
  832. inherited Create;
  833. FDuplicates:=dupIgnore;
  834. InitCriticalSection(FLock);
  835. FList:=TList.Create;
  836. end;
  837. destructor TThreadList.Destroy;
  838. begin
  839. LockList;
  840. try
  841. FList.Free;
  842. inherited Destroy;
  843. finally
  844. UnlockList;
  845. DoneCriticalSection(FLock);
  846. end;
  847. end;
  848. procedure TThreadList.Add(Item: Pointer);
  849. begin
  850. LockList;
  851. try
  852. if (Duplicates=dupAccept) or
  853. // make sure it's not already in the list
  854. (FList.IndexOf(Item)=-1) then
  855. FList.Add(Item)
  856. else if (Duplicates=dupError) then
  857. FList.Error(SDuplicateItem,PtrUInt(Item));
  858. finally
  859. UnlockList;
  860. end;
  861. end;
  862. procedure TThreadList.Clear;
  863. begin
  864. Locklist;
  865. try
  866. FList.Clear;
  867. finally
  868. UnLockList;
  869. end;
  870. end;
  871. function TThreadList.LockList: TList;
  872. begin
  873. Result:=FList;
  874. System.EnterCriticalSection(FLock);
  875. end;
  876. procedure TThreadList.Remove(Item: Pointer);
  877. begin
  878. LockList;
  879. try
  880. FList.Remove(Item);
  881. finally
  882. UnlockList;
  883. end;
  884. end;
  885. procedure TThreadList.UnlockList;
  886. begin
  887. System.LeaveCriticalSection(FLock);
  888. end;