cclasses.pas 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  3. This module provides some basic classes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  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. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cclasses;
  18. {$i fpcdefs.inc}
  19. {$ifndef VER2_0}
  20. {$define CCLASSESINLINE}
  21. {$endif}
  22. interface
  23. uses
  24. {$IFNDEF USE_FAKE_SYSUTILS}
  25. SysUtils,
  26. {$ELSE}
  27. fksysutl,
  28. {$ENDIF}
  29. globtype,
  30. CUtils,CStreams;
  31. {********************************************
  32. TMemDebug
  33. ********************************************}
  34. type
  35. tmemdebug = class
  36. private
  37. totalmem,
  38. startmem : int64;
  39. infostr : string[40];
  40. public
  41. constructor Create(const s:string);
  42. destructor Destroy;override;
  43. procedure show;
  44. procedure start;
  45. procedure stop;
  46. end;
  47. {*******************************************************
  48. TFPList (From rtl/objpas/classes/classesh.inc)
  49. ********************************************************}
  50. const
  51. SListIndexError = 'List index exceeds bounds (%d)';
  52. SListCapacityError = 'The maximum list capacity is reached (%d)';
  53. SListCapacityPower2Error = 'The capacity has to be a power of 2, but is set to %d';
  54. SListCountError = 'List count too large (%d)';
  55. type
  56. EListError = class(Exception);
  57. const
  58. MaxListSize = Maxint div 16;
  59. type
  60. PPointerList = ^TPointerList;
  61. TPointerList = array[0..MaxListSize - 1] of Pointer;
  62. TListSortCompare = function (Item1, Item2: Pointer): Integer;
  63. TListCallback = procedure(data,arg:pointer) of object;
  64. TListStaticCallback = procedure(data,arg:pointer);
  65. TDynStringArray = Array Of String;
  66. TFPList = class(TObject)
  67. private
  68. FList: PPointerList;
  69. FCount: Integer;
  70. FCapacity: Integer;
  71. protected
  72. function Get(Index: Integer): Pointer;
  73. procedure Put(Index: Integer; Item: Pointer);
  74. procedure SetCapacity(NewCapacity: Integer);
  75. procedure SetCount(NewCount: Integer);
  76. Procedure RaiseIndexError(Index : Integer);{$ifndef VER2_6}noreturn;{$endif VER2_6}
  77. public
  78. destructor Destroy; override;
  79. function Add(Item: Pointer): Integer;
  80. procedure Clear;
  81. procedure Delete(Index: Integer);
  82. class procedure Error(const Msg: string; Data: PtrInt);{$ifndef VER2_6}noreturn;{$endif VER2_6}
  83. procedure Exchange(Index1, Index2: Integer);
  84. function Expand: TFPList;
  85. function Extract(item: Pointer): Pointer;
  86. function First: Pointer;
  87. function IndexOf(Item: Pointer): Integer;
  88. procedure Insert(Index: Integer; Item: Pointer);
  89. function Last: Pointer;
  90. procedure Move(CurIndex, NewIndex: Integer);
  91. procedure Assign(Obj:TFPList);
  92. function Remove(Item: Pointer): Integer;
  93. procedure Pack;
  94. procedure Sort(Compare: TListSortCompare);
  95. procedure ForEachCall(proc2call:TListCallback;arg:pointer);
  96. procedure ForEachCall(proc2call:TListStaticCallback;arg:pointer);
  97. property Capacity: Integer read FCapacity write SetCapacity;
  98. property Count: Integer read FCount write SetCount;
  99. property Items[Index: Integer]: Pointer read Get write Put; default;
  100. property List: PPointerList read FList;
  101. end;
  102. {*******************************************************
  103. TFPObjectList (From fcl/inc/contnrs.pp)
  104. ********************************************************}
  105. TObjectListCallback = procedure(data:TObject;arg:pointer) of object;
  106. TObjectListStaticCallback = procedure(data:TObject;arg:pointer);
  107. TFPObjectList = class(TObject)
  108. private
  109. FFreeObjects : Boolean;
  110. FList: TFPList;
  111. function GetCount: integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  112. procedure SetCount(const AValue: integer);
  113. protected
  114. function GetItem(Index: Integer): TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
  115. procedure SetItem(Index: Integer; AObject: TObject);
  116. procedure SetCapacity(NewCapacity: Integer); {$ifdef CCLASSESINLINE}inline;{$endif}
  117. function GetCapacity: integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  118. public
  119. constructor Create;
  120. constructor Create(FreeObjects : Boolean);
  121. destructor Destroy; override;
  122. procedure Clear;
  123. function Add(AObject: TObject): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  124. procedure Delete(Index: Integer);
  125. procedure Exchange(Index1, Index2: Integer); {$ifdef CCLASSESINLINE}inline;{$endif}
  126. function Expand: TFPObjectList;{$ifdef CCLASSESINLINE}inline;{$endif}
  127. function Extract(Item: TObject): TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
  128. function Remove(AObject: TObject): Integer;
  129. function IndexOf(AObject: TObject): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  130. function FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt: Integer): Integer;
  131. procedure Insert(Index: Integer; AObject: TObject); {$ifdef CCLASSESINLINE}inline;{$endif}
  132. function First: TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
  133. function Last: TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
  134. procedure Move(CurIndex, NewIndex: Integer); {$ifdef CCLASSESINLINE}inline;{$endif}
  135. procedure Assign(Obj:TFPObjectList);
  136. procedure ConcatListCopy(Obj:TFPObjectList);
  137. procedure Pack; {$ifdef CCLASSESINLINE}inline;{$endif}
  138. procedure Sort(Compare: TListSortCompare); {$ifdef CCLASSESINLINE}inline;{$endif}
  139. procedure ForEachCall(proc2call:TObjectListCallback;arg:pointer); {$ifdef CCLASSESINLINE}inline;{$endif}
  140. procedure ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer); {$ifdef CCLASSESINLINE}inline;{$endif}
  141. property Capacity: Integer read GetCapacity write SetCapacity;
  142. property Count: Integer read GetCount write SetCount;
  143. property OwnsObjects: Boolean read FFreeObjects write FFreeObjects;
  144. property Items[Index: Integer]: TObject read GetItem write SetItem; default;
  145. property List: TFPList read FList;
  146. end;
  147. type
  148. THashItem=record
  149. HashValue : LongWord;
  150. StrIndex : Integer;
  151. NextIndex : Integer;
  152. Data : Pointer;
  153. end;
  154. PHashItem=^THashItem;
  155. const
  156. MaxHashListSize = Maxint div 16;
  157. MaxHashStrSize = Maxint;
  158. MaxHashTableSize = Maxint div 4;
  159. MaxItemsPerHash = 3;
  160. type
  161. PHashItemList = ^THashItemList;
  162. THashItemList = array[0..MaxHashListSize - 1] of THashItem;
  163. PHashTable = ^THashTable;
  164. THashTable = array[0..MaxHashTableSize - 1] of Integer;
  165. TFPHashList = class(TObject)
  166. private
  167. { ItemList }
  168. FHashList : PHashItemList;
  169. FCount,
  170. FCapacity : Integer;
  171. FCapacityMask: LongWord;
  172. { Hash }
  173. FHashTable : PHashTable;
  174. FHashCapacity : Integer;
  175. { Strings }
  176. {$ifdef symansistr}
  177. FStrs : PAnsiString;
  178. {$else symansistr}
  179. FStrs : PChar;
  180. {$endif symansistr}
  181. FStrCount,
  182. FStrCapacity : Integer;
  183. function InternalFind(AHash:LongWord;const AName:TSymStr;out PrevIndex:Integer):Integer;
  184. protected
  185. function Get(Index: Integer): Pointer;
  186. procedure Put(Index: Integer; Item: Pointer);
  187. procedure SetCapacity(NewCapacity: Integer);
  188. procedure SetCount(NewCount: Integer);
  189. Procedure RaiseIndexError(Index : Integer);
  190. function AddStr(const s:TSymStr): Integer;
  191. procedure AddToHashTable(Index: Integer);
  192. procedure StrExpand(MinIncSize:Integer);
  193. procedure SetStrCapacity(NewCapacity: Integer);
  194. procedure SetHashCapacity(NewCapacity: Integer);
  195. procedure ReHash;
  196. public
  197. constructor Create;
  198. destructor Destroy; override;
  199. function Add(const AName:TSymStr;Item: Pointer): Integer;
  200. procedure Clear;
  201. function NameOfIndex(Index: Integer): TSymStr;
  202. function HashOfIndex(Index: Integer): LongWord;
  203. function GetNextCollision(Index: Integer): Integer;
  204. procedure Delete(Index: Integer);
  205. class procedure Error(const Msg: string; Data: PtrInt);{$ifndef VER2_6}noreturn;{$endif VER2_6}
  206. function Expand: TFPHashList;
  207. function Extract(item: Pointer): Pointer;
  208. function IndexOf(Item: Pointer): Integer;
  209. function Find(const AName:TSymStr): Pointer;
  210. function FindIndexOf(const AName:TSymStr): Integer;
  211. function FindWithHash(const AName:TSymStr;AHash:LongWord): Pointer;
  212. function Rename(const AOldName,ANewName:TSymStr): Integer;
  213. function Remove(Item: Pointer): Integer;
  214. procedure Pack;
  215. procedure ShowStatistics;
  216. procedure ForEachCall(proc2call:TListCallback;arg:pointer);
  217. procedure ForEachCall(proc2call:TListStaticCallback;arg:pointer);
  218. procedure WhileEachCall(proc2call:TListCallback;arg:pointer);
  219. procedure WhileEachCall(proc2call:TListStaticCallback;arg:pointer);
  220. property Capacity: Integer read FCapacity write SetCapacity;
  221. property Count: Integer read FCount write SetCount;
  222. property Items[Index: Integer]: Pointer read Get write Put; default;
  223. property List: PHashItemList read FHashList;
  224. {$ifdef symansistr}
  225. property Strs: PSymStr read FStrs;
  226. {$else}
  227. property Strs: PChar read FStrs;
  228. {$endif}
  229. end;
  230. {*******************************************************
  231. TFPHashObjectList (From fcl/inc/contnrs.pp)
  232. ********************************************************}
  233. TFPHashObjectList = class;
  234. { TFPHashObject }
  235. TFPHashObject = class
  236. private
  237. FOwner : TFPHashObjectList;
  238. FStrIndex : Integer;
  239. procedure InternalChangeOwner(HashObjectList:TFPHashObjectList;const s:TSymStr);
  240. protected
  241. function GetName:TSymStr;virtual;
  242. function GetHash:Longword;virtual;
  243. public
  244. constructor CreateNotOwned;
  245. constructor Create(HashObjectList:TFPHashObjectList;const s:TSymStr);
  246. procedure ChangeOwner(HashObjectList:TFPHashObjectList);
  247. procedure ChangeOwnerAndName(HashObjectList:TFPHashObjectList;const s:TSymStr); {$ifdef CCLASSESINLINE}inline;{$endif}
  248. procedure Rename(const ANewName:TSymStr);
  249. property Name:TSymStr read GetName;
  250. property Hash:Longword read GetHash;
  251. property OwnerList: TFPHashObjectList read FOwner;
  252. end;
  253. TFPHashObjectList = class(TObject)
  254. private
  255. FFreeObjects : Boolean;
  256. FHashList: TFPHashList;
  257. function GetCount: integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  258. procedure SetCount(const AValue: integer);
  259. protected
  260. function GetItem(Index: Integer): TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
  261. procedure SetItem(Index: Integer; AObject: TObject);
  262. procedure SetCapacity(NewCapacity: Integer); {$ifdef CCLASSESINLINE}inline;{$endif}
  263. function GetCapacity: integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  264. public
  265. constructor Create(FreeObjects : boolean = True);
  266. destructor Destroy; override;
  267. procedure Clear;
  268. function Add(const AName:TSymStr;AObject: TObject): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  269. function NameOfIndex(Index: Integer): TSymStr; {$ifdef CCLASSESINLINE}inline;{$endif}
  270. function HashOfIndex(Index: Integer): LongWord; {$ifdef CCLASSESINLINE}inline;{$endif}
  271. function GetNextCollision(Index: Integer): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  272. procedure Delete(Index: Integer);
  273. function Expand: TFPHashObjectList; {$ifdef CCLASSESINLINE}inline;{$endif}
  274. function Extract(Item: TObject): TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
  275. function Remove(AObject: TObject): Integer;
  276. function IndexOf(AObject: TObject): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  277. function Find(const s:TSymStr): TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
  278. function FindIndexOf(const s:TSymStr): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  279. function FindWithHash(const AName:TSymStr;AHash:LongWord): Pointer;
  280. function Rename(const AOldName,ANewName:TSymStr): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  281. function FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt: Integer): Integer;
  282. procedure Pack; {$ifdef CCLASSESINLINE}inline;{$endif}
  283. procedure ShowStatistics; {$ifdef CCLASSESINLINE}inline;{$endif}
  284. procedure ForEachCall(proc2call:TObjectListCallback;arg:pointer); {$ifdef CCLASSESINLINE}inline;{$endif}
  285. procedure ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer); {$ifdef CCLASSESINLINE}inline;{$endif}
  286. procedure WhileEachCall(proc2call:TObjectListCallback;arg:pointer); {$ifdef CCLASSESINLINE}inline;{$endif}
  287. procedure WhileEachCall(proc2call:TObjectListStaticCallback;arg:pointer); {$ifdef CCLASSESINLINE}inline;{$endif}
  288. property Capacity: Integer read GetCapacity write SetCapacity;
  289. property Count: Integer read GetCount write SetCount;
  290. property OwnsObjects: Boolean read FFreeObjects write FFreeObjects;
  291. property Items[Index: Integer]: TObject read GetItem write SetItem; default;
  292. property List: TFPHashList read FHashList;
  293. end;
  294. {********************************************
  295. TLinkedList
  296. ********************************************}
  297. type
  298. TLinkedListItem = class
  299. public
  300. Previous,
  301. Next : TLinkedListItem;
  302. Constructor Create;
  303. Destructor Destroy;override;
  304. Function GetCopy:TLinkedListItem;virtual;
  305. end;
  306. TLinkedListItemClass = class of TLinkedListItem;
  307. TLinkedList = class
  308. private
  309. FCount : integer;
  310. FFirst,
  311. FLast : TLinkedListItem;
  312. FNoClear : boolean;
  313. public
  314. constructor Create;
  315. destructor Destroy;override;
  316. { true when the List is empty }
  317. function Empty:boolean; {$ifdef CCLASSESINLINE}inline;{$endif}
  318. { deletes all Items }
  319. procedure Clear;
  320. { inserts an Item }
  321. procedure Insert(Item:TLinkedListItem);
  322. { inserts an Item before Loc }
  323. procedure InsertBefore(Item,Loc : TLinkedListItem);
  324. { inserts an Item after Loc }
  325. procedure InsertAfter(Item,Loc : TLinkedListItem);virtual;
  326. { concats an Item }
  327. procedure Concat(Item:TLinkedListItem);
  328. { deletes an Item }
  329. procedure Remove(Item:TLinkedListItem);
  330. { Gets First Item }
  331. function GetFirst:TLinkedListItem;
  332. { Gets last Item }
  333. function GetLast:TLinkedListItem;
  334. { inserts another List at the begin and make this List empty }
  335. procedure insertList(p : TLinkedList); virtual;
  336. { inserts another List before the provided item and make this List empty }
  337. procedure insertListBefore(Item:TLinkedListItem;p : TLinkedList); virtual;
  338. { inserts another List after the provided item and make this List empty }
  339. procedure insertListAfter(Item:TLinkedListItem;p : TLinkedList); virtual;
  340. { concats another List at the end and make this List empty }
  341. procedure concatList(p : TLinkedList); virtual;
  342. { concats another List at the start and makes a copy
  343. the list is ordered in reverse.
  344. }
  345. procedure insertListcopy(p : TLinkedList); virtual;
  346. { concats another List at the end and makes a copy }
  347. procedure concatListcopy(p : TLinkedList); virtual;
  348. { removes all items from the list, the items are not freed }
  349. procedure RemoveAll; virtual;
  350. property First:TLinkedListItem read FFirst;
  351. property Last:TLinkedListItem read FLast;
  352. property Count:Integer read FCount;
  353. property NoClear:boolean write FNoClear;
  354. end;
  355. {********************************************
  356. TCmdStrList
  357. ********************************************}
  358. { string containerItem }
  359. TCmdStrListItem = class(TLinkedListItem)
  360. FPStr : TCmdStr;
  361. public
  362. constructor Create(const s:TCmdStr);
  363. destructor Destroy;override;
  364. function GetCopy:TLinkedListItem;override;
  365. property Str: TCmdStr read FPStr;
  366. end;
  367. { string container }
  368. TCmdStrList = class(TLinkedList)
  369. private
  370. FDoubles : boolean; { if this is set to true, doubles (case insensitive!) are allowed }
  371. public
  372. constructor Create;
  373. constructor Create_No_Double;
  374. { inserts an Item }
  375. procedure Insert(const s:TCmdStr);
  376. { concats an Item }
  377. procedure Concat(const s:TCmdStr);
  378. { deletes an Item }
  379. procedure Remove(const s:TCmdStr);
  380. { Gets First Item }
  381. function GetFirst:TCmdStr;
  382. { Gets last Item }
  383. function GetLast:TCmdStr;
  384. { true if string is in the container, compare case sensitive }
  385. function FindCase(const s:TCmdStr):TCmdStrListItem;
  386. { true if string is in the container }
  387. function Find(const s:TCmdStr):TCmdStrListItem;
  388. { inserts an item }
  389. procedure InsertItem(item:TCmdStrListItem);
  390. { concats an item }
  391. procedure ConcatItem(item:TCmdStrListItem);
  392. property Doubles:boolean read FDoubles write FDoubles;
  393. end;
  394. {********************************************
  395. DynamicArray
  396. ********************************************}
  397. type
  398. { can't use sizeof(integer) because it crashes gdb }
  399. tdynamicblockdata=array[0..1024*1024-1] of byte;
  400. pdynamicblock = ^tdynamicblock;
  401. tdynamicblock = record
  402. pos,
  403. size,
  404. used : longword;
  405. Next : pdynamicblock;
  406. data : tdynamicblockdata;
  407. end;
  408. const
  409. dynamicblockbasesize = sizeof(tdynamicblock)-sizeof(tdynamicblockdata);
  410. mindynamicblocksize = 8*sizeof(pointer);
  411. type
  412. tdynamicarray = class
  413. private
  414. FPosn : longword;
  415. FPosnblock : pdynamicblock;
  416. FCurrBlocksize,
  417. FMaxBlocksize : longword;
  418. FFirstblock,
  419. FLastblock : pdynamicblock;
  420. procedure grow;
  421. public
  422. constructor Create(Ablocksize:longword);
  423. destructor Destroy;override;
  424. procedure reset;
  425. function size:longword;
  426. procedure align(i:longword);
  427. procedure seek(i:longword);
  428. function read(var d;len:longword):longword;
  429. procedure write(const d;len:longword);
  430. procedure writestr(const s:string); {$ifdef CCLASSESINLINE}inline;{$endif}
  431. procedure readstream(f:TCStream;maxlen:longword);
  432. procedure writestream(f:TCStream);
  433. function equal(other:tdynamicarray):boolean;
  434. property CurrBlockSize : longword read FCurrBlocksize;
  435. property FirstBlock : PDynamicBlock read FFirstBlock;
  436. property Pos : longword read FPosn;
  437. end;
  438. {******************************************************************
  439. THashSet (keys not limited to ShortString, no indexed access)
  440. *******************************************************************}
  441. PPHashSetItem = ^PHashSetItem;
  442. PHashSetItem = ^THashSetItem;
  443. THashSetItem = record
  444. Next: PHashSetItem;
  445. Key: Pointer;
  446. KeyLength: Integer;
  447. HashValue: LongWord;
  448. Data: TObject;
  449. end;
  450. THashSet = class(TObject)
  451. private
  452. FCount: LongWord;
  453. FOwnsObjects: Boolean;
  454. FOwnsKeys: Boolean;
  455. function Lookup(Key: Pointer; KeyLen: Integer; var Found: Boolean;
  456. CanCreate: Boolean): PHashSetItem;
  457. procedure Resize(NewCapacity: LongWord);
  458. protected
  459. FBucket: PPHashSetItem;
  460. FBucketCount: LongWord;
  461. class procedure FreeItem(item:PHashSetItem); virtual;
  462. class function SizeOfItem: Integer; virtual;
  463. public
  464. constructor Create(InitSize: Integer; OwnKeys, OwnObjects: Boolean);
  465. destructor Destroy; override;
  466. procedure Clear;
  467. { finds an entry by key }
  468. function Find(Key: Pointer; KeyLen: Integer): PHashSetItem;virtual;
  469. { finds an entry, creates one if not exists }
  470. function FindOrAdd(Key: Pointer; KeyLen: Integer;
  471. var Found: Boolean): PHashSetItem;virtual;
  472. { finds an entry, creates one if not exists }
  473. function FindOrAdd(Key: Pointer; KeyLen: Integer): PHashSetItem;virtual;
  474. { returns Data by given Key }
  475. function Get(Key: Pointer; KeyLen: Integer): TObject;virtual;
  476. { removes an entry, returns False if entry wasn't there }
  477. function Remove(Entry: PHashSetItem): Boolean;
  478. property Count: LongWord read FCount;
  479. end;
  480. {******************************************************************
  481. TTagHasSet
  482. *******************************************************************}
  483. PPTagHashSetItem = ^PTagHashSetItem;
  484. PTagHashSetItem = ^TTagHashSetItem;
  485. TTagHashSetItem = record
  486. Next: PTagHashSetItem;
  487. Key: Pointer;
  488. KeyLength: Integer;
  489. HashValue: LongWord;
  490. Data: TObject;
  491. Tag: LongWord;
  492. end;
  493. TTagHashSet = class(THashSet)
  494. private
  495. function Lookup(Key: Pointer; KeyLen: Integer; Tag: LongWord; var Found: Boolean;
  496. CanCreate: Boolean): PTagHashSetItem;
  497. protected
  498. class procedure FreeItem(item:PHashSetItem); override;
  499. class function SizeOfItem: Integer; override;
  500. public
  501. { finds an entry by key }
  502. function Find(Key: Pointer; KeyLen: Integer; Tag: LongWord): PTagHashSetItem; reintroduce;
  503. { finds an entry, creates one if not exists }
  504. function FindOrAdd(Key: Pointer; KeyLen: Integer; Tag: LongWord;
  505. var Found: Boolean): PTagHashSetItem; reintroduce;
  506. { finds an entry, creates one if not exists }
  507. function FindOrAdd(Key: Pointer; KeyLen: Integer; Tag: LongWord): PTagHashSetItem; reintroduce;
  508. { returns Data by given Key }
  509. function Get(Key: Pointer; KeyLen: Integer; Tag: LongWord): TObject; reintroduce;
  510. end;
  511. {******************************************************************
  512. tbitset
  513. *******************************************************************}
  514. tbitset = class
  515. private
  516. fdata: pbyte;
  517. fdatasize: longint;
  518. public
  519. constructor create(initsize: longint);
  520. constructor create_bytesize(bytesize: longint);
  521. destructor destroy; override;
  522. procedure clear;
  523. procedure grow(nsize: longint);
  524. { sets a bit }
  525. procedure include(index: longint);
  526. { clears a bit }
  527. procedure exclude(index: longint);
  528. { finds an entry, creates one if not exists }
  529. function isset(index: longint): boolean;
  530. procedure addset(aset: tbitset);
  531. procedure subset(aset: tbitset);
  532. property data: pbyte read fdata;
  533. property datasize: longint read fdatasize;
  534. end;
  535. function FPHash(P: PChar; Len: Integer; Tag: LongWord): LongWord;
  536. function FPHash(P: PChar; Len: Integer): LongWord; inline;
  537. function FPHash(const s:shortstring):LongWord; inline;
  538. function FPHash(const a:ansistring):LongWord; inline;
  539. function ExtractStrings(Separators, WhiteSpace: TSysCharSet; Content: PChar; var Strings: TDynStringArray; AddEmptyStrings : Boolean = False): Integer;
  540. implementation
  541. {*****************************************************************************
  542. Memory debug
  543. *****************************************************************************}
  544. function ExtractStrings(Separators, WhiteSpace: TSysCharSet; Content: PChar; var Strings: TDynStringArray; AddEmptyStrings : Boolean = False): Integer;
  545. var
  546. b, c : pchar;
  547. procedure SkipWhitespace;
  548. begin
  549. while (c^ in Whitespace) do
  550. inc (c);
  551. end;
  552. procedure AddString;
  553. var
  554. l : integer;
  555. s : string;
  556. begin
  557. l := c-b;
  558. s:='';
  559. if (l > 0) or AddEmptyStrings then
  560. begin
  561. setlength(s, l);
  562. if l>0 then
  563. move (b^, s[1],l*SizeOf(char));
  564. l:=length(Strings);
  565. setlength(Strings,l+1);
  566. Strings[l]:=S;
  567. inc (result);
  568. end;
  569. end;
  570. var
  571. quoted : char;
  572. begin
  573. result := 0;
  574. c := Content;
  575. Quoted := #0;
  576. Separators := Separators + [#13, #10] - ['''','"'];
  577. SkipWhitespace;
  578. b := c;
  579. while (c^ <> #0) do
  580. begin
  581. if (c^ = Quoted) then
  582. begin
  583. if ((c+1)^ = Quoted) then
  584. inc (c)
  585. else
  586. Quoted := #0
  587. end
  588. else if (Quoted = #0) and (c^ in ['''','"']) then
  589. Quoted := c^;
  590. if (Quoted = #0) and (c^ in Separators) then
  591. begin
  592. AddString;
  593. inc (c);
  594. SkipWhitespace;
  595. b := c;
  596. end
  597. else
  598. inc (c);
  599. end;
  600. if (c <> b) then
  601. AddString;
  602. end;
  603. constructor tmemdebug.create(const s:string);
  604. begin
  605. infostr:=s;
  606. totalmem:=0;
  607. Start;
  608. end;
  609. procedure tmemdebug.start;
  610. var
  611. status : TFPCHeapStatus;
  612. begin
  613. status:=GetFPCHeapStatus;
  614. startmem:=status.CurrHeapUsed;
  615. end;
  616. procedure tmemdebug.stop;
  617. var
  618. status : TFPCHeapStatus;
  619. begin
  620. if startmem<>0 then
  621. begin
  622. status:=GetFPCHeapStatus;
  623. inc(TotalMem,startmem-status.CurrHeapUsed);
  624. startmem:=0;
  625. end;
  626. end;
  627. destructor tmemdebug.destroy;
  628. begin
  629. Stop;
  630. show;
  631. end;
  632. procedure tmemdebug.show;
  633. begin
  634. write('memory [',infostr,'] ');
  635. if TotalMem>0 then
  636. writeln(DStr(TotalMem shr 10),' Kb released')
  637. else
  638. writeln(DStr((-TotalMem) shr 10),' Kb allocated');
  639. end;
  640. {*****************************************************************************
  641. TFPObjectList (Copied from rtl/objpas/classes/lists.inc)
  642. *****************************************************************************}
  643. procedure TFPList.RaiseIndexError(Index : Integer);{$ifndef VER2_6}noreturn;{$endif VER2_6}
  644. begin
  645. Error(SListIndexError, Index);
  646. end;
  647. function TFPList.Get(Index: Integer): Pointer;
  648. begin
  649. If (Index < 0) or (Index >= FCount) then
  650. RaiseIndexError(Index);
  651. Result:=FList^[Index];
  652. end;
  653. procedure TFPList.Put(Index: Integer; Item: Pointer);
  654. begin
  655. if (Index < 0) or (Index >= FCount) then
  656. RaiseIndexError(Index);
  657. Flist^[Index] := Item;
  658. end;
  659. function TFPList.Extract(item: Pointer): Pointer;
  660. var
  661. i : Integer;
  662. begin
  663. result := nil;
  664. i := IndexOf(item);
  665. if i >= 0 then
  666. begin
  667. Result := item;
  668. FList^[i] := nil;
  669. Delete(i);
  670. end;
  671. end;
  672. procedure TFPList.SetCapacity(NewCapacity: Integer);
  673. begin
  674. If (NewCapacity < FCount) or (NewCapacity > MaxListSize) then
  675. Error (SListCapacityError, NewCapacity);
  676. if NewCapacity = FCapacity then
  677. exit;
  678. ReallocMem(FList, SizeOf(Pointer)*NewCapacity);
  679. FCapacity := NewCapacity;
  680. end;
  681. procedure TFPList.SetCount(NewCount: Integer);
  682. begin
  683. if (NewCount < 0) or (NewCount > MaxListSize)then
  684. Error(SListCountError, NewCount);
  685. If NewCount > FCount then
  686. begin
  687. If NewCount > FCapacity then
  688. SetCapacity(NewCount);
  689. If FCount < NewCount then
  690. FillChar(Flist^[FCount], (NewCount-FCount) * sizeof(Pointer), 0);
  691. end;
  692. FCount := Newcount;
  693. end;
  694. destructor TFPList.Destroy;
  695. begin
  696. Self.Clear;
  697. inherited Destroy;
  698. end;
  699. function TFPList.Add(Item: Pointer): Integer;
  700. begin
  701. if FCount = FCapacity then
  702. Self.Expand;
  703. FList^[FCount] := Item;
  704. Result := FCount;
  705. inc(FCount);
  706. end;
  707. procedure TFPList.Clear;
  708. begin
  709. if Assigned(FList) then
  710. begin
  711. SetCount(0);
  712. SetCapacity(0);
  713. FList := nil;
  714. end;
  715. end;
  716. procedure TFPList.Delete(Index: Integer);
  717. begin
  718. If (Index<0) or (Index>=FCount) then
  719. Error (SListIndexError, Index);
  720. dec(FCount);
  721. System.Move (FList^[Index+1], FList^[Index], (FCount - Index) * SizeOf(Pointer));
  722. { Shrink the list if appropriate }
  723. if (FCapacity > 256) and (FCount < FCapacity shr 2) then
  724. begin
  725. FCapacity := FCapacity shr 1;
  726. ReallocMem(FList, SizeOf(Pointer) * FCapacity);
  727. end;
  728. end;
  729. class procedure TFPList.Error(const Msg: string; Data: PtrInt);{$ifndef VER2_6}noreturn;{$endif VER2_6}
  730. begin
  731. Raise EListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame), get_caller_frame(get_frame);
  732. end;
  733. procedure TFPList.Exchange(Index1, Index2: Integer);
  734. var
  735. Temp : Pointer;
  736. begin
  737. If ((Index1 >= FCount) or (Index1 < 0)) then
  738. Error(SListIndexError, Index1);
  739. If ((Index2 >= FCount) or (Index2 < 0)) then
  740. Error(SListIndexError, Index2);
  741. Temp := FList^[Index1];
  742. FList^[Index1] := FList^[Index2];
  743. FList^[Index2] := Temp;
  744. end;
  745. function TFPList.Expand: TFPList;
  746. var
  747. IncSize : Longint;
  748. begin
  749. Result := Self;
  750. if FCount < FCapacity then
  751. exit;
  752. IncSize := sizeof(ptrint)*2;
  753. if FCapacity > 127 then
  754. Inc(IncSize, FCapacity shr 2)
  755. else if FCapacity > sizeof(ptrint)*4 then
  756. Inc(IncSize, FCapacity shr 1)
  757. else if FCapacity >= sizeof(ptrint) then
  758. inc(IncSize,sizeof(ptrint));
  759. SetCapacity(FCapacity + IncSize);
  760. end;
  761. function TFPList.First: Pointer;
  762. begin
  763. If FCount<>0 then
  764. Result := Items[0]
  765. else
  766. Result := Nil;
  767. end;
  768. function TFPList.IndexOf(Item: Pointer): Integer;
  769. var
  770. psrc : PPointer;
  771. Index : Integer;
  772. begin
  773. Result:=-1;
  774. psrc:=@FList^[0];
  775. For Index:=0 To FCount-1 Do
  776. begin
  777. if psrc^=Item then
  778. begin
  779. Result:=Index;
  780. exit;
  781. end;
  782. inc(psrc);
  783. end;
  784. end;
  785. procedure TFPList.Insert(Index: Integer; Item: Pointer);
  786. begin
  787. if (Index < 0) or (Index > FCount )then
  788. Error(SlistIndexError, Index);
  789. iF FCount = FCapacity then Self.Expand;
  790. if Index<FCount then
  791. System.Move(Flist^[Index], Flist^[Index+1], (FCount - Index) * SizeOf(Pointer));
  792. FList^[Index] := Item;
  793. FCount := FCount + 1;
  794. end;
  795. function TFPList.Last: Pointer;
  796. begin
  797. If FCount<>0 then
  798. Result := Items[FCount - 1]
  799. else
  800. Result := nil
  801. end;
  802. procedure TFPList.Move(CurIndex, NewIndex: Integer);
  803. var
  804. Temp : Pointer;
  805. begin
  806. if ((CurIndex < 0) or (CurIndex > Count - 1)) then
  807. Error(SListIndexError, CurIndex);
  808. if (NewINdex < 0) then
  809. Error(SlistIndexError, NewIndex);
  810. Temp := FList^[CurIndex];
  811. FList^[CurIndex] := nil;
  812. Self.Delete(CurIndex);
  813. Self.Insert(NewIndex, nil);
  814. FList^[NewIndex] := Temp;
  815. end;
  816. function TFPList.Remove(Item: Pointer): Integer;
  817. begin
  818. Result := IndexOf(Item);
  819. If Result <> -1 then
  820. Self.Delete(Result);
  821. end;
  822. procedure TFPList.Pack;
  823. var
  824. NewCount,
  825. i : integer;
  826. pdest,
  827. psrc : PPointer;
  828. begin
  829. NewCount:=0;
  830. psrc:=@FList^[0];
  831. pdest:=psrc;
  832. For I:=0 To FCount-1 Do
  833. begin
  834. if assigned(psrc^) then
  835. begin
  836. pdest^:=psrc^;
  837. inc(pdest);
  838. inc(NewCount);
  839. end;
  840. inc(psrc);
  841. end;
  842. FCount:=NewCount;
  843. end;
  844. Procedure QuickSort(FList: PPointerList; L, R : Longint;Compare: TListSortCompare);
  845. var
  846. I, J, P: Longint;
  847. PItem, Q : Pointer;
  848. begin
  849. repeat
  850. I := L;
  851. J := R;
  852. P := (L + R) div 2;
  853. repeat
  854. PItem := FList^[P];
  855. while Compare(PItem, FList^[i]) > 0 do
  856. I := I + 1;
  857. while Compare(PItem, FList^[J]) < 0 do
  858. J := J - 1;
  859. If I <= J then
  860. begin
  861. Q := FList^[I];
  862. Flist^[I] := FList^[J];
  863. FList^[J] := Q;
  864. if P = I then
  865. P := J
  866. else if P = J then
  867. P := I;
  868. I := I + 1;
  869. J := J - 1;
  870. end;
  871. until I > J;
  872. if L < J then
  873. QuickSort(FList, L, J, Compare);
  874. L := I;
  875. until I >= R;
  876. end;
  877. procedure TFPList.Sort(Compare: TListSortCompare);
  878. begin
  879. if Not Assigned(FList) or (FCount < 2) then exit;
  880. QuickSort(Flist, 0, FCount-1, Compare);
  881. end;
  882. procedure TFPList.Assign(Obj: TFPList);
  883. var
  884. i: Integer;
  885. begin
  886. Clear;
  887. for I := 0 to Obj.Count - 1 do
  888. Add(Obj[i]);
  889. end;
  890. procedure TFPList.ForEachCall(proc2call:TListCallback;arg:pointer);
  891. var
  892. i : integer;
  893. p : pointer;
  894. begin
  895. For I:=0 To Count-1 Do
  896. begin
  897. p:=FList^[i];
  898. if assigned(p) then
  899. proc2call(p,arg);
  900. end;
  901. end;
  902. procedure TFPList.ForEachCall(proc2call:TListStaticCallback;arg:pointer);
  903. var
  904. i : integer;
  905. p : pointer;
  906. begin
  907. For I:=0 To Count-1 Do
  908. begin
  909. p:=FList^[i];
  910. if assigned(p) then
  911. proc2call(p,arg);
  912. end;
  913. end;
  914. {*****************************************************************************
  915. TFPObjectList (Copied from rtl/objpas/classes/lists.inc)
  916. *****************************************************************************}
  917. constructor TFPObjectList.Create(FreeObjects : boolean);
  918. begin
  919. Create;
  920. FFreeObjects := Freeobjects;
  921. end;
  922. destructor TFPObjectList.Destroy;
  923. begin
  924. if (FList <> nil) then
  925. begin
  926. Clear;
  927. FList.Destroy;
  928. FList:=nil;
  929. end;
  930. inherited Destroy;
  931. end;
  932. procedure TFPObjectList.Clear;
  933. var
  934. i: integer;
  935. begin
  936. if FFreeObjects then
  937. for i := 0 to FList.Count - 1 do
  938. TObject(FList[i]).Free;
  939. FList.Clear;
  940. end;
  941. constructor TFPObjectList.Create;
  942. begin
  943. inherited Create;
  944. FList := TFPList.Create;
  945. FFreeObjects := True;
  946. end;
  947. function TFPObjectList.IndexOf(AObject: TObject): Integer;
  948. begin
  949. Result := FList.IndexOf(Pointer(AObject));
  950. end;
  951. function TFPObjectList.GetCount: integer;
  952. begin
  953. Result := FList.Count;
  954. end;
  955. procedure TFPObjectList.SetCount(const AValue: integer);
  956. begin
  957. if FList.Count <> AValue then
  958. FList.Count := AValue;
  959. end;
  960. function TFPObjectList.GetItem(Index: Integer): TObject;
  961. begin
  962. Result := TObject(FList[Index]);
  963. end;
  964. procedure TFPObjectList.SetItem(Index: Integer; AObject: TObject);
  965. begin
  966. if OwnsObjects then
  967. TObject(FList[Index]).Free;
  968. FList[index] := AObject;
  969. end;
  970. procedure TFPObjectList.SetCapacity(NewCapacity: Integer);
  971. begin
  972. FList.Capacity := NewCapacity;
  973. end;
  974. function TFPObjectList.GetCapacity: integer;
  975. begin
  976. Result := FList.Capacity;
  977. end;
  978. function TFPObjectList.Add(AObject: TObject): Integer;
  979. begin
  980. Result := FList.Add(AObject);
  981. end;
  982. procedure TFPObjectList.Delete(Index: Integer);
  983. begin
  984. if OwnsObjects then
  985. TObject(FList[Index]).Free;
  986. FList.Delete(Index);
  987. end;
  988. procedure TFPObjectList.Exchange(Index1, Index2: Integer);
  989. begin
  990. FList.Exchange(Index1, Index2);
  991. end;
  992. function TFPObjectList.Expand: TFPObjectList;
  993. begin
  994. FList.Expand;
  995. Result := Self;
  996. end;
  997. function TFPObjectList.Extract(Item: TObject): TObject;
  998. begin
  999. Result := TObject(FList.Extract(Item));
  1000. end;
  1001. function TFPObjectList.Remove(AObject: TObject): Integer;
  1002. begin
  1003. Result := IndexOf(AObject);
  1004. if (Result <> -1) then
  1005. begin
  1006. if OwnsObjects then
  1007. TObject(FList[Result]).Free;
  1008. FList.Delete(Result);
  1009. end;
  1010. end;
  1011. function TFPObjectList.FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt : Integer): Integer;
  1012. var
  1013. I : Integer;
  1014. begin
  1015. I:=AStartAt;
  1016. Result:=-1;
  1017. If AExact then
  1018. while (I<Count) and (Result=-1) do
  1019. If Items[i].ClassType=AClass then
  1020. Result:=I
  1021. else
  1022. Inc(I)
  1023. else
  1024. while (I<Count) and (Result=-1) do
  1025. If Items[i].InheritsFrom(AClass) then
  1026. Result:=I
  1027. else
  1028. Inc(I);
  1029. end;
  1030. procedure TFPObjectList.Insert(Index: Integer; AObject: TObject);
  1031. begin
  1032. FList.Insert(Index, Pointer(AObject));
  1033. end;
  1034. procedure TFPObjectList.Move(CurIndex, NewIndex: Integer);
  1035. begin
  1036. FList.Move(CurIndex, NewIndex);
  1037. end;
  1038. procedure TFPObjectList.Assign(Obj: TFPObjectList);
  1039. begin
  1040. Clear;
  1041. ConcatListCopy(Obj);
  1042. end;
  1043. procedure TFPObjectList.ConcatListCopy(Obj: TFPObjectList);
  1044. var
  1045. i: Integer;
  1046. begin
  1047. for I := 0 to Obj.Count - 1 do
  1048. Add(Obj[i]);
  1049. end;
  1050. procedure TFPObjectList.Pack;
  1051. begin
  1052. FList.Pack;
  1053. end;
  1054. procedure TFPObjectList.Sort(Compare: TListSortCompare);
  1055. begin
  1056. FList.Sort(Compare);
  1057. end;
  1058. function TFPObjectList.First: TObject;
  1059. begin
  1060. Result := TObject(FList.First);
  1061. end;
  1062. function TFPObjectList.Last: TObject;
  1063. begin
  1064. Result := TObject(FList.Last);
  1065. end;
  1066. procedure TFPObjectList.ForEachCall(proc2call:TObjectListCallback;arg:pointer);
  1067. begin
  1068. FList.ForEachCall(TListCallBack(proc2call),arg);
  1069. end;
  1070. procedure TFPObjectList.ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer);
  1071. begin
  1072. FList.ForEachCall(TListStaticCallBack(proc2call),arg);
  1073. end;
  1074. {*****************************************************************************
  1075. TFPHashList
  1076. *****************************************************************************}
  1077. function FPHash(P: PChar; Len: Integer; Tag: LongWord): LongWord;
  1078. Var
  1079. pmax : pchar;
  1080. begin
  1081. {$push}
  1082. {$q-,r-}
  1083. result:=Tag;
  1084. pmax:=p+len;
  1085. while (p<pmax) do
  1086. begin
  1087. {DJBHash: result:=result*33 + next_char}
  1088. result:=LongWord(LongInt(result shl 5) + LongInt(result)) + LongWord(P^);
  1089. inc(p);
  1090. end;
  1091. {$pop}
  1092. end;
  1093. function FPHash(P: PChar; Len: Integer): LongWord; inline;
  1094. begin
  1095. result:=fphash(P,Len, 5381);
  1096. end;
  1097. function FPHash(const s: shortstring): LongWord; inline;
  1098. begin
  1099. result:=fphash(pchar(@s[1]),length(s));
  1100. end;
  1101. function FPHash(const a: ansistring): LongWord; inline;
  1102. begin
  1103. result:=fphash(pchar(a),length(a));
  1104. end;
  1105. procedure TFPHashList.RaiseIndexError(Index : Integer);
  1106. begin
  1107. Error(SListIndexError, Index);
  1108. end;
  1109. function TFPHashList.Get(Index: Integer): Pointer;
  1110. begin
  1111. If (Index < 0) or (Index >= FCount) then
  1112. RaiseIndexError(Index);
  1113. Result:=FHashList^[Index].Data;
  1114. end;
  1115. procedure TFPHashList.Put(Index: Integer; Item: Pointer);
  1116. begin
  1117. if (Index < 0) or (Index >= FCount) then
  1118. RaiseIndexError(Index);
  1119. FHashList^[Index].Data:=Item;
  1120. end;
  1121. function TFPHashList.NameOfIndex(Index: Integer): TSymStr;
  1122. begin
  1123. If (Index < 0) or (Index >= FCount) then
  1124. RaiseIndexError(Index);
  1125. with FHashList^[Index] do
  1126. begin
  1127. if StrIndex>=0 then
  1128. Result:=PSymStr(@FStrs[StrIndex])^
  1129. else
  1130. Result:='';
  1131. end;
  1132. end;
  1133. function TFPHashList.HashOfIndex(Index: Integer): LongWord;
  1134. begin
  1135. If (Index < 0) or (Index >= FCount) then
  1136. RaiseIndexError(Index);
  1137. Result:=FHashList^[Index].HashValue;
  1138. end;
  1139. function TFPHashList.GetNextCollision(Index: Integer): Integer;
  1140. begin
  1141. Result:=-1;
  1142. if ((Index > -1) and (Index < FCount)) then
  1143. Result:=FHashList^[Index].NextIndex;
  1144. end;
  1145. function TFPHashList.Extract(item: Pointer): Pointer;
  1146. var
  1147. i : Integer;
  1148. begin
  1149. result := nil;
  1150. i := IndexOf(item);
  1151. if i >= 0 then
  1152. begin
  1153. Result := item;
  1154. Delete(i);
  1155. end;
  1156. end;
  1157. procedure TFPHashList.SetCapacity(NewCapacity: Integer);
  1158. var
  1159. power: longint;
  1160. begin
  1161. { use a power of two to be able to quickly calculate the hash table index }
  1162. if NewCapacity <> 0 then
  1163. NewCapacity := nextpowerof2((NewCapacity+(MaxItemsPerHash-1)) div MaxItemsPerHash, power) * MaxItemsPerHash;
  1164. if (NewCapacity < FCount) or (NewCapacity > MaxHashListSize) then
  1165. Error (SListCapacityError, NewCapacity);
  1166. if NewCapacity = FCapacity then
  1167. exit;
  1168. ReallocMem(FHashList, NewCapacity*SizeOf(THashItem));
  1169. FCapacity := NewCapacity;
  1170. { Maybe expand hash also }
  1171. if FCapacity>FHashCapacity*MaxItemsPerHash then
  1172. SetHashCapacity(FCapacity div MaxItemsPerHash);
  1173. end;
  1174. procedure TFPHashList.SetCount(NewCount: Integer);
  1175. begin
  1176. if (NewCount < 0) or (NewCount > MaxHashListSize)then
  1177. Error(SListCountError, NewCount);
  1178. If NewCount > FCount then
  1179. begin
  1180. If NewCount > FCapacity then
  1181. SetCapacity(NewCount);
  1182. If FCount < NewCount then
  1183. { FCapacity is NewCount rounded up to the next power of 2 }
  1184. FillChar(FHashList^[FCount], (FCapacity-FCount) div Sizeof(THashItem), 0);
  1185. end;
  1186. FCount := Newcount;
  1187. end;
  1188. procedure TFPHashList.SetStrCapacity(NewCapacity: Integer);
  1189. {$ifdef symansistr}
  1190. var
  1191. i: longint;
  1192. {$endif symansistr}
  1193. begin
  1194. {$push}{$warnings off}
  1195. If (NewCapacity < FStrCount) or (NewCapacity > MaxHashStrSize) then
  1196. Error (SListCapacityError, NewCapacity);
  1197. {$pop}
  1198. if NewCapacity = FStrCapacity then
  1199. exit;
  1200. {$ifdef symansistr}
  1201. { array of ansistrings -> finalize }
  1202. if (NewCapacity < FStrCapacity) then
  1203. for i:=NewCapacity to FStrCapacity-1 do
  1204. finalize(FStrs[i]);
  1205. ReallocMem(FStrs, NewCapacity*sizeof(pansistring));
  1206. { array of ansistrings -> initialize to nil }
  1207. if (NewCapacity > FStrCapacity) then
  1208. fillchar(FStrs[FStrCapacity],(NewCapacity-FStrCapacity)*sizeof(pansistring),0);
  1209. {$else symansistr}
  1210. ReallocMem(FStrs, NewCapacity);
  1211. {$endif symansistr}
  1212. FStrCapacity := NewCapacity;
  1213. end;
  1214. procedure TFPHashList.SetHashCapacity(NewCapacity: Integer);
  1215. var
  1216. power: longint;
  1217. begin
  1218. If (NewCapacity < 1) then
  1219. Error (SListCapacityError, NewCapacity);
  1220. if FHashCapacity=NewCapacity then
  1221. exit;
  1222. if (NewCapacity<>0) and
  1223. not ispowerof2(NewCapacity,power) then
  1224. Error(SListCapacityPower2Error, NewCapacity);
  1225. FHashCapacity:=NewCapacity;
  1226. ReallocMem(FHashTable, FHashCapacity*sizeof(Integer));
  1227. FCapacityMask:=(1 shl power)-1;
  1228. ReHash;
  1229. end;
  1230. procedure TFPHashList.ReHash;
  1231. var
  1232. i : Integer;
  1233. begin
  1234. FillDword(FHashTable^,FHashCapacity,LongWord(-1));
  1235. For i:=0 To FCount-1 Do
  1236. AddToHashTable(i);
  1237. end;
  1238. constructor TFPHashList.Create;
  1239. begin
  1240. SetHashCapacity(1);
  1241. end;
  1242. destructor TFPHashList.Destroy;
  1243. begin
  1244. Clear;
  1245. if assigned(FHashTable) then
  1246. FreeMem(FHashTable);
  1247. inherited Destroy;
  1248. end;
  1249. function TFPHashList.AddStr(const s:TSymStr): Integer;
  1250. {$ifndef symansistr}
  1251. var
  1252. Len : Integer;
  1253. {$endif symansistr}
  1254. begin
  1255. {$ifdef symansistr}
  1256. if FStrCount+1 >= FStrCapacity then
  1257. StrExpand(FStrCount+1);
  1258. FStrs[FStrCount]:=s;
  1259. result:=FStrCount;
  1260. inc(FStrCount);
  1261. {$else symansistr}
  1262. len:=length(s)+1;
  1263. if FStrCount+Len >= FStrCapacity then
  1264. StrExpand(Len);
  1265. System.Move(s[0],FStrs[FStrCount],Len);
  1266. result:=FStrCount;
  1267. inc(FStrCount,Len);
  1268. {$endif symansistr}
  1269. end;
  1270. procedure TFPHashList.AddToHashTable(Index: Integer);
  1271. var
  1272. HashIndex : Integer;
  1273. begin
  1274. with FHashList^[Index] do
  1275. begin
  1276. if not assigned(Data) then
  1277. exit;
  1278. HashIndex:=HashValue and FCapacityMask;
  1279. NextIndex:=FHashTable^[HashIndex];
  1280. FHashTable^[HashIndex]:=Index;
  1281. end;
  1282. end;
  1283. function TFPHashList.Add(const AName:TSymStr;Item: Pointer): Integer;
  1284. begin
  1285. if FCount = FCapacity then
  1286. Expand;
  1287. with FHashList^[FCount] do
  1288. begin
  1289. HashValue:=FPHash(AName);
  1290. Data:=Item;
  1291. StrIndex:=AddStr(AName);
  1292. end;
  1293. AddToHashTable(FCount);
  1294. Result := FCount;
  1295. inc(FCount);
  1296. end;
  1297. procedure TFPHashList.Clear;
  1298. begin
  1299. if Assigned(FHashList) then
  1300. begin
  1301. FCount:=0;
  1302. SetCapacity(0);
  1303. FHashList := nil;
  1304. end;
  1305. SetHashCapacity(1);
  1306. FHashTable^[0]:=-1; // sethashcapacity does not always call rehash
  1307. if Assigned(FStrs) then
  1308. begin
  1309. FStrCount:=0;
  1310. SetStrCapacity(0);
  1311. FStrs := nil;
  1312. end;
  1313. end;
  1314. procedure TFPHashList.Delete(Index: Integer);
  1315. begin
  1316. If (Index<0) or (Index>=FCount) then
  1317. Error (SListIndexError, Index);
  1318. { Remove from HashList }
  1319. dec(FCount);
  1320. System.Move (FHashList^[Index+1], FHashList^[Index], (FCount - Index) * Sizeof(THashItem));
  1321. { All indexes are updated, we need to build the hashtable again }
  1322. Rehash;
  1323. { Shrink the list if appropriate }
  1324. if (FCapacity > 256) and (FCount < FCapacity shr 2) then
  1325. begin
  1326. FCapacity := FCapacity shr 1;
  1327. ReallocMem(FHashList, Sizeof(THashItem) * FCapacity);
  1328. end;
  1329. end;
  1330. function TFPHashList.Remove(Item: Pointer): Integer;
  1331. begin
  1332. Result := IndexOf(Item);
  1333. If Result <> -1 then
  1334. Self.Delete(Result);
  1335. end;
  1336. class procedure TFPHashList.Error(const Msg: string; Data: PtrInt);{$ifndef VER2_6}noreturn;{$endif VER2_6}
  1337. begin
  1338. Raise EListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame), get_caller_frame(get_frame);
  1339. end;
  1340. function TFPHashList.Expand: TFPHashList;
  1341. var
  1342. IncSize : Longint;
  1343. begin
  1344. Result := Self;
  1345. if FCount < FCapacity then
  1346. exit;
  1347. IncSize := sizeof(ptrint)*2;
  1348. SetCapacity(FCapacity + IncSize);
  1349. end;
  1350. procedure TFPHashList.StrExpand(MinIncSize:Integer);
  1351. var
  1352. IncSize : Longint;
  1353. begin
  1354. if FStrCount+MinIncSize < FStrCapacity then
  1355. exit;
  1356. IncSize := 64;
  1357. if FStrCapacity > 255 then
  1358. Inc(IncSize, FStrCapacity shr 2);
  1359. SetStrCapacity(FStrCapacity + IncSize + MinIncSize);
  1360. end;
  1361. function TFPHashList.IndexOf(Item: Pointer): Integer;
  1362. var
  1363. psrc : PHashItem;
  1364. Index : integer;
  1365. begin
  1366. Result:=-1;
  1367. psrc:=@FHashList^[0];
  1368. For Index:=0 To FCount-1 Do
  1369. begin
  1370. if psrc^.Data=Item then
  1371. begin
  1372. Result:=Index;
  1373. exit;
  1374. end;
  1375. inc(psrc);
  1376. end;
  1377. end;
  1378. function TFPHashList.InternalFind(AHash:LongWord;const AName:TSymStr;out PrevIndex:Integer):Integer;
  1379. begin
  1380. prefetch(AName[1]);
  1381. Result:=FHashTable^[AHash and FCapacityMask];
  1382. PrevIndex:=-1;
  1383. while Result<>-1 do
  1384. begin
  1385. with FHashList^[Result] do
  1386. begin
  1387. if assigned(Data) and
  1388. (HashValue=AHash) and
  1389. (AName=PSymStr(@FStrs[StrIndex])^) then
  1390. exit;
  1391. PrevIndex:=Result;
  1392. Result:=NextIndex;
  1393. end;
  1394. end;
  1395. end;
  1396. function TFPHashList.Find(const AName:TSymStr): Pointer;
  1397. var
  1398. Index,
  1399. PrevIndex : Integer;
  1400. begin
  1401. Result:=nil;
  1402. Index:=InternalFind(FPHash(AName),AName,PrevIndex);
  1403. if Index=-1 then
  1404. exit;
  1405. Result:=FHashList^[Index].Data;
  1406. end;
  1407. function TFPHashList.FindIndexOf(const AName:TSymStr): Integer;
  1408. var
  1409. PrevIndex : Integer;
  1410. begin
  1411. Result:=InternalFind(FPHash(AName),AName,PrevIndex);
  1412. end;
  1413. function TFPHashList.FindWithHash(const AName:TSymStr;AHash:LongWord): Pointer;
  1414. var
  1415. Index,
  1416. PrevIndex : Integer;
  1417. begin
  1418. Result:=nil;
  1419. Index:=InternalFind(AHash,AName,PrevIndex);
  1420. if Index=-1 then
  1421. exit;
  1422. Result:=FHashList^[Index].Data;
  1423. end;
  1424. function TFPHashList.Rename(const AOldName,ANewName:TSymStr): Integer;
  1425. var
  1426. PrevIndex,
  1427. Index : Integer;
  1428. OldHash : LongWord;
  1429. begin
  1430. Result:=-1;
  1431. OldHash:=FPHash(AOldName);
  1432. Index:=InternalFind(OldHash,AOldName,PrevIndex);
  1433. if Index=-1 then
  1434. exit;
  1435. { Remove from current Hash }
  1436. if PrevIndex<>-1 then
  1437. FHashList^[PrevIndex].NextIndex:=FHashList^[Index].NextIndex
  1438. else
  1439. FHashTable^[OldHash and FCapacityMask]:=FHashList^[Index].NextIndex;
  1440. { Set new name and hash }
  1441. with FHashList^[Index] do
  1442. begin
  1443. HashValue:=FPHash(ANewName);
  1444. StrIndex:=AddStr(ANewName);
  1445. end;
  1446. { Insert back in Hash }
  1447. AddToHashTable(Index);
  1448. { Return Index }
  1449. Result:=Index;
  1450. end;
  1451. procedure TFPHashList.Pack;
  1452. var
  1453. NewCount,
  1454. i : integer;
  1455. pdest,
  1456. psrc : PHashItem;
  1457. begin
  1458. NewCount:=0;
  1459. psrc:=@FHashList^[0];
  1460. pdest:=psrc;
  1461. For I:=0 To FCount-1 Do
  1462. begin
  1463. if assigned(psrc^.Data) then
  1464. begin
  1465. pdest^:=psrc^;
  1466. inc(pdest);
  1467. inc(NewCount);
  1468. end;
  1469. inc(psrc);
  1470. end;
  1471. FCount:=NewCount;
  1472. { We need to ReHash to update the IndexNext }
  1473. ReHash;
  1474. { Release over-capacity }
  1475. SetCapacity(FCount);
  1476. SetStrCapacity(FStrCount);
  1477. end;
  1478. procedure TFPHashList.ShowStatistics;
  1479. var
  1480. HashMean,
  1481. HashStdDev : Double;
  1482. Index,
  1483. i,j : Integer;
  1484. begin
  1485. { Calculate Mean and StdDev }
  1486. HashMean:=0;
  1487. HashStdDev:=0;
  1488. for i:=0 to FHashCapacity-1 do
  1489. begin
  1490. j:=0;
  1491. Index:=FHashTable^[i];
  1492. while (Index<>-1) do
  1493. begin
  1494. inc(j);
  1495. Index:=FHashList^[Index].NextIndex;
  1496. end;
  1497. HashMean:=HashMean+j;
  1498. HashStdDev:=HashStdDev+Sqr(j);
  1499. end;
  1500. HashMean:=HashMean/FHashCapacity;
  1501. HashStdDev:=(HashStdDev-FHashCapacity*Sqr(HashMean));
  1502. If FHashCapacity>1 then
  1503. HashStdDev:=Sqrt(HashStdDev/(FHashCapacity-1))
  1504. else
  1505. HashStdDev:=0;
  1506. { Print info to stdout }
  1507. Writeln('HashSize : ',FHashCapacity);
  1508. Writeln('HashMean : ',HashMean:1:4);
  1509. Writeln('HashStdDev : ',HashStdDev:1:4);
  1510. Writeln('ListSize : ',FCount,'/',FCapacity);
  1511. Writeln('StringSize : ',FStrCount,'/',FStrCapacity);
  1512. end;
  1513. procedure TFPHashList.ForEachCall(proc2call:TListCallback;arg:pointer);
  1514. var
  1515. i : integer;
  1516. p : pointer;
  1517. begin
  1518. For I:=0 To Count-1 Do
  1519. begin
  1520. p:=FHashList^[i].Data;
  1521. if assigned(p) then
  1522. proc2call(p,arg);
  1523. end;
  1524. end;
  1525. procedure TFPHashList.ForEachCall(proc2call:TListStaticCallback;arg:pointer);
  1526. var
  1527. i : integer;
  1528. p : pointer;
  1529. begin
  1530. For I:=0 To Count-1 Do
  1531. begin
  1532. p:=FHashList^[i].Data;
  1533. if assigned(p) then
  1534. proc2call(p,arg);
  1535. end;
  1536. end;
  1537. procedure TFPHashList.WhileEachCall(proc2call:TListCallback;arg:pointer);
  1538. var
  1539. i : integer;
  1540. p : pointer;
  1541. begin
  1542. i:=0;
  1543. while i<count do
  1544. begin
  1545. p:=FHashList^[i].Data;
  1546. if assigned(p) then
  1547. proc2call(p,arg);
  1548. inc(i);
  1549. end;
  1550. end;
  1551. procedure TFPHashList.WhileEachCall(proc2call:TListStaticCallback;arg:pointer);
  1552. var
  1553. i : integer;
  1554. p : pointer;
  1555. begin
  1556. i:=0;
  1557. while i<count do
  1558. begin
  1559. p:=FHashList^[i].Data;
  1560. if assigned(p) then
  1561. proc2call(p,arg);
  1562. inc(i);
  1563. end;
  1564. end;
  1565. {*****************************************************************************
  1566. TFPHashObjectList (Copied from rtl/objpas/classes/lists.inc)
  1567. *****************************************************************************}
  1568. constructor TFPHashObjectList.Create(FreeObjects : boolean = True);
  1569. begin
  1570. inherited Create;
  1571. FHashList := TFPHashList.Create;
  1572. FFreeObjects := Freeobjects;
  1573. end;
  1574. destructor TFPHashObjectList.Destroy;
  1575. begin
  1576. if (FHashList <> nil) then
  1577. begin
  1578. Clear;
  1579. FHashList.Destroy;
  1580. FHashList:=nil;
  1581. end;
  1582. inherited Destroy;
  1583. end;
  1584. procedure TFPHashObjectList.Clear;
  1585. var
  1586. i: integer;
  1587. begin
  1588. if FFreeObjects then
  1589. for i := 0 to FHashList.Count - 1 do
  1590. TObject(FHashList[i]).Free;
  1591. FHashList.Clear;
  1592. end;
  1593. function TFPHashObjectList.IndexOf(AObject: TObject): Integer;
  1594. begin
  1595. Result := FHashList.IndexOf(Pointer(AObject));
  1596. end;
  1597. function TFPHashObjectList.GetCount: integer;
  1598. begin
  1599. Result := FHashList.Count;
  1600. end;
  1601. procedure TFPHashObjectList.SetCount(const AValue: integer);
  1602. begin
  1603. if FHashList.Count <> AValue then
  1604. FHashList.Count := AValue;
  1605. end;
  1606. function TFPHashObjectList.GetItem(Index: Integer): TObject;
  1607. begin
  1608. Result := TObject(FHashList[Index]);
  1609. end;
  1610. procedure TFPHashObjectList.SetItem(Index: Integer; AObject: TObject);
  1611. begin
  1612. if OwnsObjects then
  1613. TObject(FHashList[Index]).Free;
  1614. FHashList[index] := AObject;
  1615. end;
  1616. procedure TFPHashObjectList.SetCapacity(NewCapacity: Integer);
  1617. begin
  1618. FHashList.Capacity := NewCapacity;
  1619. end;
  1620. function TFPHashObjectList.GetCapacity: integer;
  1621. begin
  1622. Result := FHashList.Capacity;
  1623. end;
  1624. function TFPHashObjectList.Add(const AName:TSymStr;AObject: TObject): Integer;
  1625. begin
  1626. Result := FHashList.Add(AName,AObject);
  1627. end;
  1628. function TFPHashObjectList.NameOfIndex(Index: Integer): TSymStr;
  1629. begin
  1630. Result := FHashList.NameOfIndex(Index);
  1631. end;
  1632. function TFPHashObjectList.HashOfIndex(Index: Integer): LongWord;
  1633. begin
  1634. Result := FHashList.HashOfIndex(Index);
  1635. end;
  1636. function TFPHashObjectList.GetNextCollision(Index: Integer): Integer;
  1637. begin
  1638. Result := FHashList.GetNextCollision(Index);
  1639. end;
  1640. procedure TFPHashObjectList.Delete(Index: Integer);
  1641. begin
  1642. if OwnsObjects then
  1643. TObject(FHashList[Index]).Free;
  1644. FHashList.Delete(Index);
  1645. end;
  1646. function TFPHashObjectList.Expand: TFPHashObjectList;
  1647. begin
  1648. FHashList.Expand;
  1649. Result := Self;
  1650. end;
  1651. function TFPHashObjectList.Extract(Item: TObject): TObject;
  1652. begin
  1653. Result := TObject(FHashList.Extract(Item));
  1654. end;
  1655. function TFPHashObjectList.Remove(AObject: TObject): Integer;
  1656. begin
  1657. Result := IndexOf(AObject);
  1658. if (Result <> -1) then
  1659. begin
  1660. if OwnsObjects then
  1661. TObject(FHashList[Result]).Free;
  1662. FHashList.Delete(Result);
  1663. end;
  1664. end;
  1665. function TFPHashObjectList.Find(const s:TSymStr): TObject;
  1666. begin
  1667. result:=TObject(FHashList.Find(s));
  1668. end;
  1669. function TFPHashObjectList.FindIndexOf(const s:TSymStr): Integer;
  1670. begin
  1671. result:=FHashList.FindIndexOf(s);
  1672. end;
  1673. function TFPHashObjectList.FindWithHash(const AName:TSymStr;AHash:LongWord): Pointer;
  1674. begin
  1675. Result:=TObject(FHashList.FindWithHash(AName,AHash));
  1676. end;
  1677. function TFPHashObjectList.Rename(const AOldName,ANewName:TSymStr): Integer;
  1678. begin
  1679. Result:=FHashList.Rename(AOldName,ANewName);
  1680. end;
  1681. function TFPHashObjectList.FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt : Integer): Integer;
  1682. var
  1683. I : Integer;
  1684. begin
  1685. I:=AStartAt;
  1686. Result:=-1;
  1687. If AExact then
  1688. while (I<Count) and (Result=-1) do
  1689. If Items[i].ClassType=AClass then
  1690. Result:=I
  1691. else
  1692. Inc(I)
  1693. else
  1694. while (I<Count) and (Result=-1) do
  1695. If Items[i].InheritsFrom(AClass) then
  1696. Result:=I
  1697. else
  1698. Inc(I);
  1699. end;
  1700. procedure TFPHashObjectList.Pack;
  1701. begin
  1702. FHashList.Pack;
  1703. end;
  1704. procedure TFPHashObjectList.ShowStatistics;
  1705. begin
  1706. FHashList.ShowStatistics;
  1707. end;
  1708. procedure TFPHashObjectList.ForEachCall(proc2call:TObjectListCallback;arg:pointer);
  1709. begin
  1710. FHashList.ForEachCall(TListCallBack(proc2call),arg);
  1711. end;
  1712. procedure TFPHashObjectList.ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer);
  1713. begin
  1714. FHashList.ForEachCall(TListStaticCallBack(proc2call),arg);
  1715. end;
  1716. procedure TFPHashObjectList.WhileEachCall(proc2call:TObjectListCallback;arg:pointer);
  1717. begin
  1718. FHashList.WhileEachCall(TListCallBack(proc2call),arg);
  1719. end;
  1720. procedure TFPHashObjectList.WhileEachCall(proc2call:TObjectListStaticCallback;arg:pointer);
  1721. begin
  1722. FHashList.WhileEachCall(TListStaticCallBack(proc2call),arg);
  1723. end;
  1724. {*****************************************************************************
  1725. TFPHashObject
  1726. *****************************************************************************}
  1727. procedure TFPHashObject.InternalChangeOwner(HashObjectList:TFPHashObjectList;const s:TSymStr);
  1728. var
  1729. Index : integer;
  1730. begin
  1731. FOwner:=HashObjectList;
  1732. Index:=HashObjectList.Add(s,Self);
  1733. FStrIndex:=HashObjectList.List.List^[Index].StrIndex;
  1734. end;
  1735. constructor TFPHashObject.CreateNotOwned;
  1736. begin
  1737. FStrIndex:=-1;
  1738. end;
  1739. constructor TFPHashObject.Create(HashObjectList:TFPHashObjectList;const s:TSymStr);
  1740. begin
  1741. InternalChangeOwner(HashObjectList,s);
  1742. end;
  1743. procedure TFPHashObject.ChangeOwner(HashObjectList:TFPHashObjectList);
  1744. begin
  1745. InternalChangeOwner(HashObjectList,PSymStr(@FOwner.List.Strs[FStrIndex])^);
  1746. end;
  1747. procedure TFPHashObject.ChangeOwnerAndName(HashObjectList:TFPHashObjectList;const s:TSymStr);
  1748. begin
  1749. InternalChangeOwner(HashObjectList,s);
  1750. end;
  1751. procedure TFPHashObject.Rename(const ANewName:TSymStr);
  1752. var
  1753. Index : integer;
  1754. begin
  1755. Index:=FOwner.Rename(PSymStr(@FOwner.List.Strs[FStrIndex])^,ANewName);
  1756. if Index<>-1 then
  1757. FStrIndex:=FOwner.List.List^[Index].StrIndex;
  1758. end;
  1759. function TFPHashObject.GetName:TSymStr;
  1760. begin
  1761. if FOwner<>nil then
  1762. Result:=PSymStr(@FOwner.List.Strs[FStrIndex])^
  1763. else
  1764. Result:='';
  1765. end;
  1766. function TFPHashObject.GetHash:Longword;
  1767. begin
  1768. if FOwner<>nil then
  1769. Result:=FPHash(PSymStr(@FOwner.List.Strs[FStrIndex])^)
  1770. else
  1771. Result:=$ffffffff;
  1772. end;
  1773. {****************************************************************************
  1774. TLinkedListItem
  1775. ****************************************************************************}
  1776. constructor TLinkedListItem.Create;
  1777. begin
  1778. Previous:=nil;
  1779. Next:=nil;
  1780. end;
  1781. destructor TLinkedListItem.Destroy;
  1782. begin
  1783. end;
  1784. function TLinkedListItem.GetCopy:TLinkedListItem;
  1785. var
  1786. p : TLinkedListItem;
  1787. l : integer;
  1788. begin
  1789. p:=TLinkedListItemClass(ClassType).Create;
  1790. l:=InstanceSize;
  1791. Move(pointer(self)^,pointer(p)^,l);
  1792. Result:=p;
  1793. end;
  1794. {****************************************************************************
  1795. TLinkedList
  1796. ****************************************************************************}
  1797. constructor TLinkedList.Create;
  1798. begin
  1799. FFirst:=nil;
  1800. Flast:=nil;
  1801. FCount:=0;
  1802. FNoClear:=False;
  1803. end;
  1804. destructor TLinkedList.destroy;
  1805. begin
  1806. if not FNoClear then
  1807. Clear;
  1808. end;
  1809. function TLinkedList.empty:boolean;
  1810. begin
  1811. Empty:=(FFirst=nil);
  1812. end;
  1813. procedure TLinkedList.Insert(Item:TLinkedListItem);
  1814. begin
  1815. if FFirst=nil then
  1816. begin
  1817. FLast:=Item;
  1818. Item.Previous:=nil;
  1819. Item.Next:=nil;
  1820. end
  1821. else
  1822. begin
  1823. FFirst.Previous:=Item;
  1824. Item.Previous:=nil;
  1825. Item.Next:=FFirst;
  1826. end;
  1827. FFirst:=Item;
  1828. inc(FCount);
  1829. end;
  1830. procedure TLinkedList.InsertBefore(Item,Loc : TLinkedListItem);
  1831. begin
  1832. Item.Previous:=Loc.Previous;
  1833. Item.Next:=Loc;
  1834. Loc.Previous:=Item;
  1835. if assigned(Item.Previous) then
  1836. Item.Previous.Next:=Item
  1837. else
  1838. { if we've no next item, we've to adjust FFist }
  1839. FFirst:=Item;
  1840. inc(FCount);
  1841. end;
  1842. procedure TLinkedList.InsertAfter(Item,Loc : TLinkedListItem);
  1843. begin
  1844. Item.Next:=Loc.Next;
  1845. Loc.Next:=Item;
  1846. Item.Previous:=Loc;
  1847. if assigned(Item.Next) then
  1848. Item.Next.Previous:=Item
  1849. else
  1850. { if we've no next item, we've to adjust FLast }
  1851. FLast:=Item;
  1852. inc(FCount);
  1853. end;
  1854. procedure TLinkedList.Concat(Item:TLinkedListItem);
  1855. begin
  1856. if FFirst=nil then
  1857. begin
  1858. FFirst:=Item;
  1859. Item.Previous:=nil;
  1860. Item.Next:=nil;
  1861. end
  1862. else
  1863. begin
  1864. Flast.Next:=Item;
  1865. Item.Previous:=Flast;
  1866. Item.Next:=nil;
  1867. end;
  1868. Flast:=Item;
  1869. inc(FCount);
  1870. end;
  1871. procedure TLinkedList.remove(Item:TLinkedListItem);
  1872. begin
  1873. if Item=nil then
  1874. exit;
  1875. if (FFirst=Item) and (Flast=Item) then
  1876. begin
  1877. FFirst:=nil;
  1878. Flast:=nil;
  1879. end
  1880. else if FFirst=Item then
  1881. begin
  1882. FFirst:=Item.Next;
  1883. if assigned(FFirst) then
  1884. FFirst.Previous:=nil;
  1885. end
  1886. else if Flast=Item then
  1887. begin
  1888. Flast:=Flast.Previous;
  1889. if assigned(Flast) then
  1890. Flast.Next:=nil;
  1891. end
  1892. else
  1893. begin
  1894. Item.Previous.Next:=Item.Next;
  1895. Item.Next.Previous:=Item.Previous;
  1896. end;
  1897. Item.Next:=nil;
  1898. Item.Previous:=nil;
  1899. dec(FCount);
  1900. end;
  1901. procedure TLinkedList.clear;
  1902. var
  1903. NewNode, Next : TLinkedListItem;
  1904. begin
  1905. NewNode:=FFirst;
  1906. while assigned(NewNode) do
  1907. begin
  1908. Next:=NewNode.Next;
  1909. prefetch(next.next);
  1910. NewNode.Free;
  1911. NewNode:=Next;
  1912. end;
  1913. FLast:=nil;
  1914. FFirst:=nil;
  1915. FCount:=0;
  1916. end;
  1917. function TLinkedList.GetFirst:TLinkedListItem;
  1918. begin
  1919. if FFirst=nil then
  1920. GetFirst:=nil
  1921. else
  1922. begin
  1923. GetFirst:=FFirst;
  1924. if FFirst=FLast then
  1925. FLast:=nil;
  1926. FFirst:=FFirst.Next;
  1927. dec(FCount);
  1928. end;
  1929. end;
  1930. function TLinkedList.GetLast:TLinkedListItem;
  1931. begin
  1932. if FLast=nil then
  1933. Getlast:=nil
  1934. else
  1935. begin
  1936. Getlast:=FLast;
  1937. if FLast=FFirst then
  1938. FFirst:=nil;
  1939. FLast:=FLast.Previous;
  1940. dec(FCount);
  1941. end;
  1942. end;
  1943. procedure TLinkedList.insertList(p : TLinkedList);
  1944. begin
  1945. { empty List ? }
  1946. if (p.FFirst=nil) then
  1947. exit;
  1948. p.Flast.Next:=FFirst;
  1949. { we have a double Linked List }
  1950. if assigned(FFirst) then
  1951. FFirst.Previous:=p.Flast;
  1952. FFirst:=p.FFirst;
  1953. if (FLast=nil) then
  1954. Flast:=p.Flast;
  1955. inc(FCount,p.FCount);
  1956. { p becomes empty }
  1957. p.FFirst:=nil;
  1958. p.Flast:=nil;
  1959. p.FCount:=0;
  1960. end;
  1961. procedure TLinkedList.insertListBefore(Item:TLinkedListItem;p : TLinkedList);
  1962. begin
  1963. { empty List ? }
  1964. if (p.FFirst=nil) then
  1965. exit;
  1966. if (Item=nil) then
  1967. begin
  1968. { Insert at begin }
  1969. InsertList(p);
  1970. exit;
  1971. end
  1972. else
  1973. begin
  1974. p.FLast.Next:=Item;
  1975. p.FFirst.Previous:=Item.Previous;
  1976. if assigned(Item.Previous) then
  1977. Item.Previous.Next:=p.FFirst
  1978. else
  1979. FFirst:=p.FFirst;
  1980. Item.Previous:=p.FLast;
  1981. inc(FCount,p.FCount);
  1982. end;
  1983. { p becomes empty }
  1984. p.FFirst:=nil;
  1985. p.Flast:=nil;
  1986. p.FCount:=0;
  1987. end;
  1988. procedure TLinkedList.insertListAfter(Item:TLinkedListItem;p : TLinkedList);
  1989. begin
  1990. { empty List ? }
  1991. if (p.FFirst=nil) then
  1992. exit;
  1993. if (Item=nil) then
  1994. begin
  1995. { Insert at begin }
  1996. InsertList(p);
  1997. exit;
  1998. end
  1999. else
  2000. begin
  2001. p.FFirst.Previous:=Item;
  2002. p.FLast.Next:=Item.Next;
  2003. if assigned(Item.Next) then
  2004. Item.Next.Previous:=p.FLast
  2005. else
  2006. FLast:=p.FLast;
  2007. Item.Next:=p.FFirst;
  2008. inc(FCount,p.FCount);
  2009. end;
  2010. { p becomes empty }
  2011. p.FFirst:=nil;
  2012. p.Flast:=nil;
  2013. p.FCount:=0;
  2014. end;
  2015. procedure TLinkedList.concatList(p : TLinkedList);
  2016. begin
  2017. if (p.FFirst=nil) then
  2018. exit;
  2019. if FFirst=nil then
  2020. FFirst:=p.FFirst
  2021. else
  2022. begin
  2023. FLast.Next:=p.FFirst;
  2024. p.FFirst.Previous:=Flast;
  2025. end;
  2026. Flast:=p.Flast;
  2027. inc(FCount,p.FCount);
  2028. { make p empty }
  2029. p.Flast:=nil;
  2030. p.FFirst:=nil;
  2031. p.FCount:=0;
  2032. end;
  2033. procedure TLinkedList.insertListcopy(p : TLinkedList);
  2034. var
  2035. NewNode,NewNode2 : TLinkedListItem;
  2036. begin
  2037. NewNode:=p.Last;
  2038. while assigned(NewNode) do
  2039. begin
  2040. NewNode2:=NewNode.Getcopy;
  2041. if assigned(NewNode2) then
  2042. Insert(NewNode2);
  2043. NewNode:=NewNode.Previous;
  2044. end;
  2045. end;
  2046. procedure TLinkedList.concatListcopy(p : TLinkedList);
  2047. var
  2048. NewNode,NewNode2 : TLinkedListItem;
  2049. begin
  2050. NewNode:=p.First;
  2051. while assigned(NewNode) do
  2052. begin
  2053. NewNode2:=NewNode.Getcopy;
  2054. if assigned(NewNode2) then
  2055. Concat(NewNode2);
  2056. NewNode:=NewNode.Next;
  2057. end;
  2058. end;
  2059. procedure TLinkedList.RemoveAll;
  2060. begin
  2061. FFirst:=nil;
  2062. FLast:=nil;
  2063. FCount:=0;
  2064. end;
  2065. {****************************************************************************
  2066. TCmdStrListItem
  2067. ****************************************************************************}
  2068. constructor TCmdStrListItem.Create(const s:TCmdStr);
  2069. begin
  2070. inherited Create;
  2071. FPStr:=s;
  2072. end;
  2073. destructor TCmdStrListItem.Destroy;
  2074. begin
  2075. FPStr:='';
  2076. end;
  2077. function TCmdStrListItem.GetCopy:TLinkedListItem;
  2078. begin
  2079. Result:=(inherited GetCopy);
  2080. { TLinkedListItem.GetCopy performs a "move" to copy all data -> reinit
  2081. the ansistring, so the refcount is properly increased }
  2082. Initialize(TCmdStrListItem(Result).FPStr);
  2083. TCmdStrListItem(Result).FPStr:=FPstr;
  2084. end;
  2085. {****************************************************************************
  2086. TCmdStrList
  2087. ****************************************************************************}
  2088. constructor TCmdStrList.Create;
  2089. begin
  2090. inherited Create;
  2091. FDoubles:=true;
  2092. end;
  2093. constructor TCmdStrList.Create_no_double;
  2094. begin
  2095. inherited Create;
  2096. FDoubles:=false;
  2097. end;
  2098. procedure TCmdStrList.insert(const s : TCmdStr);
  2099. begin
  2100. if (s='') or
  2101. ((not FDoubles) and (findcase(s)<>nil)) then
  2102. exit;
  2103. inherited insert(TCmdStrListItem.create(s));
  2104. end;
  2105. procedure TCmdStrList.concat(const s : TCmdStr);
  2106. begin
  2107. if (s='') or
  2108. ((not FDoubles) and (findcase(s)<>nil)) then
  2109. exit;
  2110. inherited concat(TCmdStrListItem.create(s));
  2111. end;
  2112. procedure TCmdStrList.remove(const s : TCmdStr);
  2113. var
  2114. p : TCmdStrListItem;
  2115. begin
  2116. if s='' then
  2117. exit;
  2118. p:=findcase(s);
  2119. if assigned(p) then
  2120. begin
  2121. inherited Remove(p);
  2122. p.Free;
  2123. end;
  2124. end;
  2125. function TCmdStrList.GetFirst : TCmdStr;
  2126. var
  2127. p : TCmdStrListItem;
  2128. begin
  2129. p:=TCmdStrListItem(inherited GetFirst);
  2130. if p=nil then
  2131. GetFirst:=''
  2132. else
  2133. begin
  2134. GetFirst:=p.FPStr;
  2135. p.free;
  2136. end;
  2137. end;
  2138. function TCmdStrList.Getlast : TCmdStr;
  2139. var
  2140. p : TCmdStrListItem;
  2141. begin
  2142. p:=TCmdStrListItem(inherited Getlast);
  2143. if p=nil then
  2144. Getlast:=''
  2145. else
  2146. begin
  2147. Getlast:=p.FPStr;
  2148. p.free;
  2149. end;
  2150. end;
  2151. function TCmdStrList.FindCase(const s:TCmdStr):TCmdStrListItem;
  2152. var
  2153. NewNode : TCmdStrListItem;
  2154. begin
  2155. result:=nil;
  2156. if s='' then
  2157. exit;
  2158. NewNode:=TCmdStrListItem(FFirst);
  2159. while assigned(NewNode) do
  2160. begin
  2161. if NewNode.FPStr=s then
  2162. begin
  2163. result:=NewNode;
  2164. exit;
  2165. end;
  2166. NewNode:=TCmdStrListItem(NewNode.Next);
  2167. end;
  2168. end;
  2169. function TCmdStrList.Find(const s:TCmdStr):TCmdStrListItem;
  2170. var
  2171. NewNode : TCmdStrListItem;
  2172. begin
  2173. result:=nil;
  2174. if s='' then
  2175. exit;
  2176. NewNode:=TCmdStrListItem(FFirst);
  2177. while assigned(NewNode) do
  2178. begin
  2179. if SysUtils.CompareText(s, NewNode.FPStr)=0 then
  2180. begin
  2181. result:=NewNode;
  2182. exit;
  2183. end;
  2184. NewNode:=TCmdStrListItem(NewNode.Next);
  2185. end;
  2186. end;
  2187. procedure TCmdStrList.InsertItem(item:TCmdStrListItem);
  2188. begin
  2189. inherited Insert(item);
  2190. end;
  2191. procedure TCmdStrList.ConcatItem(item:TCmdStrListItem);
  2192. begin
  2193. inherited Concat(item);
  2194. end;
  2195. {****************************************************************************
  2196. tdynamicarray
  2197. ****************************************************************************}
  2198. constructor tdynamicarray.create(Ablocksize:longword);
  2199. begin
  2200. FPosn:=0;
  2201. FPosnblock:=nil;
  2202. FFirstblock:=nil;
  2203. FLastblock:=nil;
  2204. FCurrBlockSize:=0;
  2205. { Every block needs at least a header and alignment slack,
  2206. therefore its size cannot be arbitrarily small. However,
  2207. the blocksize argument is often confused with data size.
  2208. See e.g. Mantis #20929. }
  2209. if Ablocksize<mindynamicblocksize then
  2210. Ablocksize:=mindynamicblocksize;
  2211. FMaxBlockSize:=Ablocksize;
  2212. grow;
  2213. end;
  2214. destructor tdynamicarray.destroy;
  2215. var
  2216. hp : pdynamicblock;
  2217. begin
  2218. while assigned(FFirstblock) do
  2219. begin
  2220. hp:=FFirstblock;
  2221. FFirstblock:=FFirstblock^.Next;
  2222. Freemem(hp);
  2223. end;
  2224. end;
  2225. function tdynamicarray.size:longword;
  2226. begin
  2227. if assigned(FLastblock) then
  2228. size:=FLastblock^.pos+FLastblock^.used
  2229. else
  2230. size:=0;
  2231. end;
  2232. procedure tdynamicarray.reset;
  2233. var
  2234. hp : pdynamicblock;
  2235. begin
  2236. while assigned(FFirstblock) do
  2237. begin
  2238. hp:=FFirstblock;
  2239. FFirstblock:=FFirstblock^.Next;
  2240. Freemem(hp);
  2241. end;
  2242. FPosn:=0;
  2243. FPosnblock:=nil;
  2244. FFirstblock:=nil;
  2245. FLastblock:=nil;
  2246. grow;
  2247. end;
  2248. procedure tdynamicarray.grow;
  2249. var
  2250. nblock : pdynamicblock;
  2251. OptBlockSize,
  2252. IncSize : integer;
  2253. begin
  2254. if CurrBlockSize<FMaxBlocksize then
  2255. begin
  2256. IncSize := mindynamicblocksize;
  2257. if FCurrBlockSize > 255 then
  2258. Inc(IncSize, FCurrBlockSize shr 2);
  2259. inc(FCurrBlockSize,IncSize);
  2260. end;
  2261. if CurrBlockSize>FMaxBlocksize then
  2262. FCurrBlockSize:=FMaxBlocksize;
  2263. { Calculate the most optimal size so there is no alignment overhead
  2264. lost in the heap manager }
  2265. OptBlockSize:=cutils.Align(CurrBlockSize+dynamicblockbasesize,16)-dynamicblockbasesize-sizeof(ptrint);
  2266. Getmem(nblock,OptBlockSize+dynamicblockbasesize);
  2267. if not assigned(FFirstblock) then
  2268. begin
  2269. FFirstblock:=nblock;
  2270. FPosnblock:=nblock;
  2271. nblock^.pos:=0;
  2272. end
  2273. else
  2274. begin
  2275. FLastblock^.Next:=nblock;
  2276. nblock^.pos:=FLastblock^.pos+FLastblock^.size;
  2277. end;
  2278. nblock^.used:=0;
  2279. nblock^.size:=OptBlockSize;
  2280. nblock^.Next:=nil;
  2281. fillchar(nblock^.data,nblock^.size,0);
  2282. FLastblock:=nblock;
  2283. end;
  2284. procedure tdynamicarray.align(i:longword);
  2285. var
  2286. j : longword;
  2287. begin
  2288. j:=(FPosn mod i);
  2289. if j<>0 then
  2290. begin
  2291. j:=i-j;
  2292. if FPosnblock^.used+j>FPosnblock^.size then
  2293. begin
  2294. dec(j,FPosnblock^.size-FPosnblock^.used);
  2295. FPosnblock^.used:=FPosnblock^.size;
  2296. grow;
  2297. FPosnblock:=FLastblock;
  2298. end;
  2299. inc(FPosnblock^.used,j);
  2300. inc(FPosn,j);
  2301. end;
  2302. end;
  2303. procedure tdynamicarray.seek(i:longword);
  2304. begin
  2305. if (i<FPosnblock^.pos) or (i>=FPosnblock^.pos+FPosnblock^.size) then
  2306. begin
  2307. { set FPosnblock correct if the size is bigger then
  2308. the current block }
  2309. if FPosnblock^.pos>i then
  2310. FPosnblock:=FFirstblock;
  2311. while assigned(FPosnblock) do
  2312. begin
  2313. if FPosnblock^.pos+FPosnblock^.size>i then
  2314. break;
  2315. FPosnblock:=FPosnblock^.Next;
  2316. end;
  2317. { not found ? then increase blocks }
  2318. if not assigned(FPosnblock) then
  2319. begin
  2320. repeat
  2321. { the current FLastblock is now also fully used }
  2322. FLastblock^.used:=FLastblock^.size;
  2323. grow;
  2324. FPosnblock:=FLastblock;
  2325. until FPosnblock^.pos+FPosnblock^.size>=i;
  2326. end;
  2327. end;
  2328. FPosn:=i;
  2329. if FPosn-FPosnblock^.pos>FPosnblock^.used then
  2330. FPosnblock^.used:=FPosn-FPosnblock^.pos;
  2331. end;
  2332. procedure tdynamicarray.write(const d;len:longword);
  2333. var
  2334. p : pchar;
  2335. i,j : longword;
  2336. begin
  2337. p:=pchar(@d);
  2338. while (len>0) do
  2339. begin
  2340. i:=FPosn-FPosnblock^.pos;
  2341. if i+len>=FPosnblock^.size then
  2342. begin
  2343. j:=FPosnblock^.size-i;
  2344. move(p^,FPosnblock^.data[i],j);
  2345. inc(p,j);
  2346. inc(FPosn,j);
  2347. dec(len,j);
  2348. FPosnblock^.used:=FPosnblock^.size;
  2349. if assigned(FPosnblock^.Next) then
  2350. FPosnblock:=FPosnblock^.Next
  2351. else
  2352. begin
  2353. grow;
  2354. FPosnblock:=FLastblock;
  2355. end;
  2356. end
  2357. else
  2358. begin
  2359. move(p^,FPosnblock^.data[i],len);
  2360. inc(p,len);
  2361. inc(FPosn,len);
  2362. i:=FPosn-FPosnblock^.pos;
  2363. if i>FPosnblock^.used then
  2364. FPosnblock^.used:=i;
  2365. len:=0;
  2366. end;
  2367. end;
  2368. end;
  2369. procedure tdynamicarray.writestr(const s:string);
  2370. begin
  2371. write(s[1],length(s));
  2372. end;
  2373. function tdynamicarray.read(var d;len:longword):longword;
  2374. var
  2375. p : pchar;
  2376. i,j,res : longword;
  2377. begin
  2378. res:=0;
  2379. p:=pchar(@d);
  2380. while (len>0) do
  2381. begin
  2382. i:=FPosn-FPosnblock^.pos;
  2383. if i+len>=FPosnblock^.used then
  2384. begin
  2385. j:=FPosnblock^.used-i;
  2386. move(FPosnblock^.data[i],p^,j);
  2387. inc(p,j);
  2388. inc(FPosn,j);
  2389. inc(res,j);
  2390. dec(len,j);
  2391. if assigned(FPosnblock^.Next) then
  2392. FPosnblock:=FPosnblock^.Next
  2393. else
  2394. break;
  2395. end
  2396. else
  2397. begin
  2398. move(FPosnblock^.data[i],p^,len);
  2399. inc(p,len);
  2400. inc(FPosn,len);
  2401. inc(res,len);
  2402. len:=0;
  2403. end;
  2404. end;
  2405. read:=res;
  2406. end;
  2407. procedure tdynamicarray.readstream(f:TCStream;maxlen:longword);
  2408. var
  2409. i,left : longword;
  2410. begin
  2411. repeat
  2412. left:=FPosnblock^.size-FPosnblock^.used;
  2413. if left>maxlen then
  2414. left:=maxlen;
  2415. i:=f.Read(FPosnblock^.data[FPosnblock^.used],left);
  2416. dec(maxlen,i);
  2417. inc(FPosnblock^.used,i);
  2418. if FPosnblock^.used=FPosnblock^.size then
  2419. begin
  2420. if assigned(FPosnblock^.Next) then
  2421. FPosnblock:=FPosnblock^.Next
  2422. else
  2423. begin
  2424. grow;
  2425. FPosnblock:=FLastblock;
  2426. end;
  2427. end;
  2428. until (i<left) or (maxlen=0);
  2429. end;
  2430. procedure tdynamicarray.writestream(f:TCStream);
  2431. var
  2432. hp : pdynamicblock;
  2433. begin
  2434. hp:=FFirstblock;
  2435. while assigned(hp) do
  2436. begin
  2437. f.Write(hp^.data,hp^.used);
  2438. hp:=hp^.Next;
  2439. end;
  2440. end;
  2441. function tdynamicarray.equal(other:tdynamicarray):boolean;
  2442. begin
  2443. result:=false;
  2444. { TODO }
  2445. end;
  2446. {****************************************************************************
  2447. thashset
  2448. ****************************************************************************}
  2449. constructor THashSet.Create(InitSize: Integer; OwnKeys, OwnObjects: Boolean);
  2450. var
  2451. I: Integer;
  2452. begin
  2453. inherited Create;
  2454. FOwnsObjects := OwnObjects;
  2455. FOwnsKeys := OwnKeys;
  2456. I := 64;
  2457. while I < InitSize do I := I shl 1;
  2458. FBucketCount := I;
  2459. FBucket := AllocMem(I * sizeof(PHashSetItem));
  2460. end;
  2461. destructor THashSet.Destroy;
  2462. begin
  2463. Clear;
  2464. FreeMem(FBucket);
  2465. inherited Destroy;
  2466. end;
  2467. procedure THashSet.Clear;
  2468. var
  2469. I: Integer;
  2470. item, next: PHashSetItem;
  2471. begin
  2472. for I := 0 to FBucketCount-1 do
  2473. begin
  2474. item := FBucket[I];
  2475. while Assigned(item) do
  2476. begin
  2477. next := item^.Next;
  2478. if FOwnsObjects then
  2479. item^.Data.Free;
  2480. if FOwnsKeys then
  2481. FreeMem(item^.Key);
  2482. FreeItem(item);
  2483. item := next;
  2484. end;
  2485. end;
  2486. FillChar(FBucket^, FBucketCount * sizeof(PHashSetItem), 0);
  2487. end;
  2488. function THashSet.Find(Key: Pointer; KeyLen: Integer): PHashSetItem;
  2489. var
  2490. Dummy: Boolean;
  2491. begin
  2492. Result := Lookup(Key, KeyLen, Dummy, False);
  2493. end;
  2494. function THashSet.FindOrAdd(Key: Pointer; KeyLen: Integer;
  2495. var Found: Boolean): PHashSetItem;
  2496. begin
  2497. Result := Lookup(Key, KeyLen, Found, True);
  2498. end;
  2499. function THashSet.FindOrAdd(Key: Pointer; KeyLen: Integer): PHashSetItem;
  2500. var
  2501. Dummy: Boolean;
  2502. begin
  2503. Result := Lookup(Key, KeyLen, Dummy, True);
  2504. end;
  2505. function THashSet.Get(Key: Pointer; KeyLen: Integer): TObject;
  2506. var
  2507. e: PHashSetItem;
  2508. Dummy: Boolean;
  2509. begin
  2510. e := Lookup(Key, KeyLen, Dummy, False);
  2511. if Assigned(e) then
  2512. Result := e^.Data
  2513. else
  2514. Result := nil;
  2515. end;
  2516. function THashSet.Lookup(Key: Pointer; KeyLen: Integer;
  2517. var Found: Boolean; CanCreate: Boolean): PHashSetItem;
  2518. var
  2519. Entry: PPHashSetItem;
  2520. h: LongWord;
  2521. begin
  2522. h := FPHash(Key, KeyLen);
  2523. Entry := @FBucket[h and (FBucketCount-1)];
  2524. while Assigned(Entry^) and
  2525. not ((Entry^^.HashValue = h) and (Entry^^.KeyLength = KeyLen) and
  2526. (CompareByte(Entry^^.Key^, Key^, KeyLen) = 0)) do
  2527. Entry := @Entry^^.Next;
  2528. Found := Assigned(Entry^);
  2529. if Found or (not CanCreate) then
  2530. begin
  2531. Result := Entry^;
  2532. Exit;
  2533. end;
  2534. if FCount > FBucketCount then { arbitrary limit, probably too high }
  2535. begin
  2536. { rehash and repeat search }
  2537. Resize(FBucketCount * 2);
  2538. Result := Lookup(Key, KeyLen, Found, CanCreate);
  2539. end
  2540. else
  2541. begin
  2542. GetMem(Result,SizeOfItem);
  2543. if FOwnsKeys then
  2544. begin
  2545. GetMem(Result^.Key, KeyLen);
  2546. Move(Key^, Result^.Key^, KeyLen);
  2547. end
  2548. else
  2549. Result^.Key := Key;
  2550. Result^.KeyLength := KeyLen;
  2551. Result^.HashValue := h;
  2552. Result^.Data := nil;
  2553. Result^.Next := nil;
  2554. Inc(FCount);
  2555. Entry^ := Result;
  2556. end;
  2557. end;
  2558. procedure THashSet.Resize(NewCapacity: LongWord);
  2559. var
  2560. p, chain: PPHashSetItem;
  2561. i: Integer;
  2562. e, n: PHashSetItem;
  2563. begin
  2564. p := AllocMem(NewCapacity * SizeOf(PHashSetItem));
  2565. for i := 0 to FBucketCount-1 do
  2566. begin
  2567. e := FBucket[i];
  2568. while Assigned(e) do
  2569. begin
  2570. chain := @p[e^.HashValue and (NewCapacity-1)];
  2571. n := e^.Next;
  2572. e^.Next := chain^;
  2573. chain^ := e;
  2574. e := n;
  2575. end;
  2576. end;
  2577. FBucketCount := NewCapacity;
  2578. FreeMem(FBucket);
  2579. FBucket := p;
  2580. end;
  2581. class procedure THashSet.FreeItem(item: PHashSetItem);
  2582. begin
  2583. Dispose(item);
  2584. end;
  2585. class function THashSet.SizeOfItem: Integer;
  2586. begin
  2587. Result := SizeOf(THashSetItem);
  2588. end;
  2589. function THashSet.Remove(Entry: PHashSetItem): Boolean;
  2590. var
  2591. chain: PPHashSetItem;
  2592. begin
  2593. chain := @FBucket[Entry^.HashValue mod FBucketCount];
  2594. while Assigned(chain^) do
  2595. begin
  2596. if chain^ = Entry then
  2597. begin
  2598. chain^ := Entry^.Next;
  2599. if FOwnsObjects then
  2600. Entry^.Data.Free;
  2601. if FOwnsKeys then
  2602. FreeMem(Entry^.Key);
  2603. FreeItem(Entry);
  2604. Dec(FCount);
  2605. Result := True;
  2606. Exit;
  2607. end;
  2608. chain := @chain^^.Next;
  2609. end;
  2610. Result := False;
  2611. end;
  2612. {****************************************************************************
  2613. ttaghashset
  2614. ****************************************************************************}
  2615. function TTagHashSet.Lookup(Key: Pointer; KeyLen: Integer;
  2616. Tag: LongWord; var Found: Boolean; CanCreate: Boolean): PTagHashSetItem;
  2617. var
  2618. Entry: PPTagHashSetItem;
  2619. h: LongWord;
  2620. begin
  2621. h := FPHash(Key, KeyLen, Tag);
  2622. Entry := @PPTagHashSetItem(FBucket)[h and (FBucketCount-1)];
  2623. while Assigned(Entry^) and
  2624. not ((Entry^^.HashValue = h) and (Entry^^.KeyLength = KeyLen) and
  2625. (Entry^^.Tag = Tag) and (CompareByte(Entry^^.Key^, Key^, KeyLen) = 0)) do
  2626. Entry := @Entry^^.Next;
  2627. Found := Assigned(Entry^);
  2628. if Found or (not CanCreate) then
  2629. begin
  2630. Result := Entry^;
  2631. Exit;
  2632. end;
  2633. if FCount > FBucketCount then { arbitrary limit, probably too high }
  2634. begin
  2635. { rehash and repeat search }
  2636. Resize(FBucketCount * 2);
  2637. Result := Lookup(Key, KeyLen, Tag, Found, CanCreate);
  2638. end
  2639. else
  2640. begin
  2641. Getmem(Result,SizeOfItem);
  2642. if FOwnsKeys then
  2643. begin
  2644. GetMem(Result^.Key, KeyLen);
  2645. Move(Key^, Result^.Key^, KeyLen);
  2646. end
  2647. else
  2648. Result^.Key := Key;
  2649. Result^.KeyLength := KeyLen;
  2650. Result^.HashValue := h;
  2651. Result^.Tag := Tag;
  2652. Result^.Data := nil;
  2653. Result^.Next := nil;
  2654. Inc(FCount);
  2655. Entry^ := Result;
  2656. end;
  2657. end;
  2658. class procedure TTagHashSet.FreeItem(item: PHashSetItem);
  2659. begin
  2660. Dispose(PTagHashSetItem(item));
  2661. end;
  2662. class function TTagHashSet.SizeOfItem: Integer;
  2663. begin
  2664. Result := SizeOf(TTagHashSetItem);
  2665. end;
  2666. function TTagHashSet.Find(Key: Pointer; KeyLen: Integer; Tag: LongWord): PTagHashSetItem;
  2667. var
  2668. Dummy: Boolean;
  2669. begin
  2670. Result := Lookup(Key, KeyLen, Tag, Dummy, False);
  2671. end;
  2672. function TTagHashSet.FindOrAdd(Key: Pointer; KeyLen: Integer; Tag: LongWord;
  2673. var Found: Boolean): PTagHashSetItem;
  2674. begin
  2675. Result := Lookup(Key, KeyLen, Tag, Found, True);
  2676. end;
  2677. function TTagHashSet.FindOrAdd(Key: Pointer; KeyLen: Integer; Tag: LongWord): PTagHashSetItem;
  2678. var
  2679. Dummy: Boolean;
  2680. begin
  2681. Result := Lookup(Key, KeyLen, Tag, Dummy, True);
  2682. end;
  2683. function TTagHashSet.Get(Key: Pointer; KeyLen: Integer; Tag: LongWord): TObject;
  2684. var
  2685. e: PTagHashSetItem;
  2686. Dummy: Boolean;
  2687. begin
  2688. e := Lookup(Key, KeyLen, Tag, Dummy, False);
  2689. if Assigned(e) then
  2690. Result := e^.Data
  2691. else
  2692. Result := nil;
  2693. end;
  2694. {****************************************************************************
  2695. tbitset
  2696. ****************************************************************************}
  2697. constructor tbitset.create(initsize: longint);
  2698. begin
  2699. create_bytesize((initsize+7) div 8);
  2700. end;
  2701. constructor tbitset.create_bytesize(bytesize: longint);
  2702. begin
  2703. fdatasize:=bytesize;
  2704. getmem(fdata,fdataSize);
  2705. clear;
  2706. end;
  2707. destructor tbitset.destroy;
  2708. begin
  2709. freemem(fdata,fdatasize);
  2710. inherited destroy;
  2711. end;
  2712. procedure tbitset.clear;
  2713. begin
  2714. fillchar(fdata^,fdatasize,0);
  2715. end;
  2716. procedure tbitset.grow(nsize: longint);
  2717. begin
  2718. reallocmem(fdata,nsize);
  2719. fillchar(fdata[fdatasize],nsize-fdatasize,0);
  2720. fdatasize:=nsize;
  2721. end;
  2722. procedure tbitset.include(index: longint);
  2723. var
  2724. dataindex: longint;
  2725. begin
  2726. { don't use bitpacked array, not endian-safe }
  2727. dataindex:=index shr 3;
  2728. if (dataindex>=datasize) then
  2729. grow(dataindex+16);
  2730. fdata[dataindex]:=fdata[dataindex] or (1 shl (index and 7));
  2731. end;
  2732. procedure tbitset.exclude(index: longint);
  2733. var
  2734. dataindex: longint;
  2735. begin
  2736. dataindex:=index shr 3;
  2737. if (dataindex>=datasize) then
  2738. exit;
  2739. fdata[dataindex]:=fdata[dataindex] and not(1 shl (index and 7));
  2740. end;
  2741. function tbitset.isset(index: longint): boolean;
  2742. var
  2743. dataindex: longint;
  2744. begin
  2745. dataindex:=index shr 3;
  2746. result:=
  2747. (dataindex<datasize) and
  2748. (((fdata[dataindex] shr (index and 7)) and 1)<>0);
  2749. end;
  2750. procedure tbitset.addset(aset: tbitset);
  2751. var
  2752. i: longint;
  2753. begin
  2754. if (aset.datasize>datasize) then
  2755. grow(aset.datasize);
  2756. for i:=0 to aset.datasize-1 do
  2757. fdata[i]:=fdata[i] or aset.data[i];
  2758. end;
  2759. procedure tbitset.subset(aset: tbitset);
  2760. var
  2761. i: longint;
  2762. begin
  2763. for i:=0 to min(datasize,aset.datasize)-1 do
  2764. fdata[i]:=fdata[i] and not(aset.data[i]);
  2765. end;
  2766. end.