cclasses.pas 80 KB

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