fgl.pp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2006 by Micha Nelissen
  4. member of the Free Pascal development team
  5. It contains the Free Pascal generics library
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$mode objfpc}
  13. { be aware, this unit is a prototype and subject to be changed heavily }
  14. unit fgl;
  15. interface
  16. uses
  17. types, sysutils;
  18. {$IF defined(VER2_4)}
  19. {$DEFINE OldSyntax}
  20. {$IFEND}
  21. const
  22. MaxListSize = Maxint div 16;
  23. type
  24. EListError = class(Exception);
  25. TFPSList = class;
  26. TFPSListCompareFunc = function(Key1, Key2: Pointer): Integer of object;
  27. TFPSList = class(TObject)
  28. protected
  29. FList: PByte;
  30. FCount: Integer;
  31. FCapacity: Integer; { list is one longer than capacity, for temp }
  32. FItemSize: Integer;
  33. procedure CopyItem(Src, Dest: Pointer); virtual;
  34. procedure Deref(Item: Pointer); virtual; overload;
  35. procedure Deref(FromIndex, ToIndex: Integer); overload;
  36. function Get(Index: Integer): Pointer;
  37. procedure InternalExchange(Index1, Index2: Integer);
  38. function InternalGet(Index: Integer): Pointer; {$ifdef CLASSESINLINE} inline; {$endif}
  39. procedure InternalPut(Index: Integer; NewItem: Pointer);
  40. procedure Put(Index: Integer; Item: Pointer);
  41. procedure QuickSort(L, R: Integer; Compare: TFPSListCompareFunc);
  42. procedure SetCapacity(NewCapacity: Integer);
  43. procedure SetCount(NewCount: Integer);
  44. procedure RaiseIndexError(Index : Integer);
  45. property InternalItems[Index: Integer]: Pointer read InternalGet write InternalPut;
  46. public
  47. constructor Create(AItemSize: Integer = sizeof(Pointer));
  48. destructor Destroy; override;
  49. function Add(Item: Pointer): Integer;
  50. procedure Clear;
  51. procedure Delete(Index: Integer);
  52. class procedure Error(const Msg: string; Data: PtrInt);
  53. procedure Exchange(Index1, Index2: Integer);
  54. function Expand: TFPSList;
  55. function Extract(Item: Pointer): Pointer;
  56. function First: Pointer;
  57. function IndexOf(Item: Pointer): Integer;
  58. procedure Insert(Index: Integer; Item: Pointer);
  59. function Insert(Index: Integer): Pointer;
  60. function Last: Pointer;
  61. procedure Move(CurIndex, NewIndex: Integer);
  62. procedure Assign(Obj: TFPSList);
  63. function Remove(Item: Pointer): Integer;
  64. procedure Pack;
  65. procedure Sort(Compare: TFPSListCompareFunc);
  66. property Capacity: Integer read FCapacity write SetCapacity;
  67. property Count: Integer read FCount write SetCount;
  68. property Items[Index: Integer]: Pointer read Get write Put; default;
  69. property ItemSize: Integer read FItemSize;
  70. property List: PByte read FList;
  71. end;
  72. {$ifndef VER2_0}
  73. const
  74. MaxGListSize = MaxInt div 1024;
  75. type
  76. generic TFPGListEnumerator<T> = class(TObject)
  77. protected
  78. FList: TFPSList;
  79. FPosition: Integer;
  80. function GetCurrent: T;
  81. public
  82. constructor Create(AList: TFPSList);
  83. function MoveNext: Boolean;
  84. property Current: T read GetCurrent;
  85. end;
  86. generic TFPGList<T> = class(TFPSList)
  87. public
  88. type
  89. TCompareFunc = function(const Item1, Item2: T): Integer;
  90. TTypeList = array[0..MaxGListSize] of T;
  91. PTypeList = ^TTypeList;
  92. PT = ^T;
  93. TFPGListEnumeratorSpec = specialize TFPGListEnumerator<T>;
  94. {$ifndef OldSyntax}protected var{$else}var protected{$endif}
  95. FOnCompare: TCompareFunc;
  96. procedure CopyItem(Src, Dest: Pointer); override;
  97. procedure Deref(Item: Pointer); override;
  98. function Get(Index: Integer): T; {$ifdef CLASSESINLINE} inline; {$endif}
  99. function GetList: PTypeList; {$ifdef CLASSESINLINE} inline; {$endif}
  100. function ItemPtrCompare(Item1, Item2: Pointer): Integer;
  101. procedure Put(Index: Integer; const Item: T); {$ifdef CLASSESINLINE} inline; {$endif}
  102. public
  103. constructor Create;
  104. function Add(const Item: T): Integer; {$ifdef CLASSESINLINE} inline; {$endif}
  105. function Extract(const Item: T): T; {$ifdef CLASSESINLINE} inline; {$endif}
  106. function First: T; {$ifdef CLASSESINLINE} inline; {$endif}
  107. function GetEnumerator: TFPGListEnumeratorSpec; {$ifdef CLASSESINLINE} inline; {$endif}
  108. function IndexOf(const Item: T): Integer;
  109. procedure Insert(Index: Integer; const Item: T); {$ifdef CLASSESINLINE} inline; {$endif}
  110. function Last: T; {$ifdef CLASSESINLINE} inline; {$endif}
  111. {$info FIXME: bug #10479: implement TFPGList<T>.Assign(TFPGList) to work somehow}
  112. {procedure Assign(Source: TFPGList);}
  113. function Remove(const Item: T): Integer; {$ifdef CLASSESINLINE} inline; {$endif}
  114. procedure Sort(Compare: TCompareFunc);
  115. property Items[Index: Integer]: T read Get write Put; default;
  116. property List: PTypeList read GetList;
  117. end;
  118. generic TFPGObjectList<T> = class(TFPSList)
  119. public
  120. type
  121. TCompareFunc = function(const Item1, Item2: T): Integer;
  122. TTypeList = array[0..MaxGListSize] of T;
  123. PTypeList = ^TTypeList;
  124. PT = ^T;
  125. {$ifndef OldSyntax}protected var{$else}var protected{$endif}
  126. FOnCompare: TCompareFunc;
  127. FFreeObjects: Boolean;
  128. procedure CopyItem(Src, Dest: Pointer); override;
  129. procedure Deref(Item: Pointer); override;
  130. function Get(Index: Integer): T; {$ifdef CLASSESINLINE} inline; {$endif}
  131. function GetList: PTypeList; {$ifdef CLASSESINLINE} inline; {$endif}
  132. function ItemPtrCompare(Item1, Item2: Pointer): Integer;
  133. procedure Put(Index: Integer; const Item: T); {$ifdef CLASSESINLINE} inline; {$endif}
  134. public
  135. constructor Create(FreeObjects: Boolean = True);
  136. function Add(const Item: T): Integer; {$ifdef CLASSESINLINE} inline; {$endif}
  137. function Extract(const Item: T): T; {$ifdef CLASSESINLINE} inline; {$endif}
  138. function First: T; {$ifdef CLASSESINLINE} inline; {$endif}
  139. function IndexOf(const Item: T): Integer;
  140. procedure Insert(Index: Integer; const Item: T); {$ifdef CLASSESINLINE} inline; {$endif}
  141. function Last: T; {$ifdef CLASSESINLINE} inline; {$endif}
  142. {$info FIXME: bug #10479: implement TFPGObjectList<T>.Assign(TFPGList) to work somehow}
  143. {procedure Assign(Source: TFPGList);}
  144. function Remove(const Item: T): Integer; {$ifdef CLASSESINLINE} inline; {$endif}
  145. procedure Sort(Compare: TCompareFunc);
  146. property Items[Index: Integer]: T read Get write Put; default;
  147. property List: PTypeList read GetList;
  148. property FreeObjects: Boolean read FFreeObjects write FFreeObjects;
  149. end;
  150. generic TFPGInterfacedObjectList<T> = class(TFPSList)
  151. public
  152. type
  153. TCompareFunc = function(const Item1, Item2: T): Integer;
  154. TTypeList = array[0..MaxGListSize] of T;
  155. PTypeList = ^TTypeList;
  156. PT = ^T;
  157. {$ifndef OldSyntax}protected var{$else}var protected{$endif}
  158. FOnCompare: TCompareFunc;
  159. procedure CopyItem(Src, Dest: Pointer); override;
  160. procedure Deref(Item: Pointer); override;
  161. function Get(Index: Integer): T; {$ifdef CLASSESINLINE} inline; {$endif}
  162. function GetList: PTypeList; {$ifdef CLASSESINLINE} inline; {$endif}
  163. function ItemPtrCompare(Item1, Item2: Pointer): Integer;
  164. procedure Put(Index: Integer; const Item: T); {$ifdef CLASSESINLINE} inline; {$endif}
  165. public
  166. constructor Create;
  167. function Add(const Item: T): Integer; {$ifdef CLASSESINLINE} inline; {$endif}
  168. function Extract(const Item: T): T; {$ifdef CLASSESINLINE} inline; {$endif}
  169. function First: T; {$ifdef CLASSESINLINE} inline; {$endif}
  170. function IndexOf(const Item: T): Integer;
  171. procedure Insert(Index: Integer; const Item: T); {$ifdef CLASSESINLINE} inline; {$endif}
  172. function Last: T; {$ifdef CLASSESINLINE} inline; {$endif}
  173. {$info FIXME: bug #10479: implement TFPGInterfacedObjectList<T>.Assign(TFPGList) to work somehow}
  174. {procedure Assign(Source: TFPGList);}
  175. function Remove(const Item: T): Integer; {$ifdef CLASSESINLINE} inline; {$endif}
  176. procedure Sort(Compare: TCompareFunc);
  177. property Items[Index: Integer]: T read Get write Put; default;
  178. property List: PTypeList read GetList;
  179. end;
  180. {$endif}
  181. TFPSMap = class(TFPSList)
  182. private
  183. FKeySize: Integer;
  184. FDataSize: Integer;
  185. FDuplicates: TDuplicates;
  186. FSorted: Boolean;
  187. FOnKeyPtrCompare: TFPSListCompareFunc;
  188. FOnDataPtrCompare: TFPSListCompareFunc;
  189. procedure SetSorted(Value: Boolean);
  190. protected
  191. function BinaryCompareKey(Key1, Key2: Pointer): Integer;
  192. function BinaryCompareData(Data1, Data2: Pointer): Integer;
  193. procedure SetOnKeyPtrCompare(Proc: TFPSListCompareFunc);
  194. procedure SetOnDataPtrCompare(Proc: TFPSListCompareFunc);
  195. procedure InitOnPtrCompare; virtual;
  196. procedure CopyKey(Src, Dest: Pointer); virtual;
  197. procedure CopyData(Src, Dest: Pointer); virtual;
  198. function GetKey(Index: Integer): Pointer;
  199. function GetKeyData(AKey: Pointer): Pointer;
  200. function GetData(Index: Integer): Pointer;
  201. function LinearIndexOf(AKey: Pointer): Integer;
  202. procedure PutKey(Index: Integer; AKey: Pointer);
  203. procedure PutKeyData(AKey: Pointer; NewData: Pointer);
  204. procedure PutData(Index: Integer; AData: Pointer);
  205. public
  206. constructor Create(AKeySize: Integer = sizeof(Pointer);
  207. ADataSize: integer = sizeof(Pointer));
  208. function Add(AKey, AData: Pointer): Integer;
  209. function Add(AKey: Pointer): Integer;
  210. function Find(AKey: Pointer; out Index: Integer): Boolean;
  211. function IndexOf(AKey: Pointer): Integer;
  212. function IndexOfData(AData: Pointer): Integer;
  213. function Insert(Index: Integer): Pointer;
  214. procedure Insert(Index: Integer; out AKey, AData: Pointer);
  215. procedure InsertKey(Index: Integer; AKey: Pointer);
  216. procedure InsertKeyData(Index: Integer; AKey, AData: Pointer);
  217. function Remove(AKey: Pointer): Integer;
  218. procedure Sort;
  219. property Duplicates: TDuplicates read FDuplicates write FDuplicates;
  220. property KeySize: Integer read FKeySize;
  221. property DataSize: Integer read FDataSize;
  222. property Keys[Index: Integer]: Pointer read GetKey write PutKey;
  223. property Data[Index: Integer]: Pointer read GetData write PutData;
  224. property KeyData[Key: Pointer]: Pointer read GetKeyData write PutKeyData; default;
  225. property Sorted: Boolean read FSorted write SetSorted;
  226. property OnPtrCompare: TFPSListCompareFunc read FOnKeyPtrCompare write SetOnKeyPtrCompare; //deprecated;
  227. property OnKeyPtrCompare: TFPSListCompareFunc read FOnKeyPtrCompare write SetOnKeyPtrCompare;
  228. property OnDataPtrCompare: TFPSListCompareFunc read FOnDataPtrCompare write SetOnDataPtrCompare;
  229. end;
  230. {$ifndef VER2_0}
  231. generic TFPGMap<TKey, TData> = class(TFPSMap)
  232. public
  233. type
  234. TKeyCompareFunc = function(const Key1, Key2: TKey): Integer;
  235. TDataCompareFunc = function(const Data1, Data2: TData): Integer;
  236. PKey = ^TKey;
  237. PData = ^TData;
  238. {$ifndef OldSyntax}protected var{$else}var protected{$endif}
  239. FOnKeyCompare: TKeyCompareFunc;
  240. FOnDataCompare: TDataCompareFunc;
  241. procedure CopyItem(Src, Dest: Pointer); override;
  242. procedure CopyKey(Src, Dest: Pointer); override;
  243. procedure CopyData(Src, Dest: Pointer); override;
  244. procedure Deref(Item: Pointer); override;
  245. procedure InitOnPtrCompare; override;
  246. function GetKey(Index: Integer): TKey; {$ifdef CLASSESINLINE} inline; {$endif}
  247. function GetKeyData(const AKey: TKey): TData; {$ifdef CLASSESINLINE} inline; {$endif}
  248. function GetData(Index: Integer): TData; {$ifdef CLASSESINLINE} inline; {$endif}
  249. function KeyCompare(Key1, Key2: Pointer): Integer;
  250. function KeyCustomCompare(Key1, Key2: Pointer): Integer;
  251. //function DataCompare(Data1, Data2: Pointer): Integer;
  252. function DataCustomCompare(Data1, Data2: Pointer): Integer;
  253. procedure PutKey(Index: Integer; const NewKey: TKey); {$ifdef CLASSESINLINE} inline; {$endif}
  254. procedure PutKeyData(const AKey: TKey; const NewData: TData); {$ifdef CLASSESINLINE} inline; {$endif}
  255. procedure PutData(Index: Integer; const NewData: TData); {$ifdef CLASSESINLINE} inline; {$endif}
  256. procedure SetOnKeyCompare(NewCompare: TKeyCompareFunc);
  257. procedure SetOnDataCompare(NewCompare: TDataCompareFunc);
  258. public
  259. constructor Create;
  260. function Add(const AKey: TKey; const AData: TData): Integer; {$ifdef CLASSESINLINE} inline; {$endif}
  261. function Add(const AKey: TKey): Integer; {$ifdef CLASSESINLINE} inline; {$endif}
  262. function Find(const AKey: TKey; out Index: Integer): Boolean; {$ifdef CLASSESINLINE} inline; {$endif}
  263. function IndexOf(const AKey: TKey): Integer; {$ifdef CLASSESINLINE} inline; {$endif}
  264. function IndexOfData(const AData: TData): Integer;
  265. procedure InsertKey(Index: Integer; const AKey: TKey);
  266. procedure InsertKeyData(Index: Integer; const AKey: TKey; const AData: TData);
  267. function Remove(const AKey: TKey): Integer;
  268. property Keys[Index: Integer]: TKey read GetKey write PutKey;
  269. property Data[Index: Integer]: TData read GetData write PutData;
  270. property KeyData[const AKey: TKey]: TData read GetKeyData write PutKeyData; default;
  271. property OnCompare: TKeyCompareFunc read FOnKeyCompare write SetOnKeyCompare; //deprecated;
  272. property OnKeyCompare: TKeyCompareFunc read FOnKeyCompare write SetOnKeyCompare;
  273. property OnDataCompare: TDataCompareFunc read FOnDataCompare write SetOnDataCompare;
  274. end;
  275. {$endif}
  276. implementation
  277. uses
  278. rtlconsts;
  279. {****************************************************************************
  280. TFPSList
  281. ****************************************************************************}
  282. constructor TFPSList.Create(AItemSize: integer);
  283. begin
  284. inherited Create;
  285. FItemSize := AItemSize;
  286. end;
  287. destructor TFPSList.Destroy;
  288. begin
  289. Clear;
  290. // Clear() does not clear the whole list; there is always a single temp entry
  291. // at the end which is never freed. Take care of that one here.
  292. FreeMem(FList);
  293. inherited Destroy;
  294. end;
  295. procedure TFPSList.CopyItem(Src, Dest: Pointer);
  296. begin
  297. System.Move(Src^, Dest^, FItemSize);
  298. end;
  299. procedure TFPSList.RaiseIndexError(Index : Integer);
  300. begin
  301. Error(SListIndexError, Index);
  302. end;
  303. function TFPSList.InternalGet(Index: Integer): Pointer;
  304. begin
  305. Result:=FList+Index*ItemSize;
  306. end;
  307. procedure TFPSList.InternalPut(Index: Integer; NewItem: Pointer);
  308. var
  309. ListItem: Pointer;
  310. begin
  311. ListItem := InternalItems[Index];
  312. CopyItem(NewItem, ListItem);
  313. end;
  314. function TFPSList.Get(Index: Integer): Pointer;
  315. begin
  316. if (Index < 0) or (Index >= FCount) then
  317. RaiseIndexError(Index);
  318. Result := InternalItems[Index];
  319. end;
  320. procedure TFPSList.Put(Index: Integer; Item: Pointer);
  321. begin
  322. if (Index < 0) or (Index >= FCount) then
  323. RaiseIndexError(Index);
  324. InternalItems[Index] := Item;
  325. end;
  326. procedure TFPSList.SetCapacity(NewCapacity: Integer);
  327. begin
  328. if (NewCapacity < FCount) or (NewCapacity > MaxListSize) then
  329. Error(SListCapacityError, NewCapacity);
  330. if NewCapacity = FCapacity then
  331. exit;
  332. ReallocMem(FList, (NewCapacity+1) * FItemSize);
  333. FillChar(InternalItems[FCapacity]^, (NewCapacity+1-FCapacity) * FItemSize, #0);
  334. FCapacity := NewCapacity;
  335. end;
  336. procedure TFPSList.Deref(Item: Pointer);
  337. begin
  338. end;
  339. procedure TFPSList.Deref(FromIndex, ToIndex: Integer);
  340. var
  341. ListItem, ListItemLast: Pointer;
  342. begin
  343. ListItem := InternalItems[FromIndex];
  344. ListItemLast := InternalItems[ToIndex];
  345. repeat
  346. Deref(ListItem);
  347. if ListItem = ListItemLast then
  348. break;
  349. ListItem := PByte(ListItem) + ItemSize;
  350. until false;
  351. end;
  352. procedure TFPSList.SetCount(NewCount: Integer);
  353. begin
  354. if (NewCount < 0) or (NewCount > MaxListSize) then
  355. Error(SListCountError, NewCount);
  356. if NewCount > FCapacity then
  357. SetCapacity(NewCount);
  358. if NewCount > FCount then
  359. FillByte(InternalItems[FCount]^, (NewCount-FCount) * FItemSize, 0)
  360. else if NewCount < FCount then
  361. Deref(NewCount, FCount-1);
  362. FCount := NewCount;
  363. end;
  364. function TFPSList.Add(Item: Pointer): Integer;
  365. begin
  366. if FCount = FCapacity then
  367. Self.Expand;
  368. CopyItem(Item, InternalItems[FCount]);
  369. Result := FCount;
  370. Inc(FCount);
  371. end;
  372. procedure TFPSList.Clear;
  373. begin
  374. if Assigned(FList) then
  375. begin
  376. SetCount(0);
  377. SetCapacity(0);
  378. end;
  379. end;
  380. procedure TFPSList.Delete(Index: Integer);
  381. var
  382. ListItem: Pointer;
  383. begin
  384. if (Index < 0) or (Index >= FCount) then
  385. Error(SListIndexError, Index);
  386. Dec(FCount);
  387. ListItem := InternalItems[Index];
  388. Deref(ListItem);
  389. System.Move(InternalItems[Index+1]^, ListItem^, (FCount - Index) * FItemSize);
  390. // Shrink the list if appropriate
  391. if (FCapacity > 256) and (FCount < FCapacity shr 2) then
  392. begin
  393. FCapacity := FCapacity shr 1;
  394. ReallocMem(FList, (FCapacity+1) * FItemSize);
  395. end;
  396. end;
  397. function TFPSList.Extract(Item: Pointer): Pointer;
  398. var
  399. i : Integer;
  400. begin
  401. Result := nil;
  402. i := IndexOf(Item);
  403. if i >= 0 then
  404. begin
  405. Result := InternalItems[i];
  406. System.Move(Result^, InternalItems[FCapacity]^, FItemSize);
  407. Delete(i);
  408. end;
  409. end;
  410. class procedure TFPSList.Error(const Msg: string; Data: PtrInt);
  411. begin
  412. raise EListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame);
  413. end;
  414. procedure TFPSList.Exchange(Index1, Index2: Integer);
  415. begin
  416. if ((Index1 >= FCount) or (Index1 < 0)) then
  417. Error(SListIndexError, Index1);
  418. if ((Index2 >= FCount) or (Index2 < 0)) then
  419. Error(SListIndexError, Index2);
  420. InternalExchange(Index1, Index2);
  421. end;
  422. procedure TFPSList.InternalExchange(Index1, Index2: Integer);
  423. begin
  424. System.Move(InternalItems[Index1]^, InternalItems[FCapacity]^, FItemSize);
  425. System.Move(InternalItems[Index2]^, InternalItems[Index1]^, FItemSize);
  426. System.Move(InternalItems[FCapacity]^, InternalItems[Index2]^, FItemSize);
  427. end;
  428. function TFPSList.Expand: TFPSList;
  429. var
  430. IncSize : Longint;
  431. begin
  432. if FCount < FCapacity then exit;
  433. IncSize := 4;
  434. if FCapacity > 3 then IncSize := IncSize + 4;
  435. if FCapacity > 8 then IncSize := IncSize + 8;
  436. if FCapacity > 127 then Inc(IncSize, FCapacity shr 2);
  437. SetCapacity(FCapacity + IncSize);
  438. Result := Self;
  439. end;
  440. function TFPSList.First: Pointer;
  441. begin
  442. If FCount = 0 then
  443. Result := Nil
  444. else
  445. Result := InternalItems[0];
  446. end;
  447. function TFPSList.IndexOf(Item: Pointer): Integer;
  448. var
  449. ListItem: Pointer;
  450. begin
  451. Result := 0;
  452. ListItem := First;
  453. while (Result < FCount) and (CompareByte(ListItem^, Item^, FItemSize) <> 0) do
  454. begin
  455. Inc(Result);
  456. ListItem := PByte(ListItem)+FItemSize;
  457. end;
  458. if Result = FCount then Result := -1;
  459. end;
  460. function TFPSList.Insert(Index: Integer): Pointer;
  461. begin
  462. if (Index < 0) or (Index > FCount) then
  463. Error(SListIndexError, Index);
  464. if FCount = FCapacity then Self.Expand;
  465. Result := InternalItems[Index];
  466. if Index<FCount then
  467. begin
  468. System.Move(Result^, (Result+FItemSize)^, (FCount - Index) * FItemSize);
  469. { clear for compiler assisted types }
  470. System.FillByte(Result^, FItemSize, 0);
  471. end;
  472. Inc(FCount);
  473. end;
  474. procedure TFPSList.Insert(Index: Integer; Item: Pointer);
  475. begin
  476. CopyItem(Item, Insert(Index));
  477. end;
  478. function TFPSList.Last: Pointer;
  479. begin
  480. if FCount = 0 then
  481. Result := nil
  482. else
  483. Result := InternalItems[FCount - 1];
  484. end;
  485. procedure TFPSList.Move(CurIndex, NewIndex: Integer);
  486. var
  487. CurItem, NewItem, TmpItem, Src, Dest: Pointer;
  488. MoveCount: Integer;
  489. begin
  490. if (CurIndex < 0) or (CurIndex >= Count) then
  491. Error(SListIndexError, CurIndex);
  492. if (NewIndex < 0) or (NewIndex >= Count) then
  493. Error(SListIndexError, NewIndex);
  494. if CurIndex = NewIndex then
  495. exit;
  496. CurItem := InternalItems[CurIndex];
  497. NewItem := InternalItems[NewIndex];
  498. TmpItem := InternalItems[FCapacity];
  499. System.Move(CurItem^, TmpItem^, FItemSize);
  500. if NewIndex > CurIndex then
  501. begin
  502. Src := InternalItems[CurIndex+1];
  503. Dest := CurItem;
  504. MoveCount := NewIndex - CurIndex;
  505. end else begin
  506. Src := NewItem;
  507. Dest := InternalItems[NewIndex+1];
  508. MoveCount := CurIndex - NewIndex;
  509. end;
  510. System.Move(Src^, Dest^, MoveCount * FItemSize);
  511. System.Move(TmpItem^, NewItem^, FItemSize);
  512. end;
  513. function TFPSList.Remove(Item: Pointer): Integer;
  514. begin
  515. Result := IndexOf(Item);
  516. if Result <> -1 then
  517. Delete(Result);
  518. end;
  519. procedure TFPSList.Pack;
  520. var
  521. NewCount,
  522. i : integer;
  523. pdest,
  524. psrc : Pointer;
  525. begin
  526. NewCount:=0;
  527. psrc:=First;
  528. pdest:=psrc;
  529. For I:=0 To FCount-1 Do
  530. begin
  531. if assigned(pointer(psrc^)) then
  532. begin
  533. System.Move(psrc^, pdest^, FItemSize);
  534. inc(pdest);
  535. inc(NewCount);
  536. end;
  537. inc(psrc);
  538. end;
  539. FCount:=NewCount;
  540. end;
  541. // Needed by Sort method.
  542. procedure TFPSList.QuickSort(L, R: Integer; Compare: TFPSListCompareFunc);
  543. var
  544. I, J, P: Integer;
  545. PivotItem: Pointer;
  546. begin
  547. repeat
  548. I := L;
  549. J := R;
  550. P := (L + R) div 2;
  551. repeat
  552. PivotItem := InternalItems[P];
  553. while Compare(PivotItem, InternalItems[I]) > 0 do
  554. Inc(I);
  555. while Compare(PivotItem, InternalItems[J]) < 0 do
  556. Dec(J);
  557. if I <= J then
  558. begin
  559. InternalExchange(I, J);
  560. if P = I then
  561. P := J
  562. else if P = J then
  563. P := I;
  564. Inc(I);
  565. Dec(J);
  566. end;
  567. until I > J;
  568. if L < J then
  569. QuickSort(L, J, Compare);
  570. L := I;
  571. until I >= R;
  572. end;
  573. procedure TFPSList.Sort(Compare: TFPSListCompareFunc);
  574. begin
  575. if not Assigned(FList) or (FCount < 2) then exit;
  576. QuickSort(0, FCount-1, Compare);
  577. end;
  578. procedure TFPSList.Assign(Obj: TFPSList);
  579. var
  580. i: Integer;
  581. begin
  582. if Obj.ItemSize <> FItemSize then
  583. Error(SListItemSizeError, 0);
  584. Clear;
  585. for I := 0 to Obj.Count - 1 do
  586. Add(Obj[i]);
  587. end;
  588. {$ifndef VER2_0}
  589. {****************************************************************************}
  590. {* TFPGListEnumerator *}
  591. {****************************************************************************}
  592. function TFPGListEnumerator.GetCurrent: T;
  593. begin
  594. Result := T(FList.Items[FPosition]^);
  595. end;
  596. constructor TFPGListEnumerator.Create(AList: TFPSList);
  597. begin
  598. inherited Create;
  599. FList := AList;
  600. FPosition := -1;
  601. end;
  602. function TFPGListEnumerator.MoveNext: Boolean;
  603. begin
  604. inc(FPosition);
  605. Result := FPosition < FList.Count;
  606. end;
  607. {****************************************************************************}
  608. {* TFPGList *}
  609. {****************************************************************************}
  610. constructor TFPGList.Create;
  611. begin
  612. inherited Create(sizeof(T));
  613. end;
  614. procedure TFPGList.CopyItem(Src, Dest: Pointer);
  615. begin
  616. T(Dest^) := T(Src^);
  617. end;
  618. procedure TFPGList.Deref(Item: Pointer);
  619. begin
  620. Finalize(T(Item^));
  621. end;
  622. function TFPGList.Get(Index: Integer): T;
  623. begin
  624. Result := T(inherited Get(Index)^);
  625. end;
  626. function TFPGList.GetList: PTypeList;
  627. begin
  628. Result := PTypeList(FList);
  629. end;
  630. function TFPGList.ItemPtrCompare(Item1, Item2: Pointer): Integer;
  631. begin
  632. Result := FOnCompare(T(Item1^), T(Item2^));
  633. end;
  634. procedure TFPGList.Put(Index: Integer; const Item: T);
  635. begin
  636. inherited Put(Index, @Item);
  637. end;
  638. function TFPGList.Add(const Item: T): Integer;
  639. begin
  640. Result := inherited Add(@Item);
  641. end;
  642. function TFPGList.Extract(const Item: T): T;
  643. var
  644. ResPtr: Pointer;
  645. begin
  646. ResPtr := inherited Extract(@Item);
  647. if ResPtr <> nil then
  648. Result := T(ResPtr^)
  649. else
  650. FillByte(Result, sizeof(T), 0);
  651. end;
  652. function TFPGList.First: T;
  653. begin
  654. Result := T(inherited First^);
  655. end;
  656. function TFPGList.GetEnumerator: TFPGListEnumeratorSpec;
  657. begin
  658. Result := TFPGListEnumeratorSpec.Create(Self);
  659. end;
  660. function TFPGList.IndexOf(const Item: T): Integer;
  661. begin
  662. Result := 0;
  663. {$info TODO: fix inlining to work! InternalItems[Result]^}
  664. while (Result < FCount) and (PT(FList)[Result] <> Item) do
  665. Inc(Result);
  666. if Result = FCount then
  667. Result := -1;
  668. end;
  669. procedure TFPGList.Insert(Index: Integer; const Item: T);
  670. begin
  671. T(inherited Insert(Index)^) := Item;
  672. end;
  673. function TFPGList.Last: T;
  674. begin
  675. Result := T(inherited Last^);
  676. end;
  677. function TFPGList.Remove(const Item: T): Integer;
  678. begin
  679. Result := IndexOf(Item);
  680. if Result >= 0 then
  681. Delete(Result);
  682. end;
  683. procedure TFPGList.Sort(Compare: TCompareFunc);
  684. begin
  685. FOnCompare := Compare;
  686. inherited Sort(@ItemPtrCompare);
  687. end;
  688. {****************************************************************************}
  689. {* TFPGObjectList *}
  690. {****************************************************************************}
  691. constructor TFPGObjectList.Create(FreeObjects: Boolean);
  692. begin
  693. inherited Create;
  694. FFreeObjects := FreeObjects;
  695. end;
  696. procedure TFPGObjectList.CopyItem(Src, Dest: Pointer);
  697. begin
  698. T(Dest^) := T(Src^);
  699. {if TObject(Dest^) is TInterfacedObject then
  700. T(Dest^)._AddRef;}
  701. end;
  702. procedure TFPGObjectList.Deref(Item: Pointer);
  703. begin
  704. {if TObject(Item^) is TInterfacedObject then
  705. T(Item^)._Release
  706. else}
  707. if FFreeObjects then
  708. T(Item^).Free;
  709. end;
  710. function TFPGObjectList.Get(Index: Integer): T;
  711. begin
  712. Result := T(inherited Get(Index)^);
  713. end;
  714. function TFPGObjectList.GetList: PTypeList;
  715. begin
  716. Result := PTypeList(FList);
  717. end;
  718. function TFPGObjectList.ItemPtrCompare(Item1, Item2: Pointer): Integer;
  719. begin
  720. Result := FOnCompare(T(Item1^), T(Item2^));
  721. end;
  722. procedure TFPGObjectList.Put(Index: Integer; const Item: T);
  723. begin
  724. inherited Put(Index, @Item);
  725. end;
  726. function TFPGObjectList.Add(const Item: T): Integer;
  727. begin
  728. Result := inherited Add(@Item);
  729. end;
  730. function TFPGObjectList.Extract(const Item: T): T;
  731. var
  732. ResPtr: Pointer;
  733. begin
  734. ResPtr := inherited Extract(@Item);
  735. if ResPtr <> nil then
  736. Result := T(ResPtr^)
  737. else
  738. FillByte(Result, sizeof(T), 0);
  739. end;
  740. function TFPGObjectList.First: T;
  741. begin
  742. Result := T(inherited First^);
  743. end;
  744. function TFPGObjectList.IndexOf(const Item: T): Integer;
  745. begin
  746. Result := 0;
  747. {$info TODO: fix inlining to work! InternalItems[Result]^}
  748. while (Result < FCount) and (PT(FList)[Result] <> Item) do
  749. Inc(Result);
  750. if Result = FCount then
  751. Result := -1;
  752. end;
  753. procedure TFPGObjectList.Insert(Index: Integer; const Item: T);
  754. begin
  755. T(inherited Insert(Index)^) := Item;
  756. end;
  757. function TFPGObjectList.Last: T;
  758. begin
  759. Result := T(inherited Last^);
  760. end;
  761. function TFPGObjectList.Remove(const Item: T): Integer;
  762. begin
  763. Result := IndexOf(Item);
  764. if Result >= 0 then
  765. Delete(Result);
  766. end;
  767. procedure TFPGObjectList.Sort(Compare: TCompareFunc);
  768. begin
  769. FOnCompare := Compare;
  770. inherited Sort(@ItemPtrCompare);
  771. end;
  772. {****************************************************************************}
  773. {* TFPGInterfacedObjectList *}
  774. {****************************************************************************}
  775. constructor TFPGInterfacedObjectList.Create;
  776. begin
  777. inherited Create;
  778. end;
  779. procedure TFPGInterfacedObjectList.CopyItem(Src, Dest: Pointer);
  780. begin
  781. T(Dest^) := T(Src^);
  782. if Assigned(Pointer(Dest^)) then
  783. T(Dest^)._AddRef;
  784. end;
  785. procedure TFPGInterfacedObjectList.Deref(Item: Pointer);
  786. begin
  787. if Assigned(Pointer(Item^)) then
  788. T(Item^)._Release;
  789. end;
  790. function TFPGInterfacedObjectList.Get(Index: Integer): T;
  791. begin
  792. Result := T(inherited Get(Index)^);
  793. end;
  794. function TFPGInterfacedObjectList.GetList: PTypeList;
  795. begin
  796. Result := PTypeList(FList);
  797. end;
  798. function TFPGInterfacedObjectList.ItemPtrCompare(Item1, Item2: Pointer): Integer;
  799. begin
  800. Result := FOnCompare(T(Item1^), T(Item2^));
  801. end;
  802. procedure TFPGInterfacedObjectList.Put(Index: Integer; const Item: T);
  803. begin
  804. inherited Put(Index, @Item);
  805. end;
  806. function TFPGInterfacedObjectList.Add(const Item: T): Integer;
  807. begin
  808. Result := inherited Add(@Item);
  809. end;
  810. function TFPGInterfacedObjectList.Extract(const Item: T): T;
  811. var
  812. ResPtr: Pointer;
  813. begin
  814. ResPtr := inherited Extract(@Item);
  815. if ResPtr <> nil then
  816. Result := T(ResPtr^)
  817. else
  818. FillByte(Result, sizeof(T), 0);
  819. end;
  820. function TFPGInterfacedObjectList.First: T;
  821. begin
  822. Result := T(inherited First^);
  823. end;
  824. function TFPGInterfacedObjectList.IndexOf(const Item: T): Integer;
  825. begin
  826. Result := 0;
  827. {$info TODO: fix inlining to work! InternalItems[Result]^}
  828. while (Result < FCount) and (PT(FList)[Result] <> Item) do
  829. Inc(Result);
  830. if Result = FCount then
  831. Result := -1;
  832. end;
  833. procedure TFPGInterfacedObjectList.Insert(Index: Integer; const Item: T);
  834. begin
  835. T(inherited Insert(Index)^) := Item;
  836. end;
  837. function TFPGInterfacedObjectList.Last: T;
  838. begin
  839. Result := T(inherited Last^);
  840. end;
  841. function TFPGInterfacedObjectList.Remove(const Item: T): Integer;
  842. begin
  843. Result := IndexOf(Item);
  844. if Result >= 0 then
  845. Delete(Result);
  846. end;
  847. procedure TFPGInterfacedObjectList.Sort(Compare: TCompareFunc);
  848. begin
  849. FOnCompare := Compare;
  850. inherited Sort(@ItemPtrCompare);
  851. end;
  852. {$endif}
  853. {****************************************************************************
  854. TFPSMap
  855. ****************************************************************************}
  856. constructor TFPSMap.Create(AKeySize: Integer; ADataSize: integer);
  857. begin
  858. inherited Create(AKeySize+ADataSize);
  859. FKeySize := AKeySize;
  860. FDataSize := ADataSize;
  861. InitOnPtrCompare;
  862. end;
  863. procedure TFPSMap.CopyKey(Src, Dest: Pointer);
  864. begin
  865. System.Move(Src^, Dest^, FKeySize);
  866. end;
  867. procedure TFPSMap.CopyData(Src, Dest: Pointer);
  868. begin
  869. System.Move(Src^, Dest^, FDataSize);
  870. end;
  871. function TFPSMap.GetKey(Index: Integer): Pointer;
  872. begin
  873. Result := Items[Index];
  874. end;
  875. function TFPSMap.GetData(Index: Integer): Pointer;
  876. begin
  877. Result := PByte(Items[Index])+FKeySize;
  878. end;
  879. function TFPSMap.GetKeyData(AKey: Pointer): Pointer;
  880. var
  881. I: Integer;
  882. begin
  883. I := IndexOf(AKey);
  884. if I >= 0 then
  885. Result := InternalItems[I]+FKeySize
  886. else
  887. Error(SMapKeyError, PtrUInt(AKey));
  888. end;
  889. function TFPSMap.BinaryCompareKey(Key1, Key2: Pointer): Integer;
  890. begin
  891. Result := CompareByte(Key1^, Key2^, FKeySize);
  892. end;
  893. function TFPSMap.BinaryCompareData(Data1, Data2: Pointer): Integer;
  894. begin
  895. Result := CompareByte(Data1^, Data1^, FDataSize);
  896. end;
  897. procedure TFPSMap.SetOnKeyPtrCompare(Proc: TFPSListCompareFunc);
  898. begin
  899. if Proc <> nil then
  900. FOnKeyPtrCompare := Proc
  901. else
  902. FOnKeyPtrCompare := @BinaryCompareKey;
  903. end;
  904. procedure TFPSMap.SetOnDataPtrCompare(Proc: TFPSListCompareFunc);
  905. begin
  906. if Proc <> nil then
  907. FOnDataPtrCompare := Proc
  908. else
  909. FOnDataPtrCompare := @BinaryCompareData;
  910. end;
  911. procedure TFPSMap.InitOnPtrCompare;
  912. begin
  913. SetOnKeyPtrCompare(nil);
  914. SetOnDataPtrCompare(nil);
  915. end;
  916. procedure TFPSMap.PutKey(Index: Integer; AKey: Pointer);
  917. begin
  918. if FSorted then
  919. Error(SSortedListError, 0);
  920. CopyKey(AKey, Items[Index]);
  921. end;
  922. procedure TFPSMap.PutData(Index: Integer; AData: Pointer);
  923. begin
  924. CopyData(AData, PByte(Items[Index])+FKeySize);
  925. end;
  926. procedure TFPSMap.PutKeyData(AKey: Pointer; NewData: Pointer);
  927. var
  928. I: Integer;
  929. begin
  930. I := IndexOf(AKey);
  931. if I >= 0 then
  932. Data[I] := NewData
  933. else
  934. Add(AKey, NewData);
  935. end;
  936. procedure TFPSMap.SetSorted(Value: Boolean);
  937. begin
  938. if Value = FSorted then exit;
  939. FSorted := Value;
  940. if Value then Sort;
  941. end;
  942. function TFPSMap.Add(AKey: Pointer): Integer;
  943. begin
  944. if Sorted then
  945. begin
  946. if Find(AKey, Result) then
  947. case Duplicates of
  948. dupIgnore: exit;
  949. dupError: Error(SDuplicateItem, 0)
  950. end;
  951. end else
  952. Result := Count;
  953. CopyKey(AKey, inherited Insert(Result));
  954. end;
  955. function TFPSMap.Add(AKey, AData: Pointer): Integer;
  956. begin
  957. Result := Add(AKey);
  958. Data[Result] := AData;
  959. end;
  960. function TFPSMap.Find(AKey: Pointer; out Index: Integer): Boolean;
  961. { Searches for the first item <= Key, returns True if exact match,
  962. sets index to the index f the found string. }
  963. var
  964. I,L,R,Dir: Integer;
  965. begin
  966. Result := false;
  967. // Use binary search.
  968. L := 0;
  969. R := FCount-1;
  970. while L<=R do
  971. begin
  972. I := (L+R) div 2;
  973. Dir := FOnKeyPtrCompare(Items[I], AKey);
  974. if Dir < 0 then
  975. L := I+1
  976. else begin
  977. R := I-1;
  978. if Dir = 0 then
  979. begin
  980. Result := true;
  981. if Duplicates <> dupAccept then
  982. L := I;
  983. end;
  984. end;
  985. end;
  986. Index := L;
  987. end;
  988. function TFPSMap.LinearIndexOf(AKey: Pointer): Integer;
  989. var
  990. ListItem: Pointer;
  991. begin
  992. Result := 0;
  993. ListItem := First;
  994. while (Result < FCount) and (FOnKeyPtrCompare(ListItem, AKey) <> 0) do
  995. begin
  996. Inc(Result);
  997. ListItem := PByte(ListItem)+FItemSize;
  998. end;
  999. if Result = FCount then Result := -1;
  1000. end;
  1001. function TFPSMap.IndexOf(AKey: Pointer): Integer;
  1002. begin
  1003. if Sorted then
  1004. begin
  1005. if not Find(AKey, Result) then
  1006. Result := -1;
  1007. end else
  1008. Result := LinearIndexOf(AKey);
  1009. end;
  1010. function TFPSMap.IndexOfData(AData: Pointer): Integer;
  1011. var
  1012. ListItem: Pointer;
  1013. begin
  1014. Result := 0;
  1015. ListItem := First+FKeySize;
  1016. while (Result < FCount) and (FOnDataPtrCompare(ListItem, AData) <> 0) do
  1017. begin
  1018. Inc(Result);
  1019. ListItem := PByte(ListItem)+FItemSize;
  1020. end;
  1021. if Result = FCount then Result := -1;
  1022. end;
  1023. function TFPSMap.Insert(Index: Integer): Pointer;
  1024. begin
  1025. if FSorted then
  1026. Error(SSortedListError, 0);
  1027. Result := inherited Insert(Index);
  1028. end;
  1029. procedure TFPSMap.Insert(Index: Integer; out AKey, AData: Pointer);
  1030. begin
  1031. AKey := Insert(Index);
  1032. AData := PByte(AKey) + FKeySize;
  1033. end;
  1034. procedure TFPSMap.InsertKey(Index: Integer; AKey: Pointer);
  1035. begin
  1036. CopyKey(AKey, Insert(Index));
  1037. end;
  1038. procedure TFPSMap.InsertKeyData(Index: Integer; AKey, AData: Pointer);
  1039. var
  1040. ListItem: Pointer;
  1041. begin
  1042. ListItem := Insert(Index);
  1043. CopyKey(AKey, ListItem);
  1044. CopyData(AData, PByte(ListItem)+FKeySize);
  1045. end;
  1046. function TFPSMap.Remove(AKey: Pointer): Integer;
  1047. begin
  1048. Result := IndexOf(AKey);
  1049. if Result >= 0 then
  1050. Delete(Result);
  1051. end;
  1052. procedure TFPSMap.Sort;
  1053. begin
  1054. inherited Sort(FOnKeyPtrCompare);
  1055. end;
  1056. {****************************************************************************
  1057. TFPGMap
  1058. ****************************************************************************}
  1059. {$ifndef VER2_0}
  1060. constructor TFPGMap.Create;
  1061. begin
  1062. inherited Create(SizeOf(TKey), SizeOf(TData));
  1063. end;
  1064. procedure TFPGMap.CopyItem(Src, Dest: Pointer);
  1065. begin
  1066. CopyKey(Src, Dest);
  1067. CopyData(PByte(Src)+KeySize, PByte(Dest)+KeySize);
  1068. end;
  1069. procedure TFPGMap.CopyKey(Src, Dest: Pointer);
  1070. begin
  1071. TKey(Dest^) := TKey(Src^);
  1072. end;
  1073. procedure TFPGMap.CopyData(Src, Dest: Pointer);
  1074. begin
  1075. TData(Dest^) := TData(Src^);
  1076. end;
  1077. procedure TFPGMap.Deref(Item: Pointer);
  1078. begin
  1079. Finalize(TKey(Item^));
  1080. Finalize(TData(Pointer(PByte(Item)+KeySize)^));
  1081. end;
  1082. function TFPGMap.GetKey(Index: Integer): TKey;
  1083. begin
  1084. Result := TKey(inherited GetKey(Index)^);
  1085. end;
  1086. function TFPGMap.GetData(Index: Integer): TData;
  1087. begin
  1088. Result := TData(inherited GetData(Index)^);
  1089. end;
  1090. function TFPGMap.GetKeyData(const AKey: TKey): TData;
  1091. begin
  1092. Result := TData(inherited GetKeyData(@AKey)^);
  1093. end;
  1094. function TFPGMap.KeyCompare(Key1, Key2: Pointer): Integer;
  1095. begin
  1096. if PKey(Key1)^ < PKey(Key2)^ then
  1097. Result := -1
  1098. else if PKey(Key1)^ > PKey(Key2)^ then
  1099. Result := 1
  1100. else
  1101. Result := 0;
  1102. end;
  1103. {function TFPGMap.DataCompare(Data1, Data2: Pointer): Integer;
  1104. begin
  1105. if PData(Data1)^ < PData(Data2)^ then
  1106. Result := -1
  1107. else if PData(Data1)^ > PData(Data2)^ then
  1108. Result := 1
  1109. else
  1110. Result := 0;
  1111. end;}
  1112. function TFPGMap.KeyCustomCompare(Key1, Key2: Pointer): Integer;
  1113. begin
  1114. Result := FOnKeyCompare(TKey(Key1^), TKey(Key2^));
  1115. end;
  1116. function TFPGMap.DataCustomCompare(Data1, Data2: Pointer): Integer;
  1117. begin
  1118. Result := FOnDataCompare(TData(Data1^), TData(Data2^));
  1119. end;
  1120. procedure TFPGMap.SetOnKeyCompare(NewCompare: TKeyCompareFunc);
  1121. begin
  1122. FOnKeyCompare := NewCompare;
  1123. if NewCompare <> nil then
  1124. OnKeyPtrCompare := @KeyCustomCompare
  1125. else
  1126. OnKeyPtrCompare := @KeyCompare;
  1127. end;
  1128. procedure TFPGMap.SetOnDataCompare(NewCompare: TDataCompareFunc);
  1129. begin
  1130. FOnDataCompare := NewCompare;
  1131. if NewCompare <> nil then
  1132. OnDataPtrCompare := @DataCustomCompare
  1133. else
  1134. OnDataPtrCompare := nil;
  1135. end;
  1136. procedure TFPGMap.InitOnPtrCompare;
  1137. begin
  1138. SetOnKeyCompare(nil);
  1139. SetOnDataCompare(nil);
  1140. end;
  1141. procedure TFPGMap.PutKey(Index: Integer; const NewKey: TKey);
  1142. begin
  1143. inherited PutKey(Index, @NewKey);
  1144. end;
  1145. procedure TFPGMap.PutData(Index: Integer; const NewData: TData);
  1146. begin
  1147. inherited PutData(Index, @NewData);
  1148. end;
  1149. procedure TFPGMap.PutKeyData(const AKey: TKey; const NewData: TData);
  1150. begin
  1151. inherited PutKeyData(@AKey, @NewData);
  1152. end;
  1153. function TFPGMap.Add(const AKey: TKey): Integer;
  1154. begin
  1155. Result := inherited Add(@AKey);
  1156. end;
  1157. function TFPGMap.Add(const AKey: TKey; const AData: TData): Integer;
  1158. begin
  1159. Result := inherited Add(@AKey, @AData);
  1160. end;
  1161. function TFPGMap.Find(const AKey: TKey; out Index: Integer): Boolean;
  1162. begin
  1163. Result := inherited Find(@AKey, Index);
  1164. end;
  1165. function TFPGMap.IndexOf(const AKey: TKey): Integer;
  1166. begin
  1167. Result := inherited IndexOf(@AKey);
  1168. end;
  1169. function TFPGMap.IndexOfData(const AData: TData): Integer;
  1170. begin
  1171. { TODO: loop ? }
  1172. Result := inherited IndexOfData(@AData);
  1173. end;
  1174. procedure TFPGMap.InsertKey(Index: Integer; const AKey: TKey);
  1175. begin
  1176. inherited InsertKey(Index, @AKey);
  1177. end;
  1178. procedure TFPGMap.InsertKeyData(Index: Integer; const AKey: TKey; const AData: TData);
  1179. begin
  1180. inherited InsertKeyData(Index, @AKey, @AData);
  1181. end;
  1182. function TFPGMap.Remove(const AKey: TKey): Integer;
  1183. begin
  1184. Result := inherited Remove(@AKey);
  1185. end;
  1186. {$endif}
  1187. end.