12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582 |
- {
- Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
- This module provides some basic classes
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- ****************************************************************************
- }
- unit cclasses;
- {$i fpcdefs.inc}
- interface
- uses
- {$IFNDEF USE_FAKE_SYSUTILS}
- SysUtils,
- {$ELSE}
- fksysutl,
- {$ENDIF}
- CUtils,CStreams;
- {********************************************
- TMemDebug
- ********************************************}
- type
- tmemdebug = class
- private
- totalmem,
- startmem : integer;
- infostr : string[40];
- public
- constructor Create(const s:string);
- destructor Destroy;override;
- procedure show;
- procedure start;
- procedure stop;
- end;
- {*******************************************************
- TFPList (From rtl/objpas/classes/classesh.inc)
- ********************************************************}
- const
- SListIndexError = 'List index exceeds bounds (%d)';
- SListCapacityError = 'The maximum list capacity is reached (%d)';
- SListCountError = 'List count too large (%d)';
- type
- EListError = class(Exception);
- const
- MaxListSize = Maxint div 16;
- type
- PPointerList = ^TPointerList;
- TPointerList = array[0..MaxListSize - 1] of Pointer;
- TListSortCompare = function (Item1, Item2: Pointer): Integer;
- TListCallback = procedure(data,arg:pointer) of object;
- TListStaticCallback = procedure(data,arg:pointer);
- TFPList = class(TObject)
- private
- FList: PPointerList;
- FCount: Integer;
- FCapacity: Integer;
- protected
- function Get(Index: Integer): Pointer; inline;
- procedure Put(Index: Integer; Item: Pointer); inline;
- procedure SetCapacity(NewCapacity: Integer); inline;
- procedure SetCount(NewCount: Integer);
- Procedure RaiseIndexError(Index : Integer);
- public
- destructor Destroy; override;
- function Add(Item: Pointer): Integer;
- procedure Clear;
- procedure Delete(Index: Integer);
- class procedure Error(const Msg: string; Data: PtrInt);
- procedure Exchange(Index1, Index2: Integer);
- function Expand: TFPList;
- function Extract(item: Pointer): Pointer;
- function First: Pointer; inline;
- function IndexOf(Item: Pointer): Integer;
- procedure Insert(Index: Integer; Item: Pointer);
- function Last: Pointer; inline;
- procedure Move(CurIndex, NewIndex: Integer);
- procedure Assign(Obj:TFPList);
- function Remove(Item: Pointer): Integer;
- procedure Pack;
- procedure Sort(Compare: TListSortCompare);
- procedure ForEachCall(proc2call:TListCallback;arg:pointer);
- procedure ForEachCall(proc2call:TListStaticCallback;arg:pointer);
- property Capacity: Integer read FCapacity write SetCapacity;
- property Count: Integer read FCount write SetCount;
- property Items[Index: Integer]: Pointer read Get write Put; default;
- property List: PPointerList read FList;
- end;
- {*******************************************************
- TFPObjectList (From fcl/inc/contnrs.pp)
- ********************************************************}
- TObjectListCallback = procedure(data:TObject;arg:pointer) of object;
- TObjectListStaticCallback = procedure(data:TObject;arg:pointer);
- TFPObjectList = class(TObject)
- private
- FFreeObjects : Boolean;
- FList: TFPList;
- function GetCount: integer; inline;
- procedure SetCount(const AValue: integer); inline;
- protected
- function GetItem(Index: Integer): TObject; inline;
- procedure SetItem(Index: Integer; AObject: TObject); inline;
- procedure SetCapacity(NewCapacity: Integer); inline;
- function GetCapacity: integer; inline;
- public
- constructor Create;
- constructor Create(FreeObjects : Boolean);
- destructor Destroy; override;
- procedure Clear;
- function Add(AObject: TObject): Integer; inline;
- procedure Delete(Index: Integer); inline;
- procedure Exchange(Index1, Index2: Integer); inline;
- function Expand: TFPObjectList;inline;
- function Extract(Item: TObject): TObject; inline;
- function Remove(AObject: TObject): Integer;
- function IndexOf(AObject: TObject): Integer; inline;
- function FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt: Integer): Integer;
- procedure Insert(Index: Integer; AObject: TObject); inline;
- function First: TObject; inline;
- function Last: TObject; inline;
- procedure Move(CurIndex, NewIndex: Integer); inline;
- procedure Assign(Obj:TFPObjectList); inline;
- procedure Pack; inline;
- procedure Sort(Compare: TListSortCompare); inline;
- procedure ForEachCall(proc2call:TObjectListCallback;arg:pointer); inline;
- procedure ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer); inline;
- property Capacity: Integer read GetCapacity write SetCapacity;
- property Count: Integer read GetCount write SetCount;
- property OwnsObjects: Boolean read FFreeObjects write FFreeObjects;
- property Items[Index: Integer]: TObject read GetItem write SetItem; default;
- property List: TFPList read FList;
- end;
- type
- THashItem=record
- HashValue : LongWord;
- StrIndex : Integer;
- NextIndex : Integer;
- Data : Pointer;
- end;
- PHashItem=^THashItem;
- const
- MaxHashListSize = Maxint div 16;
- MaxHashStrSize = Maxint;
- MaxHashTableSize = Maxint div 4;
- MaxItemsPerHash = 3;
- type
- PHashItemList = ^THashItemList;
- THashItemList = array[0..MaxHashListSize - 1] of THashItem;
- PHashTable = ^THashTable;
- THashTable = array[0..MaxHashTableSize - 1] of Integer;
- TFPHashList = class(TObject)
- private
- { ItemList }
- FHashList : PHashItemList;
- FCount,
- FCapacity : Integer;
- { Hash }
- FHashTable : PHashTable;
- FHashCapacity : Integer;
- { Strings }
- FStrs : PChar;
- FStrCount,
- FStrCapacity : Integer;
- function InternalFind(AHash:LongWord;const AName:string;out PrevIndex:Integer):Integer;
- protected
- function Get(Index: Integer): Pointer; inline;
- procedure SetCapacity(NewCapacity: Integer);
- procedure SetCount(NewCount: Integer);
- Procedure RaiseIndexError(Index : Integer);
- function AddStr(const s:string): Integer;
- procedure AddToHashTable(Index: Integer);
- procedure StrExpand(MinIncSize:Integer);
- procedure SetStrCapacity(NewCapacity: Integer);
- procedure SetHashCapacity(NewCapacity: Integer);
- procedure ReHash;
- public
- constructor Create;
- destructor Destroy; override;
- function Add(const AName:string;Item: Pointer): Integer;
- procedure Clear;
- function NameOfIndex(Index: Integer): String; inline;
- function HashOfIndex(Index: Integer): LongWord; inline;
- procedure Delete(Index: Integer);
- class procedure Error(const Msg: string; Data: PtrInt);
- function Expand: TFPHashList;
- function Extract(item: Pointer): Pointer;
- function IndexOf(Item: Pointer): Integer;
- function Find(const AName:string): Pointer;
- function FindWithHash(const AName:string;AHash:LongWord): Pointer;
- function Rename(const AOldName,ANewName:string): Integer;
- function Remove(Item: Pointer): Integer;
- procedure Pack;
- procedure ShowStatistics;
- procedure ForEachCall(proc2call:TListCallback;arg:pointer);
- procedure ForEachCall(proc2call:TListStaticCallback;arg:pointer);
- property Capacity: Integer read FCapacity write SetCapacity;
- property Count: Integer read FCount write SetCount;
- property Items[Index: Integer]: Pointer read Get; default;
- property List: PHashItemList read FHashList;
- property Strs: PChar read FStrs;
- end;
- {*******************************************************
- TFPHashObjectList (From fcl/inc/contnrs.pp)
- ********************************************************}
- TFPHashObjectList = class;
- { TFPHashObject }
- TFPHashObject = class
- private
- FOwner : TFPHashObjectList;
- FCachedStr : pshortstring;
- FStrIndex : Integer;
- procedure InternalChangeOwner(HashObjectList:TFPHashObjectList;const s:string);
- protected
- function GetName:string;virtual;
- function GetHash:Longword;virtual;
- public
- constructor CreateNotOwned;
- constructor Create(HashObjectList:TFPHashObjectList;const s:string);
- procedure ChangeOwner(HashObjectList:TFPHashObjectList); inline;
- procedure ChangeOwnerAndName(HashObjectList:TFPHashObjectList;const s:string); inline;
- procedure Rename(const ANewName:string);
- property Name:string read GetName;
- property Hash:Longword read GetHash;
- end;
- TFPHashObjectList = class(TObject)
- private
- FFreeObjects : Boolean;
- FHashList: TFPHashList;
- function GetCount: integer; inline;
- procedure SetCount(const AValue: integer); inline;
- protected
- function GetItem(Index: Integer): TObject; inline;
- procedure SetCapacity(NewCapacity: Integer); inline;
- function GetCapacity: integer; inline;
- public
- constructor Create(FreeObjects : boolean = True);
- destructor Destroy; override;
- procedure Clear;
- function Add(const AName:string;AObject: TObject): Integer; inline;
- function NameOfIndex(Index: Integer): String; inline;
- function HashOfIndex(Index: Integer): LongWord; inline;
- procedure Delete(Index: Integer);
- function Expand: TFPHashObjectList; inline;
- function Extract(Item: TObject): TObject; inline;
- function Remove(AObject: TObject): Integer;
- function IndexOf(AObject: TObject): Integer; inline;
- function Find(const s:string): TObject; inline;
- function FindWithHash(const AName:string;AHash:LongWord): Pointer;
- function Rename(const AOldName,ANewName:string): Integer; inline;
- function FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt: Integer): Integer;
- procedure Pack; inline;
- procedure ShowStatistics; inline;
- procedure ForEachCall(proc2call:TObjectListCallback;arg:pointer); inline;
- procedure ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer); inline;
- property Capacity: Integer read GetCapacity write SetCapacity;
- property Count: Integer read GetCount write SetCount;
- property OwnsObjects: Boolean read FFreeObjects write FFreeObjects;
- property Items[Index: Integer]: TObject read GetItem; default;
- property List: TFPHashList read FHashList;
- end;
- {********************************************
- TLinkedList
- ********************************************}
- type
- TLinkedListItem = class
- public
- Previous,
- Next : TLinkedListItem;
- Constructor Create;
- Destructor Destroy;override;
- Function GetCopy:TLinkedListItem;virtual;
- end;
- TLinkedListItemClass = class of TLinkedListItem;
- TLinkedList = class
- private
- FCount : integer;
- FFirst,
- FLast : TLinkedListItem;
- FNoClear : boolean;
- public
- constructor Create;
- destructor Destroy;override;
- { true when the List is empty }
- function Empty:boolean; inline;
- { deletes all Items }
- procedure Clear;
- { inserts an Item }
- procedure Insert(Item:TLinkedListItem);
- { inserts an Item before Loc }
- procedure InsertBefore(Item,Loc : TLinkedListItem);
- { inserts an Item after Loc }
- procedure InsertAfter(Item,Loc : TLinkedListItem);virtual;
- { concats an Item }
- procedure Concat(Item:TLinkedListItem);
- { deletes an Item }
- procedure Remove(Item:TLinkedListItem);
- { Gets First Item }
- function GetFirst:TLinkedListItem;
- { Gets last Item }
- function GetLast:TLinkedListItem;
- { inserts another List at the begin and make this List empty }
- procedure insertList(p : TLinkedList);
- { inserts another List before the provided item and make this List empty }
- procedure insertListBefore(Item:TLinkedListItem;p : TLinkedList);
- { inserts another List after the provided item and make this List empty }
- procedure insertListAfter(Item:TLinkedListItem;p : TLinkedList);
- { concats another List at the end and make this List empty }
- procedure concatList(p : TLinkedList);
- { concats another List at the start and makes a copy
- the list is ordered in reverse.
- }
- procedure insertListcopy(p : TLinkedList);
- { concats another List at the end and makes a copy }
- procedure concatListcopy(p : TLinkedList);
- property First:TLinkedListItem read FFirst;
- property Last:TLinkedListItem read FLast;
- property Count:Integer read FCount;
- property NoClear:boolean write FNoClear;
- end;
- {********************************************
- TStringList
- ********************************************}
- { string containerItem }
- TStringListItem = class(TLinkedListItem)
- FPStr : pshortstring;
- public
- constructor Create(const s:string);
- destructor Destroy;override;
- function GetCopy:TLinkedListItem;override;
- function Str:string; inline;
- end;
- { string container }
- TStringList = class(TLinkedList)
- private
- FDoubles : boolean; { if this is set to true, doubles are allowed }
- public
- constructor Create;
- constructor Create_No_Double;
- { inserts an Item }
- procedure Insert(const s:string);
- { concats an Item }
- procedure Concat(const s:string);
- { deletes an Item }
- procedure Remove(const s:string);
- { Gets First Item }
- function GetFirst:string;
- { Gets last Item }
- function GetLast:string;
- { true if string is in the container, compare case sensitive }
- function FindCase(const s:string):TStringListItem;
- { true if string is in the container }
- function Find(const s:string):TStringListItem;
- { inserts an item }
- procedure InsertItem(item:TStringListItem); inline;
- { concats an item }
- procedure ConcatItem(item:TStringListItem); inline;
- property Doubles:boolean read FDoubles write FDoubles;
- procedure readstream(f:TCStream);
- procedure writestream(f:TCStream);
- end;
- {********************************************
- DynamicArray
- ********************************************}
- type
- { can't use sizeof(integer) because it crashes gdb }
- tdynamicblockdata=array[0..1024*1024-1] of byte;
-
- pdynamicblock = ^tdynamicblock;
- tdynamicblock = record
- pos,
- size,
- used : integer;
- Next : pdynamicblock;
- data : tdynamicblockdata;
- end;
- const
- dynamicblockbasesize = sizeof(tdynamicblock)-sizeof(tdynamicblockdata);
- type
- tdynamicarray = class
- private
- FPosn : integer;
- FPosnblock : pdynamicblock;
- FCurrBlocksize,
- FMaxBlocksize : integer;
- FFirstblock,
- FLastblock : pdynamicblock;
- procedure grow;
- public
- constructor Create(Ablocksize:integer);
- destructor Destroy;override;
- procedure reset;
- function size:integer;
- procedure align(i:integer);
- procedure seek(i:integer);
- function read(var d;len:integer):integer;
- procedure write(const d;len:integer);
- procedure writestr(const s:string); inline;
- procedure readstream(f:TCStream;maxlen:longint);
- procedure writestream(f:TCStream);
- property CurrBlockSize : integer read FCurrBlocksize;
- property FirstBlock : PDynamicBlock read FFirstBlock;
- property Pos : integer read FPosn;
- end;
- function FPHash(const s:string):LongWord;
- implementation
- {*****************************************************************************
- Memory debug
- *****************************************************************************}
- constructor tmemdebug.create(const s:string);
- begin
- infostr:=s;
- totalmem:=0;
- Start;
- end;
- procedure tmemdebug.start;
- var
- status : TFPCHeapStatus;
- begin
- status:=GetFPCHeapStatus;
- startmem:=status.CurrHeapUsed;
- end;
- procedure tmemdebug.stop;
- var
- status : TFPCHeapStatus;
- begin
- if startmem<>0 then
- begin
- status:=GetFPCHeapStatus;
- inc(TotalMem,startmem-status.CurrHeapUsed);
- startmem:=0;
- end;
- end;
- destructor tmemdebug.destroy;
- begin
- Stop;
- show;
- end;
- procedure tmemdebug.show;
- begin
- write('memory [',infostr,'] ');
- if TotalMem>0 then
- writeln(DStr(TotalMem shr 10),' Kb released')
- else
- writeln(DStr((-TotalMem) shr 10),' Kb allocated');
- end;
- {*****************************************************************************
- TFPObjectList (Copied from rtl/objpas/classes/lists.inc)
- *****************************************************************************}
- Const
- // Ratio of Pointer and Word Size.
- WordRatio = SizeOf(Pointer) Div SizeOf(Word);
- procedure TFPList.RaiseIndexError(Index : Integer);
- begin
- Error(SListIndexError, Index);
- end;
- function TFPList.Get(Index: Integer): Pointer;
- begin
- If (Index < 0) or (Index >= FCount) then
- RaiseIndexError(Index);
- Result:=FList^[Index];
- end;
- procedure TFPList.Put(Index: Integer; Item: Pointer);
- begin
- if (Index < 0) or (Index >= FCount) then
- RaiseIndexError(Index);
- Flist^[Index] := Item;
- end;
- function TFPList.Extract(item: Pointer): Pointer;
- var
- i : Integer;
- begin
- result := nil;
- i := IndexOf(item);
- if i >= 0 then
- begin
- Result := item;
- FList^[i] := nil;
- Delete(i);
- end;
- end;
- procedure TFPList.SetCapacity(NewCapacity: Integer);
- begin
- If (NewCapacity < FCount) or (NewCapacity > MaxListSize) then
- Error (SListCapacityError, NewCapacity);
- if NewCapacity = FCapacity then
- exit;
- ReallocMem(FList, SizeOf(Pointer)*NewCapacity);
- FCapacity := NewCapacity;
- end;
- procedure TFPList.SetCount(NewCount: Integer);
- begin
- if (NewCount < 0) or (NewCount > MaxListSize)then
- Error(SListCountError, NewCount);
- If NewCount > FCount then
- begin
- If NewCount > FCapacity then
- SetCapacity(NewCount);
- If FCount < NewCount then
- FillWord(Flist^[FCount], (NewCount-FCount) * WordRatio, 0);
- end;
- FCount := Newcount;
- end;
- destructor TFPList.Destroy;
- begin
- Self.Clear;
- inherited Destroy;
- end;
- function TFPList.Add(Item: Pointer): Integer;
- begin
- if FCount = FCapacity then
- Self.Expand;
- FList^[FCount] := Item;
- Result := FCount;
- FCount := FCount + 1;
- end;
- procedure TFPList.Clear;
- begin
- if Assigned(FList) then
- begin
- SetCount(0);
- SetCapacity(0);
- FList := nil;
- end;
- end;
- procedure TFPList.Delete(Index: Integer);
- begin
- If (Index<0) or (Index>=FCount) then
- Error (SListIndexError, Index);
- FCount := FCount-1;
- System.Move (FList^[Index+1], FList^[Index], (FCount - Index) * SizeOf(Pointer));
- // Shrink the list if appropriate
- if (FCapacity > 256) and (FCount < FCapacity shr 2) then
- begin
- FCapacity := FCapacity shr 1;
- ReallocMem(FList, SizeOf(Pointer) * FCapacity);
- end;
- end;
- class procedure TFPList.Error(const Msg: string; Data: PtrInt);
- begin
- Raise EListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame);
- end;
- procedure TFPList.Exchange(Index1, Index2: Integer);
- var
- Temp : Pointer;
- begin
- If ((Index1 >= FCount) or (Index1 < 0)) then
- Error(SListIndexError, Index1);
- If ((Index2 >= FCount) or (Index2 < 0)) then
- Error(SListIndexError, Index2);
- Temp := FList^[Index1];
- FList^[Index1] := FList^[Index2];
- FList^[Index2] := Temp;
- end;
- function TFPList.Expand: TFPList;
- var
- IncSize : Longint;
- begin
- Result := Self;
- if FCount < FCapacity then
- exit;
- IncSize := sizeof(ptrint)*2;
- if FCapacity > 127 then
- Inc(IncSize, FCapacity shr 2)
- else if FCapacity > sizeof(ptrint)*4 then
- Inc(IncSize, FCapacity shr 1)
- else if FCapacity >= sizeof(ptrint) then
- inc(IncSize,sizeof(ptrint));
- SetCapacity(FCapacity + IncSize);
- end;
- function TFPList.First: Pointer;
- begin
- If FCount = 0 then
- Result := Nil
- else
- Result := Items[0];
- end;
- function TFPList.IndexOf(Item: Pointer): Integer;
- var
- psrc : PPointer;
- Index : Integer;
- begin
- Result:=-1;
- psrc:=@FList^[0];
- For Index:=0 To FCount-1 Do
- begin
- if psrc^=Item then
- begin
- Result:=Index;
- exit;
- end;
- inc(psrc);
- end;
- end;
- procedure TFPList.Insert(Index: Integer; Item: Pointer);
- begin
- if (Index < 0) or (Index > FCount )then
- Error(SlistIndexError, Index);
- iF FCount = FCapacity then Self.Expand;
- if Index<FCount then
- System.Move(Flist^[Index], Flist^[Index+1], (FCount - Index) * SizeOf(Pointer));
- FList^[Index] := Item;
- FCount := FCount + 1;
- end;
- function TFPList.Last: Pointer;
- begin
- { Wouldn't it be better to return nil if the count is zero ?}
- If FCount = 0 then
- Result := nil
- else
- Result := Items[FCount - 1];
- end;
- procedure TFPList.Move(CurIndex, NewIndex: Integer);
- var
- Temp : Pointer;
- begin
- if ((CurIndex < 0) or (CurIndex > Count - 1)) then
- Error(SListIndexError, CurIndex);
- if (NewINdex < 0) then
- Error(SlistIndexError, NewIndex);
- Temp := FList^[CurIndex];
- FList^[CurIndex] := nil;
- Self.Delete(CurIndex);
- Self.Insert(NewIndex, nil);
- FList^[NewIndex] := Temp;
- end;
- function TFPList.Remove(Item: Pointer): Integer;
- begin
- Result := IndexOf(Item);
- If Result <> -1 then
- Self.Delete(Result);
- end;
- procedure TFPList.Pack;
- var
- NewCount,
- i : integer;
- pdest,
- psrc : PPointer;
- begin
- NewCount:=0;
- psrc:=@FList^[0];
- pdest:=psrc;
- For I:=0 To FCount-1 Do
- begin
- if assigned(psrc^) then
- begin
- pdest^:=psrc^;
- inc(pdest);
- inc(NewCount);
- end;
- inc(psrc);
- end;
- FCount:=NewCount;
- end;
- // Needed by Sort method.
- Procedure QuickSort(FList: PPointerList; L, R : Longint;
- Compare: TListSortCompare);
- var
- I, J : Longint;
- P, Q : Pointer;
- begin
- repeat
- I := L;
- J := R;
- P := FList^[ (L + R) div 2 ];
- repeat
- while Compare(P, FList^[i]) > 0 do
- I := I + 1;
- while Compare(P, FList^[J]) < 0 do
- J := J - 1;
- If I <= J then
- begin
- Q := FList^[I];
- Flist^[I] := FList^[J];
- FList^[J] := Q;
- I := I + 1;
- J := J - 1;
- end;
- until I > J;
- if L < J then
- QuickSort(FList, L, J, Compare);
- L := I;
- until I >= R;
- end;
- procedure TFPList.Sort(Compare: TListSortCompare);
- begin
- if Not Assigned(FList) or (FCount < 2) then exit;
- QuickSort(Flist, 0, FCount-1, Compare);
- end;
- procedure TFPList.Assign(Obj: TFPList);
- var
- i: Integer;
- begin
- Clear;
- for I := 0 to Obj.Count - 1 do
- Add(Obj[i]);
- end;
- procedure TFPList.ForEachCall(proc2call:TListCallback;arg:pointer);
- var
- i : integer;
- p : pointer;
- begin
- For I:=0 To Count-1 Do
- begin
- p:=FList^[i];
- if assigned(p) then
- proc2call(p,arg);
- end;
- end;
- procedure TFPList.ForEachCall(proc2call:TListStaticCallback;arg:pointer);
- var
- i : integer;
- p : pointer;
- begin
- For I:=0 To Count-1 Do
- begin
- p:=FList^[i];
- if assigned(p) then
- proc2call(p,arg);
- end;
- end;
- {*****************************************************************************
- TFPObjectList (Copied from rtl/objpas/classes/lists.inc)
- *****************************************************************************}
- constructor TFPObjectList.Create(FreeObjects : boolean);
- begin
- Create;
- FFreeObjects := Freeobjects;
- end;
- destructor TFPObjectList.Destroy;
- begin
- if (FList <> nil) then
- begin
- Clear;
- FList.Destroy;
- end;
- inherited Destroy;
- end;
- procedure TFPObjectList.Clear;
- var
- i: integer;
- begin
- if FFreeObjects then
- for i := 0 to FList.Count - 1 do
- TObject(FList[i]).Free;
- FList.Clear;
- end;
- constructor TFPObjectList.Create;
- begin
- inherited Create;
- FList := TFPList.Create;
- FFreeObjects := True;
- end;
- function TFPObjectList.GetCount: integer;
- begin
- Result := FList.Count;
- end;
- procedure TFPObjectList.SetCount(const AValue: integer);
- begin
- if FList.Count <> AValue then
- FList.Count := AValue;
- end;
- function TFPObjectList.GetItem(Index: Integer): TObject; inline;
- begin
- Result := TObject(FList[Index]);
- end;
- procedure TFPObjectList.SetItem(Index: Integer; AObject: TObject); inline;
- begin
- if OwnsObjects then
- TObject(FList[Index]).Free;
- FList[index] := AObject;
- end;
- procedure TFPObjectList.SetCapacity(NewCapacity: Integer);inline;
- begin
- FList.Capacity := NewCapacity;
- end;
- function TFPObjectList.GetCapacity: integer;
- begin
- Result := FList.Capacity;
- end;
- function TFPObjectList.Add(AObject: TObject): Integer; inline;
- begin
- Result := FList.Add(AObject);
- end;
- procedure TFPObjectList.Delete(Index: Integer); inline;
- begin
- if OwnsObjects then
- TObject(FList[Index]).Free;
- FList.Delete(Index);
- end;
- procedure TFPObjectList.Exchange(Index1, Index2: Integer);inline;
- begin
- FList.Exchange(Index1, Index2);
- end;
- function TFPObjectList.Expand: TFPObjectList;
- begin
- FList.Expand;
- Result := Self;
- end;
- function TFPObjectList.Extract(Item: TObject): TObject;
- begin
- Result := TObject(FList.Extract(Item));
- end;
- function TFPObjectList.Remove(AObject: TObject): Integer;
- begin
- Result := IndexOf(AObject);
- if (Result <> -1) then
- begin
- if OwnsObjects then
- TObject(FList[Result]).Free;
- FList.Delete(Result);
- end;
- end;
- function TFPObjectList.IndexOf(AObject: TObject): Integer;
- begin
- Result := FList.IndexOf(Pointer(AObject));
- end;
- function TFPObjectList.FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt : Integer): Integer;
- var
- I : Integer;
- begin
- I:=AStartAt;
- Result:=-1;
- If AExact then
- while (I<Count) and (Result=-1) do
- If Items[i].ClassType=AClass then
- Result:=I
- else
- Inc(I)
- else
- while (I<Count) and (Result=-1) do
- If Items[i].InheritsFrom(AClass) then
- Result:=I
- else
- Inc(I);
- end;
- procedure TFPObjectList.Insert(Index: Integer; AObject: TObject);
- begin
- FList.Insert(Index, Pointer(AObject));
- end;
- procedure TFPObjectList.Move(CurIndex, NewIndex: Integer);
- begin
- FList.Move(CurIndex, NewIndex);
- end;
- procedure TFPObjectList.Assign(Obj: TFPObjectList);
- var
- i: Integer;
- begin
- Clear;
- for I := 0 to Obj.Count - 1 do
- Add(Obj[i]);
- end;
- procedure TFPObjectList.Pack;
- begin
- FList.Pack;
- end;
- procedure TFPObjectList.Sort(Compare: TListSortCompare);
- begin
- FList.Sort(Compare);
- end;
- function TFPObjectList.First: TObject;
- begin
- Result := TObject(FList.First);
- end;
- function TFPObjectList.Last: TObject;
- begin
- Result := TObject(FList.Last);
- end;
- procedure TFPObjectList.ForEachCall(proc2call:TObjectListCallback;arg:pointer);
- begin
- FList.ForEachCall(TListCallBack(proc2call),arg);
- end;
- procedure TFPObjectList.ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer);
- begin
- FList.ForEachCall(TListStaticCallBack(proc2call),arg);
- end;
- {*****************************************************************************
- TFPHashList
- *****************************************************************************}
- function FPHash1(const s:string):LongWord;
- Var
- g : LongWord;
- p,pmax : pchar;
- begin
- result:=0;
- p:=@s[1];
- pmax:=@s[length(s)+1];
- while (p<pmax) do
- begin
- result:=result shl 4 + LongWord(p^);
- g:=result and LongWord($F0000000);
- if g<>0 then
- result:=result xor (g shr 24) xor g;
- inc(p);
- end;
- If result=0 then
- result:=$ffffffff;
- end;
- function FPHash(const s:string):LongWord;
- Var
- p,pmax : pchar;
- begin
- {$ifopt Q+}
- {$define overflowon}
- {$Q-}
- {$endif}
- result:=0;
- p:=@s[1];
- pmax:=@s[length(s)+1];
- while (p<pmax) do
- begin
- result:=LongWord((result shl 5) - result) xor LongWord(P^);
- inc(p);
- end;
- {$ifdef overflowon}
- {$Q+}
- {$undef overflowon}
- {$endif}
- end;
- procedure TFPHashList.RaiseIndexError(Index : Integer);
- begin
- Error(SListIndexError, Index);
- end;
- function TFPHashList.Get(Index: Integer): Pointer;
- begin
- If (Index < 0) or (Index >= FCount) then
- RaiseIndexError(Index);
- Result:=FHashList^[Index].Data;
- end;
- function TFPHashList.NameOfIndex(Index: Integer): String;
- begin
- If (Index < 0) or (Index >= FCount) then
- RaiseIndexError(Index);
- with FHashList^[Index] do
- begin
- if StrIndex>=0 then
- Result:=PShortString(@FStrs[StrIndex])^
- else
- Result:='';
- end;
- end;
- function TFPHashList.HashOfIndex(Index: Integer): LongWord;
- begin
- If (Index < 0) or (Index >= FCount) then
- RaiseIndexError(Index);
- Result:=FHashList^[Index].HashValue;
- end;
- function TFPHashList.Extract(item: Pointer): Pointer;
- var
- i : Integer;
- begin
- result := nil;
- i := IndexOf(item);
- if i >= 0 then
- begin
- Result := item;
- Delete(i);
- end;
- end;
- procedure TFPHashList.SetCapacity(NewCapacity: Integer);
- begin
- If (NewCapacity < FCount) or (NewCapacity > MaxHashListSize) then
- Error (SListCapacityError, NewCapacity);
- if NewCapacity = FCapacity then
- exit;
- ReallocMem(FHashList, NewCapacity*SizeOf(THashItem));
- FCapacity := NewCapacity;
- end;
- procedure TFPHashList.SetCount(NewCount: Integer);
- begin
- if (NewCount < 0) or (NewCount > MaxHashListSize)then
- Error(SListCountError, NewCount);
- If NewCount > FCount then
- begin
- If NewCount > FCapacity then
- SetCapacity(NewCount);
- If FCount < NewCount then
- FillChar(FHashList^[FCount], (NewCount-FCount) div Sizeof(THashItem), 0);
- end;
- FCount := Newcount;
- end;
- procedure TFPHashList.SetStrCapacity(NewCapacity: Integer);
- begin
- If (NewCapacity < FStrCount) or (NewCapacity > MaxHashStrSize) then
- Error (SListCapacityError, NewCapacity);
- if NewCapacity = FStrCapacity then
- exit;
- ReallocMem(FStrs, NewCapacity);
- FStrCapacity := NewCapacity;
- end;
- procedure TFPHashList.SetHashCapacity(NewCapacity: Integer);
- begin
- If (NewCapacity < 1) then
- Error (SListCapacityError, NewCapacity);
- if FHashCapacity=NewCapacity then
- exit;
- FHashCapacity:=NewCapacity;
- ReallocMem(FHashTable, FHashCapacity*sizeof(Integer));
- ReHash;
- end;
- procedure TFPHashList.ReHash;
- var
- i : Integer;
- begin
- FillDword(FHashTable^,FHashCapacity,LongWord(-1));
- For i:=0 To FCount-1 Do
- AddToHashTable(i);
- end;
- constructor TFPHashList.Create;
- begin
- SetHashCapacity(1);
- end;
- destructor TFPHashList.Destroy;
- begin
- Clear;
- if assigned(FHashTable) then
- FreeMem(FHashTable);
- inherited Destroy;
- end;
- function TFPHashList.AddStr(const s:string): Integer;
- var
- Len : Integer;
- begin
- len:=length(s)+1;
- if FStrCount+Len >= FStrCapacity then
- StrExpand(Len);
- System.Move(s[0],FStrs[FStrCount],Len);
- result:=FStrCount;
- inc(FStrCount,Len);
- end;
- procedure TFPHashList.AddToHashTable(Index: Integer);
- var
- HashIndex : Integer;
- begin
- with FHashList^[Index] do
- begin
- if not assigned(Data) then
- exit;
- HashIndex:=HashValue mod LongWord(FHashCapacity);
- NextIndex:=FHashTable^[HashIndex];
- FHashTable^[HashIndex]:=Index;
- end;
- end;
- function TFPHashList.Add(const AName:string;Item: Pointer): Integer;
- begin
- if FCount = FCapacity then
- Expand;
- with FHashList^[FCount] do
- begin
- HashValue:=FPHash(AName);
- Data:=Item;
- StrIndex:=AddStr(AName);
- end;
- AddToHashTable(FCount);
- Result := FCount;
- inc(FCount);
- end;
- procedure TFPHashList.Clear;
- begin
- if Assigned(FHashList) then
- begin
- FCount:=0;
- SetCapacity(0);
- FHashList := nil;
- end;
- SetHashCapacity(1);
- if Assigned(FStrs) then
- begin
- FStrCount:=0;
- SetStrCapacity(0);
- FStrs := nil;
- end;
- end;
- procedure TFPHashList.Delete(Index: Integer);
- begin
- If (Index<0) or (Index>=FCount) then
- Error (SListIndexError, Index);
- with FHashList^[Index] do
- begin
- Data:=nil;
- StrIndex:=-1;
- end;
- end;
- class procedure TFPHashList.Error(const Msg: string; Data: PtrInt);
- begin
- Raise EListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame);
- end;
- function TFPHashList.Expand: TFPHashList;
- var
- IncSize : Longint;
- begin
- Result := Self;
- if FCount < FCapacity then
- exit;
- IncSize := sizeof(ptrint)*2;
- if FCapacity > 127 then
- Inc(IncSize, FCapacity shr 2)
- else if FCapacity > sizeof(ptrint)*3 then
- Inc(IncSize, FCapacity shr 1)
- else if FCapacity >= sizeof(ptrint) then
- inc(IncSize,sizeof(ptrint));
- SetCapacity(FCapacity + IncSize);
- { Maybe expand hash also }
- if FCount>FHashCapacity*MaxItemsPerHash then
- SetHashCapacity(FCount div MaxItemsPerHash);
- end;
- procedure TFPHashList.StrExpand(MinIncSize:Integer);
- var
- IncSize : Longint;
- begin
- if FStrCount+MinIncSize < FStrCapacity then
- exit;
- IncSize := 64;
- if FStrCapacity > 255 then
- Inc(IncSize, FStrCapacity shr 2);
- SetStrCapacity(FStrCapacity + IncSize + MinIncSize);
- end;
- function TFPHashList.IndexOf(Item: Pointer): Integer;
- var
- psrc : PHashItem;
- Index : integer;
- begin
- Result:=-1;
- psrc:=@FHashList^[0];
- For Index:=0 To FCount-1 Do
- begin
- if psrc^.Data=Item then
- begin
- Result:=Index;
- exit;
- end;
- inc(psrc);
- end;
- end;
- function TFPHashList.Remove(Item: Pointer): Integer;
- begin
- Result := IndexOf(Item);
- If Result <> -1 then
- Self.Delete(Result);
- end;
- function TFPHashList.InternalFind(AHash:LongWord;const AName:string;out PrevIndex:Integer):Integer;
- var
- HashIndex : Integer;
- Len,
- LastChar : Char;
- begin
- HashIndex:=AHash mod LongWord(FHashCapacity);
- Result:=FHashTable^[HashIndex];
- Len:=Char(Length(AName));
- LastChar:=AName[Byte(Len)];
- PrevIndex:=-1;
- while Result<>-1 do
- begin
- with FHashList^[Result] do
- begin
- if assigned(Data) and
- (HashValue=AHash) and
- (Len=FStrs[StrIndex]) and
- (LastChar=FStrs[StrIndex+Byte(Len)]) and
- (AName=PShortString(@FStrs[StrIndex])^) then
- exit;
- PrevIndex:=Result;
- Result:=NextIndex;
- end;
- end;
- end;
- function TFPHashList.Find(const AName:string): Pointer;
- var
- Index,
- PrevIndex : Integer;
- begin
- Result:=nil;
- Index:=InternalFind(FPHash(AName),AName,PrevIndex);
- if Index=-1 then
- exit;
- Result:=FHashList^[Index].Data;
- end;
- function TFPHashList.FindWithHash(const AName:string;AHash:LongWord): Pointer;
- var
- Index,
- PrevIndex : Integer;
- begin
- Result:=nil;
- Index:=InternalFind(AHash,AName,PrevIndex);
- if Index=-1 then
- exit;
- Result:=FHashList^[Index].Data;
- end;
- function TFPHashList.Rename(const AOldName,ANewName:string): Integer;
- var
- PrevIndex,
- Index : Integer;
- OldHash : LongWord;
- begin
- Result:=-1;
- OldHash:=FPHash(AOldName);
- Index:=InternalFind(OldHash,AOldName,PrevIndex);
- if Index=-1 then
- exit;
- { Remove from current Hash }
- if PrevIndex<>-1 then
- FHashList^[PrevIndex].NextIndex:=FHashList^[Index].NextIndex
- else
- FHashTable^[OldHash mod LongWord(FHashCapacity)]:=FHashList^[Index].NextIndex;
- { Set new name and hash }
- with FHashList^[Index] do
- begin
- HashValue:=FPHash(ANewName);
- StrIndex:=AddStr(ANewName);
- end;
- { Insert back in Hash }
- AddToHashTable(Index);
- { Return Index }
- Result:=Index;
- end;
- procedure TFPHashList.Pack;
- var
- NewCount,
- i : integer;
- pdest,
- psrc : PHashItem;
- begin
- NewCount:=0;
- psrc:=@FHashList^[0];
- pdest:=psrc;
- For I:=0 To FCount-1 Do
- begin
- if assigned(psrc^.Data) then
- begin
- pdest^:=psrc^;
- inc(pdest);
- inc(NewCount);
- end;
- inc(psrc);
- end;
- FCount:=NewCount;
- { We need to ReHash to update the IndexNext }
- ReHash;
- { Release over-capacity }
- SetCapacity(FCount);
- SetStrCapacity(FStrCount);
- end;
- procedure TFPHashList.ShowStatistics;
- var
- HashMean,
- HashStdDev : Double;
- Index,
- i,j : Integer;
- begin
- { Calculate Mean and StdDev }
- HashMean:=0;
- HashStdDev:=0;
- for i:=0 to FHashCapacity-1 do
- begin
- j:=0;
- Index:=FHashTable^[i];
- while (Index<>-1) do
- begin
- inc(j);
- Index:=FHashList^[Index].NextIndex;
- end;
- HashMean:=HashMean+j;
- HashStdDev:=HashStdDev+Sqr(j);
- end;
- HashMean:=HashMean/FHashCapacity;
- HashStdDev:=(HashStdDev-FHashCapacity*Sqr(HashMean));
- If FHashCapacity>1 then
- HashStdDev:=Sqrt(HashStdDev/(FHashCapacity-1))
- else
- HashStdDev:=0;
- { Print info to stdout }
- Writeln('HashSize : ',FHashCapacity);
- Writeln('HashMean : ',HashMean:1:4);
- Writeln('HashStdDev : ',HashStdDev:1:4);
- Writeln('ListSize : ',FCount,'/',FCapacity);
- Writeln('StringSize : ',FStrCount,'/',FStrCapacity);
- end;
- procedure TFPHashList.ForEachCall(proc2call:TListCallback;arg:pointer);
- var
- i : integer;
- p : pointer;
- begin
- For I:=0 To Count-1 Do
- begin
- p:=FHashList^[i].Data;
- if assigned(p) then
- proc2call(p,arg);
- end;
- end;
- procedure TFPHashList.ForEachCall(proc2call:TListStaticCallback;arg:pointer);
- var
- i : integer;
- p : pointer;
- begin
- For I:=0 To Count-1 Do
- begin
- p:=FHashList^[i].Data;
- if assigned(p) then
- proc2call(p,arg);
- end;
- end;
- {*****************************************************************************
- TFPHashObject
- *****************************************************************************}
- procedure TFPHashObject.InternalChangeOwner(HashObjectList:TFPHashObjectList;const s:string);
- var
- Index : integer;
- begin
- FOwner:=HashObjectList;
- Index:=HashObjectList.Add(s,Self);
- FStrIndex:=HashObjectList.List.List^[Index].StrIndex;
- FCachedStr:=PShortString(@FOwner.List.Strs[FStrIndex]);
- end;
- constructor TFPHashObject.CreateNotOwned;
- begin
- FStrIndex:=-1;
- end;
- constructor TFPHashObject.Create(HashObjectList:TFPHashObjectList;const s:string);
- begin
- InternalChangeOwner(HashObjectList,s);
- end;
- procedure TFPHashObject.ChangeOwner(HashObjectList:TFPHashObjectList);
- begin
- InternalChangeOwner(HashObjectList,PShortString(@FOwner.List.Strs[FStrIndex])^);
- end;
- procedure TFPHashObject.ChangeOwnerAndName(HashObjectList:TFPHashObjectList;const s:string);
- begin
- InternalChangeOwner(HashObjectList,s);
- end;
- procedure TFPHashObject.Rename(const ANewName:string);
- var
- Index : integer;
- begin
- Index:=FOwner.Rename(PShortString(@FOwner.List.Strs[FStrIndex])^,ANewName);
- if Index<>-1 then
- begin
- FStrIndex:=FOwner.List.List^[Index].StrIndex;
- FCachedStr:=PShortString(@FOwner.List.Strs[FStrIndex]);
- end;
- end;
- function TFPHashObject.GetName:string;
- begin
- if FOwner<>nil then
- begin
- FCachedStr:=PShortString(@FOwner.List.Strs[FStrIndex]);
- Result:=FCachedStr^;
- end
- else
- Result:='';
- end;
- function TFPHashObject.GetHash:Longword;
- begin
- if FOwner<>nil then
- Result:=FPHash(PShortString(@FOwner.List.Strs[FStrIndex])^)
- else
- Result:=$ffffffff;
- end;
- {*****************************************************************************
- TFPHashObjectList (Copied from rtl/objpas/classes/lists.inc)
- *****************************************************************************}
- constructor TFPHashObjectList.Create(FreeObjects : boolean = True);
- begin
- inherited Create;
- FHashList := TFPHashList.Create;
- FFreeObjects := Freeobjects;
- end;
- destructor TFPHashObjectList.Destroy;
- begin
- if (FHashList <> nil) then
- begin
- Clear;
- FHashList.Destroy;
- end;
- inherited Destroy;
- end;
- procedure TFPHashObjectList.Clear;
- var
- i: integer;
- begin
- if FFreeObjects then
- for i := 0 to FHashList.Count - 1 do
- TObject(FHashList[i]).Free;
- FHashList.Clear;
- end;
- function TFPHashObjectList.GetCount: integer;
- begin
- Result := FHashList.Count;
- end;
- procedure TFPHashObjectList.SetCount(const AValue: integer);
- begin
- if FHashList.Count <> AValue then
- FHashList.Count := AValue;
- end;
- function TFPHashObjectList.GetItem(Index: Integer): TObject;
- begin
- Result := TObject(FHashList[Index]);
- end;
- procedure TFPHashObjectList.SetCapacity(NewCapacity: Integer);
- begin
- FHashList.Capacity := NewCapacity;
- end;
- function TFPHashObjectList.GetCapacity: integer;
- begin
- Result := FHashList.Capacity;
- end;
- function TFPHashObjectList.Add(const AName:string;AObject: TObject): Integer;
- begin
- Result := FHashList.Add(AName,AObject);
- end;
- function TFPHashObjectList.NameOfIndex(Index: Integer): String;
- begin
- Result := FHashList.NameOfIndex(Index);
- end;
- function TFPHashObjectList.HashOfIndex(Index: Integer): LongWord;
- begin
- Result := FHashList.HashOfIndex(Index);
- end;
- procedure TFPHashObjectList.Delete(Index: Integer);
- begin
- if OwnsObjects then
- TObject(FHashList[Index]).Free;
- FHashList.Delete(Index);
- end;
- function TFPHashObjectList.Expand: TFPHashObjectList;
- begin
- FHashList.Expand;
- Result := Self;
- end;
- function TFPHashObjectList.Extract(Item: TObject): TObject;
- begin
- Result := TObject(FHashList.Extract(Item));
- end;
- function TFPHashObjectList.Remove(AObject: TObject): Integer;
- begin
- Result := IndexOf(AObject);
- if (Result <> -1) then
- begin
- if OwnsObjects then
- TObject(FHashList[Result]).Free;
- FHashList.Delete(Result);
- end;
- end;
- function TFPHashObjectList.IndexOf(AObject: TObject): Integer;
- begin
- Result := FHashList.IndexOf(Pointer(AObject));
- end;
- function TFPHashObjectList.Find(const s:string): TObject;
- begin
- result:=TObject(FHashList.Find(s));
- end;
- function TFPHashObjectList.FindWithHash(const AName:string;AHash:LongWord): Pointer;
- begin
- Result:=TObject(FHashList.FindWithHash(AName,AHash));
- end;
- function TFPHashObjectList.Rename(const AOldName,ANewName:string): Integer;
- begin
- Result:=FHashList.Rename(AOldName,ANewName);
- end;
- function TFPHashObjectList.FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt : Integer): Integer;
- var
- I : Integer;
- begin
- I:=AStartAt;
- Result:=-1;
- If AExact then
- while (I<Count) and (Result=-1) do
- If Items[i].ClassType=AClass then
- Result:=I
- else
- Inc(I)
- else
- while (I<Count) and (Result=-1) do
- If Items[i].InheritsFrom(AClass) then
- Result:=I
- else
- Inc(I);
- end;
- procedure TFPHashObjectList.Pack;
- begin
- FHashList.Pack;
- end;
- procedure TFPHashObjectList.ShowStatistics;
- begin
- FHashList.ShowStatistics;
- end;
- procedure TFPHashObjectList.ForEachCall(proc2call:TObjectListCallback;arg:pointer);
- begin
- FHashList.ForEachCall(TListCallBack(proc2call),arg);
- end;
- procedure TFPHashObjectList.ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer);
- begin
- FHashList.ForEachCall(TListStaticCallBack(proc2call),arg);
- end;
- {****************************************************************************
- TLinkedListItem
- ****************************************************************************}
- constructor TLinkedListItem.Create;
- begin
- Previous:=nil;
- Next:=nil;
- end;
- destructor TLinkedListItem.Destroy;
- begin
- end;
- function TLinkedListItem.GetCopy:TLinkedListItem;
- var
- p : TLinkedListItem;
- l : integer;
- begin
- p:=TLinkedListItemClass(ClassType).Create;
- l:=InstanceSize;
- Move(pointer(self)^,pointer(p)^,l);
- Result:=p;
- end;
- {****************************************************************************
- TLinkedList
- ****************************************************************************}
- constructor TLinkedList.Create;
- begin
- FFirst:=nil;
- Flast:=nil;
- FCount:=0;
- FNoClear:=False;
- end;
- destructor TLinkedList.destroy;
- begin
- if not FNoClear then
- Clear;
- end;
- function TLinkedList.empty:boolean;
- begin
- Empty:=(FFirst=nil);
- end;
- procedure TLinkedList.Insert(Item:TLinkedListItem);
- begin
- if FFirst=nil then
- begin
- FLast:=Item;
- Item.Previous:=nil;
- Item.Next:=nil;
- end
- else
- begin
- FFirst.Previous:=Item;
- Item.Previous:=nil;
- Item.Next:=FFirst;
- end;
- FFirst:=Item;
- inc(FCount);
- end;
- procedure TLinkedList.InsertBefore(Item,Loc : TLinkedListItem);
- begin
- Item.Previous:=Loc.Previous;
- Item.Next:=Loc;
- Loc.Previous:=Item;
- if assigned(Item.Previous) then
- Item.Previous.Next:=Item
- else
- { if we've no next item, we've to adjust FFist }
- FFirst:=Item;
- inc(FCount);
- end;
- procedure TLinkedList.InsertAfter(Item,Loc : TLinkedListItem);
- begin
- Item.Next:=Loc.Next;
- Loc.Next:=Item;
- Item.Previous:=Loc;
- if assigned(Item.Next) then
- Item.Next.Previous:=Item
- else
- { if we've no next item, we've to adjust FLast }
- FLast:=Item;
- inc(FCount);
- end;
- procedure TLinkedList.Concat(Item:TLinkedListItem);
- begin
- if FFirst=nil then
- begin
- FFirst:=Item;
- Item.Previous:=nil;
- Item.Next:=nil;
- end
- else
- begin
- Flast.Next:=Item;
- Item.Previous:=Flast;
- Item.Next:=nil;
- end;
- Flast:=Item;
- inc(FCount);
- end;
- procedure TLinkedList.remove(Item:TLinkedListItem);
- begin
- if Item=nil then
- exit;
- if (FFirst=Item) and (Flast=Item) then
- begin
- FFirst:=nil;
- Flast:=nil;
- end
- else if FFirst=Item then
- begin
- FFirst:=Item.Next;
- if assigned(FFirst) then
- FFirst.Previous:=nil;
- end
- else if Flast=Item then
- begin
- Flast:=Flast.Previous;
- if assigned(Flast) then
- Flast.Next:=nil;
- end
- else
- begin
- Item.Previous.Next:=Item.Next;
- Item.Next.Previous:=Item.Previous;
- end;
- Item.Next:=nil;
- Item.Previous:=nil;
- dec(FCount);
- end;
- procedure TLinkedList.clear;
- var
- NewNode : TLinkedListItem;
- begin
- NewNode:=FFirst;
- while assigned(NewNode) do
- begin
- FFirst:=NewNode.Next;
- NewNode.Free;
- NewNode:=FFirst;
- end;
- FLast:=nil;
- FFirst:=nil;
- FCount:=0;
- end;
- function TLinkedList.GetFirst:TLinkedListItem;
- begin
- if FFirst=nil then
- GetFirst:=nil
- else
- begin
- GetFirst:=FFirst;
- if FFirst=FLast then
- FLast:=nil;
- FFirst:=FFirst.Next;
- dec(FCount);
- end;
- end;
- function TLinkedList.GetLast:TLinkedListItem;
- begin
- if FLast=nil then
- Getlast:=nil
- else
- begin
- Getlast:=FLast;
- if FLast=FFirst then
- FFirst:=nil;
- FLast:=FLast.Previous;
- dec(FCount);
- end;
- end;
- procedure TLinkedList.insertList(p : TLinkedList);
- begin
- { empty List ? }
- if (p.FFirst=nil) then
- exit;
- p.Flast.Next:=FFirst;
- { we have a double Linked List }
- if assigned(FFirst) then
- FFirst.Previous:=p.Flast;
- FFirst:=p.FFirst;
- if (FLast=nil) then
- Flast:=p.Flast;
- inc(FCount,p.FCount);
- { p becomes empty }
- p.FFirst:=nil;
- p.Flast:=nil;
- p.FCount:=0;
- end;
- procedure TLinkedList.insertListBefore(Item:TLinkedListItem;p : TLinkedList);
- begin
- { empty List ? }
- if (p.FFirst=nil) then
- exit;
- if (Item=nil) then
- begin
- { Insert at begin }
- InsertList(p);
- exit;
- end
- else
- begin
- p.FLast.Next:=Item;
- p.FFirst.Previous:=Item.Previous;
- if assigned(Item.Previous) then
- Item.Previous.Next:=p.FFirst
- else
- FFirst:=p.FFirst;
- Item.Previous:=p.FLast;
- inc(FCount,p.FCount);
- end;
- { p becomes empty }
- p.FFirst:=nil;
- p.Flast:=nil;
- p.FCount:=0;
- end;
- procedure TLinkedList.insertListAfter(Item:TLinkedListItem;p : TLinkedList);
- begin
- { empty List ? }
- if (p.FFirst=nil) then
- exit;
- if (Item=nil) then
- begin
- { Insert at begin }
- InsertList(p);
- exit;
- end
- else
- begin
- p.FFirst.Previous:=Item;
- p.FLast.Next:=Item.Next;
- if assigned(Item.Next) then
- Item.Next.Previous:=p.FLast
- else
- FLast:=p.FLast;
- Item.Next:=p.FFirst;
- inc(FCount,p.FCount);
- end;
- { p becomes empty }
- p.FFirst:=nil;
- p.Flast:=nil;
- p.FCount:=0;
- end;
- procedure TLinkedList.concatList(p : TLinkedList);
- begin
- if (p.FFirst=nil) then
- exit;
- if FFirst=nil then
- FFirst:=p.FFirst
- else
- begin
- FLast.Next:=p.FFirst;
- p.FFirst.Previous:=Flast;
- end;
- Flast:=p.Flast;
- inc(FCount,p.FCount);
- { make p empty }
- p.Flast:=nil;
- p.FFirst:=nil;
- p.FCount:=0;
- end;
- procedure TLinkedList.insertListcopy(p : TLinkedList);
- var
- NewNode,NewNode2 : TLinkedListItem;
- begin
- NewNode:=p.First;
- while assigned(NewNode) do
- begin
- NewNode2:=NewNode.Getcopy;
- if assigned(NewNode2) then
- Insert(NewNode2);
- NewNode:=NewNode.Next;
- end;
- end;
- procedure TLinkedList.concatListcopy(p : TLinkedList);
- var
- NewNode,NewNode2 : TLinkedListItem;
- begin
- NewNode:=p.First;
- while assigned(NewNode) do
- begin
- NewNode2:=NewNode.Getcopy;
- if assigned(NewNode2) then
- Concat(NewNode2);
- NewNode:=NewNode.Next;
- end;
- end;
- {****************************************************************************
- TStringListItem
- ****************************************************************************}
- constructor TStringListItem.Create(const s:string);
- begin
- inherited Create;
- FPStr:=stringdup(s);
- end;
- destructor TStringListItem.Destroy;
- begin
- stringdispose(FPStr);
- end;
- function TStringListItem.Str:string;
- begin
- Str:=FPStr^;
- end;
- function TStringListItem.GetCopy:TLinkedListItem;
- begin
- Result:=(inherited GetCopy);
- TStringListItem(Result).FPStr:=stringdup(FPstr^);
- end;
- {****************************************************************************
- TSTRINGList
- ****************************************************************************}
- constructor tstringList.Create;
- begin
- inherited Create;
- FDoubles:=true;
- end;
- constructor tstringList.Create_no_double;
- begin
- inherited Create;
- FDoubles:=false;
- end;
- procedure tstringList.insert(const s : string);
- begin
- if (s='') or
- ((not FDoubles) and (find(s)<>nil)) then
- exit;
- inherited insert(tstringListItem.create(s));
- end;
- procedure tstringList.concat(const s : string);
- begin
- if (s='') or
- ((not FDoubles) and (find(s)<>nil)) then
- exit;
- inherited concat(tstringListItem.create(s));
- end;
- procedure tstringList.remove(const s : string);
- var
- p : tstringListItem;
- begin
- if s='' then
- exit;
- p:=find(s);
- if assigned(p) then
- begin
- inherited Remove(p);
- p.Free;
- end;
- end;
- function tstringList.GetFirst : string;
- var
- p : tstringListItem;
- begin
- p:=tstringListItem(inherited GetFirst);
- if p=nil then
- GetFirst:=''
- else
- begin
- GetFirst:=p.FPStr^;
- p.free;
- end;
- end;
- function tstringList.Getlast : string;
- var
- p : tstringListItem;
- begin
- p:=tstringListItem(inherited Getlast);
- if p=nil then
- Getlast:=''
- else
- begin
- Getlast:=p.FPStr^;
- p.free;
- end;
- end;
- function tstringList.FindCase(const s:string):TstringListItem;
- var
- NewNode : tstringListItem;
- begin
- result:=nil;
- if s='' then
- exit;
- NewNode:=tstringListItem(FFirst);
- while assigned(NewNode) do
- begin
- if NewNode.FPStr^=s then
- begin
- result:=NewNode;
- exit;
- end;
- NewNode:=tstringListItem(NewNode.Next);
- end;
- end;
- function tstringList.Find(const s:string):TstringListItem;
- var
- NewNode : tstringListItem;
- ups : string;
- begin
- result:=nil;
- if s='' then
- exit;
- ups:=upper(s);
- NewNode:=tstringListItem(FFirst);
- while assigned(NewNode) do
- begin
- if upper(NewNode.FPStr^)=ups then
- begin
- result:=NewNode;
- exit;
- end;
- NewNode:=tstringListItem(NewNode.Next);
- end;
- end;
- procedure TStringList.InsertItem(item:TStringListItem);
- begin
- inherited Insert(item);
- end;
- procedure TStringList.ConcatItem(item:TStringListItem);
- begin
- inherited Concat(item);
- end;
- procedure TStringList.readstream(f:TCStream);
- const
- BufSize = 16384;
- var
- Hsp,
- p,maxp,
- Buf : PChar;
- Prev : Char;
- HsPos,
- ReadLen,
- BufPos,
- BufEnd : Longint;
- hs : string;
- procedure ReadBuf;
- begin
- if BufPos<BufEnd then
- begin
- Move(Buf[BufPos],Buf[0],BufEnd-BufPos);
- Dec(BufEnd,BufPos);
- BufPos:=0;
- end;
- ReadLen:=f.Read(buf[BufEnd],BufSize-BufEnd);
- inc(BufEnd,ReadLen);
- end;
- begin
- Getmem(Buf,Bufsize);
- BufPos:=0;
- BufEnd:=0;
- HsPos:=1;
- ReadBuf;
- repeat
- hsp:=@hs[hsPos];
- p:=@Buf[BufPos];
- maxp:=@Buf[BufEnd];
- while (p<maxp) and not(P^ in [#10,#13]) do
- begin
- hsp^:=p^;
- inc(p);
- if hsp-@hs[1]<255 then
- inc(hsp);
- end;
- inc(BufPos,maxp-p);
- inc(HsPos,maxp-p);
- prev:=p^;
- inc(BufPos);
- { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
- { #13#10 = Dos), so if we've got #10, we can safely exit }
- if (prev<>#10) then
- begin
- if (BufPos>=BufEnd) then
- begin
- ReadBuf;
- if BufPos>=BufEnd then
- break;
- end;
- { is there also a #10 after it? }
- if prev=#13 then
- begin
- if (Buf[BufPos]=#10) then
- inc(BufPos);
- prev:=#10;
- end;
- end;
- if prev=#10 then
- begin
- hs[0]:=char(hsp-@hs[1]);
- Concat(hs);
- HsPos:=1;
- end;
- until BufPos>=BufEnd;
- hs[0]:=char(hsp-@hs[1]);
- Concat(hs);
- freemem(buf);
- end;
- procedure TStringList.writestream(f:TCStream);
- var
- Node : TStringListItem;
- LineEnd : string[2];
- begin
- Case DefaultTextLineBreakStyle Of
- tlbsLF: LineEnd := #10;
- tlbsCRLF: LineEnd := #13#10;
- tlbsCR: LineEnd := #13;
- End;
- Node:=tstringListItem(FFirst);
- while assigned(Node) do
- begin
- f.Write(Node.FPStr^[1],Length(Node.FPStr^));
- f.Write(LineEnd[1],length(LineEnd));
- Node:=tstringListItem(Node.Next);
- end;
- end;
- {****************************************************************************
- tdynamicarray
- ****************************************************************************}
- constructor tdynamicarray.create(Ablocksize:integer);
- begin
- FPosn:=0;
- FPosnblock:=nil;
- FFirstblock:=nil;
- FLastblock:=nil;
- FCurrBlockSize:=0;
- FMaxBlockSize:=Ablocksize;
- grow;
- end;
- destructor tdynamicarray.destroy;
- var
- hp : pdynamicblock;
- begin
- while assigned(FFirstblock) do
- begin
- hp:=FFirstblock;
- FFirstblock:=FFirstblock^.Next;
- Freemem(hp);
- end;
- end;
- function tdynamicarray.size:integer;
- begin
- if assigned(FLastblock) then
- size:=FLastblock^.pos+FLastblock^.used
- else
- size:=0;
- end;
- procedure tdynamicarray.reset;
- var
- hp : pdynamicblock;
- begin
- while assigned(FFirstblock) do
- begin
- hp:=FFirstblock;
- FFirstblock:=FFirstblock^.Next;
- Freemem(hp);
- end;
- FPosn:=0;
- FPosnblock:=nil;
- FFirstblock:=nil;
- FLastblock:=nil;
- grow;
- end;
- procedure tdynamicarray.grow;
- var
- nblock : pdynamicblock;
- OptBlockSize,
- IncSize : integer;
- begin
- if CurrBlockSize<FMaxBlocksize then
- begin
- IncSize := sizeof(ptrint)*8;
- if FCurrBlockSize > 255 then
- Inc(IncSize, FCurrBlockSize shr 2);
- inc(FCurrBlockSize,IncSize);
- end;
- if CurrBlockSize>FMaxBlocksize then
- FCurrBlockSize:=FMaxBlocksize;
- { Calculate the most optimal size so there is no alignment overhead
- lost in the heap manager }
- OptBlockSize:=cutils.Align(CurrBlockSize+dynamicblockbasesize,16)-dynamicblockbasesize-sizeof(ptrint);
- Getmem(nblock,OptBlockSize+dynamicblockbasesize);
- if not assigned(FFirstblock) then
- begin
- FFirstblock:=nblock;
- FPosnblock:=nblock;
- nblock^.pos:=0;
- end
- else
- begin
- FLastblock^.Next:=nblock;
- nblock^.pos:=FLastblock^.pos+FLastblock^.size;
- end;
- nblock^.used:=0;
- nblock^.size:=OptBlockSize;
- nblock^.Next:=nil;
- fillchar(nblock^.data,nblock^.size,0);
- FLastblock:=nblock;
- end;
- procedure tdynamicarray.align(i:integer);
- var
- j : integer;
- begin
- j:=(FPosn mod i);
- if j<>0 then
- begin
- j:=i-j;
- if FPosnblock^.used+j>FPosnblock^.size then
- begin
- dec(j,FPosnblock^.size-FPosnblock^.used);
- FPosnblock^.used:=FPosnblock^.size;
- grow;
- FPosnblock:=FLastblock;
- end;
- inc(FPosnblock^.used,j);
- inc(FPosn,j);
- end;
- end;
- procedure tdynamicarray.seek(i:integer);
- begin
- if (i<FPosnblock^.pos) or (i>=FPosnblock^.pos+FPosnblock^.size) then
- begin
- { set FPosnblock correct if the size is bigger then
- the current block }
- if FPosnblock^.pos>i then
- FPosnblock:=FFirstblock;
- while assigned(FPosnblock) do
- begin
- if FPosnblock^.pos+FPosnblock^.size>i then
- break;
- FPosnblock:=FPosnblock^.Next;
- end;
- { not found ? then increase blocks }
- if not assigned(FPosnblock) then
- begin
- repeat
- { the current FLastblock is now also fully used }
- FLastblock^.used:=FLastblock^.size;
- grow;
- FPosnblock:=FLastblock;
- until FPosnblock^.pos+FPosnblock^.size>=i;
- end;
- end;
- FPosn:=i;
- if FPosn-FPosnblock^.pos>FPosnblock^.used then
- FPosnblock^.used:=FPosn-FPosnblock^.pos;
- end;
- procedure tdynamicarray.write(const d;len:integer);
- var
- p : pchar;
- i,j : integer;
- begin
- p:=pchar(@d);
- while (len>0) do
- begin
- i:=FPosn-FPosnblock^.pos;
- if i+len>=FPosnblock^.size then
- begin
- j:=FPosnblock^.size-i;
- move(p^,FPosnblock^.data[i],j);
- inc(p,j);
- inc(FPosn,j);
- dec(len,j);
- FPosnblock^.used:=FPosnblock^.size;
- if assigned(FPosnblock^.Next) then
- FPosnblock:=FPosnblock^.Next
- else
- begin
- grow;
- FPosnblock:=FLastblock;
- end;
- end
- else
- begin
- move(p^,FPosnblock^.data[i],len);
- inc(p,len);
- inc(FPosn,len);
- i:=FPosn-FPosnblock^.pos;
- if i>FPosnblock^.used then
- FPosnblock^.used:=i;
- len:=0;
- end;
- end;
- end;
- procedure tdynamicarray.writestr(const s:string);
- begin
- write(s[1],length(s));
- end;
- function tdynamicarray.read(var d;len:integer):integer;
- var
- p : pchar;
- i,j,res : integer;
- begin
- res:=0;
- p:=pchar(@d);
- while (len>0) do
- begin
- i:=FPosn-FPosnblock^.pos;
- if i+len>=FPosnblock^.used then
- begin
- j:=FPosnblock^.used-i;
- move(FPosnblock^.data[i],p^,j);
- inc(p,j);
- inc(FPosn,j);
- inc(res,j);
- dec(len,j);
- if assigned(FPosnblock^.Next) then
- FPosnblock:=FPosnblock^.Next
- else
- break;
- end
- else
- begin
- move(FPosnblock^.data[i],p^,len);
- inc(p,len);
- inc(FPosn,len);
- inc(res,len);
- len:=0;
- end;
- end;
- read:=res;
- end;
- procedure tdynamicarray.readstream(f:TCStream;maxlen:longint);
- var
- i,left : integer;
- begin
- if maxlen=-1 then
- maxlen:=maxlongint;
- repeat
- left:=FPosnblock^.size-FPosnblock^.used;
- if left>maxlen then
- left:=maxlen;
- i:=f.Read(FPosnblock^.data[FPosnblock^.used],left);
- dec(maxlen,i);
- inc(FPosnblock^.used,i);
- if FPosnblock^.used=FPosnblock^.size then
- begin
- if assigned(FPosnblock^.Next) then
- FPosnblock:=FPosnblock^.Next
- else
- begin
- grow;
- FPosnblock:=FLastblock;
- end;
- end;
- until (i<left) or (maxlen=0);
- end;
- procedure tdynamicarray.writestream(f:TCStream);
- var
- hp : pdynamicblock;
- begin
- hp:=FFirstblock;
- while assigned(hp) do
- begin
- f.Write(hp^.data,hp^.used);
- hp:=hp^.Next;
- end;
- end;
- end.
|