cclasses.pas 82 KB

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