fgl.pp 59 KB

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