123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770 |
- {
- $Id$
- 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
- cutils,cstreams;
- {********************************************
- TMemDebug
- ********************************************}
- type
- tmemdebug = class
- private
- startmem : integer;
- infostr : string[40];
- public
- constructor Create(const s:string);
- destructor Destroy;override;
- procedure show;
- 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;
- { deletes all Items }
- procedure Clear;
- { inserts an Item }
- procedure Insert(Item:TLinkedListItem);
- { 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);
- { concats another List at the end and make this List empty }
- procedure concatList(p : TLinkedList);
- { concats another List at the end and makes a copy }
- 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 : PString;
- public
- constructor Create(const s:string);
- destructor Destroy;override;
- function GetCopy:TLinkedListItem;override;
- function Str:string;
- 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 }
- function Find(const s:string):TStringListItem;
- { inserts an item }
- procedure InsertItem(item:TStringListItem);
- { concats an item }
- procedure ConcatItem(item:TStringListItem);
- property Doubles:boolean read FDoubles write FDoubles;
- end;
- {********************************************
- Dictionary
- ********************************************}
- const
- { the real size will be [0..hasharray-1] ! }
- hasharraysize = 512;
- type
- { namedindexobect for use with dictionary and indexarray }
- TNamedIndexItem=class
- private
- { indexarray }
- FIndexNr : integer;
- FIndexNext : TNamedIndexItem;
- { dictionary }
- FLeft,
- FRight : TNamedIndexItem;
- FSpeedValue : cardinal;
- { singleList }
- FListNext : TNamedIndexItem;
- protected
- function GetName:string;virtual;
- procedure SetName(const n:string);virtual;
- public
- FName : Pstring;
- constructor Create;
- constructor CreateName(const n:string);
- destructor Destroy;override;
- property IndexNr:integer read FIndexNr write FIndexNr;
- property IndexNext:TNamedIndexItem read FIndexNext write FIndexNext;
- property Name:string read GetName write SetName;
- property SpeedValue:cardinal read FSpeedValue;
- property ListNext:TNamedIndexItem read FListNext;
- property Left:TNamedIndexItem read FLeft write FLeft;
- property Right:TNamedIndexItem read FRight write FRight;
- end;
- Pdictionaryhasharray=^Tdictionaryhasharray;
- Tdictionaryhasharray=array[0..hasharraysize-1] of TNamedIndexItem;
- TnamedIndexCallback = procedure(p:TNamedIndexItem;arg:pointer) of object;
- TnamedIndexStaticCallback = procedure(p:TNamedIndexItem;arg:pointer);
- Tdictionary=class
- private
- FRoot : TNamedIndexItem;
- FHashArray : Pdictionaryhasharray;
- procedure cleartree(obj:TNamedIndexItem);
- function insertNode(NewNode:TNamedIndexItem;var currNode:TNamedIndexItem):TNamedIndexItem;
- procedure inserttree(currtree,currroot:TNamedIndexItem);
- public
- noclear : boolean;
- replace_existing : boolean;
- delete_doubles : boolean;
- constructor Create;
- destructor Destroy;override;
- procedure usehash;
- procedure clear;
- function delete(const s:string):TNamedIndexItem;
- function empty:boolean;
- procedure foreach(proc2call:TNamedIndexcallback;arg:pointer);
- procedure foreach_static(proc2call:TNamedIndexStaticCallback;arg:pointer);
- function insert(obj:TNamedIndexItem):TNamedIndexItem;
- function rename(const olds,News : string):TNamedIndexItem;
- function search(const s:string):TNamedIndexItem;
- function speedsearch(const s:string;SpeedValue:cardinal):TNamedIndexItem;
- property Items[const s:string]:TNamedIndexItem read Search;default;
- end;
- tsingleList=class
- First,
- last : TNamedIndexItem;
- constructor Create;
- procedure reset;
- procedure clear;
- procedure insert(p:TNamedIndexItem);
- end;
- tindexobjectarray=array[1..16000] of TNamedIndexItem;
- pnamedindexobjectarray=^tindexobjectarray;
- tindexarray=class
- noclear : boolean;
- First : TNamedIndexItem;
- count : integer;
- constructor Create(Agrowsize:integer);
- destructor destroy;override;
- procedure clear;
- procedure foreach(proc2call : Tnamedindexcallback;arg:pointer);
- procedure foreach_static(proc2call : Tnamedindexstaticcallback;arg:pointer);
- procedure deleteindex(p:TNamedIndexItem);
- procedure delete(var p:TNamedIndexItem);
- procedure insert(p:TNamedIndexItem);
- function search(nr:integer):TNamedIndexItem;
- private
- growsize,
- size : integer;
- data : pnamedindexobjectarray;
- procedure grow(gsize:integer);
- end;
- {********************************************
- DynamicArray
- ********************************************}
- const
- dynamicblockbasesize = 12;
- type
- pdynamicblock = ^tdynamicblock;
- tdynamicblock = record
- pos,
- used : integer;
- Next : pdynamicblock;
- data : array[0..high(integer)-20] of byte;
- end;
- tdynamicarray = class
- private
- FPosn : integer;
- FPosnblock : pdynamicblock;
- FBlocksize : integer;
- FFirstblock,
- FLastblock : pdynamicblock;
- procedure grow;
- public
- constructor Create(Ablocksize:integer);
- destructor Destroy;override;
- 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);
- procedure readstream(f:TCStream;maxlen:longint);
- procedure writestream(f:TCStream);
- property BlockSize : integer read FBlocksize;
- property FirstBlock : PDynamicBlock read FFirstBlock;
- end;
- implementation
- {*****************************************************************************
- Memory debug
- *****************************************************************************}
- constructor tmemdebug.create(const s:string);
- begin
- infostr:=s;
- {$ifdef Delphi}
- startmem:=0;
- {$else}
- startmem:=memavail;
- {$endif Delphi}
- end;
- destructor tmemdebug.destroy;
- begin
- show;
- end;
- procedure tmemdebug.show;
- {$ifndef Delphi}
- var
- l : integer;
- {$endif}
- begin
- {$ifndef Delphi}
- write('memory [',infostr,'] ');
- l:=memavail;
- if l>startmem then
- writeln(l-startmem,' released')
- else
- writeln(startmem-l,' allocated');
- {$endif Delphi}
- end;
- {*****************************************************************************
- Stack
- *****************************************************************************}
- {$ifdef fixLeaksOnError}
- constructor TStack.init;
- begin
- head := nil;
- end;
- procedure TStack.push(p: pointer);
- var s: PStackItem;
- begin
- New(s);
- s^.data := p;
- s^.Next := head;
- head := s;
- end;
- function TStack.pop: pointer;
- var s: PStackItem;
- begin
- pop := top;
- if assigned(head) then
- begin
- s := head^.Next;
- dispose(head);
- head := s;
- end
- end;
- function TStack.top: pointer;
- begin
- if not isEmpty then
- top := head^.data
- else top := NIL;
- end;
- function TStack.isEmpty: boolean;
- begin
- isEmpty := head = nil;
- end;
- destructor TStack.done;
- var temp: PStackItem;
- begin
- while head <> nil do
- begin
- temp := head^.Next;
- dispose(head);
- head := temp;
- end;
- end;
- {$endif fixLeaksOnError}
- {****************************************************************************
- 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.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;
- { p becomes empty }
- p.FFirst:=nil;
- p.Flast:=nil;
- 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;
- { make p empty }
- p.Flast:=nil;
- p.FFirst:=nil;
- 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.find(const s:string):TstringListItem;
- var
- NewNode : tstringListItem;
- begin
- find:=nil;
- if s='' then
- exit;
- NewNode:=tstringListItem(FFirst);
- while assigned(NewNode) do
- begin
- if NewNode.FPStr^=s then
- begin
- find:=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;
- {****************************************************************************
- TNamedIndexItem
- ****************************************************************************}
- constructor TNamedIndexItem.Create;
- begin
- { index }
- Findexnr:=-1;
- FindexNext:=nil;
- { dictionary }
- Fleft:=nil;
- Fright:=nil;
- FName:=nil;
- Fspeedvalue:=cardinal($ffffffff);
- { List }
- FListNext:=nil;
- end;
- constructor TNamedIndexItem.Createname(const n:string);
- begin
- { index }
- Findexnr:=-1;
- FindexNext:=nil;
- { dictionary }
- Fleft:=nil;
- Fright:=nil;
- Fspeedvalue:=cardinal($ffffffff);
- FName:=stringdup(n);
- { List }
- FListNext:=nil;
- end;
- destructor TNamedIndexItem.destroy;
- begin
- stringdispose(FName);
- end;
- procedure TNamedIndexItem.setname(const n:string);
- begin
- if speedvalue=cardinal($ffffffff) then
- begin
- if assigned(FName) then
- stringdispose(FName);
- FName:=stringdup(n);
- end;
- end;
- function TNamedIndexItem.GetName:string;
- begin
- if assigned(FName) then
- Getname:=FName^
- else
- Getname:='';
- end;
- {****************************************************************************
- TDICTIONARY
- ****************************************************************************}
- constructor Tdictionary.Create;
- begin
- FRoot:=nil;
- FHashArray:=nil;
- noclear:=false;
- replace_existing:=false;
- delete_doubles:=false;
- end;
- procedure Tdictionary.usehash;
- begin
- if not(assigned(FRoot)) and
- not(assigned(FHashArray)) then
- begin
- New(FHashArray);
- fillchar(FHashArray^,sizeof(FHashArray^),0);
- end;
- end;
- function counttree(p: tnamedindexitem): longint;
- begin
- if not assigned(p) then
- exit(0);
- result := 1;
- inc(result,counttree(p.fleft));
- inc(result,counttree(p.fright));
- end;
- destructor Tdictionary.destroy;
- {$ifdef hashdebug}
- var
- i, unused, slots_with_col, collissions, treecount, maxcol: longint;
- {$endif hashdebug}
- begin
- if not noclear then
- clear;
- if assigned(FHashArray) then
- begin
- {$ifdef hashdebug}
- unused := 0;
- collissions := 0;
- maxcol := 0;
- slots_with_col := 0;
- for i := low(fhasharray^) to high(fhasharray^) do
- if assigned(fhasharray^[i]) then
- begin
- treecount := counttree(fhasharray^[i]);
- inc(collissions,sqr(treecount-1));
- if treecount > maxcol then
- maxcol := treecount;
- inc(slots_with_col,ord(treecount>1));
- end
- else
- inc(unused);
- writeln('Slots unused: ',unused,' out of ',hasharraysize,
- ' (',slots_with_col,' with >1 items)');
- writeln('Mean number of collissions: ',
- (sqrt(collissions / extended(hasharraysize-1))):0:3,' (max: ',maxcol,')');
- writeln;
- {$endif hashdebug}
- dispose(FHashArray);
- end;
- end;
- procedure Tdictionary.cleartree(obj:TNamedIndexItem);
- begin
- if assigned(obj.Fleft) then
- cleartree(obj.FLeft);
- if assigned(obj.FRight) then
- cleartree(obj.FRight);
- obj.free;
- obj:=nil;
- end;
- procedure Tdictionary.clear;
- var
- w : integer;
- begin
- if assigned(FRoot) then
- cleartree(FRoot);
- if assigned(FHashArray) then
- for w:= low(FHashArray^) to high(FHashArray^) do
- if assigned(FHashArray^[w]) then
- cleartree(FHashArray^[w]);
- end;
- function Tdictionary.delete(const s:string):TNamedIndexItem;
- var
- p,SpeedValue : cardinal;
- n : TNamedIndexItem;
- procedure insert_right_bottom(var root,Atree:TNamedIndexItem);
- begin
- while root.FRight<>nil do
- root:=root.FRight;
- root.FRight:=Atree;
- end;
- function delete_from_tree(root:TNamedIndexItem):TNamedIndexItem;
- type
- leftright=(left,right);
- var
- lr : leftright;
- oldroot : TNamedIndexItem;
- begin
- oldroot:=nil;
- while (root<>nil) and (root.SpeedValue<>SpeedValue) do
- begin
- oldroot:=root;
- if SpeedValue<root.SpeedValue then
- begin
- root:=root.FRight;
- lr:=right;
- end
- else
- begin
- root:=root.FLeft;
- lr:=left;
- end;
- end;
- while (root<>nil) and (root.FName^<>s) do
- begin
- oldroot:=root;
- if s<root.FName^ then
- begin
- root:=root.FRight;
- lr:=right;
- end
- else
- begin
- root:=root.FLeft;
- lr:=left;
- end;
- end;
- if root.FLeft<>nil then
- begin
- { Now the Node pointing to root must point to the left
- subtree of root. The right subtree of root must be
- connected to the right bottom of the left subtree.}
- if lr=left then
- oldroot.FLeft:=root.FLeft
- else
- oldroot.FRight:=root.FLeft;
- if root.FRight<>nil then
- insert_right_bottom(root.FLeft,root.FRight);
- end
- else
- begin
- { There is no left subtree. So we can just replace the Node to
- delete with the right subtree.}
- if lr=left then
- oldroot.FLeft:=root.FRight
- else
- oldroot.FRight:=root.FRight;
- end;
- delete_from_tree:=root;
- end;
- begin
- SpeedValue:=GetSpeedValue(s);
- n:=FRoot;
- if assigned(FHashArray) then
- begin
- { First, check if the Node to delete directly located under
- the hasharray.}
- p:=SpeedValue mod hasharraysize;
- n:=FHashArray^[p];
- if (n<>nil) and (n.SpeedValue=SpeedValue) and
- (n.FName^=s) then
- begin
- { The Node to delete is directly located under the
- hasharray. Make the hasharray point to the left
- subtree of the Node and place the right subtree on
- the right-bottom of the left subtree.}
- if n.FLeft<>nil then
- begin
- FHashArray^[p]:=n.FLeft;
- if n.FRight<>nil then
- insert_right_bottom(n.FLeft,n.FRight);
- end
- else
- FHashArray^[p]:=n.FRight;
- delete:=n;
- exit;
- end;
- end
- else
- begin
- { First check if the Node to delete is the root.}
- if (FRoot<>nil) and (n.SpeedValue=SpeedValue) and
- (n.FName^=s) then
- begin
- if n.FLeft<>nil then
- begin
- FRoot:=n.FLeft;
- if n.FRight<>nil then
- insert_right_bottom(n.FLeft,n.FRight);
- end
- else
- FRoot:=n.FRight;
- delete:=n;
- exit;
- end;
- end;
- delete:=delete_from_tree(n);
- end;
- function Tdictionary.empty:boolean;
- var
- w : integer;
- begin
- if assigned(FHashArray) then
- begin
- empty:=false;
- for w:=low(FHashArray^) to high(FHashArray^) do
- if assigned(FHashArray^[w]) then
- exit;
- empty:=true;
- end
- else
- empty:=(FRoot=nil);
- end;
- procedure Tdictionary.foreach(proc2call:TNamedIndexcallback;arg:pointer);
- procedure a(p:TNamedIndexItem;arg:pointer);
- begin
- proc2call(p,arg);
- if assigned(p.FLeft) then
- a(p.FLeft,arg);
- if assigned(p.FRight) then
- a(p.FRight,arg);
- end;
- var
- i : integer;
- begin
- if assigned(FHashArray) then
- begin
- for i:=low(FHashArray^) to high(FHashArray^) do
- if assigned(FHashArray^[i]) then
- a(FHashArray^[i],arg);
- end
- else
- if assigned(FRoot) then
- a(FRoot,arg);
- end;
- procedure Tdictionary.foreach_static(proc2call:TNamedIndexStaticCallback;arg:pointer);
- procedure a(p:TNamedIndexItem;arg:pointer);
- begin
- proc2call(p,arg);
- if assigned(p.FLeft) then
- a(p.FLeft,arg);
- if assigned(p.FRight) then
- a(p.FRight,arg);
- end;
- var
- i : integer;
- begin
- if assigned(FHashArray) then
- begin
- for i:=low(FHashArray^) to high(FHashArray^) do
- if assigned(FHashArray^[i]) then
- a(FHashArray^[i],arg);
- end
- else
- if assigned(FRoot) then
- a(FRoot,arg);
- end;
- function Tdictionary.insert(obj:TNamedIndexItem):TNamedIndexItem;
- begin
- obj.FSpeedValue:=GetSpeedValue(obj.FName^);
- if assigned(FHashArray) then
- insert:=insertNode(obj,FHashArray^[obj.SpeedValue mod hasharraysize])
- else
- insert:=insertNode(obj,FRoot);
- end;
- function tdictionary.insertNode(NewNode:TNamedIndexItem;var currNode:TNamedIndexItem):TNamedIndexItem;
- begin
- if currNode=nil then
- begin
- currNode:=NewNode;
- insertNode:=NewNode;
- end
- { First check SpeedValue, to allow a fast insert }
- else
- if currNode.SpeedValue>NewNode.SpeedValue then
- insertNode:=insertNode(NewNode,currNode.FRight)
- else
- if currNode.SpeedValue<NewNode.SpeedValue then
- insertNode:=insertNode(NewNode,currNode.FLeft)
- else
- begin
- if currNode.FName^>NewNode.FName^ then
- insertNode:=insertNode(NewNode,currNode.FRight)
- else
- if currNode.FName^<NewNode.FName^ then
- insertNode:=insertNode(NewNode,currNode.FLeft)
- else
- begin
- if (replace_existing or delete_doubles) and
- assigned(currNode) then
- begin
- NewNode.FLeft:=currNode.FLeft;
- NewNode.FRight:=currNode.FRight;
- if delete_doubles then
- begin
- currnode.FLeft:=nil;
- currnode.FRight:=nil;
- currnode.free;
- end;
- currNode:=NewNode;
- insertNode:=NewNode;
- end
- else
- insertNode:=currNode;
- end;
- end;
- end;
- procedure tdictionary.inserttree(currtree,currroot:TNamedIndexItem);
- begin
- if assigned(currtree) then
- begin
- inserttree(currtree.FLeft,currroot);
- inserttree(currtree.FRight,currroot);
- currtree.FRight:=nil;
- currtree.FLeft:=nil;
- insertNode(currtree,currroot);
- end;
- end;
- function tdictionary.rename(const olds,News : string):TNamedIndexItem;
- var
- spdval : cardinal;
- lasthp,
- hp,hp2,hp3 : TNamedIndexItem;
- begin
- spdval:=GetSpeedValue(olds);
- if assigned(FHashArray) then
- hp:=FHashArray^[spdval mod hasharraysize]
- else
- hp:=FRoot;
- lasthp:=nil;
- while assigned(hp) do
- begin
- if spdval>hp.SpeedValue then
- begin
- lasthp:=hp;
- hp:=hp.FLeft
- end
- else
- if spdval<hp.SpeedValue then
- begin
- lasthp:=hp;
- hp:=hp.FRight
- end
- else
- begin
- if (hp.FName^=olds) then
- begin
- { Get in hp2 the replacer for the root or hasharr }
- hp2:=hp.FLeft;
- hp3:=hp.FRight;
- if not assigned(hp2) then
- begin
- hp2:=hp.FRight;
- hp3:=hp.FLeft;
- end;
- { remove entry from the tree }
- if assigned(lasthp) then
- begin
- if lasthp.FLeft=hp then
- lasthp.FLeft:=hp2
- else
- lasthp.FRight:=hp2;
- end
- else
- begin
- if assigned(FHashArray) then
- FHashArray^[spdval mod hasharraysize]:=hp2
- else
- FRoot:=hp2;
- end;
- { reinsert the hp3 in the tree from hp2 }
- inserttree(hp3,hp2);
- { reset Node with New values }
- hp.FLeft:=nil;
- hp.FRight:=nil;
- stringdispose(hp.FName);
- hp.FName:=stringdup(newS);
- hp.FSpeedValue:=GetSpeedValue(newS);
- { reinsert }
- if assigned(FHashArray) then
- rename:=insertNode(hp,FHashArray^[hp.SpeedValue mod hasharraysize])
- else
- rename:=insertNode(hp,FRoot);
- exit;
- end
- else
- if olds>hp.FName^ then
- begin
- lasthp:=hp;
- hp:=hp.FLeft
- end
- else
- begin
- lasthp:=hp;
- hp:=hp.FRight;
- end;
- end;
- end;
- result := nil;
- end;
- function Tdictionary.search(const s:string):TNamedIndexItem;
- begin
- search:=speedsearch(s,GetSpeedValue(s));
- end;
- function Tdictionary.speedsearch(const s:string;SpeedValue:cardinal):TNamedIndexItem;
- var
- NewNode:TNamedIndexItem;
- begin
- if assigned(FHashArray) then
- NewNode:=FHashArray^[SpeedValue mod hasharraysize]
- else
- NewNode:=FRoot;
- while assigned(NewNode) do
- begin
- if SpeedValue>NewNode.SpeedValue then
- NewNode:=NewNode.FLeft
- else
- if SpeedValue<NewNode.SpeedValue then
- NewNode:=NewNode.FRight
- else
- begin
- if (NewNode.FName^=s) then
- begin
- speedsearch:=NewNode;
- exit;
- end
- else
- if s>NewNode.FName^ then
- NewNode:=NewNode.FLeft
- else
- NewNode:=NewNode.FRight;
- end;
- end;
- speedsearch:=nil;
- end;
- {****************************************************************************
- tsingleList
- ****************************************************************************}
- constructor tsingleList.create;
- begin
- First:=nil;
- last:=nil;
- end;
- procedure tsingleList.reset;
- begin
- First:=nil;
- last:=nil;
- end;
- procedure tsingleList.clear;
- var
- hp,hp2 : TNamedIndexItem;
- begin
- hp:=First;
- while assigned(hp) do
- begin
- hp2:=hp;
- hp:=hp.FListNext;
- hp2.free;
- end;
- First:=nil;
- last:=nil;
- end;
- procedure tsingleList.insert(p:TNamedIndexItem);
- begin
- if not assigned(First) then
- First:=p
- else
- last.FListNext:=p;
- last:=p;
- p.FListNext:=nil;
- end;
- {****************************************************************************
- tindexarray
- ****************************************************************************}
- constructor tindexarray.create(Agrowsize:integer);
- begin
- growsize:=Agrowsize;
- size:=0;
- count:=0;
- data:=nil;
- First:=nil;
- noclear:=false;
- end;
- destructor tindexarray.destroy;
- begin
- if assigned(data) then
- begin
- if not noclear then
- clear;
- freemem(data);
- data:=nil;
- end;
- end;
- function tindexarray.search(nr:integer):TNamedIndexItem;
- begin
- if nr<=count then
- search:=data^[nr]
- else
- search:=nil;
- end;
- procedure tindexarray.clear;
- var
- i : integer;
- begin
- for i:=1 to count do
- if assigned(data^[i]) then
- begin
- data^[i].free;
- data^[i]:=nil;
- end;
- count:=0;
- First:=nil;
- end;
- procedure tindexarray.foreach(proc2call : Tnamedindexcallback;arg:pointer);
- var
- i : integer;
- begin
- for i:=1 to count do
- if assigned(data^[i]) then
- proc2call(data^[i],arg);
- end;
- procedure tindexarray.foreach_static(proc2call : Tnamedindexstaticcallback;arg:pointer);
- var
- i : integer;
- begin
- for i:=1 to count do
- if assigned(data^[i]) then
- proc2call(data^[i],arg);
- end;
- procedure tindexarray.grow(gsize:integer);
- var
- osize : integer;
- begin
- osize:=size;
- inc(size,gsize);
- reallocmem(data,size*4);
- fillchar(data^[osize+1],gsize*4,0);
- end;
- procedure tindexarray.deleteindex(p:TNamedIndexItem);
- var
- i : integer;
- begin
- i:=p.Findexnr;
- { update counter }
- if i=count then
- dec(count);
- { update Linked List }
- while (i>0) do
- begin
- dec(i);
- if (i>0) and assigned(data^[i]) then
- begin
- data^[i].FindexNext:=data^[p.Findexnr].FindexNext;
- break;
- end;
- end;
- if i=0 then
- First:=p.FindexNext;
- data^[p.FIndexnr]:=nil;
- { clear entry }
- p.FIndexnr:=-1;
- p.FIndexNext:=nil;
- end;
- procedure tindexarray.delete(var p:TNamedIndexItem);
- begin
- deleteindex(p);
- p.free;
- p:=nil;
- end;
- procedure tindexarray.insert(p:TNamedIndexItem);
- var
- i : integer;
- begin
- if p.FIndexnr=-1 then
- begin
- inc(count);
- p.FIndexnr:=count;
- end;
- if p.FIndexnr>count then
- count:=p.FIndexnr;
- if count>size then
- grow(((count div growsize)+1)*growsize);
- Assert(not assigned(data^[p.FIndexnr]) or (p=data^[p.FIndexnr]));
- data^[p.FIndexnr]:=p;
- { update Linked List backward }
- i:=p.FIndexnr;
- while (i>0) do
- begin
- dec(i);
- if (i>0) and assigned(data^[i]) then
- begin
- data^[i].FIndexNext:=p;
- break;
- end;
- end;
- if i=0 then
- First:=p;
- { update Linked List forward }
- i:=p.FIndexnr;
- while (i<=count) do
- begin
- inc(i);
- if (i<=count) and assigned(data^[i]) then
- begin
- p.FIndexNext:=data^[i];
- exit;
- end;
- end;
- if i>count then
- p.FIndexNext:=nil;
- end;
- {****************************************************************************
- tdynamicarray
- ****************************************************************************}
- constructor tdynamicarray.create(Ablocksize:integer);
- begin
- FPosn:=0;
- FPosnblock:=nil;
- FFirstblock:=nil;
- FLastblock:=nil;
- Fblocksize:=Ablocksize;
- grow;
- end;
- destructor tdynamicarray.destroy;
- var
- hp : pdynamicblock;
- begin
- while assigned(FFirstblock) do
- begin
- hp:=FFirstblock;
- FFirstblock:=FFirstblock^.Next;
- freemem(hp,blocksize+dynamicblockbasesize);
- end;
- end;
- function tdynamicarray.size:integer;
- begin
- if assigned(FLastblock) then
- size:=FLastblock^.pos+FLastblock^.used
- else
- size:=0;
- end;
- procedure tdynamicarray.grow;
- var
- nblock : pdynamicblock;
- begin
- Getmem(nblock,blocksize+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^.used;
- end;
- nblock^.used:=0;
- nblock^.Next:=nil;
- fillchar(nblock^.data,blocksize,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>blocksize then
- begin
- dec(j,blocksize-FPosnblock^.used);
- FPosnblock^.used:=blocksize;
- 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+blocksize) 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+blocksize>i then
- break;
- FPosnblock:=FPosnblock^.Next;
- end;
- { not found ? then increase blocks }
- if not assigned(FPosnblock) then
- begin
- { the current FLastblock is now also fully used }
- FLastblock^.used:=blocksize;
- repeat
- grow;
- FPosnblock:=FLastblock;
- until FPosnblock^.pos+blocksize>=i;
- end;
- end;
- FPosn:=i;
- if FPosn mod blocksize>FPosnblock^.used then
- FPosnblock^.used:=FPosn mod blocksize;
- 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 mod blocksize;
- if i+len>=blocksize then
- begin
- j:=blocksize-i;
- move(p^,FPosnblock^.data[i],j);
- inc(p,j);
- inc(FPosn,j);
- dec(len,j);
- FPosnblock^.used:=blocksize;
- 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 mod blocksize;
- 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 mod blocksize;
- 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:=blocksize-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=blocksize 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.
- {
- $Log$
- Revision 1.15 2002-07-01 18:46:21 peter
- * internal linker
- * reorganized aasm layer
- Revision 1.14 2002/06/17 13:56:14 jonas
- * tdictionary.rename() returns nil if the original object wasn't found
- (reported by Sergey Korshunoff <[email protected]>)
- Revision 1.13 2002/05/18 13:34:05 peter
- * readded missing revisions
- Revision 1.12 2002/05/16 19:46:35 carl
- + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
- + try to fix temp allocation (still in ifdef)
- + generic constructor calls
- + start of tassembler / tmodulebase class cleanup
- Revision 1.10 2002/05/12 16:53:04 peter
- * moved entry and exitcode to ncgutil and cgobj
- * foreach gets extra argument for passing local data to the
- iterator function
- * -CR checks also class typecasts at runtime by changing them
- into as
- * fixed compiler to cycle with the -CR option
- * fixed stabs with elf writer, finally the global variables can
- be watched
- * removed a lot of routines from cga unit and replaced them by
- calls to cgobj
- * u32bit-s32bit updates for and,or,xor nodes. When one element is
- u32bit then the other is typecasted also to u32bit without giving
- a rangecheck warning/error.
- * fixed pascal calling method with reversing also the high tree in
- the parast, detected by tcalcst3 test
- }
|