fgl.pp 58 KB

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