fgl.pp 58 KB

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