fgl.pp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  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. Var
  992. P: Pointer;
  993. begin
  994. if FCount<>0 then
  995. Result := T(inherited GetFirst^)
  996. else
  997. Result := Default(T)
  998. end;
  999. procedure TFPGObjectList.SetFirst(const Value: T);
  1000. begin
  1001. inherited SetFirst(@Value);
  1002. end;
  1003. function TFPGObjectList.GetEnumerator: TFPGListEnumeratorSpec;
  1004. begin
  1005. Result := TFPGListEnumeratorSpec.Create(Self);
  1006. end;
  1007. function TFPGObjectList.IndexOf(const Item: T): Integer;
  1008. begin
  1009. Result := 0;
  1010. {$info TODO: fix inlining to work! InternalItems[Result]^}
  1011. while (Result < FCount) and (PT(FList)[Result] <> Item) do
  1012. Inc(Result);
  1013. if Result = FCount then
  1014. Result := -1;
  1015. end;
  1016. procedure TFPGObjectList.Insert(Index: Integer; const Item: T);
  1017. begin
  1018. T(inherited Insert(Index)^) := Item;
  1019. end;
  1020. function TFPGObjectList.GetLast: T;
  1021. begin
  1022. if FCount<>0 then
  1023. Result := T(inherited GetLast^)
  1024. else
  1025. Result :=Default(T);
  1026. end;
  1027. procedure TFPGObjectList.SetLast(const Value: T);
  1028. begin
  1029. inherited SetLast(@Value);
  1030. end;
  1031. procedure TFPGObjectList.AddList(Source: TFPGObjectList);
  1032. var
  1033. i: Integer;
  1034. begin
  1035. for I := 0 to Source.Count - 1 do
  1036. Add(Source[i]);
  1037. end;
  1038. procedure TFPGObjectList.Assign(Source: TFPGObjectList);
  1039. begin
  1040. Clear;
  1041. AddList(Source);
  1042. end;
  1043. function TFPGObjectList.Remove(const Item: T): Integer;
  1044. begin
  1045. Result := IndexOf(Item);
  1046. if Result >= 0 then
  1047. Delete(Result);
  1048. end;
  1049. procedure TFPGObjectList.Sort(Compare: TCompareFunc);
  1050. begin
  1051. FOnCompare := Compare;
  1052. inherited Sort(@ItemPtrCompare);
  1053. end;
  1054. procedure TFPGObjectList.Sort(Compare: TCompareFunc; SortingAlgorithm: PSortingAlgorithm);
  1055. begin
  1056. FOnCompare := Compare;
  1057. inherited Sort(@ItemPtrCompare, SortingAlgorithm);
  1058. end;
  1059. {****************************************************************************}
  1060. {* TFPGInterfacedObjectList *}
  1061. {****************************************************************************}
  1062. constructor TFPGInterfacedObjectList.Create;
  1063. begin
  1064. inherited Create;
  1065. end;
  1066. procedure TFPGInterfacedObjectList.CopyItem(Src, Dest: Pointer);
  1067. begin
  1068. if Assigned(Pointer(Dest^)) then
  1069. T(Dest^)._Release;
  1070. Pointer(Dest^) := Pointer(Src^);
  1071. if Assigned(Pointer(Dest^)) then
  1072. T(Dest^)._AddRef;
  1073. end;
  1074. procedure TFPGInterfacedObjectList.Deref(Item: Pointer);
  1075. begin
  1076. if Assigned(Pointer(Item^)) then
  1077. T(Item^)._Release;
  1078. end;
  1079. function TFPGInterfacedObjectList.Get(Index: Integer): T;
  1080. begin
  1081. Result := T(inherited Get(Index)^);
  1082. end;
  1083. function TFPGInterfacedObjectList.GetList: PTypeList;
  1084. begin
  1085. Result := PTypeList(@FList);
  1086. end;
  1087. function TFPGInterfacedObjectList.ItemPtrCompare(Item1, Item2: Pointer): Integer;
  1088. begin
  1089. Result := FOnCompare(T(Item1^), T(Item2^));
  1090. end;
  1091. procedure TFPGInterfacedObjectList.Put(Index: Integer; const Item: T);
  1092. begin
  1093. CheckIndex(Index);
  1094. InternalItems[Index] := @Item; // eventually calls copyitem()
  1095. end;
  1096. function TFPGInterfacedObjectList.Add(const Item: T): Integer;
  1097. begin
  1098. Result := inherited Add(@Item);
  1099. end;
  1100. function TFPGInterfacedObjectList.Extract(const Item: T): T;
  1101. begin
  1102. inherited Extract(@Item, @Result);
  1103. end;
  1104. function TFPGInterfacedObjectList.GetFirst: T;
  1105. begin
  1106. Result := T(inherited GetFirst^);
  1107. end;
  1108. procedure TFPGInterfacedObjectList.SetFirst(const Value: T);
  1109. begin
  1110. inherited SetFirst(@Value);
  1111. end;
  1112. function TFPGInterfacedObjectList.GetEnumerator: TFPGListEnumeratorSpec;
  1113. begin
  1114. Result := TFPGListEnumeratorSpec.Create(Self);
  1115. end;
  1116. function TFPGInterfacedObjectList.IndexOf(const Item: T): Integer;
  1117. begin
  1118. Result := 0;
  1119. {$info TODO: fix inlining to work! InternalItems[Result]^}
  1120. while (Result < FCount) and (PT(FList)[Result] <> Item) do
  1121. Inc(Result);
  1122. if Result = FCount then
  1123. Result := -1;
  1124. end;
  1125. procedure TFPGInterfacedObjectList.Insert(Index: Integer; const Item: T);
  1126. begin
  1127. T(inherited Insert(Index)^) := Item;
  1128. end;
  1129. function TFPGInterfacedObjectList.GetLast: T;
  1130. begin
  1131. Result := T(inherited GetLast^);
  1132. end;
  1133. procedure TFPGInterfacedObjectList.SetLast(const Value: T);
  1134. begin
  1135. inherited SetLast(@Value);
  1136. end;
  1137. procedure TFPGInterfacedObjectList.Assign(Source: TFPGInterfacedObjectList);
  1138. begin
  1139. Clear;
  1140. AddList(Source);
  1141. end;
  1142. procedure TFPGInterfacedObjectList.AddList(Source: TFPGInterfacedObjectList);
  1143. var
  1144. i: Integer;
  1145. begin
  1146. for I := 0 to Source.Count - 1 do
  1147. Add(Source[i]);
  1148. end;
  1149. function TFPGInterfacedObjectList.Remove(const Item: T): Integer;
  1150. begin
  1151. Result := IndexOf(Item);
  1152. if Result >= 0 then
  1153. Delete(Result);
  1154. end;
  1155. procedure TFPGInterfacedObjectList.Sort(Compare: TCompareFunc);
  1156. begin
  1157. FOnCompare := Compare;
  1158. inherited Sort(@ItemPtrCompare);
  1159. end;
  1160. procedure TFPGInterfacedObjectList.Sort(Compare: TCompareFunc; SortingAlgorithm: PSortingAlgorithm);
  1161. begin
  1162. FOnCompare := Compare;
  1163. inherited Sort(@ItemPtrCompare, SortingAlgorithm);
  1164. end;
  1165. {****************************************************************************
  1166. TFPSMap
  1167. ****************************************************************************}
  1168. constructor TFPSMap.Create(AKeySize: Integer; ADataSize: integer);
  1169. begin
  1170. inherited Create(AKeySize+ADataSize);
  1171. FKeySize := AKeySize;
  1172. FDataSize := ADataSize;
  1173. InitOnPtrCompare;
  1174. end;
  1175. procedure TFPSMap.CopyKey(Src, Dest: Pointer);
  1176. begin
  1177. System.Move(Src^, Dest^, FKeySize);
  1178. end;
  1179. procedure TFPSMap.CopyData(Src, Dest: Pointer);
  1180. begin
  1181. System.Move(Src^, Dest^, FDataSize);
  1182. end;
  1183. function TFPSMap.GetKey(Index: Integer): Pointer;
  1184. begin
  1185. Result := Items[Index];
  1186. end;
  1187. function TFPSMap.GetData(Index: Integer): Pointer;
  1188. begin
  1189. Result := PByte(Items[Index])+FKeySize;
  1190. end;
  1191. function TFPSMap.GetKeyData(AKey: Pointer): Pointer;
  1192. var
  1193. I: Integer;
  1194. begin
  1195. I := IndexOf(AKey);
  1196. if I >= 0 then
  1197. Result := InternalItems[I]+FKeySize
  1198. else
  1199. Error(SMapKeyError, PtrUInt(AKey));
  1200. end;
  1201. function TFPSMap.BinaryCompareKey(Key1, Key2: Pointer): Integer;
  1202. begin
  1203. Result := CompareByte(Key1^, Key2^, FKeySize);
  1204. end;
  1205. function TFPSMap.BinaryCompareData(Data1, Data2: Pointer): Integer;
  1206. begin
  1207. Result := CompareByte(Data1^, Data2^, FDataSize);
  1208. end;
  1209. procedure TFPSMap.SetOnKeyPtrCompare(Proc: TFPSListCompareFunc);
  1210. begin
  1211. if Proc <> nil then
  1212. FOnKeyPtrCompare := Proc
  1213. else
  1214. FOnKeyPtrCompare := @BinaryCompareKey;
  1215. end;
  1216. procedure TFPSMap.SetOnDataPtrCompare(Proc: TFPSListCompareFunc);
  1217. begin
  1218. if Proc <> nil then
  1219. FOnDataPtrCompare := Proc
  1220. else
  1221. FOnDataPtrCompare := @BinaryCompareData;
  1222. end;
  1223. procedure TFPSMap.InitOnPtrCompare;
  1224. begin
  1225. SetOnKeyPtrCompare(nil);
  1226. SetOnDataPtrCompare(nil);
  1227. end;
  1228. procedure TFPSMap.PutKey(Index: Integer; AKey: Pointer);
  1229. begin
  1230. if FSorted then
  1231. Error(SSortedListError, 0);
  1232. CopyKey(AKey, Items[Index]);
  1233. end;
  1234. procedure TFPSMap.PutData(Index: Integer; AData: Pointer);
  1235. begin
  1236. CopyData(AData, PByte(Items[Index])+FKeySize);
  1237. end;
  1238. procedure TFPSMap.PutKeyData(AKey: Pointer; NewData: Pointer);
  1239. var
  1240. I: Integer;
  1241. begin
  1242. I := IndexOf(AKey);
  1243. if I >= 0 then
  1244. Data[I] := NewData
  1245. else
  1246. Add(AKey, NewData);
  1247. end;
  1248. procedure TFPSMap.SetSorted(Value: Boolean);
  1249. begin
  1250. if Value = FSorted then exit;
  1251. FSorted := Value;
  1252. if Value then Sort;
  1253. end;
  1254. function TFPSMap.Add(AKey: Pointer): Integer;
  1255. begin
  1256. if Sorted then
  1257. begin
  1258. if Find(AKey, Result) then
  1259. case Duplicates of
  1260. dupIgnore: exit;
  1261. dupError: Error(SDuplicateItem, 0)
  1262. end;
  1263. end else
  1264. Result := Count;
  1265. CopyKey(AKey, inherited Insert(Result));
  1266. end;
  1267. function TFPSMap.Add(AKey, AData: Pointer): Integer;
  1268. begin
  1269. Result := Add(AKey);
  1270. Data[Result] := AData;
  1271. end;
  1272. function TFPSMap.Find(AKey: Pointer; out Index: Integer): Boolean;
  1273. { Searches for the first item <= Key, returns True if exact match,
  1274. sets index to the index of the found string. }
  1275. var
  1276. I,L,R,Dir: Integer;
  1277. begin
  1278. Result := false;
  1279. Index := -1;
  1280. if not Sorted then
  1281. raise EListError.Create(SErrFindNeedsSortedList);
  1282. // Use binary search.
  1283. L := 0;
  1284. R := FCount-1;
  1285. while L<=R do
  1286. begin
  1287. I := L + (R - L) div 2;
  1288. Dir := FOnKeyPtrCompare(Items[I], AKey);
  1289. if Dir < 0 then
  1290. L := I+1
  1291. else begin
  1292. R := I-1;
  1293. if Dir = 0 then
  1294. begin
  1295. Result := true;
  1296. if Duplicates <> dupAccept then
  1297. L := I;
  1298. end;
  1299. end;
  1300. end;
  1301. Index := L;
  1302. end;
  1303. function TFPSMap.LinearIndexOf(AKey: Pointer): Integer;
  1304. var
  1305. ListItem: Pointer;
  1306. begin
  1307. Result := 0;
  1308. ListItem := First;
  1309. while (Result < FCount) and (FOnKeyPtrCompare(ListItem, AKey) <> 0) do
  1310. begin
  1311. Inc(Result);
  1312. ListItem := PByte(ListItem)+FItemSize;
  1313. end;
  1314. if Result = FCount then Result := -1;
  1315. end;
  1316. function TFPSMap.IndexOf(AKey: Pointer): Integer;
  1317. begin
  1318. if Sorted then
  1319. begin
  1320. if not Find(AKey, Result) then
  1321. Result := -1;
  1322. end else
  1323. Result := LinearIndexOf(AKey);
  1324. end;
  1325. function TFPSMap.IndexOfData(AData: Pointer): Integer;
  1326. var
  1327. ListItem: Pointer;
  1328. begin
  1329. Result := 0;
  1330. ListItem := First+FKeySize;
  1331. while (Result < FCount) and (FOnDataPtrCompare(ListItem, AData) <> 0) do
  1332. begin
  1333. Inc(Result);
  1334. ListItem := PByte(ListItem)+FItemSize;
  1335. end;
  1336. if Result = FCount then Result := -1;
  1337. end;
  1338. function TFPSMap.Insert(Index: Integer): Pointer;
  1339. begin
  1340. if FSorted then
  1341. Error(SSortedListError, 0);
  1342. Result := inherited Insert(Index);
  1343. end;
  1344. procedure TFPSMap.Insert(Index: Integer; out AKey, AData: Pointer);
  1345. begin
  1346. AKey := Insert(Index);
  1347. AData := PByte(AKey) + FKeySize;
  1348. end;
  1349. procedure TFPSMap.InsertKey(Index: Integer; AKey: Pointer);
  1350. begin
  1351. CopyKey(AKey, Insert(Index));
  1352. end;
  1353. procedure TFPSMap.InsertKeyData(Index: Integer; AKey, AData: Pointer);
  1354. var
  1355. ListItem: Pointer;
  1356. begin
  1357. ListItem := Insert(Index);
  1358. CopyKey(AKey, ListItem);
  1359. CopyData(AData, PByte(ListItem)+FKeySize);
  1360. end;
  1361. function TFPSMap.Remove(AKey: Pointer): Integer;
  1362. begin
  1363. Result := IndexOf(AKey);
  1364. if Result >= 0 then
  1365. Delete(Result);
  1366. end;
  1367. procedure TFPSMap.Sort;
  1368. begin
  1369. inherited Sort(FOnKeyPtrCompare);
  1370. end;
  1371. procedure TFPSMap.Sort(SortingAlgorithm: PSortingAlgorithm);
  1372. begin
  1373. inherited Sort(FOnKeyPtrCompare, SortingAlgorithm);
  1374. end;
  1375. {****************************************************************************
  1376. TFPGMap
  1377. ****************************************************************************}
  1378. constructor TFPGMap.Create;
  1379. begin
  1380. inherited Create(SizeOf(TKey), SizeOf(TData));
  1381. end;
  1382. procedure TFPGMap.CopyItem(Src, Dest: Pointer);
  1383. begin
  1384. CopyKey(Src, Dest);
  1385. CopyData(PByte(Src)+KeySize, PByte(Dest)+KeySize);
  1386. end;
  1387. procedure TFPGMap.CopyKey(Src, Dest: Pointer);
  1388. begin
  1389. TKey(Dest^) := TKey(Src^);
  1390. end;
  1391. procedure TFPGMap.CopyData(Src, Dest: Pointer);
  1392. begin
  1393. TData(Dest^) := TData(Src^);
  1394. end;
  1395. procedure TFPGMap.Deref(Item: Pointer);
  1396. begin
  1397. Finalize(TKey(Item^));
  1398. Finalize(TData(Pointer(PByte(Item)+KeySize)^));
  1399. end;
  1400. function TFPGMap.GetKey(Index: Integer): TKey;
  1401. begin
  1402. Result := TKey(inherited GetKey(Index)^);
  1403. end;
  1404. function TFPGMap.GetData(Index: Integer): TData;
  1405. begin
  1406. Result := TData(inherited GetData(Index)^);
  1407. end;
  1408. function TFPGMap.GetKeyData(const AKey: TKey): TData;
  1409. begin
  1410. Result := TData(inherited GetKeyData(@AKey)^);
  1411. end;
  1412. function TFPGMap.KeyCompare(Key1, Key2: Pointer): Integer;
  1413. begin
  1414. if PKey(Key1)^ < PKey(Key2)^ then
  1415. Result := -1
  1416. else if PKey(Key1)^ > PKey(Key2)^ then
  1417. Result := 1
  1418. else
  1419. Result := 0;
  1420. end;
  1421. {function TFPGMap.DataCompare(Data1, Data2: Pointer): Integer;
  1422. begin
  1423. if PData(Data1)^ < PData(Data2)^ then
  1424. Result := -1
  1425. else if PData(Data1)^ > PData(Data2)^ then
  1426. Result := 1
  1427. else
  1428. Result := 0;
  1429. end;}
  1430. function TFPGMap.KeyCustomCompare(Key1, Key2: Pointer): Integer;
  1431. begin
  1432. Result := FOnKeyCompare(TKey(Key1^), TKey(Key2^));
  1433. end;
  1434. function TFPGMap.DataCustomCompare(Data1, Data2: Pointer): Integer;
  1435. begin
  1436. Result := FOnDataCompare(TData(Data1^), TData(Data2^));
  1437. end;
  1438. procedure TFPGMap.SetOnKeyCompare(NewCompare: TKeyCompareFunc);
  1439. begin
  1440. FOnKeyCompare := NewCompare;
  1441. if NewCompare <> nil then
  1442. OnKeyPtrCompare := @KeyCustomCompare
  1443. else
  1444. OnKeyPtrCompare := @KeyCompare;
  1445. end;
  1446. procedure TFPGMap.SetOnDataCompare(NewCompare: TDataCompareFunc);
  1447. begin
  1448. FOnDataCompare := NewCompare;
  1449. if NewCompare <> nil then
  1450. OnDataPtrCompare := @DataCustomCompare
  1451. else
  1452. OnDataPtrCompare := nil;
  1453. end;
  1454. procedure TFPGMap.InitOnPtrCompare;
  1455. begin
  1456. SetOnKeyCompare(nil);
  1457. SetOnDataCompare(nil);
  1458. end;
  1459. procedure TFPGMap.PutKey(Index: Integer; const NewKey: TKey);
  1460. begin
  1461. inherited PutKey(Index, @NewKey);
  1462. end;
  1463. procedure TFPGMap.PutData(Index: Integer; const NewData: TData);
  1464. begin
  1465. inherited PutData(Index, @NewData);
  1466. end;
  1467. procedure TFPGMap.PutKeyData(const AKey: TKey; const NewData: TData);
  1468. begin
  1469. inherited PutKeyData(@AKey, @NewData);
  1470. end;
  1471. function TFPGMap.Add(const AKey: TKey): Integer;
  1472. begin
  1473. Result := inherited Add(@AKey);
  1474. end;
  1475. function TFPGMap.Add(const AKey: TKey; const AData: TData): Integer;
  1476. begin
  1477. Result := inherited Add(@AKey, @AData);
  1478. end;
  1479. function TFPGMap.Find(const AKey: TKey; out Index: Integer): Boolean;
  1480. begin
  1481. Result := inherited Find(@AKey, Index);
  1482. end;
  1483. function TFPGMap.TryGetData(const AKey: TKey; out AData: TData): Boolean;
  1484. var
  1485. I: Integer;
  1486. begin
  1487. I := IndexOf(AKey);
  1488. Result := (I >= 0);
  1489. if Result then
  1490. AData := TData(inherited GetData(I)^)
  1491. else
  1492. AData := Default(TData);
  1493. end;
  1494. procedure TFPGMap.AddOrSetData(const AKey: TKey; const AData: TData);
  1495. begin
  1496. inherited PutKeyData(@AKey, @AData);
  1497. end;
  1498. function TFPGMap.IndexOf(const AKey: TKey): Integer;
  1499. begin
  1500. Result := inherited IndexOf(@AKey);
  1501. end;
  1502. function TFPGMap.IndexOfData(const AData: TData): Integer;
  1503. begin
  1504. { TODO: loop ? }
  1505. Result := inherited IndexOfData(@AData);
  1506. end;
  1507. procedure TFPGMap.InsertKey(Index: Integer; const AKey: TKey);
  1508. begin
  1509. inherited InsertKey(Index, @AKey);
  1510. end;
  1511. procedure TFPGMap.InsertKeyData(Index: Integer; const AKey: TKey; const AData: TData);
  1512. begin
  1513. inherited InsertKeyData(Index, @AKey, @AData);
  1514. end;
  1515. function TFPGMap.Remove(const AKey: TKey): Integer;
  1516. begin
  1517. Result := inherited Remove(@AKey);
  1518. end;
  1519. {****************************************************************************
  1520. TFPGMapObject
  1521. ****************************************************************************}
  1522. constructor TFPGMapObject.Create(AFreeObjects: Boolean);
  1523. begin
  1524. inherited Create(SizeOf(TKey), SizeOf(TData));
  1525. FFreeObjects := AFreeObjects;
  1526. end;
  1527. constructor TFPGMapObject.Create;
  1528. begin
  1529. Create(True);
  1530. end;
  1531. procedure TFPGMapObject.CopyItem(Src, Dest: Pointer);
  1532. begin
  1533. CopyKey(Src, Dest);
  1534. CopyData(PByte(Src)+KeySize, PByte(Dest)+KeySize);
  1535. end;
  1536. procedure TFPGMapObject.CopyKey(Src, Dest: Pointer);
  1537. begin
  1538. TKey(Dest^) := TKey(Src^);
  1539. end;
  1540. procedure TFPGMapObject.CopyData(Src, Dest: Pointer);
  1541. begin
  1542. if Assigned(Pointer(Dest^)) And FFreeObjects then
  1543. TData(Dest^).Free;
  1544. TData(Dest^) := TData(Src^);
  1545. end;
  1546. procedure TFPGMapObject.Deref(Item: Pointer);
  1547. begin
  1548. Finalize(TKey(Item^));
  1549. if Assigned(PPointer(PByte(Item)+KeySize)^) and FFreeObjects then
  1550. TData(Pointer(PByte(Item)+KeySize)^).Free;
  1551. end;
  1552. function TFPGMapObject.GetKey(Index: Integer): TKey;
  1553. begin
  1554. Result := TKey(inherited GetKey(Index)^);
  1555. end;
  1556. function TFPGMapObject.GetData(Index: Integer): TData;
  1557. begin
  1558. Result := TData(inherited GetData(Index)^);
  1559. end;
  1560. function TFPGMapObject.GetKeyData(const AKey: TKey): TData;
  1561. begin
  1562. Result := TData(inherited GetKeyData(@AKey)^);
  1563. end;
  1564. function TFPGMapObject.KeyCompare(Key1, Key2: Pointer): Integer;
  1565. begin
  1566. if PKey(Key1)^ < PKey(Key2)^ then
  1567. Result := -1
  1568. else if PKey(Key1)^ > PKey(Key2)^ then
  1569. Result := 1
  1570. else
  1571. Result := 0;
  1572. end;
  1573. {function TFPGMapObject.DataCompare(Data1, Data2: Pointer): Integer;
  1574. begin
  1575. if PData(Data1)^ < PData(Data2)^ then
  1576. Result := -1
  1577. else if PData(Data1)^ > PData(Data2)^ then
  1578. Result := 1
  1579. else
  1580. Result := 0;
  1581. end;}
  1582. function TFPGMapObject.KeyCustomCompare(Key1, Key2: Pointer): Integer;
  1583. begin
  1584. Result := FOnKeyCompare(TKey(Key1^), TKey(Key2^));
  1585. end;
  1586. function TFPGMapObject.DataCustomCompare(Data1, Data2: Pointer): Integer;
  1587. begin
  1588. Result := FOnDataCompare(TData(Data1^), TData(Data2^));
  1589. end;
  1590. procedure TFPGMapObject.SetOnKeyCompare(NewCompare: TKeyCompareFunc);
  1591. begin
  1592. FOnKeyCompare := NewCompare;
  1593. if NewCompare <> nil then
  1594. OnKeyPtrCompare := @KeyCustomCompare
  1595. else
  1596. OnKeyPtrCompare := @KeyCompare;
  1597. end;
  1598. procedure TFPGMapObject.SetOnDataCompare(NewCompare: TDataCompareFunc);
  1599. begin
  1600. FOnDataCompare := NewCompare;
  1601. if NewCompare <> nil then
  1602. OnDataPtrCompare := @DataCustomCompare
  1603. else
  1604. OnDataPtrCompare := nil;
  1605. end;
  1606. procedure TFPGMapObject.InitOnPtrCompare;
  1607. begin
  1608. SetOnKeyCompare(nil);
  1609. SetOnDataCompare(nil);
  1610. end;
  1611. procedure TFPGMapObject.PutKey(Index: Integer; const NewKey: TKey);
  1612. begin
  1613. inherited PutKey(Index, @NewKey);
  1614. end;
  1615. procedure TFPGMapObject.PutData(Index: Integer; const NewData: TData);
  1616. begin
  1617. inherited PutData(Index, @NewData);
  1618. end;
  1619. procedure TFPGMapObject.PutKeyData(const AKey: TKey; const NewData: TData);
  1620. begin
  1621. inherited PutKeyData(@AKey, @NewData);
  1622. end;
  1623. function TFPGMapObject.Add(const AKey: TKey): Integer;
  1624. begin
  1625. Result := inherited Add(@AKey);
  1626. end;
  1627. function TFPGMapObject.Add(const AKey: TKey; const AData: TData): Integer;
  1628. begin
  1629. Result := inherited Add(@AKey, @AData);
  1630. end;
  1631. function TFPGMapObject.Find(const AKey: TKey; out Index: Integer): Boolean;
  1632. begin
  1633. Result := inherited Find(@AKey, Index);
  1634. end;
  1635. function TFPGMapObject.TryGetData(const AKey: TKey; out AData: TData): Boolean;
  1636. var
  1637. I: Integer;
  1638. begin
  1639. I := IndexOf(AKey);
  1640. Result := (I >= 0);
  1641. if Result then
  1642. AData := TData(inherited GetData(I)^)
  1643. else
  1644. AData := Default(TData);
  1645. end;
  1646. procedure TFPGMapObject.AddOrSetData(const AKey: TKey; const AData: TData);
  1647. begin
  1648. inherited PutKeyData(@AKey, @AData);
  1649. end;
  1650. function TFPGMapObject.IndexOf(const AKey: TKey): Integer;
  1651. begin
  1652. Result := inherited IndexOf(@AKey);
  1653. end;
  1654. function TFPGMapObject.IndexOfData(const AData: TData): Integer;
  1655. begin
  1656. { TODO: loop ? }
  1657. Result := inherited IndexOfData(@AData);
  1658. end;
  1659. procedure TFPGMapObject.InsertKey(Index: Integer; const AKey: TKey);
  1660. begin
  1661. inherited InsertKey(Index, @AKey);
  1662. end;
  1663. procedure TFPGMapObject.InsertKeyData(Index: Integer; const AKey: TKey; const AData: TData);
  1664. begin
  1665. inherited InsertKeyData(Index, @AKey, @AData);
  1666. end;
  1667. function TFPGMapObject.Remove(const AKey: TKey): Integer;
  1668. begin
  1669. Result := inherited Remove(@AKey);
  1670. end;
  1671. {****************************************************************************
  1672. TFPGMapInterfacedObjectData
  1673. ****************************************************************************}
  1674. constructor TFPGMapInterfacedObjectData.Create;
  1675. begin
  1676. inherited Create(SizeOf(TKey), SizeOf(TData));
  1677. end;
  1678. procedure TFPGMapInterfacedObjectData.CopyItem(Src, Dest: Pointer);
  1679. begin
  1680. CopyKey(Src, Dest);
  1681. CopyData(PByte(Src)+KeySize, PByte(Dest)+KeySize);
  1682. end;
  1683. procedure TFPGMapInterfacedObjectData.CopyKey(Src, Dest: Pointer);
  1684. begin
  1685. TKey(Dest^) := TKey(Src^);
  1686. end;
  1687. procedure TFPGMapInterfacedObjectData.CopyData(Src, Dest: Pointer);
  1688. begin
  1689. if Assigned(Pointer(Dest^)) then
  1690. TData(Dest^)._Release;
  1691. TData(Dest^) := TData(Src^);
  1692. if Assigned(Pointer(Dest^)) then
  1693. TData(Dest^)._AddRef;
  1694. end;
  1695. procedure TFPGMapInterfacedObjectData.Deref(Item: Pointer);
  1696. begin
  1697. Finalize(TKey(Item^));
  1698. if Assigned(PPointer(PByte(Item)+KeySize)^) then
  1699. TData(Pointer(PByte(Item)+KeySize)^)._Release;
  1700. end;
  1701. function TFPGMapInterfacedObjectData.GetKey(Index: Integer): TKey;
  1702. begin
  1703. Result := TKey(inherited GetKey(Index)^);
  1704. end;
  1705. function TFPGMapInterfacedObjectData.GetData(Index: Integer): TData;
  1706. begin
  1707. Result := TData(inherited GetData(Index)^);
  1708. end;
  1709. function TFPGMapInterfacedObjectData.GetKeyData(const AKey: TKey): TData;
  1710. begin
  1711. Result := TData(inherited GetKeyData(@AKey)^);
  1712. end;
  1713. function TFPGMapInterfacedObjectData.KeyCompare(Key1, Key2: Pointer): Integer;
  1714. begin
  1715. if PKey(Key1)^ < PKey(Key2)^ then
  1716. Result := -1
  1717. else if PKey(Key1)^ > PKey(Key2)^ then
  1718. Result := 1
  1719. else
  1720. Result := 0;
  1721. end;
  1722. {function TFPGMapInterfacedObjectData.DataCompare(Data1, Data2: Pointer): Integer;
  1723. begin
  1724. if PData(Data1)^ < PData(Data2)^ then
  1725. Result := -1
  1726. else if PData(Data1)^ > PData(Data2)^ then
  1727. Result := 1
  1728. else
  1729. Result := 0;
  1730. end;}
  1731. function TFPGMapInterfacedObjectData.KeyCustomCompare(Key1, Key2: Pointer): Integer;
  1732. begin
  1733. Result := FOnKeyCompare(TKey(Key1^), TKey(Key2^));
  1734. end;
  1735. function TFPGMapInterfacedObjectData.DataCustomCompare(Data1, Data2: Pointer): Integer;
  1736. begin
  1737. Result := FOnDataCompare(TData(Data1^), TData(Data2^));
  1738. end;
  1739. procedure TFPGMapInterfacedObjectData.SetOnKeyCompare(NewCompare: TKeyCompareFunc);
  1740. begin
  1741. FOnKeyCompare := NewCompare;
  1742. if NewCompare <> nil then
  1743. OnKeyPtrCompare := @KeyCustomCompare
  1744. else
  1745. OnKeyPtrCompare := @KeyCompare;
  1746. end;
  1747. procedure TFPGMapInterfacedObjectData.SetOnDataCompare(NewCompare: TDataCompareFunc);
  1748. begin
  1749. FOnDataCompare := NewCompare;
  1750. if NewCompare <> nil then
  1751. OnDataPtrCompare := @DataCustomCompare
  1752. else
  1753. OnDataPtrCompare := nil;
  1754. end;
  1755. procedure TFPGMapInterfacedObjectData.InitOnPtrCompare;
  1756. begin
  1757. SetOnKeyCompare(nil);
  1758. SetOnDataCompare(nil);
  1759. end;
  1760. procedure TFPGMapInterfacedObjectData.PutKey(Index: Integer; const NewKey: TKey);
  1761. begin
  1762. inherited PutKey(Index, @NewKey);
  1763. end;
  1764. procedure TFPGMapInterfacedObjectData.PutData(Index: Integer; const NewData: TData);
  1765. begin
  1766. inherited PutData(Index, @NewData);
  1767. end;
  1768. procedure TFPGMapInterfacedObjectData.PutKeyData(const AKey: TKey; const NewData: TData);
  1769. begin
  1770. inherited PutKeyData(@AKey, @NewData);
  1771. end;
  1772. function TFPGMapInterfacedObjectData.Add(const AKey: TKey): Integer;
  1773. begin
  1774. Result := inherited Add(@AKey);
  1775. end;
  1776. function TFPGMapInterfacedObjectData.Add(const AKey: TKey; const AData: TData): Integer;
  1777. begin
  1778. Result := inherited Add(@AKey, @AData);
  1779. end;
  1780. function TFPGMapInterfacedObjectData.Find(const AKey: TKey; out Index: Integer): Boolean;
  1781. begin
  1782. Result := inherited Find(@AKey, Index);
  1783. end;
  1784. function TFPGMapInterfacedObjectData.TryGetData(const AKey: TKey; out AData: TData): Boolean;
  1785. var
  1786. I: Integer;
  1787. begin
  1788. I := IndexOf(AKey);
  1789. Result := (I >= 0);
  1790. if Result then
  1791. AData := TData(inherited GetData(I)^)
  1792. else
  1793. AData := Default(TData);
  1794. end;
  1795. procedure TFPGMapInterfacedObjectData.AddOrSetData(const AKey: TKey;
  1796. const AData: TData);
  1797. begin
  1798. inherited PutKeyData(@AKey, @AData);
  1799. end;
  1800. function TFPGMapInterfacedObjectData.IndexOf(const AKey: TKey): Integer;
  1801. begin
  1802. Result := inherited IndexOf(@AKey);
  1803. end;
  1804. function TFPGMapInterfacedObjectData.IndexOfData(const AData: TData): Integer;
  1805. begin
  1806. { TODO: loop ? }
  1807. Result := inherited IndexOfData(@AData);
  1808. end;
  1809. procedure TFPGMapInterfacedObjectData.InsertKey(Index: Integer; const AKey: TKey);
  1810. begin
  1811. inherited InsertKey(Index, @AKey);
  1812. end;
  1813. procedure TFPGMapInterfacedObjectData.InsertKeyData(Index: Integer; const AKey: TKey; const AData: TData);
  1814. begin
  1815. inherited InsertKeyData(Index, @AKey, @AData);
  1816. end;
  1817. function TFPGMapInterfacedObjectData.Remove(const AKey: TKey): Integer;
  1818. begin
  1819. Result := inherited Remove(@AKey);
  1820. end;
  1821. end.