fgl.pp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110
  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. {$define FGLINLINE}
  14. {$ifdef FGLINLINE}
  15. {$inline on}
  16. {$endif FGLINLINE}
  17. {$IFNDEF FPC_DOTTEDUNITS}
  18. unit fgl;
  19. {$ENDIF FPC_DOTTEDUNITS}
  20. interface
  21. {$IFDEF FPC_DOTTEDUNITS}
  22. uses
  23. System.Types, System.SysUtils, System.SortBase;
  24. {$ELSE FPC_DOTTEDUNITS}
  25. uses
  26. types, sysutils, sortbase;
  27. {$ENDIF FPC_DOTTEDUNITS}
  28. const
  29. MaxListSize = Maxint div 16;
  30. type
  31. EListError = class(Exception);
  32. TFPSList = class;
  33. TFPSListCompareFunc = function(Key1, Key2: Pointer): Integer of object;
  34. { TFPSList }
  35. TFPSList = class(TObject)
  36. protected
  37. FList: PByte;
  38. FCount: Integer;
  39. FCapacity: Integer; { list has room for capacity+1 items, contains room for a temporary item }
  40. FItemSize: Integer;
  41. procedure CopyItem(Src, Dest: Pointer); virtual;
  42. procedure CopyItems(Src, Dest: Pointer; aCount : Integer); virtual;
  43. procedure Deref(Item: Pointer); virtual; overload;
  44. procedure Deref(FromIndex, ToIndex: Integer); overload;
  45. function Get(Index: Integer): Pointer;
  46. procedure InternalExchange(Index1, Index2: Integer);
  47. function InternalGet(Index: Integer): Pointer; {$ifdef FGLINLINE} inline; {$endif}
  48. procedure InternalPut(Index: Integer; NewItem: Pointer);
  49. procedure Put(Index: Integer; Item: Pointer);
  50. procedure QuickSort(L, R: Integer; Compare: TFPSListCompareFunc);
  51. procedure SetCapacity(NewCapacity: Integer);
  52. procedure SetCount(NewCount: Integer);
  53. procedure RaiseIndexError(Index : Integer);
  54. property InternalItems[Index: Integer]: Pointer read InternalGet write InternalPut;
  55. function GetLast: Pointer;
  56. procedure SetLast(const Value: Pointer);
  57. function GetFirst: Pointer;
  58. procedure SetFirst(const Value: Pointer);
  59. Procedure CheckIndex(AIndex : Integer); inline;
  60. public
  61. constructor Create(AItemSize: Integer = sizeof(Pointer));
  62. destructor Destroy; override;
  63. class Function ItemIsManaged : Boolean; virtual;
  64. function Add(Item: Pointer): Integer;
  65. procedure Clear;
  66. procedure Delete(Index: Integer);
  67. procedure DeleteRange(IndexFrom, IndexTo : Integer);
  68. class procedure Error(const Msg: string; Data: PtrInt);
  69. procedure Exchange(Index1, Index2: Integer);
  70. function Expand: TFPSList;
  71. procedure Extract(Item: Pointer; ResultPtr: Pointer);
  72. function IndexOf(Item: Pointer): Integer;
  73. procedure Insert(Index: Integer; Item: Pointer);
  74. function Insert(Index: Integer): Pointer;
  75. procedure Move(CurIndex, NewIndex: Integer);
  76. procedure Assign(Obj: TFPSList);
  77. procedure AddList(Obj: TFPSList);
  78. function Remove(Item: Pointer): Integer;
  79. procedure Pack;
  80. procedure Sort(Compare: TFPSListCompareFunc);
  81. procedure Sort(Compare: TFPSListCompareFunc; SortingAlgorithm: PSortingAlgorithm);
  82. property Capacity: Integer read FCapacity write SetCapacity;
  83. property Count: Integer read FCount write SetCount;
  84. property Items[Index: Integer]: Pointer read Get write Put; default;
  85. property ItemSize: Integer read FItemSize;
  86. property List: PByte read FList;
  87. property First: Pointer read GetFirst write SetFirst;
  88. property Last: Pointer read GetLast write SetLast;
  89. end;
  90. const
  91. {$ifdef cpu16}
  92. MaxGListSize = {MaxInt div} 1024 deprecated;
  93. {$else cpu16}
  94. MaxGListSize = MaxInt div 1024 deprecated;
  95. {$endif cpu16}
  96. type
  97. generic TFPGListEnumerator<T> = class(TObject)
  98. protected
  99. FList: TFPSList;
  100. FPosition: Integer;
  101. function GetCurrent: T;
  102. public
  103. constructor Create(AList: TFPSList);
  104. function MoveNext: Boolean;
  105. property Current: T read GetCurrent;
  106. end;
  107. { TFPGList }
  108. generic TFPGList<T> = class(TFPSList)
  109. private
  110. type
  111. TCompareFunc = function(const Item1, Item2: T): Integer;
  112. PT = ^T;
  113. TTypeList = PT;
  114. PTypeList = ^TTypeList;
  115. protected
  116. var
  117. FOnCompare: TCompareFunc;
  118. procedure CopyItem(Src, Dest: Pointer); override;
  119. procedure Deref(Item: Pointer); override;
  120. function Get(Index: Integer): T; {$ifdef FGLINLINE} inline; {$endif}
  121. function GetList: PTypeList; {$ifdef FGLINLINE} inline; {$endif}
  122. function ItemPtrCompare(Item1, Item2: Pointer): Integer;
  123. procedure Put(Index: Integer; const Item: T); {$ifdef FGLINLINE} inline; {$endif}
  124. function GetLast: T; {$ifdef FGLINLINE} inline; {$endif}
  125. procedure SetLast(const Value: T); {$ifdef FGLINLINE} inline; {$endif}
  126. function GetFirst: T; {$ifdef FGLINLINE} inline; {$endif}
  127. procedure SetFirst(const Value: T); {$ifdef FGLINLINE} inline; {$endif}
  128. public
  129. Type
  130. TFPGListEnumeratorSpec = specialize TFPGListEnumerator<T>;
  131. constructor Create;
  132. class Function ItemIsManaged : Boolean; override;
  133. function Add(const Item: T): Integer; {$ifdef FGLINLINE} inline; {$endif}
  134. function Extract(const Item: T): T; {$ifdef FGLINLINE} inline; {$endif}
  135. property First: T read GetFirst write SetFirst;
  136. function GetEnumerator: TFPGListEnumeratorSpec; {$ifdef FGLINLINE} inline; {$endif}
  137. function IndexOf(const Item: T): Integer;
  138. procedure Insert(Index: Integer; const Item: T); {$ifdef FGLINLINE} inline; {$endif}
  139. property Last: T read GetLast write SetLast;
  140. procedure Assign(Source: TFPGList);
  141. procedure AddList(Source: TFPGList);
  142. function Remove(const Item: T): Integer; {$ifdef FGLINLINE} inline; {$endif}
  143. procedure Sort(Compare: TCompareFunc);
  144. procedure Sort(Compare: TCompareFunc; SortingAlgorithm: PSortingAlgorithm);
  145. property Items[Index: Integer]: T read Get write Put; default;
  146. property List: PTypeList read GetList;
  147. end;
  148. generic TFPGObjectList<T: TObject> = class(TFPSList)
  149. private
  150. type
  151. TCompareFunc = function(const Item1, Item2: T): Integer;
  152. PT = ^T;
  153. TTypeList = PT;
  154. PTypeList = ^TTypeList;
  155. TFPGListEnumeratorSpec = specialize TFPGListEnumerator<T>;
  156. protected
  157. var
  158. FOnCompare: TCompareFunc;
  159. FFreeObjects: Boolean;
  160. procedure CopyItem(Src, Dest: Pointer); override;
  161. procedure Deref(Item: Pointer); override;
  162. function Get(Index: Integer): T; {$ifdef FGLINLINE} inline; {$endif}
  163. function GetList: PTypeList; {$ifdef FGLINLINE} inline; {$endif}
  164. function ItemPtrCompare(Item1, Item2: Pointer): Integer;
  165. procedure Put(Index: Integer; const Item: T); {$ifdef FGLINLINE} inline; {$endif}
  166. function GetLast: T; {$ifdef FGLINLINE} inline; {$endif}
  167. procedure SetLast(const Value: T); {$ifdef FGLINLINE} inline; {$endif}
  168. function GetFirst: T; {$ifdef FGLINLINE} inline; {$endif}
  169. procedure SetFirst(const Value: T); {$ifdef FGLINLINE} inline; {$endif}
  170. public
  171. constructor Create(FreeObjects: Boolean = True);
  172. function Add(const Item: T): Integer; {$ifdef FGLINLINE} inline; {$endif}
  173. function Extract(const Item: T): T; {$ifdef FGLINLINE} inline; {$endif}
  174. property First: T read GetFirst write SetFirst;
  175. function GetEnumerator: TFPGListEnumeratorSpec; {$ifdef FGLINLINE} inline; {$endif}
  176. function IndexOf(const Item: T): Integer;
  177. procedure Insert(Index: Integer; const Item: T); {$ifdef FGLINLINE} inline; {$endif}
  178. property Last: T read GetLast write SetLast;
  179. procedure AddList(Source: TFPGObjectList);
  180. procedure Assign(Source: TFPGObjectList);
  181. function Remove(const Item: T): Integer; {$ifdef FGLINLINE} inline; {$endif}
  182. procedure Sort(Compare: TCompareFunc);
  183. procedure Sort(Compare: TCompareFunc; SortingAlgorithm: PSortingAlgorithm);
  184. property Items[Index: Integer]: T read Get write Put; default;
  185. property List: PTypeList read GetList;
  186. property FreeObjects: Boolean read FFreeObjects write FFreeObjects;
  187. end;
  188. generic TFPGInterfacedObjectList<T> = class(TFPSList)
  189. private
  190. type
  191. TCompareFunc = function(const Item1, Item2: T): Integer;
  192. PT = ^T;
  193. TTypeList = PT;
  194. PTypeList = ^TTypeList;
  195. TFPGListEnumeratorSpec = specialize TFPGListEnumerator<T>;
  196. protected
  197. var
  198. FOnCompare: TCompareFunc;
  199. procedure CopyItem(Src, Dest: Pointer); override;
  200. procedure Deref(Item: Pointer); override;
  201. function Get(Index: Integer): T; {$ifdef FGLINLINE} inline; {$endif}
  202. function GetList: PTypeList; {$ifdef FGLINLINE} inline; {$endif}
  203. function ItemPtrCompare(Item1, Item2: Pointer): Integer;
  204. procedure Put(Index: Integer; const Item: T); {$ifdef FGLINLINE} inline; {$endif}
  205. function GetLast: T; {$ifdef FGLINLINE} inline; {$endif}
  206. procedure SetLast(const Value: T); {$ifdef FGLINLINE} inline; {$endif}
  207. function GetFirst: T; {$ifdef FGLINLINE} inline; {$endif}
  208. procedure SetFirst(const Value: T); {$ifdef FGLINLINE} inline; {$endif}
  209. public
  210. constructor Create;
  211. function Add(const Item: T): Integer; {$ifdef FGLINLINE} inline; {$endif}
  212. function Extract(const Item: T): T; {$ifdef FGLINLINE} inline; {$endif}
  213. property First: T read GetFirst write SetFirst;
  214. function GetEnumerator: TFPGListEnumeratorSpec; {$ifdef FGLINLINE} inline; {$endif}
  215. function IndexOf(const Item: T): Integer;
  216. procedure Insert(Index: Integer; const Item: T); {$ifdef FGLINLINE} inline; {$endif}
  217. property Last: T read GetLast write SetLast;
  218. procedure Assign(Source: TFPGInterfacedObjectList);
  219. procedure AddList(Source: TFPGInterfacedObjectList);
  220. function Remove(const Item: T): Integer; {$ifdef FGLINLINE} inline; {$endif}
  221. procedure Sort(Compare: TCompareFunc);
  222. procedure Sort(Compare: TCompareFunc; SortingAlgorithm: PSortingAlgorithm);
  223. property Items[Index: Integer]: T read Get write Put; default;
  224. property List: PTypeList read GetList;
  225. end;
  226. TFPSMap = class(TFPSList)
  227. private
  228. FKeySize: Integer;
  229. FDataSize: Integer;
  230. FDuplicates: TDuplicates;
  231. FSorted: Boolean;
  232. FOnKeyPtrCompare: TFPSListCompareFunc;
  233. FOnDataPtrCompare: TFPSListCompareFunc;
  234. procedure SetSorted(Value: Boolean);
  235. protected
  236. function BinaryCompareKey(Key1, Key2: Pointer): Integer;
  237. function BinaryCompareData(Data1, Data2: Pointer): Integer;
  238. procedure SetOnKeyPtrCompare(Proc: TFPSListCompareFunc);
  239. procedure SetOnDataPtrCompare(Proc: TFPSListCompareFunc);
  240. procedure InitOnPtrCompare; virtual;
  241. procedure CopyKey(Src, Dest: Pointer); virtual;
  242. procedure CopyData(Src, Dest: Pointer); virtual;
  243. function GetKey(Index: Integer): Pointer;
  244. function GetKeyData(AKey: Pointer): Pointer;
  245. function GetData(Index: Integer): Pointer;
  246. function LinearIndexOf(AKey: Pointer): Integer;
  247. procedure PutKey(Index: Integer; AKey: Pointer);
  248. procedure PutKeyData(AKey: Pointer; NewData: Pointer);
  249. procedure PutData(Index: Integer; AData: Pointer);
  250. public
  251. constructor Create(AKeySize: Integer = sizeof(Pointer);
  252. ADataSize: integer = sizeof(Pointer));
  253. function Add(AKey, AData: Pointer): Integer;
  254. function Add(AKey: Pointer): Integer;
  255. function Find(AKey: Pointer; out Index: Integer): Boolean;
  256. function IndexOf(AKey: Pointer): Integer;
  257. function IndexOfData(AData: Pointer): Integer;
  258. function Insert(Index: Integer): Pointer;
  259. procedure Insert(Index: Integer; out AKey, AData: Pointer);
  260. procedure InsertKey(Index: Integer; AKey: Pointer);
  261. procedure InsertKeyData(Index: Integer; AKey, AData: Pointer);
  262. function Remove(AKey: Pointer): Integer;
  263. procedure Sort;
  264. procedure Sort(SortingAlgorithm: PSortingAlgorithm);
  265. property Duplicates: TDuplicates read FDuplicates write FDuplicates;
  266. property KeySize: Integer read FKeySize;
  267. property DataSize: Integer read FDataSize;
  268. property Keys[Index: Integer]: Pointer read GetKey write PutKey;
  269. property Data[Index: Integer]: Pointer read GetData write PutData;
  270. property KeyData[Key: Pointer]: Pointer read GetKeyData write PutKeyData; default;
  271. property Sorted: Boolean read FSorted write SetSorted;
  272. property OnPtrCompare: TFPSListCompareFunc read FOnKeyPtrCompare write SetOnKeyPtrCompare; //deprecated;
  273. property OnKeyPtrCompare: TFPSListCompareFunc read FOnKeyPtrCompare write SetOnKeyPtrCompare;
  274. property OnDataPtrCompare: TFPSListCompareFunc read FOnDataPtrCompare write SetOnDataPtrCompare;
  275. end;
  276. generic TFPGMap<TKey, TData> = class(TFPSMap)
  277. private
  278. type
  279. TKeyCompareFunc = function(const Key1, Key2: TKey): Integer;
  280. TDataCompareFunc = function(const Data1, Data2: TData): Integer;
  281. PKey = ^TKey;
  282. // unsed PData = ^TData;
  283. protected
  284. var
  285. FOnKeyCompare: TKeyCompareFunc;
  286. FOnDataCompare: TDataCompareFunc;
  287. procedure CopyItem(Src, Dest: Pointer); override;
  288. procedure CopyKey(Src, Dest: Pointer); override;
  289. procedure CopyData(Src, Dest: Pointer); override;
  290. procedure Deref(Item: Pointer); override;
  291. procedure InitOnPtrCompare; override;
  292. function GetKey(Index: Integer): TKey; {$ifdef FGLINLINE} inline; {$endif}
  293. function GetKeyData(const AKey: TKey): TData; {$ifdef FGLINLINE} inline; {$endif}
  294. function GetData(Index: Integer): TData; {$ifdef FGLINLINE} inline; {$endif}
  295. function KeyCompare(Key1, Key2: Pointer): Integer;
  296. function KeyCustomCompare(Key1, Key2: Pointer): Integer;
  297. //function DataCompare(Data1, Data2: Pointer): Integer;
  298. function DataCustomCompare(Data1, Data2: Pointer): Integer;
  299. procedure PutKey(Index: Integer; const NewKey: TKey); {$ifdef FGLINLINE} inline; {$endif}
  300. procedure PutKeyData(const AKey: TKey; const NewData: TData); {$ifdef FGLINLINE} inline; {$endif}
  301. procedure PutData(Index: Integer; const NewData: TData); {$ifdef FGLINLINE} inline; {$endif}
  302. procedure SetOnKeyCompare(NewCompare: TKeyCompareFunc);
  303. procedure SetOnDataCompare(NewCompare: TDataCompareFunc);
  304. public
  305. constructor Create;
  306. function Add(const AKey: TKey; const AData: TData): Integer; {$ifdef FGLINLINE} inline; {$endif}
  307. function Add(const AKey: TKey): Integer; {$ifdef FGLINLINE} inline; {$endif}
  308. function Find(const AKey: TKey; out Index: Integer): Boolean; {$ifdef FGLINLINE} inline; {$endif}
  309. function TryGetData(const AKey: TKey; out AData: TData): Boolean; {$ifdef FGLINLINE} inline; {$endif}
  310. procedure AddOrSetData(const AKey: TKey; const AData: TData); {$ifdef FGLINLINE} inline; {$endif}
  311. function IndexOf(const AKey: TKey): Integer; {$ifdef FGLINLINE} inline; {$endif}
  312. function IndexOfData(const AData: TData): Integer;
  313. procedure InsertKey(Index: Integer; const AKey: TKey);
  314. procedure InsertKeyData(Index: Integer; const AKey: TKey; const AData: TData);
  315. function Remove(const AKey: TKey): Integer;
  316. property Keys[Index: Integer]: TKey read GetKey write PutKey;
  317. property Data[Index: Integer]: TData read GetData write PutData;
  318. property KeyData[const AKey: TKey]: TData read GetKeyData write PutKeyData; default;
  319. property OnCompare: TKeyCompareFunc read FOnKeyCompare write SetOnKeyCompare; //deprecated;
  320. property OnKeyCompare: TKeyCompareFunc read FOnKeyCompare write SetOnKeyCompare;
  321. property OnDataCompare: TDataCompareFunc read FOnDataCompare write SetOnDataCompare;
  322. end;
  323. generic TFPGMapObject<TKey; TData: TObject> = class(TFPSMap)
  324. private
  325. type
  326. TKeyCompareFunc = function(const Key1, Key2: TKey): Integer;
  327. TDataCompareFunc = function(const Data1, Data2: TData): Integer;
  328. PKey = ^TKey;
  329. // unsed PData = ^TData;
  330. protected
  331. var
  332. FOnKeyCompare: TKeyCompareFunc;
  333. FOnDataCompare: TDataCompareFunc;
  334. FFreeObjects: Boolean;
  335. procedure CopyItem(Src, Dest: Pointer); override;
  336. procedure CopyKey(Src, Dest: Pointer); override;
  337. procedure CopyData(Src, Dest: Pointer); override;
  338. procedure Deref(Item: Pointer); override;
  339. procedure InitOnPtrCompare; override;
  340. function GetKey(Index: Integer): TKey; {$ifdef FGLINLINE} inline; {$endif}
  341. function GetKeyData(const AKey: TKey): TData; {$ifdef FGLINLINE} inline; {$endif}
  342. function GetData(Index: Integer): TData; {$ifdef FGLINLINE} inline; {$endif}
  343. function KeyCompare(Key1, Key2: Pointer): Integer;
  344. function KeyCustomCompare(Key1, Key2: Pointer): Integer;
  345. //function DataCompare(Data1, Data2: Pointer): Integer;
  346. function DataCustomCompare(Data1, Data2: Pointer): Integer;
  347. procedure PutKey(Index: Integer; const NewKey: TKey); {$ifdef FGLINLINE} inline; {$endif}
  348. procedure PutKeyData(const AKey: TKey; const NewData: TData); {$ifdef FGLINLINE} inline; {$endif}
  349. procedure PutData(Index: Integer; const NewData: TData); {$ifdef FGLINLINE} inline; {$endif}
  350. procedure SetOnKeyCompare(NewCompare: TKeyCompareFunc);
  351. procedure SetOnDataCompare(NewCompare: TDataCompareFunc);
  352. public
  353. constructor Create(AFreeObjects: Boolean);
  354. constructor Create;
  355. function Add(const AKey: TKey; const AData: TData): Integer; {$ifdef FGLINLINE} inline; {$endif}
  356. function Add(const AKey: TKey): Integer; {$ifdef FGLINLINE} inline; {$endif}
  357. function Find(const AKey: TKey; out Index: Integer): Boolean; {$ifdef FGLINLINE} inline; {$endif}
  358. function TryGetData(const AKey: TKey; out AData: TData): Boolean; {$ifdef FGLINLINE} inline; {$endif}
  359. procedure AddOrSetData(const AKey: TKey; const AData: TData); {$ifdef FGLINLINE} inline; {$endif}
  360. function IndexOf(const AKey: TKey): Integer; {$ifdef FGLINLINE} inline; {$endif}
  361. function IndexOfData(const AData: TData): Integer;
  362. procedure InsertKey(Index: Integer; const AKey: TKey);
  363. procedure InsertKeyData(Index: Integer; const AKey: TKey; const AData: TData);
  364. function Remove(const AKey: TKey): Integer;
  365. property Keys[Index: Integer]: TKey read GetKey write PutKey;
  366. property Data[Index: Integer]: TData read GetData write PutData;
  367. property KeyData[const AKey: TKey]: TData read GetKeyData write PutKeyData; default;
  368. property OnCompare: TKeyCompareFunc read FOnKeyCompare write SetOnKeyCompare; //deprecated;
  369. property OnKeyCompare: TKeyCompareFunc read FOnKeyCompare write SetOnKeyCompare;
  370. property OnDataCompare: TDataCompareFunc read FOnDataCompare write SetOnDataCompare;
  371. end;
  372. generic TFPGMapInterfacedObjectData<TKey, TData> = class(TFPSMap)
  373. private
  374. type
  375. TKeyCompareFunc = function(const Key1, Key2: TKey): Integer;
  376. TDataCompareFunc = function(const Data1, Data2: TData): Integer;
  377. PKey = ^TKey;
  378. // unsed PData = ^TData;
  379. protected
  380. var
  381. FOnKeyCompare: TKeyCompareFunc;
  382. FOnDataCompare: TDataCompareFunc;
  383. procedure CopyItem(Src, Dest: Pointer); override;
  384. procedure CopyKey(Src, Dest: Pointer); override;
  385. procedure CopyData(Src, Dest: Pointer); override;
  386. procedure Deref(Item: Pointer); override;
  387. procedure InitOnPtrCompare; override;
  388. function GetKey(Index: Integer): TKey; {$ifdef FGLINLINE} inline; {$endif}
  389. function GetKeyData(const AKey: TKey): TData; {$ifdef FGLINLINE} inline; {$endif}
  390. function GetData(Index: Integer): TData; {$ifdef FGLINLINE} inline; {$endif}
  391. function KeyCompare(Key1, Key2: Pointer): Integer;
  392. function KeyCustomCompare(Key1, Key2: Pointer): Integer;
  393. //function DataCompare(Data1, Data2: Pointer): Integer;
  394. function DataCustomCompare(Data1, Data2: Pointer): Integer;
  395. procedure PutKey(Index: Integer; const NewKey: TKey); {$ifdef FGLINLINE} inline; {$endif}
  396. procedure PutKeyData(const AKey: TKey; const NewData: TData); {$ifdef FGLINLINE} inline; {$endif}
  397. procedure PutData(Index: Integer; const NewData: TData); {$ifdef FGLINLINE} inline; {$endif}
  398. procedure SetOnKeyCompare(NewCompare: TKeyCompareFunc);
  399. procedure SetOnDataCompare(NewCompare: TDataCompareFunc);
  400. public
  401. constructor Create;
  402. function Add(const AKey: TKey; const AData: TData): Integer; {$ifdef FGLINLINE} inline; {$endif}
  403. function Add(const AKey: TKey): Integer; {$ifdef FGLINLINE} inline; {$endif}
  404. function Find(const AKey: TKey; out Index: Integer): Boolean; {$ifdef FGLINLINE} inline; {$endif}
  405. function TryGetData(const AKey: TKey; out AData: TData): Boolean; {$ifdef FGLINLINE} inline; {$endif}
  406. procedure AddOrSetData(const AKey: TKey; const AData: TData); {$ifdef FGLINLINE} inline; {$endif}
  407. function IndexOf(const AKey: TKey): Integer; {$ifdef FGLINLINE} inline; {$endif}
  408. function IndexOfData(const AData: TData): Integer;
  409. procedure InsertKey(Index: Integer; const AKey: TKey);
  410. procedure InsertKeyData(Index: Integer; const AKey: TKey; const AData: TData);
  411. function Remove(const AKey: TKey): Integer;
  412. property Keys[Index: Integer]: TKey read GetKey write PutKey;
  413. property Data[Index: Integer]: TData read GetData write PutData;
  414. property KeyData[const AKey: TKey]: TData read GetKeyData write PutKeyData; default;
  415. property OnCompare: TKeyCompareFunc read FOnKeyCompare write SetOnKeyCompare; //deprecated;
  416. property OnKeyCompare: TKeyCompareFunc read FOnKeyCompare write SetOnKeyCompare;
  417. property OnDataCompare: TDataCompareFunc read FOnDataCompare write SetOnDataCompare;
  418. end;
  419. implementation
  420. {$IFDEF FPC_DOTTEDUNITS}
  421. uses
  422. System.RtlConsts;
  423. {$ELSE FPC_DOTTEDUNITS}
  424. uses
  425. rtlconsts;
  426. {$ENDIF FPC_DOTTEDUNITS}
  427. {****************************************************************************
  428. TFPSList
  429. ****************************************************************************}
  430. constructor TFPSList.Create(AItemSize: integer);
  431. begin
  432. inherited Create;
  433. FItemSize := AItemSize;
  434. end;
  435. destructor TFPSList.Destroy;
  436. begin
  437. Clear;
  438. // Clear() does not clear the whole list; there is always a single temp entry
  439. // at the end which is never freed. Take care of that one here.
  440. FreeMem(FList);
  441. inherited Destroy;
  442. end;
  443. procedure TFPSList.CopyItem(Src, Dest: Pointer);
  444. begin
  445. System.Move(Src^, Dest^, FItemSize);
  446. end;
  447. procedure TFPSList.CopyItems(Src, Dest: Pointer; aCount: Integer);
  448. begin
  449. System.Move(Src^, Dest^, FItemSize*aCount);
  450. end;
  451. procedure TFPSList.RaiseIndexError(Index : Integer);
  452. begin
  453. Error(SListIndexError, Index);
  454. end;
  455. function TFPSList.InternalGet(Index: Integer): Pointer;
  456. begin
  457. Result:=FList+Index*ItemSize;
  458. end;
  459. procedure TFPSList.InternalPut(Index: Integer; NewItem: Pointer);
  460. var
  461. ListItem: Pointer;
  462. begin
  463. ListItem := InternalItems[Index];
  464. CopyItem(NewItem, ListItem);
  465. end;
  466. function TFPSList.Get(Index: Integer): Pointer;
  467. begin
  468. CheckIndex(Index);
  469. Result := InternalItems[Index];
  470. end;
  471. procedure TFPSList.Put(Index: Integer; Item: Pointer);
  472. var p : Pointer;
  473. begin
  474. CheckIndex(Index);
  475. p:=InternalItems[Index];
  476. if assigned(p) then
  477. DeRef(p);
  478. InternalItems[Index] := Item;
  479. end;
  480. procedure TFPSList.SetCapacity(NewCapacity: Integer);
  481. begin
  482. if (NewCapacity < FCount) or (NewCapacity > MaxListSize) then
  483. Error(SListCapacityError, NewCapacity);
  484. if NewCapacity = FCapacity then
  485. exit;
  486. ReallocMem(FList, (NewCapacity+1) * FItemSize);
  487. FillChar(InternalItems[FCapacity]^, (NewCapacity+1-FCapacity) * FItemSize, #0);
  488. FCapacity := NewCapacity;
  489. end;
  490. procedure TFPSList.Deref(Item: Pointer);
  491. begin
  492. end;
  493. procedure TFPSList.Deref(FromIndex, ToIndex: Integer);
  494. var
  495. ListItem, ListItemLast: Pointer;
  496. begin
  497. ListItem := InternalItems[FromIndex];
  498. ListItemLast := InternalItems[ToIndex];
  499. repeat
  500. Deref(ListItem);
  501. if ListItem = ListItemLast then
  502. break;
  503. ListItem := PByte(ListItem) + ItemSize;
  504. until false;
  505. end;
  506. procedure TFPSList.SetCount(NewCount: Integer);
  507. begin
  508. if (NewCount < 0) or (NewCount > MaxListSize) then
  509. Error(SListCountError, NewCount);
  510. if NewCount > FCapacity then
  511. SetCapacity(NewCount);
  512. if NewCount > FCount then
  513. FillByte(InternalItems[FCount]^, (NewCount-FCount) * FItemSize, 0)
  514. else if NewCount < FCount then
  515. Deref(NewCount, FCount-1);
  516. FCount := NewCount;
  517. end;
  518. function TFPSList.Add(Item: Pointer): Integer;
  519. begin
  520. if FCount = FCapacity then
  521. Self.Expand;
  522. CopyItem(Item, InternalItems[FCount]);
  523. Result := FCount;
  524. Inc(FCount);
  525. end;
  526. procedure TFPSList.CheckIndex(AIndex : Integer);
  527. begin
  528. if (AIndex < 0) or (AIndex >= FCount) then
  529. Error(SListIndexError, AIndex);
  530. end;
  531. class function TFPSList.ItemIsManaged: Boolean;
  532. begin
  533. Result:=False;
  534. end;
  535. procedure TFPSList.Clear;
  536. begin
  537. if Assigned(FList) then
  538. begin
  539. SetCount(0);
  540. SetCapacity(0);
  541. end;
  542. end;
  543. procedure TFPSList.Delete(Index: Integer);
  544. var
  545. ListItem: Pointer;
  546. begin
  547. CheckIndex(Index);
  548. Dec(FCount);
  549. ListItem := InternalItems[Index];
  550. Deref(ListItem);
  551. System.Move(InternalItems[Index+1]^, ListItem^, (FCount - Index) * FItemSize);
  552. // Shrink the list if appropriate
  553. if (FCapacity > 256) and (FCount < FCapacity shr 2) then
  554. begin
  555. FCapacity := FCapacity shr 1;
  556. ReallocMem(FList, (FCapacity+1) * FItemSize);
  557. end;
  558. { Keep the ending of the list filled with zeros, don't leave garbage data
  559. there. Otherwise, we could accidentally have there a copy of some item
  560. on the list, and accidentally Deref it too soon.
  561. See http://bugs.freepascal.org/view.php?id=20005. }
  562. FillChar(InternalItems[FCount]^, (FCapacity+1-FCount) * FItemSize, #0);
  563. end;
  564. procedure TFPSList.DeleteRange(IndexFrom, IndexTo : Integer);
  565. var
  566. ListItem: Pointer;
  567. I: Integer;
  568. OldCnt : Integer;
  569. begin
  570. CheckIndex(IndexTo);
  571. CheckIndex(IndexFrom);
  572. OldCnt:=FCount;
  573. Dec(FCount,IndexTo-IndexFrom+1);
  574. For I :=IndexFrom To Indexto Do
  575. begin
  576. ListItem := InternalItems[I];
  577. Deref(ListItem);
  578. end;
  579. System.Move(InternalItems[IndexTo+1]^, InternalItems[IndexFrom]^, (OldCnt - IndexTo-1) * FItemSize);
  580. // Shrink the list if appropriate
  581. if (FCapacity > 256) and (FCount < FCapacity shr 2) then
  582. begin
  583. FCapacity := FCapacity shr 1;
  584. ReallocMem(FList, (FCapacity+1) * FItemSize);
  585. end;
  586. { Keep the ending of the list filled with zeros, don't leave garbage data
  587. there. Otherwise, we could accidentally have there a copy of some item
  588. on the list, and accidentally Deref it too soon.
  589. See http://bugs.freepascal.org/view.php?id=20005. }
  590. FillChar(InternalItems[FCount]^, (FCapacity+1-FCount) * FItemSize, #0);
  591. end;
  592. procedure TFPSList.Extract(Item: Pointer; ResultPtr: Pointer);
  593. var
  594. i : Integer;
  595. ListItemPtr : Pointer;
  596. begin
  597. i := IndexOf(Item);
  598. if i >= 0 then
  599. begin
  600. ListItemPtr := InternalItems[i];
  601. System.Move(ListItemPtr^, ResultPtr^, FItemSize);
  602. { fill with zeros, to avoid freeing/decreasing reference on following Delete }
  603. System.FillByte(ListItemPtr^, FItemSize, 0);
  604. Delete(i);
  605. end else
  606. System.FillByte(ResultPtr^, FItemSize, 0);
  607. end;
  608. class procedure TFPSList.Error(const Msg: string; Data: PtrInt);
  609. begin
  610. raise EListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame), get_caller_frame(get_frame);
  611. end;
  612. procedure TFPSList.Exchange(Index1, Index2: Integer);
  613. begin
  614. CheckIndex(Index1);
  615. CheckIndex(Index2);
  616. InternalExchange(Index1, Index2);
  617. end;
  618. procedure TFPSList.InternalExchange(Index1, Index2: Integer);
  619. begin
  620. System.Move(InternalItems[Index1]^, InternalItems[FCapacity]^, FItemSize);
  621. System.Move(InternalItems[Index2]^, InternalItems[Index1]^, FItemSize);
  622. System.Move(InternalItems[FCapacity]^, InternalItems[Index2]^, FItemSize);
  623. end;
  624. function TFPSList.Expand: TFPSList;
  625. var
  626. IncSize : Longint;
  627. begin
  628. if FCount < FCapacity then exit;
  629. IncSize := 4;
  630. if FCapacity > 3 then IncSize := IncSize + 4;
  631. if FCapacity > 8 then IncSize := IncSize + 8;
  632. if FCapacity > 127 then Inc(IncSize, FCapacity shr 2);
  633. SetCapacity(FCapacity + IncSize);
  634. Result := Self;
  635. end;
  636. function TFPSList.GetFirst: Pointer;
  637. begin
  638. If FCount = 0 then
  639. Result := Nil
  640. else
  641. Result := InternalItems[0];
  642. end;
  643. procedure TFPSList.SetFirst(const Value: Pointer);
  644. begin
  645. Put(0, Value);
  646. end;
  647. function TFPSList.IndexOf(Item: Pointer): Integer;
  648. var
  649. ListItem: Pointer;
  650. begin
  651. Result := 0;
  652. ListItem := First;
  653. while (Result < FCount) and (CompareByte(ListItem^, Item^, FItemSize) <> 0) do
  654. begin
  655. Inc(Result);
  656. ListItem := PByte(ListItem)+FItemSize;
  657. end;
  658. if Result = FCount then Result := -1;
  659. end;
  660. function TFPSList.Insert(Index: Integer): Pointer;
  661. begin
  662. if (Index < 0) or (Index > FCount) then
  663. Error(SListIndexError, Index);
  664. if FCount = FCapacity then Self.Expand;
  665. Result := InternalItems[Index];
  666. if Index<FCount then
  667. begin
  668. System.Move(Result^, (Result+FItemSize)^, (FCount - Index) * FItemSize);
  669. { clear for compiler assisted types }
  670. System.FillByte(Result^, FItemSize, 0);
  671. end;
  672. Inc(FCount);
  673. end;
  674. procedure TFPSList.Insert(Index: Integer; Item: Pointer);
  675. begin
  676. CopyItem(Item, Insert(Index));
  677. end;
  678. function TFPSList.GetLast: Pointer;
  679. begin
  680. if FCount = 0 then
  681. Result := nil
  682. else
  683. Result := InternalItems[FCount - 1];
  684. end;
  685. procedure TFPSList.SetLast(const Value: Pointer);
  686. begin
  687. Put(FCount - 1, Value);
  688. end;
  689. procedure TFPSList.Move(CurIndex, NewIndex: Integer);
  690. var
  691. CurItem, NewItem, TmpItem, Src, Dest: Pointer;
  692. MoveCount: Integer;
  693. begin
  694. CheckIndex(CurIndex);
  695. CheckIndex(NewIndex);
  696. if CurIndex = NewIndex then
  697. exit;
  698. CurItem := InternalItems[CurIndex];
  699. NewItem := InternalItems[NewIndex];
  700. TmpItem := InternalItems[FCapacity];
  701. System.Move(CurItem^, TmpItem^, FItemSize);
  702. if NewIndex > CurIndex then
  703. begin
  704. Src := InternalItems[CurIndex+1];
  705. Dest := CurItem;
  706. MoveCount := NewIndex - CurIndex;
  707. end else begin
  708. Src := NewItem;
  709. Dest := InternalItems[NewIndex+1];
  710. MoveCount := CurIndex - NewIndex;
  711. end;
  712. System.Move(Src^, Dest^, MoveCount * FItemSize);
  713. System.Move(TmpItem^, NewItem^, FItemSize);
  714. end;
  715. function TFPSList.Remove(Item: Pointer): Integer;
  716. begin
  717. Result := IndexOf(Item);
  718. if Result <> -1 then
  719. Delete(Result);
  720. end;
  721. const LocalThreshold = 64;
  722. procedure TFPSList.Pack;
  723. var
  724. LItemSize : integer;
  725. NewCount,
  726. i : integer;
  727. pdest,
  728. psrc : Pointer;
  729. localnul : array[0..LocalThreshold-1] of byte;
  730. pnul : pointer;
  731. begin
  732. LItemSize:=FItemSize;
  733. pnul:=@localnul;
  734. if LItemSize>Localthreshold then
  735. getmem(pnul,LItemSize);
  736. fillchar(pnul^,LItemSize,#0);
  737. NewCount:=0;
  738. psrc:=First;
  739. pdest:=psrc;
  740. For I:=0 To FCount-1 Do
  741. begin
  742. if not CompareMem(psrc,pnul,LItemSize) then
  743. begin
  744. System.Move(psrc^, pdest^, LItemSize);
  745. inc(pdest,LItemSIze);
  746. inc(NewCount);
  747. end
  748. else
  749. deref(psrc);
  750. inc(psrc,LitemSize);
  751. end;
  752. if LItemSize>Localthreshold then
  753. FreeMem(pnul,LItemSize);
  754. FCount:=NewCount;
  755. end;
  756. procedure TFPSList.Sort(Compare: TFPSListCompareFunc);
  757. begin
  758. Sort(Compare, {$IFDEF FPC_DOTTEDUNITS}System.{$ENDIF}SortBase.DefaultSortingAlgorithm);
  759. end;
  760. type
  761. PFPSList_Sort_Comparer_Context = ^TFPSList_Sort_Comparer_Context;
  762. TFPSList_Sort_Comparer_Context = record
  763. Compare: TFPSListCompareFunc;
  764. end;
  765. function TFPSList_Sort_Comparer(Item1, Item2, Context: Pointer): Integer;
  766. begin
  767. Result := PFPSList_Sort_Comparer_Context(Context)^.Compare(Item1, Item2);
  768. end;
  769. procedure TFPSList.Sort(Compare: TFPSListCompareFunc; SortingAlgorithm: PSortingAlgorithm);
  770. var
  771. Context: TFPSList_Sort_Comparer_Context;
  772. begin
  773. Context.Compare := Compare;
  774. SortingAlgorithm^.ItemListSorter_ContextComparer(FList, FCount, FItemSize, @TFPSList_Sort_Comparer, @Context);
  775. end;
  776. procedure TFPSList.QuickSort(L, R: Integer; Compare: TFPSListCompareFunc);
  777. var
  778. Context: TFPSList_Sort_Comparer_Context;
  779. SortingAlgorithm: PSortingAlgorithm;
  780. begin
  781. if (R > L) and (L >= 0) then
  782. begin
  783. Context.Compare := Compare;
  784. SortingAlgorithm := {$IFDEF FPC_DOTTEDUNITS}System.{$ENDIF}SortBase.DefaultSortingAlgorithm;
  785. SortingAlgorithm^.ItemListSorter_ContextComparer(FList + FItemSize*L, R-L+1, FItemSize, @TFPSList_Sort_Comparer, @Context);
  786. end;
  787. end;
  788. procedure TFPSList.AddList(Obj: TFPSList);
  789. var
  790. i: Integer;
  791. begin
  792. if Obj.ItemSize <> FItemSize then
  793. Error(SListItemSizeError, 0);
  794. // Do this now.
  795. Capacity:=Capacity+Obj.Count;
  796. if ItemIsManaged then
  797. begin
  798. // nothing for it, need to do it manually to give deref a chance.
  799. For I:=0 to Obj.Count-1 do
  800. Add(Obj[i])
  801. end
  802. else
  803. begin
  804. if Obj.Count=0 then
  805. exit;
  806. CopyItems(Obj.InternalItems[0],InternalItems[FCount],Obj.Count);
  807. FCount:=FCount+Obj.Count;
  808. end
  809. end;
  810. procedure TFPSList.Assign(Obj: TFPSList);
  811. begin
  812. // We must do this check here, to avoid clearing the list.
  813. if Obj.ItemSize <> FItemSize then
  814. Error(SListItemSizeError, 0);
  815. Clear;
  816. AddList(Obj);
  817. end;
  818. {****************************************************************************}
  819. {* TFPGListEnumerator *}
  820. {****************************************************************************}
  821. function TFPGListEnumerator.GetCurrent: T;
  822. begin
  823. Result := T(FList.Items[FPosition]^);
  824. end;
  825. constructor TFPGListEnumerator.Create(AList: TFPSList);
  826. begin
  827. inherited Create;
  828. FList := AList;
  829. FPosition := -1;
  830. end;
  831. function TFPGListEnumerator.MoveNext: Boolean;
  832. begin
  833. inc(FPosition);
  834. Result := FPosition < FList.Count;
  835. end;
  836. {****************************************************************************}
  837. {* TFPGList *}
  838. {****************************************************************************}
  839. constructor TFPGList.Create;
  840. begin
  841. inherited Create(sizeof(T));
  842. end;
  843. procedure TFPGList.CopyItem(Src, Dest: Pointer);
  844. begin
  845. T(Dest^) := T(Src^);
  846. end;
  847. procedure TFPGList.Deref(Item: Pointer);
  848. begin
  849. Finalize(T(Item^));
  850. end;
  851. function TFPGList.Get(Index: Integer): T;
  852. begin
  853. Result := T(inherited Get(Index)^);
  854. end;
  855. function TFPGList.GetList: PTypeList;
  856. begin
  857. Result := PTypeList(@FList);
  858. end;
  859. function TFPGList.ItemPtrCompare(Item1, Item2: Pointer): Integer;
  860. begin
  861. Result := FOnCompare(T(Item1^), T(Item2^));
  862. end;
  863. procedure TFPGList.Put(Index: Integer; const Item: T);
  864. begin
  865. inherited Put(Index, @Item);
  866. end;
  867. function TFPGList.Add(const Item: T): Integer;
  868. begin
  869. Result := inherited Add(@Item);
  870. end;
  871. function TFPGList.Extract(const Item: T): T;
  872. begin
  873. inherited Extract(@Item, @Result);
  874. end;
  875. function TFPGList.GetFirst: T;
  876. begin
  877. if FCount<>0 then
  878. Result := T(inherited GetFirst^)
  879. else
  880. Result:=Default(T);
  881. end;
  882. procedure TFPGList.SetFirst(const Value: T);
  883. begin
  884. inherited SetFirst(@Value);
  885. end;
  886. class function TFPGList.ItemIsManaged: Boolean;
  887. begin
  888. {$IFNDEF VER3_0}
  889. Result:=IsManagedType(T);
  890. {$ELSE}
  891. Result:=True; // Fallback to old behaviour
  892. {$ENDIF}
  893. end;
  894. function TFPGList.GetEnumerator: TFPGListEnumeratorSpec;
  895. begin
  896. Result := TFPGListEnumeratorSpec.Create(Self);
  897. end;
  898. function TFPGList.IndexOf(const Item: T): Integer;
  899. begin
  900. Result := 0;
  901. {$info TODO: fix inlining to work! InternalItems[Result]^}
  902. while (Result < FCount) and (PT(FList)[Result] <> Item) do
  903. Inc(Result);
  904. if Result = FCount then
  905. Result := -1;
  906. end;
  907. procedure TFPGList.Insert(Index: Integer; const Item: T);
  908. begin
  909. T(inherited Insert(Index)^) := Item;
  910. end;
  911. function TFPGList.GetLast: T;
  912. begin
  913. if FCount<>0 then
  914. Result := T(inherited GetLast^)
  915. else
  916. result:=Default(T);
  917. end;
  918. procedure TFPGList.SetLast(const Value: T);
  919. begin
  920. inherited SetLast(@Value);
  921. end;
  922. procedure TFPGList.AddList(Source: TFPGList);
  923. var
  924. i: Integer;
  925. begin
  926. if ItemIsManaged then
  927. begin
  928. Capacity:=Capacity+Source.Count;
  929. for I := 0 to Source.Count - 1 do
  930. Add(Source[i]);
  931. end
  932. else
  933. Inherited AddList(TFPSList(source))
  934. end;
  935. procedure TFPGList.Assign(Source: TFPGList);
  936. begin
  937. if ItemIsManaged then
  938. begin
  939. Clear;
  940. AddList(Source);
  941. end
  942. else
  943. Inherited Assign(TFPSList(source))
  944. end;
  945. function TFPGList.Remove(const Item: T): Integer;
  946. begin
  947. Result := IndexOf(Item);
  948. if Result >= 0 then
  949. Delete(Result);
  950. end;
  951. procedure TFPGList.Sort(Compare: TCompareFunc);
  952. begin
  953. FOnCompare := Compare;
  954. inherited Sort(@ItemPtrCompare);
  955. end;
  956. procedure TFPGList.Sort(Compare: TCompareFunc; SortingAlgorithm: PSortingAlgorithm);
  957. begin
  958. FOnCompare := Compare;
  959. inherited Sort(@ItemPtrCompare, SortingAlgorithm);
  960. end;
  961. {****************************************************************************}
  962. {* TFPGObjectList *}
  963. {****************************************************************************}
  964. constructor TFPGObjectList.Create(FreeObjects: Boolean);
  965. begin
  966. inherited Create;
  967. FFreeObjects := FreeObjects;
  968. end;
  969. procedure TFPGObjectList.CopyItem(Src, Dest: Pointer);
  970. begin
  971. T(Dest^) := T(Src^);
  972. end;
  973. procedure TFPGObjectList.Deref(Item: Pointer);
  974. begin
  975. if FFreeObjects then
  976. T(Item^).Free;
  977. end;
  978. function TFPGObjectList.Get(Index: Integer): T;
  979. begin
  980. Result := T(inherited Get(Index)^);
  981. end;
  982. function TFPGObjectList.GetList: PTypeList;
  983. begin
  984. Result := PTypeList(@FList);
  985. end;
  986. function TFPGObjectList.ItemPtrCompare(Item1, Item2: Pointer): Integer;
  987. begin
  988. Result := FOnCompare(T(Item1^), T(Item2^));
  989. end;
  990. procedure TFPGObjectList.Put(Index: Integer; const Item: T);
  991. begin
  992. inherited Put(Index, @Item);
  993. end;
  994. function TFPGObjectList.Add(const Item: T): Integer;
  995. begin
  996. Result := inherited Add(@Item);
  997. end;
  998. function TFPGObjectList.Extract(const Item: T): T;
  999. begin
  1000. inherited Extract(@Item, @Result);
  1001. end;
  1002. function TFPGObjectList.GetFirst: T;
  1003. begin
  1004. if FCount<>0 then
  1005. Result := T(inherited GetFirst^)
  1006. else
  1007. Result := Default(T)
  1008. end;
  1009. procedure TFPGObjectList.SetFirst(const Value: T);
  1010. begin
  1011. inherited SetFirst(@Value);
  1012. end;
  1013. function TFPGObjectList.GetEnumerator: TFPGListEnumeratorSpec;
  1014. begin
  1015. Result := TFPGListEnumeratorSpec.Create(Self);
  1016. end;
  1017. function TFPGObjectList.IndexOf(const Item: T): Integer;
  1018. begin
  1019. Result :=
  1020. {$if sizeof(pointer) = sizeof(word)}
  1021. IndexWord
  1022. {$elseif sizeof(pointer) = sizeof(dword)}
  1023. IndexDWord
  1024. {$elseif sizeof(pointer) = sizeof(qword)}
  1025. IndexQWord
  1026. {$else}
  1027. {$error unknown pointer size}
  1028. {$endif}
  1029. (FList^, FCount, PtrUint(Pointer(Item)));
  1030. end;
  1031. procedure TFPGObjectList.Insert(Index: Integer; const Item: T);
  1032. begin
  1033. T(inherited Insert(Index)^) := Item;
  1034. end;
  1035. function TFPGObjectList.GetLast: T;
  1036. begin
  1037. if FCount<>0 then
  1038. Result := T(inherited GetLast^)
  1039. else
  1040. Result :=Default(T);
  1041. end;
  1042. procedure TFPGObjectList.SetLast(const Value: T);
  1043. begin
  1044. inherited SetLast(@Value);
  1045. end;
  1046. procedure TFPGObjectList.AddList(Source: TFPGObjectList);
  1047. var
  1048. i: Integer;
  1049. begin
  1050. for I := 0 to Source.Count - 1 do
  1051. Add(Source[i]);
  1052. end;
  1053. procedure TFPGObjectList.Assign(Source: TFPGObjectList);
  1054. begin
  1055. Clear;
  1056. AddList(Source);
  1057. end;
  1058. function TFPGObjectList.Remove(const Item: T): Integer;
  1059. begin
  1060. Result := IndexOf(Item);
  1061. if Result >= 0 then
  1062. Delete(Result);
  1063. end;
  1064. procedure TFPGObjectList.Sort(Compare: TCompareFunc);
  1065. begin
  1066. FOnCompare := Compare;
  1067. inherited Sort(@ItemPtrCompare);
  1068. end;
  1069. procedure TFPGObjectList.Sort(Compare: TCompareFunc; SortingAlgorithm: PSortingAlgorithm);
  1070. begin
  1071. FOnCompare := Compare;
  1072. inherited Sort(@ItemPtrCompare, SortingAlgorithm);
  1073. end;
  1074. {****************************************************************************}
  1075. {* TFPGInterfacedObjectList *}
  1076. {****************************************************************************}
  1077. constructor TFPGInterfacedObjectList.Create;
  1078. begin
  1079. inherited Create;
  1080. end;
  1081. procedure TFPGInterfacedObjectList.CopyItem(Src, Dest: Pointer);
  1082. begin
  1083. if Assigned(Pointer(Dest^)) then
  1084. T(Dest^)._Release;
  1085. Pointer(Dest^) := Pointer(Src^);
  1086. if Assigned(Pointer(Dest^)) then
  1087. T(Dest^)._AddRef;
  1088. end;
  1089. procedure TFPGInterfacedObjectList.Deref(Item: Pointer);
  1090. begin
  1091. if Assigned(Pointer(Item^)) then
  1092. T(Item^)._Release;
  1093. end;
  1094. function TFPGInterfacedObjectList.Get(Index: Integer): T;
  1095. begin
  1096. Result := T(inherited Get(Index)^);
  1097. end;
  1098. function TFPGInterfacedObjectList.GetList: PTypeList;
  1099. begin
  1100. Result := PTypeList(@FList);
  1101. end;
  1102. function TFPGInterfacedObjectList.ItemPtrCompare(Item1, Item2: Pointer): Integer;
  1103. begin
  1104. Result := FOnCompare(T(Item1^), T(Item2^));
  1105. end;
  1106. procedure TFPGInterfacedObjectList.Put(Index: Integer; const Item: T);
  1107. begin
  1108. CheckIndex(Index);
  1109. InternalItems[Index] := @Item; // eventually calls copyitem()
  1110. end;
  1111. function TFPGInterfacedObjectList.Add(const Item: T): Integer;
  1112. begin
  1113. Result := inherited Add(@Item);
  1114. end;
  1115. function TFPGInterfacedObjectList.Extract(const Item: T): T;
  1116. begin
  1117. inherited Extract(@Item, @Result);
  1118. end;
  1119. function TFPGInterfacedObjectList.GetFirst: T;
  1120. begin
  1121. Result := T(inherited GetFirst^);
  1122. end;
  1123. procedure TFPGInterfacedObjectList.SetFirst(const Value: T);
  1124. begin
  1125. inherited SetFirst(@Value);
  1126. end;
  1127. function TFPGInterfacedObjectList.GetEnumerator: TFPGListEnumeratorSpec;
  1128. begin
  1129. Result := TFPGListEnumeratorSpec.Create(Self);
  1130. end;
  1131. function TFPGInterfacedObjectList.IndexOf(const Item: T): Integer;
  1132. begin
  1133. Result :=
  1134. {$if sizeof(pointer) = sizeof(word)}
  1135. IndexWord
  1136. {$elseif sizeof(pointer) = sizeof(dword)}
  1137. IndexDWord
  1138. {$elseif sizeof(pointer) = sizeof(qword)}
  1139. IndexQWord
  1140. {$else}
  1141. {$error unknown pointer size}
  1142. {$endif}
  1143. (FList^, FCount, PtrUint(Pointer(Item)));
  1144. end;
  1145. procedure TFPGInterfacedObjectList.Insert(Index: Integer; const Item: T);
  1146. begin
  1147. T(inherited Insert(Index)^) := Item;
  1148. end;
  1149. function TFPGInterfacedObjectList.GetLast: T;
  1150. begin
  1151. Result := T(inherited GetLast^);
  1152. end;
  1153. procedure TFPGInterfacedObjectList.SetLast(const Value: T);
  1154. begin
  1155. inherited SetLast(@Value);
  1156. end;
  1157. procedure TFPGInterfacedObjectList.Assign(Source: TFPGInterfacedObjectList);
  1158. begin
  1159. Clear;
  1160. AddList(Source);
  1161. end;
  1162. procedure TFPGInterfacedObjectList.AddList(Source: TFPGInterfacedObjectList);
  1163. var
  1164. i: Integer;
  1165. begin
  1166. for I := 0 to Source.Count - 1 do
  1167. Add(Source[i]);
  1168. end;
  1169. function TFPGInterfacedObjectList.Remove(const Item: T): Integer;
  1170. begin
  1171. Result := IndexOf(Item);
  1172. if Result >= 0 then
  1173. Delete(Result);
  1174. end;
  1175. procedure TFPGInterfacedObjectList.Sort(Compare: TCompareFunc);
  1176. begin
  1177. FOnCompare := Compare;
  1178. inherited Sort(@ItemPtrCompare);
  1179. end;
  1180. procedure TFPGInterfacedObjectList.Sort(Compare: TCompareFunc; SortingAlgorithm: PSortingAlgorithm);
  1181. begin
  1182. FOnCompare := Compare;
  1183. inherited Sort(@ItemPtrCompare, SortingAlgorithm);
  1184. end;
  1185. {****************************************************************************
  1186. TFPSMap
  1187. ****************************************************************************}
  1188. constructor TFPSMap.Create(AKeySize: Integer; ADataSize: integer);
  1189. begin
  1190. inherited Create(AKeySize+ADataSize);
  1191. FKeySize := AKeySize;
  1192. FDataSize := ADataSize;
  1193. InitOnPtrCompare;
  1194. end;
  1195. procedure TFPSMap.CopyKey(Src, Dest: Pointer);
  1196. begin
  1197. System.Move(Src^, Dest^, FKeySize);
  1198. end;
  1199. procedure TFPSMap.CopyData(Src, Dest: Pointer);
  1200. begin
  1201. System.Move(Src^, Dest^, FDataSize);
  1202. end;
  1203. function TFPSMap.GetKey(Index: Integer): Pointer;
  1204. begin
  1205. Result := Items[Index];
  1206. end;
  1207. function TFPSMap.GetData(Index: Integer): Pointer;
  1208. begin
  1209. Result := PByte(Items[Index])+FKeySize;
  1210. end;
  1211. function TFPSMap.GetKeyData(AKey: Pointer): Pointer;
  1212. var
  1213. I: Integer;
  1214. begin
  1215. I := IndexOf(AKey);
  1216. if I >= 0 then
  1217. Result := InternalItems[I]+FKeySize
  1218. else
  1219. Error(SMapKeyError, PtrUInt(AKey));
  1220. end;
  1221. function TFPSMap.BinaryCompareKey(Key1, Key2: Pointer): Integer;
  1222. begin
  1223. Result := CompareByte(Key1^, Key2^, FKeySize);
  1224. end;
  1225. function TFPSMap.BinaryCompareData(Data1, Data2: Pointer): Integer;
  1226. begin
  1227. Result := CompareByte(Data1^, Data2^, FDataSize);
  1228. end;
  1229. procedure TFPSMap.SetOnKeyPtrCompare(Proc: TFPSListCompareFunc);
  1230. begin
  1231. if Proc <> nil then
  1232. FOnKeyPtrCompare := Proc
  1233. else
  1234. FOnKeyPtrCompare := @BinaryCompareKey;
  1235. end;
  1236. procedure TFPSMap.SetOnDataPtrCompare(Proc: TFPSListCompareFunc);
  1237. begin
  1238. if Proc <> nil then
  1239. FOnDataPtrCompare := Proc
  1240. else
  1241. FOnDataPtrCompare := @BinaryCompareData;
  1242. end;
  1243. procedure TFPSMap.InitOnPtrCompare;
  1244. begin
  1245. SetOnKeyPtrCompare(nil);
  1246. SetOnDataPtrCompare(nil);
  1247. end;
  1248. procedure TFPSMap.PutKey(Index: Integer; AKey: Pointer);
  1249. begin
  1250. if FSorted then
  1251. Error(SSortedListError, 0);
  1252. CopyKey(AKey, Items[Index]);
  1253. end;
  1254. procedure TFPSMap.PutData(Index: Integer; AData: Pointer);
  1255. begin
  1256. CopyData(AData, PByte(Items[Index])+FKeySize);
  1257. end;
  1258. procedure TFPSMap.PutKeyData(AKey: Pointer; NewData: Pointer);
  1259. var
  1260. I: Integer;
  1261. begin
  1262. I := IndexOf(AKey);
  1263. if I >= 0 then
  1264. Data[I] := NewData
  1265. else
  1266. Add(AKey, NewData);
  1267. end;
  1268. procedure TFPSMap.SetSorted(Value: Boolean);
  1269. begin
  1270. if Value = FSorted then exit;
  1271. FSorted := Value;
  1272. if Value then Sort;
  1273. end;
  1274. function TFPSMap.Add(AKey: Pointer): Integer;
  1275. begin
  1276. if Sorted then
  1277. begin
  1278. if Find(AKey, Result) then
  1279. case Duplicates of
  1280. dupIgnore: exit;
  1281. dupError: Error(SDuplicateItem, 0)
  1282. end;
  1283. end else
  1284. Result := Count;
  1285. CopyKey(AKey, inherited Insert(Result));
  1286. end;
  1287. function TFPSMap.Add(AKey, AData: Pointer): Integer;
  1288. begin
  1289. Result := Add(AKey);
  1290. Data[Result] := AData;
  1291. end;
  1292. function TFPSMap.Find(AKey: Pointer; out Index: Integer): Boolean;
  1293. { Searches for the first item <= Key, returns True if exact match,
  1294. sets index to the index of the found string. }
  1295. var
  1296. I,L,R,Dir: Integer;
  1297. begin
  1298. Result := false;
  1299. Index := -1;
  1300. if not Sorted then
  1301. raise EListError.Create(SErrFindNeedsSortedList);
  1302. // Use binary search.
  1303. L := 0;
  1304. R := FCount-1;
  1305. while L<=R do
  1306. begin
  1307. I := L + (R - L) div 2;
  1308. Dir := FOnKeyPtrCompare(Items[I], AKey);
  1309. if Dir < 0 then
  1310. L := I+1
  1311. else begin
  1312. R := I-1;
  1313. if Dir = 0 then
  1314. begin
  1315. Result := true;
  1316. if Duplicates <> dupAccept then
  1317. L := I;
  1318. end;
  1319. end;
  1320. end;
  1321. Index := L;
  1322. end;
  1323. function TFPSMap.LinearIndexOf(AKey: Pointer): Integer;
  1324. var
  1325. ListItem: Pointer;
  1326. begin
  1327. Result := 0;
  1328. ListItem := First;
  1329. while (Result < FCount) and (FOnKeyPtrCompare(ListItem, AKey) <> 0) do
  1330. begin
  1331. Inc(Result);
  1332. ListItem := PByte(ListItem)+FItemSize;
  1333. end;
  1334. if Result = FCount then Result := -1;
  1335. end;
  1336. function TFPSMap.IndexOf(AKey: Pointer): Integer;
  1337. begin
  1338. if Sorted then
  1339. begin
  1340. if not Find(AKey, Result) then
  1341. Result := -1;
  1342. end else
  1343. Result := LinearIndexOf(AKey);
  1344. end;
  1345. function TFPSMap.IndexOfData(AData: Pointer): Integer;
  1346. var
  1347. ListItem: Pointer;
  1348. begin
  1349. Result := 0;
  1350. ListItem := First+FKeySize;
  1351. while (Result < FCount) and (FOnDataPtrCompare(ListItem, AData) <> 0) do
  1352. begin
  1353. Inc(Result);
  1354. ListItem := PByte(ListItem)+FItemSize;
  1355. end;
  1356. if Result = FCount then Result := -1;
  1357. end;
  1358. function TFPSMap.Insert(Index: Integer): Pointer;
  1359. begin
  1360. if FSorted then
  1361. Error(SSortedListError, 0);
  1362. Result := inherited Insert(Index);
  1363. end;
  1364. procedure TFPSMap.Insert(Index: Integer; out AKey, AData: Pointer);
  1365. begin
  1366. AKey := Insert(Index);
  1367. AData := PByte(AKey) + FKeySize;
  1368. end;
  1369. procedure TFPSMap.InsertKey(Index: Integer; AKey: Pointer);
  1370. begin
  1371. CopyKey(AKey, Insert(Index));
  1372. end;
  1373. procedure TFPSMap.InsertKeyData(Index: Integer; AKey, AData: Pointer);
  1374. var
  1375. ListItem: Pointer;
  1376. begin
  1377. ListItem := Insert(Index);
  1378. CopyKey(AKey, ListItem);
  1379. CopyData(AData, PByte(ListItem)+FKeySize);
  1380. end;
  1381. function TFPSMap.Remove(AKey: Pointer): Integer;
  1382. begin
  1383. Result := IndexOf(AKey);
  1384. if Result >= 0 then
  1385. Delete(Result);
  1386. end;
  1387. procedure TFPSMap.Sort;
  1388. begin
  1389. inherited Sort(FOnKeyPtrCompare);
  1390. end;
  1391. procedure TFPSMap.Sort(SortingAlgorithm: PSortingAlgorithm);
  1392. begin
  1393. inherited Sort(FOnKeyPtrCompare, SortingAlgorithm);
  1394. end;
  1395. {****************************************************************************
  1396. TFPGMap
  1397. ****************************************************************************}
  1398. constructor TFPGMap.Create;
  1399. begin
  1400. inherited Create(SizeOf(TKey), SizeOf(TData));
  1401. end;
  1402. procedure TFPGMap.CopyItem(Src, Dest: Pointer);
  1403. begin
  1404. CopyKey(Src, Dest);
  1405. CopyData(PByte(Src)+KeySize, PByte(Dest)+KeySize);
  1406. end;
  1407. procedure TFPGMap.CopyKey(Src, Dest: Pointer);
  1408. begin
  1409. TKey(Dest^) := TKey(Src^);
  1410. end;
  1411. procedure TFPGMap.CopyData(Src, Dest: Pointer);
  1412. begin
  1413. TData(Dest^) := TData(Src^);
  1414. end;
  1415. procedure TFPGMap.Deref(Item: Pointer);
  1416. begin
  1417. Finalize(TKey(Item^));
  1418. Finalize(TData(Pointer(PByte(Item)+KeySize)^));
  1419. end;
  1420. function TFPGMap.GetKey(Index: Integer): TKey;
  1421. begin
  1422. Result := TKey(inherited GetKey(Index)^);
  1423. end;
  1424. function TFPGMap.GetData(Index: Integer): TData;
  1425. begin
  1426. Result := TData(inherited GetData(Index)^);
  1427. end;
  1428. function TFPGMap.GetKeyData(const AKey: TKey): TData;
  1429. begin
  1430. Result := TData(inherited GetKeyData(@AKey)^);
  1431. end;
  1432. function TFPGMap.KeyCompare(Key1, Key2: Pointer): Integer;
  1433. begin
  1434. if PKey(Key1)^ < PKey(Key2)^ then
  1435. Result := -1
  1436. else if PKey(Key1)^ > PKey(Key2)^ then
  1437. Result := 1
  1438. else
  1439. Result := 0;
  1440. end;
  1441. {function TFPGMap.DataCompare(Data1, Data2: Pointer): Integer;
  1442. begin
  1443. if PData(Data1)^ < PData(Data2)^ then
  1444. Result := -1
  1445. else if PData(Data1)^ > PData(Data2)^ then
  1446. Result := 1
  1447. else
  1448. Result := 0;
  1449. end;}
  1450. function TFPGMap.KeyCustomCompare(Key1, Key2: Pointer): Integer;
  1451. begin
  1452. Result := FOnKeyCompare(TKey(Key1^), TKey(Key2^));
  1453. end;
  1454. function TFPGMap.DataCustomCompare(Data1, Data2: Pointer): Integer;
  1455. begin
  1456. Result := FOnDataCompare(TData(Data1^), TData(Data2^));
  1457. end;
  1458. procedure TFPGMap.SetOnKeyCompare(NewCompare: TKeyCompareFunc);
  1459. begin
  1460. FOnKeyCompare := NewCompare;
  1461. if NewCompare <> nil then
  1462. OnKeyPtrCompare := @KeyCustomCompare
  1463. else
  1464. OnKeyPtrCompare := @KeyCompare;
  1465. end;
  1466. procedure TFPGMap.SetOnDataCompare(NewCompare: TDataCompareFunc);
  1467. begin
  1468. FOnDataCompare := NewCompare;
  1469. if NewCompare <> nil then
  1470. OnDataPtrCompare := @DataCustomCompare
  1471. else
  1472. OnDataPtrCompare := nil;
  1473. end;
  1474. procedure TFPGMap.InitOnPtrCompare;
  1475. begin
  1476. SetOnKeyCompare(nil);
  1477. SetOnDataCompare(nil);
  1478. end;
  1479. procedure TFPGMap.PutKey(Index: Integer; const NewKey: TKey);
  1480. begin
  1481. inherited PutKey(Index, @NewKey);
  1482. end;
  1483. procedure TFPGMap.PutData(Index: Integer; const NewData: TData);
  1484. begin
  1485. inherited PutData(Index, @NewData);
  1486. end;
  1487. procedure TFPGMap.PutKeyData(const AKey: TKey; const NewData: TData);
  1488. begin
  1489. inherited PutKeyData(@AKey, @NewData);
  1490. end;
  1491. function TFPGMap.Add(const AKey: TKey): Integer;
  1492. begin
  1493. Result := inherited Add(@AKey);
  1494. end;
  1495. function TFPGMap.Add(const AKey: TKey; const AData: TData): Integer;
  1496. begin
  1497. Result := inherited Add(@AKey, @AData);
  1498. end;
  1499. function TFPGMap.Find(const AKey: TKey; out Index: Integer): Boolean;
  1500. begin
  1501. Result := inherited Find(@AKey, Index);
  1502. end;
  1503. function TFPGMap.TryGetData(const AKey: TKey; out AData: TData): Boolean;
  1504. var
  1505. I: Integer;
  1506. begin
  1507. I := IndexOf(AKey);
  1508. Result := (I >= 0);
  1509. if Result then
  1510. AData := TData(inherited GetData(I)^)
  1511. else
  1512. AData := Default(TData);
  1513. end;
  1514. procedure TFPGMap.AddOrSetData(const AKey: TKey; const AData: TData);
  1515. begin
  1516. inherited PutKeyData(@AKey, @AData);
  1517. end;
  1518. function TFPGMap.IndexOf(const AKey: TKey): Integer;
  1519. begin
  1520. Result := inherited IndexOf(@AKey);
  1521. end;
  1522. function TFPGMap.IndexOfData(const AData: TData): Integer;
  1523. begin
  1524. { TODO: loop ? }
  1525. Result := inherited IndexOfData(@AData);
  1526. end;
  1527. procedure TFPGMap.InsertKey(Index: Integer; const AKey: TKey);
  1528. begin
  1529. inherited InsertKey(Index, @AKey);
  1530. end;
  1531. procedure TFPGMap.InsertKeyData(Index: Integer; const AKey: TKey; const AData: TData);
  1532. begin
  1533. inherited InsertKeyData(Index, @AKey, @AData);
  1534. end;
  1535. function TFPGMap.Remove(const AKey: TKey): Integer;
  1536. begin
  1537. Result := inherited Remove(@AKey);
  1538. end;
  1539. {****************************************************************************
  1540. TFPGMapObject
  1541. ****************************************************************************}
  1542. constructor TFPGMapObject.Create(AFreeObjects: Boolean);
  1543. begin
  1544. inherited Create(SizeOf(TKey), SizeOf(TData));
  1545. FFreeObjects := AFreeObjects;
  1546. end;
  1547. constructor TFPGMapObject.Create;
  1548. begin
  1549. Create(True);
  1550. end;
  1551. procedure TFPGMapObject.CopyItem(Src, Dest: Pointer);
  1552. begin
  1553. CopyKey(Src, Dest);
  1554. CopyData(PByte(Src)+KeySize, PByte(Dest)+KeySize);
  1555. end;
  1556. procedure TFPGMapObject.CopyKey(Src, Dest: Pointer);
  1557. begin
  1558. TKey(Dest^) := TKey(Src^);
  1559. end;
  1560. procedure TFPGMapObject.CopyData(Src, Dest: Pointer);
  1561. begin
  1562. if Assigned(Pointer(Dest^)) And FFreeObjects then
  1563. TData(Dest^).Free;
  1564. TData(Dest^) := TData(Src^);
  1565. end;
  1566. procedure TFPGMapObject.Deref(Item: Pointer);
  1567. begin
  1568. Finalize(TKey(Item^));
  1569. if Assigned(PPointer(PByte(Item)+KeySize)^) and FFreeObjects then
  1570. TData(Pointer(PByte(Item)+KeySize)^).Free;
  1571. end;
  1572. function TFPGMapObject.GetKey(Index: Integer): TKey;
  1573. begin
  1574. Result := TKey(inherited GetKey(Index)^);
  1575. end;
  1576. function TFPGMapObject.GetData(Index: Integer): TData;
  1577. begin
  1578. Result := TData(inherited GetData(Index)^);
  1579. end;
  1580. function TFPGMapObject.GetKeyData(const AKey: TKey): TData;
  1581. begin
  1582. Result := TData(inherited GetKeyData(@AKey)^);
  1583. end;
  1584. function TFPGMapObject.KeyCompare(Key1, Key2: Pointer): Integer;
  1585. begin
  1586. if PKey(Key1)^ < PKey(Key2)^ then
  1587. Result := -1
  1588. else if PKey(Key1)^ > PKey(Key2)^ then
  1589. Result := 1
  1590. else
  1591. Result := 0;
  1592. end;
  1593. {function TFPGMapObject.DataCompare(Data1, Data2: Pointer): Integer;
  1594. begin
  1595. if PData(Data1)^ < PData(Data2)^ then
  1596. Result := -1
  1597. else if PData(Data1)^ > PData(Data2)^ then
  1598. Result := 1
  1599. else
  1600. Result := 0;
  1601. end;}
  1602. function TFPGMapObject.KeyCustomCompare(Key1, Key2: Pointer): Integer;
  1603. begin
  1604. Result := FOnKeyCompare(TKey(Key1^), TKey(Key2^));
  1605. end;
  1606. function TFPGMapObject.DataCustomCompare(Data1, Data2: Pointer): Integer;
  1607. begin
  1608. Result := FOnDataCompare(TData(Data1^), TData(Data2^));
  1609. end;
  1610. procedure TFPGMapObject.SetOnKeyCompare(NewCompare: TKeyCompareFunc);
  1611. begin
  1612. FOnKeyCompare := NewCompare;
  1613. if NewCompare <> nil then
  1614. OnKeyPtrCompare := @KeyCustomCompare
  1615. else
  1616. OnKeyPtrCompare := @KeyCompare;
  1617. end;
  1618. procedure TFPGMapObject.SetOnDataCompare(NewCompare: TDataCompareFunc);
  1619. begin
  1620. FOnDataCompare := NewCompare;
  1621. if NewCompare <> nil then
  1622. OnDataPtrCompare := @DataCustomCompare
  1623. else
  1624. OnDataPtrCompare := nil;
  1625. end;
  1626. procedure TFPGMapObject.InitOnPtrCompare;
  1627. begin
  1628. SetOnKeyCompare(nil);
  1629. SetOnDataCompare(nil);
  1630. end;
  1631. procedure TFPGMapObject.PutKey(Index: Integer; const NewKey: TKey);
  1632. begin
  1633. inherited PutKey(Index, @NewKey);
  1634. end;
  1635. procedure TFPGMapObject.PutData(Index: Integer; const NewData: TData);
  1636. begin
  1637. inherited PutData(Index, @NewData);
  1638. end;
  1639. procedure TFPGMapObject.PutKeyData(const AKey: TKey; const NewData: TData);
  1640. begin
  1641. inherited PutKeyData(@AKey, @NewData);
  1642. end;
  1643. function TFPGMapObject.Add(const AKey: TKey): Integer;
  1644. begin
  1645. Result := inherited Add(@AKey);
  1646. end;
  1647. function TFPGMapObject.Add(const AKey: TKey; const AData: TData): Integer;
  1648. begin
  1649. Result := inherited Add(@AKey, @AData);
  1650. end;
  1651. function TFPGMapObject.Find(const AKey: TKey; out Index: Integer): Boolean;
  1652. begin
  1653. Result := inherited Find(@AKey, Index);
  1654. end;
  1655. function TFPGMapObject.TryGetData(const AKey: TKey; out AData: TData): Boolean;
  1656. var
  1657. I: Integer;
  1658. begin
  1659. I := IndexOf(AKey);
  1660. Result := (I >= 0);
  1661. if Result then
  1662. AData := TData(inherited GetData(I)^)
  1663. else
  1664. AData := Default(TData);
  1665. end;
  1666. procedure TFPGMapObject.AddOrSetData(const AKey: TKey; const AData: TData);
  1667. begin
  1668. inherited PutKeyData(@AKey, @AData);
  1669. end;
  1670. function TFPGMapObject.IndexOf(const AKey: TKey): Integer;
  1671. begin
  1672. Result := inherited IndexOf(@AKey);
  1673. end;
  1674. function TFPGMapObject.IndexOfData(const AData: TData): Integer;
  1675. begin
  1676. { TODO: loop ? }
  1677. Result := inherited IndexOfData(@AData);
  1678. end;
  1679. procedure TFPGMapObject.InsertKey(Index: Integer; const AKey: TKey);
  1680. begin
  1681. inherited InsertKey(Index, @AKey);
  1682. end;
  1683. procedure TFPGMapObject.InsertKeyData(Index: Integer; const AKey: TKey; const AData: TData);
  1684. begin
  1685. inherited InsertKeyData(Index, @AKey, @AData);
  1686. end;
  1687. function TFPGMapObject.Remove(const AKey: TKey): Integer;
  1688. begin
  1689. Result := inherited Remove(@AKey);
  1690. end;
  1691. {****************************************************************************
  1692. TFPGMapInterfacedObjectData
  1693. ****************************************************************************}
  1694. constructor TFPGMapInterfacedObjectData.Create;
  1695. begin
  1696. inherited Create(SizeOf(TKey), SizeOf(TData));
  1697. end;
  1698. procedure TFPGMapInterfacedObjectData.CopyItem(Src, Dest: Pointer);
  1699. begin
  1700. CopyKey(Src, Dest);
  1701. CopyData(PByte(Src)+KeySize, PByte(Dest)+KeySize);
  1702. end;
  1703. procedure TFPGMapInterfacedObjectData.CopyKey(Src, Dest: Pointer);
  1704. begin
  1705. TKey(Dest^) := TKey(Src^);
  1706. end;
  1707. procedure TFPGMapInterfacedObjectData.CopyData(Src, Dest: Pointer);
  1708. begin
  1709. if Assigned(Pointer(Dest^)) then
  1710. TData(Dest^)._Release;
  1711. TData(Dest^) := TData(Src^);
  1712. if Assigned(Pointer(Dest^)) then
  1713. TData(Dest^)._AddRef;
  1714. end;
  1715. procedure TFPGMapInterfacedObjectData.Deref(Item: Pointer);
  1716. begin
  1717. Finalize(TKey(Item^));
  1718. if Assigned(PPointer(PByte(Item)+KeySize)^) then
  1719. TData(Pointer(PByte(Item)+KeySize)^)._Release;
  1720. end;
  1721. function TFPGMapInterfacedObjectData.GetKey(Index: Integer): TKey;
  1722. begin
  1723. Result := TKey(inherited GetKey(Index)^);
  1724. end;
  1725. function TFPGMapInterfacedObjectData.GetData(Index: Integer): TData;
  1726. begin
  1727. Result := TData(inherited GetData(Index)^);
  1728. end;
  1729. function TFPGMapInterfacedObjectData.GetKeyData(const AKey: TKey): TData;
  1730. begin
  1731. Result := TData(inherited GetKeyData(@AKey)^);
  1732. end;
  1733. function TFPGMapInterfacedObjectData.KeyCompare(Key1, Key2: Pointer): Integer;
  1734. begin
  1735. if PKey(Key1)^ < PKey(Key2)^ then
  1736. Result := -1
  1737. else if PKey(Key1)^ > PKey(Key2)^ then
  1738. Result := 1
  1739. else
  1740. Result := 0;
  1741. end;
  1742. {function TFPGMapInterfacedObjectData.DataCompare(Data1, Data2: Pointer): Integer;
  1743. begin
  1744. if PData(Data1)^ < PData(Data2)^ then
  1745. Result := -1
  1746. else if PData(Data1)^ > PData(Data2)^ then
  1747. Result := 1
  1748. else
  1749. Result := 0;
  1750. end;}
  1751. function TFPGMapInterfacedObjectData.KeyCustomCompare(Key1, Key2: Pointer): Integer;
  1752. begin
  1753. Result := FOnKeyCompare(TKey(Key1^), TKey(Key2^));
  1754. end;
  1755. function TFPGMapInterfacedObjectData.DataCustomCompare(Data1, Data2: Pointer): Integer;
  1756. begin
  1757. Result := FOnDataCompare(TData(Data1^), TData(Data2^));
  1758. end;
  1759. procedure TFPGMapInterfacedObjectData.SetOnKeyCompare(NewCompare: TKeyCompareFunc);
  1760. begin
  1761. FOnKeyCompare := NewCompare;
  1762. if NewCompare <> nil then
  1763. OnKeyPtrCompare := @KeyCustomCompare
  1764. else
  1765. OnKeyPtrCompare := @KeyCompare;
  1766. end;
  1767. procedure TFPGMapInterfacedObjectData.SetOnDataCompare(NewCompare: TDataCompareFunc);
  1768. begin
  1769. FOnDataCompare := NewCompare;
  1770. if NewCompare <> nil then
  1771. OnDataPtrCompare := @DataCustomCompare
  1772. else
  1773. OnDataPtrCompare := nil;
  1774. end;
  1775. procedure TFPGMapInterfacedObjectData.InitOnPtrCompare;
  1776. begin
  1777. SetOnKeyCompare(nil);
  1778. SetOnDataCompare(nil);
  1779. end;
  1780. procedure TFPGMapInterfacedObjectData.PutKey(Index: Integer; const NewKey: TKey);
  1781. begin
  1782. inherited PutKey(Index, @NewKey);
  1783. end;
  1784. procedure TFPGMapInterfacedObjectData.PutData(Index: Integer; const NewData: TData);
  1785. begin
  1786. inherited PutData(Index, @NewData);
  1787. end;
  1788. procedure TFPGMapInterfacedObjectData.PutKeyData(const AKey: TKey; const NewData: TData);
  1789. begin
  1790. inherited PutKeyData(@AKey, @NewData);
  1791. end;
  1792. function TFPGMapInterfacedObjectData.Add(const AKey: TKey): Integer;
  1793. begin
  1794. Result := inherited Add(@AKey);
  1795. end;
  1796. function TFPGMapInterfacedObjectData.Add(const AKey: TKey; const AData: TData): Integer;
  1797. begin
  1798. Result := inherited Add(@AKey, @AData);
  1799. end;
  1800. function TFPGMapInterfacedObjectData.Find(const AKey: TKey; out Index: Integer): Boolean;
  1801. begin
  1802. Result := inherited Find(@AKey, Index);
  1803. end;
  1804. function TFPGMapInterfacedObjectData.TryGetData(const AKey: TKey; out AData: TData): Boolean;
  1805. var
  1806. I: Integer;
  1807. begin
  1808. I := IndexOf(AKey);
  1809. Result := (I >= 0);
  1810. if Result then
  1811. AData := TData(inherited GetData(I)^)
  1812. else
  1813. AData := Default(TData);
  1814. end;
  1815. procedure TFPGMapInterfacedObjectData.AddOrSetData(const AKey: TKey;
  1816. const AData: TData);
  1817. begin
  1818. inherited PutKeyData(@AKey, @AData);
  1819. end;
  1820. function TFPGMapInterfacedObjectData.IndexOf(const AKey: TKey): Integer;
  1821. begin
  1822. Result := inherited IndexOf(@AKey);
  1823. end;
  1824. function TFPGMapInterfacedObjectData.IndexOfData(const AData: TData): Integer;
  1825. begin
  1826. { TODO: loop ? }
  1827. Result := inherited IndexOfData(@AData);
  1828. end;
  1829. procedure TFPGMapInterfacedObjectData.InsertKey(Index: Integer; const AKey: TKey);
  1830. begin
  1831. inherited InsertKey(Index, @AKey);
  1832. end;
  1833. procedure TFPGMapInterfacedObjectData.InsertKeyData(Index: Integer; const AKey: TKey; const AData: TData);
  1834. begin
  1835. inherited InsertKeyData(Index, @AKey, @AData);
  1836. end;
  1837. function TFPGMapInterfacedObjectData.Remove(const AKey: TKey): Integer;
  1838. begin
  1839. Result := inherited Remove(@AKey);
  1840. end;
  1841. end.