cclasses.pas 79 KB

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