fgl.pp 59 KB

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