cclasses.pas 83 KB

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