cclasses.pas 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl and Peter Vreman
  4. This module provides some basic classes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cclasses;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. cutils,cstreams;
  23. {********************************************
  24. TMemDebug
  25. ********************************************}
  26. type
  27. tmemdebug = class
  28. private
  29. startmem : integer;
  30. infostr : string[40];
  31. public
  32. constructor Create(const s:string);
  33. destructor Destroy;override;
  34. procedure show;
  35. end;
  36. {********************************************
  37. TLinkedList
  38. ********************************************}
  39. type
  40. TLinkedListItem = class
  41. public
  42. Previous,
  43. Next : TLinkedListItem;
  44. Constructor Create;
  45. Destructor Destroy;override;
  46. Function GetCopy:TLinkedListItem;virtual;
  47. end;
  48. TLinkedListItemClass = class of TLinkedListItem;
  49. TLinkedList = class
  50. private
  51. FCount : integer;
  52. FFirst,
  53. FLast : TLinkedListItem;
  54. public
  55. constructor Create;
  56. destructor Destroy;override;
  57. { true when the List is empty }
  58. function Empty:boolean;
  59. { deletes all Items }
  60. procedure Clear;
  61. { inserts an Item }
  62. procedure Insert(Item:TLinkedListItem);
  63. { concats an Item }
  64. procedure Concat(Item:TLinkedListItem);
  65. { deletes an Item }
  66. procedure Remove(Item:TLinkedListItem);
  67. { Gets First Item }
  68. function GetFirst:TLinkedListItem;
  69. { Gets last Item }
  70. function GetLast:TLinkedListItem;
  71. { inserts another List at the begin and make this List empty }
  72. procedure insertList(p : TLinkedList);
  73. { concats another List at the end and make this List empty }
  74. procedure concatList(p : TLinkedList);
  75. { concats another List at the end and makes a copy }
  76. procedure insertListcopy(p : TLinkedList);
  77. { concats another List at the end and makes a copy }
  78. procedure concatListcopy(p : TLinkedList);
  79. property First:TLinkedListItem read FFirst;
  80. property Last:TLinkedListItem read FLast;
  81. property Count:Integer read FCount;
  82. end;
  83. {********************************************
  84. TStringList
  85. ********************************************}
  86. { string containerItem }
  87. TStringListItem = class(TLinkedListItem)
  88. FPStr : PString;
  89. public
  90. constructor Create(const s:string);
  91. destructor Destroy;override;
  92. function GetCopy:TLinkedListItem;override;
  93. function Str:string;
  94. end;
  95. { string container }
  96. TStringList = class(TLinkedList)
  97. private
  98. FDoubles : boolean; { if this is set to true, doubles are allowed }
  99. public
  100. constructor Create;
  101. constructor Create_No_Double;
  102. { inserts an Item }
  103. procedure Insert(const s:string);
  104. { concats an Item }
  105. procedure Concat(const s:string);
  106. { deletes an Item }
  107. procedure Remove(const s:string);
  108. { Gets First Item }
  109. function GetFirst:string;
  110. { Gets last Item }
  111. function GetLast:string;
  112. { true if string is in the container }
  113. function Find(const s:string):TStringListItem;
  114. { inserts an item }
  115. procedure InsertItem(item:TStringListItem);
  116. { concats an item }
  117. procedure ConcatItem(item:TStringListItem);
  118. property Doubles:boolean read FDoubles write FDoubles;
  119. end;
  120. {********************************************
  121. Dictionary
  122. ********************************************}
  123. const
  124. { the real size will be [0..hasharray-1] ! }
  125. hasharraysize = 512;
  126. type
  127. { namedindexobect for use with dictionary and indexarray }
  128. TNamedIndexItem=class
  129. private
  130. { indexarray }
  131. FIndexNr : integer;
  132. FIndexNext : TNamedIndexItem;
  133. { dictionary }
  134. FLeft,
  135. FRight : TNamedIndexItem;
  136. FSpeedValue : cardinal;
  137. { singleList }
  138. FListNext : TNamedIndexItem;
  139. protected
  140. function GetName:string;virtual;
  141. procedure SetName(const n:string);virtual;
  142. public
  143. FName : Pstring;
  144. constructor Create;
  145. constructor CreateName(const n:string);
  146. destructor Destroy;override;
  147. property IndexNr:integer read FIndexNr write FIndexNr;
  148. property IndexNext:TNamedIndexItem read FIndexNext write FIndexNext;
  149. property Name:string read GetName write SetName;
  150. property SpeedValue:cardinal read FSpeedValue;
  151. property ListNext:TNamedIndexItem read FListNext;
  152. property Left:TNamedIndexItem read FLeft write FLeft;
  153. property Right:TNamedIndexItem read FRight write FRight;
  154. end;
  155. Pdictionaryhasharray=^Tdictionaryhasharray;
  156. Tdictionaryhasharray=array[0..hasharraysize-1] of TNamedIndexItem;
  157. TnamedIndexCallback = procedure(p:TNamedIndexItem) of object;
  158. TnamedIndexStaticCallback = procedure(p:TNamedIndexItem);
  159. Tdictionary=class
  160. private
  161. FRoot : TNamedIndexItem;
  162. FHashArray : Pdictionaryhasharray;
  163. procedure cleartree(obj:TNamedIndexItem);
  164. function insertNode(NewNode:TNamedIndexItem;var currNode:TNamedIndexItem):TNamedIndexItem;
  165. procedure inserttree(currtree,currroot:TNamedIndexItem);
  166. public
  167. noclear : boolean;
  168. replace_existing : boolean;
  169. delete_doubles : boolean;
  170. constructor Create;
  171. destructor Destroy;override;
  172. procedure usehash;
  173. procedure clear;
  174. function delete(const s:string):TNamedIndexItem;
  175. function empty:boolean;
  176. procedure foreach(proc2call:TNamedIndexcallback);
  177. procedure foreach_static(proc2call:TNamedIndexStaticCallback);
  178. function insert(obj:TNamedIndexItem):TNamedIndexItem;
  179. function rename(const olds,News : string):TNamedIndexItem;
  180. function search(const s:string):TNamedIndexItem;
  181. function speedsearch(const s:string;SpeedValue:cardinal):TNamedIndexItem;
  182. property Items[const s:string]:TNamedIndexItem read Search;default;
  183. end;
  184. tsingleList=class
  185. First,
  186. last : TNamedIndexItem;
  187. constructor Create;
  188. procedure reset;
  189. procedure clear;
  190. procedure insert(p:TNamedIndexItem);
  191. end;
  192. tindexobjectarray=array[1..16000] of TNamedIndexItem;
  193. pnamedindexobjectarray=^tindexobjectarray;
  194. tindexarray=class
  195. noclear : boolean;
  196. First : TNamedIndexItem;
  197. count : integer;
  198. constructor Create(Agrowsize:integer);
  199. destructor destroy;override;
  200. procedure clear;
  201. procedure foreach(proc2call : Tnamedindexcallback);
  202. procedure foreach_static(proc2call : Tnamedindexstaticcallback);
  203. procedure deleteindex(p:TNamedIndexItem);
  204. procedure delete(var p:TNamedIndexItem);
  205. procedure insert(p:TNamedIndexItem);
  206. function search(nr:integer):TNamedIndexItem;
  207. private
  208. growsize,
  209. size : integer;
  210. data : pnamedindexobjectarray;
  211. procedure grow(gsize:integer);
  212. end;
  213. {********************************************
  214. DynamicArray
  215. ********************************************}
  216. const
  217. dynamicblockbasesize = 12;
  218. type
  219. pdynamicblock = ^tdynamicblock;
  220. tdynamicblock = record
  221. pos,
  222. used : integer;
  223. Next : pdynamicblock;
  224. data : array[0..high(integer)-20] of byte;
  225. end;
  226. tdynamicarray = class
  227. private
  228. FPosn : integer;
  229. FPosnblock : pdynamicblock;
  230. FBlocksize : integer;
  231. FFirstblock,
  232. FLastblock : pdynamicblock;
  233. procedure grow;
  234. public
  235. constructor Create(Ablocksize:integer);
  236. destructor Destroy;override;
  237. function size:integer;
  238. procedure align(i:integer);
  239. procedure seek(i:integer);
  240. function read(var d;len:integer):integer;
  241. procedure write(const d;len:integer);
  242. procedure writestr(const s:string);
  243. procedure readstream(f:TCStream;maxlen:longint);
  244. procedure writestream(f:TCStream);
  245. property BlockSize : integer read FBlocksize;
  246. property FirstBlock : PDynamicBlock read FFirstBlock;
  247. end;
  248. implementation
  249. {*****************************************************************************
  250. Memory debug
  251. *****************************************************************************}
  252. constructor tmemdebug.create(const s:string);
  253. begin
  254. infostr:=s;
  255. {$ifdef Delphi}
  256. startmem:=0;
  257. {$else}
  258. startmem:=memavail;
  259. {$endif Delphi}
  260. end;
  261. destructor tmemdebug.destroy;
  262. begin
  263. show;
  264. end;
  265. procedure tmemdebug.show;
  266. {$ifndef Delphi}
  267. var
  268. l : integer;
  269. {$endif}
  270. begin
  271. {$ifndef Delphi}
  272. write('memory [',infostr,'] ');
  273. l:=memavail;
  274. if l>startmem then
  275. writeln(l-startmem,' released')
  276. else
  277. writeln(startmem-l,' allocated');
  278. {$endif Delphi}
  279. end;
  280. {*****************************************************************************
  281. Stack
  282. *****************************************************************************}
  283. {$ifdef fixLeaksOnError}
  284. constructor TStack.init;
  285. begin
  286. head := nil;
  287. end;
  288. procedure TStack.push(p: pointer);
  289. var s: PStackItem;
  290. begin
  291. New(s);
  292. s^.data := p;
  293. s^.Next := head;
  294. head := s;
  295. end;
  296. function TStack.pop: pointer;
  297. var s: PStackItem;
  298. begin
  299. pop := top;
  300. if assigned(head) then
  301. begin
  302. s := head^.Next;
  303. dispose(head);
  304. head := s;
  305. end
  306. end;
  307. function TStack.top: pointer;
  308. begin
  309. if not isEmpty then
  310. top := head^.data
  311. else top := NIL;
  312. end;
  313. function TStack.isEmpty: boolean;
  314. begin
  315. isEmpty := head = nil;
  316. end;
  317. destructor TStack.done;
  318. var temp: PStackItem;
  319. begin
  320. while head <> nil do
  321. begin
  322. temp := head^.Next;
  323. dispose(head);
  324. head := temp;
  325. end;
  326. end;
  327. {$endif fixLeaksOnError}
  328. {****************************************************************************
  329. TLinkedListItem
  330. ****************************************************************************}
  331. constructor TLinkedListItem.Create;
  332. begin
  333. Previous:=nil;
  334. Next:=nil;
  335. end;
  336. destructor TLinkedListItem.Destroy;
  337. begin
  338. end;
  339. function TLinkedListItem.GetCopy:TLinkedListItem;
  340. var
  341. p : TLinkedListItem;
  342. l : integer;
  343. begin
  344. p:=TLinkedListItemClass(ClassType).Create;
  345. l:=InstanceSize;
  346. Move(pointer(self)^,pointer(p)^,l);
  347. Result:=p;
  348. end;
  349. {****************************************************************************
  350. TLinkedList
  351. ****************************************************************************}
  352. constructor TLinkedList.Create;
  353. begin
  354. FFirst:=nil;
  355. Flast:=nil;
  356. FCount:=0;
  357. end;
  358. destructor TLinkedList.destroy;
  359. begin
  360. Clear;
  361. end;
  362. function TLinkedList.empty:boolean;
  363. begin
  364. Empty:=(FFirst=nil);
  365. end;
  366. procedure TLinkedList.Insert(Item:TLinkedListItem);
  367. begin
  368. if FFirst=nil then
  369. begin
  370. FLast:=Item;
  371. Item.Previous:=nil;
  372. Item.Next:=nil;
  373. end
  374. else
  375. begin
  376. FFirst.Previous:=Item;
  377. Item.Previous:=nil;
  378. Item.Next:=FFirst;
  379. end;
  380. FFirst:=Item;
  381. inc(FCount);
  382. end;
  383. procedure TLinkedList.Concat(Item:TLinkedListItem);
  384. begin
  385. if FFirst=nil then
  386. begin
  387. FFirst:=Item;
  388. Item.Previous:=nil;
  389. Item.Next:=nil;
  390. end
  391. else
  392. begin
  393. Flast.Next:=Item;
  394. Item.Previous:=Flast;
  395. Item.Next:=nil;
  396. end;
  397. Flast:=Item;
  398. inc(FCount);
  399. end;
  400. procedure TLinkedList.remove(Item:TLinkedListItem);
  401. begin
  402. if Item=nil then
  403. exit;
  404. if (FFirst=Item) and (Flast=Item) then
  405. begin
  406. FFirst:=nil;
  407. Flast:=nil;
  408. end
  409. else if FFirst=Item then
  410. begin
  411. FFirst:=Item.Next;
  412. if assigned(FFirst) then
  413. FFirst.Previous:=nil;
  414. end
  415. else if Flast=Item then
  416. begin
  417. Flast:=Flast.Previous;
  418. if assigned(Flast) then
  419. Flast.Next:=nil;
  420. end
  421. else
  422. begin
  423. Item.Previous.Next:=Item.Next;
  424. Item.Next.Previous:=Item.Previous;
  425. end;
  426. Item.Next:=nil;
  427. Item.Previous:=nil;
  428. dec(FCount);
  429. end;
  430. procedure TLinkedList.clear;
  431. var
  432. NewNode : TLinkedListItem;
  433. begin
  434. NewNode:=FFirst;
  435. while assigned(NewNode) do
  436. begin
  437. FFirst:=NewNode.Next;
  438. NewNode.Free;
  439. NewNode:=FFirst;
  440. end;
  441. FLast:=nil;
  442. FFirst:=nil;
  443. FCount:=0;
  444. end;
  445. function TLinkedList.GetFirst:TLinkedListItem;
  446. begin
  447. if FFirst=nil then
  448. GetFirst:=nil
  449. else
  450. begin
  451. GetFirst:=FFirst;
  452. if FFirst=FLast then
  453. FLast:=nil;
  454. FFirst:=FFirst.Next;
  455. dec(FCount);
  456. end;
  457. end;
  458. function TLinkedList.GetLast:TLinkedListItem;
  459. begin
  460. if FLast=nil then
  461. Getlast:=nil
  462. else
  463. begin
  464. Getlast:=FLast;
  465. if FLast=FFirst then
  466. FFirst:=nil;
  467. FLast:=FLast.Previous;
  468. dec(FCount);
  469. end;
  470. end;
  471. procedure TLinkedList.insertList(p : TLinkedList);
  472. begin
  473. { empty List ? }
  474. if (p.FFirst=nil) then
  475. exit;
  476. p.Flast.Next:=FFirst;
  477. { we have a double Linked List }
  478. if assigned(FFirst) then
  479. FFirst.Previous:=p.Flast;
  480. FFirst:=p.FFirst;
  481. if (FLast=nil) then
  482. Flast:=p.Flast;
  483. { p becomes empty }
  484. p.FFirst:=nil;
  485. p.Flast:=nil;
  486. end;
  487. procedure TLinkedList.concatList(p : TLinkedList);
  488. begin
  489. if (p.FFirst=nil) then
  490. exit;
  491. if FFirst=nil then
  492. FFirst:=p.FFirst
  493. else
  494. begin
  495. FLast.Next:=p.FFirst;
  496. p.FFirst.Previous:=Flast;
  497. end;
  498. Flast:=p.Flast;
  499. { make p empty }
  500. p.Flast:=nil;
  501. p.FFirst:=nil;
  502. end;
  503. procedure TLinkedList.insertListcopy(p : TLinkedList);
  504. var
  505. NewNode,NewNode2 : TLinkedListItem;
  506. begin
  507. NewNode:=p.First;
  508. while assigned(NewNode) do
  509. begin
  510. NewNode2:=NewNode.Getcopy;
  511. if assigned(NewNode2) then
  512. Insert(NewNode2);
  513. NewNode:=NewNode.Next;
  514. end;
  515. end;
  516. procedure TLinkedList.concatListcopy(p : TLinkedList);
  517. var
  518. NewNode,NewNode2 : TLinkedListItem;
  519. begin
  520. NewNode:=p.First;
  521. while assigned(NewNode) do
  522. begin
  523. NewNode2:=NewNode.Getcopy;
  524. if assigned(NewNode2) then
  525. Concat(NewNode2);
  526. NewNode:=NewNode.Next;
  527. end;
  528. end;
  529. {****************************************************************************
  530. TStringListItem
  531. ****************************************************************************}
  532. constructor TStringListItem.Create(const s:string);
  533. begin
  534. inherited Create;
  535. FPStr:=stringdup(s);
  536. end;
  537. destructor TStringListItem.Destroy;
  538. begin
  539. stringdispose(FPStr);
  540. end;
  541. function TStringListItem.Str:string;
  542. begin
  543. Str:=FPStr^;
  544. end;
  545. function TStringListItem.GetCopy:TLinkedListItem;
  546. begin
  547. Result:=(inherited GetCopy);
  548. TStringListItem(Result).FPStr:=stringdup(FPstr^);
  549. end;
  550. {****************************************************************************
  551. TSTRINGList
  552. ****************************************************************************}
  553. constructor tstringList.Create;
  554. begin
  555. inherited Create;
  556. FDoubles:=true;
  557. end;
  558. constructor tstringList.Create_no_double;
  559. begin
  560. inherited Create;
  561. FDoubles:=false;
  562. end;
  563. procedure tstringList.insert(const s : string);
  564. begin
  565. if (s='') or
  566. ((not FDoubles) and (find(s)<>nil)) then
  567. exit;
  568. inherited insert(tstringListItem.create(s));
  569. end;
  570. procedure tstringList.concat(const s : string);
  571. begin
  572. if (s='') or
  573. ((not FDoubles) and (find(s)<>nil)) then
  574. exit;
  575. inherited concat(tstringListItem.create(s));
  576. end;
  577. procedure tstringList.remove(const s : string);
  578. var
  579. p : tstringListItem;
  580. begin
  581. if s='' then
  582. exit;
  583. p:=find(s);
  584. if assigned(p) then
  585. begin
  586. inherited Remove(p);
  587. p.Free;
  588. end;
  589. end;
  590. function tstringList.GetFirst : string;
  591. var
  592. p : tstringListItem;
  593. begin
  594. p:=tstringListItem(inherited GetFirst);
  595. if p=nil then
  596. GetFirst:=''
  597. else
  598. begin
  599. GetFirst:=p.FPStr^;
  600. p.free;
  601. end;
  602. end;
  603. function tstringList.Getlast : string;
  604. var
  605. p : tstringListItem;
  606. begin
  607. p:=tstringListItem(inherited Getlast);
  608. if p=nil then
  609. Getlast:=''
  610. else
  611. begin
  612. Getlast:=p.FPStr^;
  613. p.free;
  614. end;
  615. end;
  616. function tstringList.find(const s:string):TstringListItem;
  617. var
  618. NewNode : tstringListItem;
  619. begin
  620. find:=nil;
  621. if s='' then
  622. exit;
  623. NewNode:=tstringListItem(FFirst);
  624. while assigned(NewNode) do
  625. begin
  626. if NewNode.FPStr^=s then
  627. begin
  628. find:=NewNode;
  629. exit;
  630. end;
  631. NewNode:=tstringListItem(NewNode.Next);
  632. end;
  633. end;
  634. procedure TStringList.InsertItem(item:TStringListItem);
  635. begin
  636. inherited Insert(item);
  637. end;
  638. procedure TStringList.ConcatItem(item:TStringListItem);
  639. begin
  640. inherited Concat(item);
  641. end;
  642. {****************************************************************************
  643. TNamedIndexItem
  644. ****************************************************************************}
  645. constructor TNamedIndexItem.Create;
  646. begin
  647. { index }
  648. Findexnr:=-1;
  649. FindexNext:=nil;
  650. { dictionary }
  651. Fleft:=nil;
  652. Fright:=nil;
  653. FName:=nil;
  654. Fspeedvalue:=cardinal($ffffffff);
  655. { List }
  656. FListNext:=nil;
  657. end;
  658. constructor TNamedIndexItem.Createname(const n:string);
  659. begin
  660. { index }
  661. Findexnr:=-1;
  662. FindexNext:=nil;
  663. { dictionary }
  664. Fleft:=nil;
  665. Fright:=nil;
  666. Fspeedvalue:=cardinal($ffffffff);
  667. FName:=stringdup(n);
  668. { List }
  669. FListNext:=nil;
  670. end;
  671. destructor TNamedIndexItem.destroy;
  672. begin
  673. stringdispose(FName);
  674. end;
  675. procedure TNamedIndexItem.setname(const n:string);
  676. begin
  677. if speedvalue=cardinal($ffffffff) then
  678. begin
  679. if assigned(FName) then
  680. stringdispose(FName);
  681. FName:=stringdup(n);
  682. end;
  683. end;
  684. function TNamedIndexItem.GetName:string;
  685. begin
  686. if assigned(FName) then
  687. Getname:=FName^
  688. else
  689. Getname:='';
  690. end;
  691. {****************************************************************************
  692. TDICTIONARY
  693. ****************************************************************************}
  694. constructor Tdictionary.Create;
  695. begin
  696. FRoot:=nil;
  697. FHashArray:=nil;
  698. noclear:=false;
  699. replace_existing:=false;
  700. delete_doubles:=false;
  701. end;
  702. procedure Tdictionary.usehash;
  703. begin
  704. if not(assigned(FRoot)) and
  705. not(assigned(FHashArray)) then
  706. begin
  707. New(FHashArray);
  708. fillchar(FHashArray^,sizeof(FHashArray^),0);
  709. end;
  710. end;
  711. function counttree(p: tnamedindexitem): longint;
  712. begin
  713. if not assigned(p) then
  714. exit(0);
  715. result := 1;
  716. inc(result,counttree(p.fleft));
  717. inc(result,counttree(p.fright));
  718. end;
  719. destructor Tdictionary.destroy;
  720. {$ifdef hashdebug}
  721. var
  722. i, unused, slots_with_col, collissions, treecount, maxcol: longint;
  723. {$endif hashdebug}
  724. begin
  725. if not noclear then
  726. clear;
  727. if assigned(FHashArray) then
  728. begin
  729. {$ifdef hashdebug}
  730. unused := 0;
  731. collissions := 0;
  732. maxcol := 0;
  733. slots_with_col := 0;
  734. for i := low(fhasharray^) to high(fhasharray^) do
  735. if assigned(fhasharray^[i]) then
  736. begin
  737. treecount := counttree(fhasharray^[i]);
  738. inc(collissions,sqr(treecount-1));
  739. if treecount > maxcol then
  740. maxcol := treecount;
  741. inc(slots_with_col,ord(treecount>1));
  742. end
  743. else
  744. inc(unused);
  745. writeln('Slots unused: ',unused,' out of ',hasharraysize,
  746. ' (',slots_with_col,' with >1 items)');
  747. writeln('Mean number of collissions: ',
  748. (sqrt(collissions / extended(hasharraysize-1))):0:3,' (max: ',maxcol,')');
  749. writeln;
  750. {$endif hashdebug}
  751. dispose(FHashArray);
  752. end;
  753. end;
  754. procedure Tdictionary.cleartree(obj:TNamedIndexItem);
  755. begin
  756. if assigned(obj.Fleft) then
  757. cleartree(obj.FLeft);
  758. if assigned(obj.FRight) then
  759. cleartree(obj.FRight);
  760. obj.free;
  761. obj:=nil;
  762. end;
  763. procedure Tdictionary.clear;
  764. var
  765. w : integer;
  766. begin
  767. if assigned(FRoot) then
  768. cleartree(FRoot);
  769. if assigned(FHashArray) then
  770. for w:= low(FHashArray^) to high(FHashArray^) do
  771. if assigned(FHashArray^[w]) then
  772. cleartree(FHashArray^[w]);
  773. end;
  774. function Tdictionary.delete(const s:string):TNamedIndexItem;
  775. var
  776. p,SpeedValue : cardinal;
  777. n : TNamedIndexItem;
  778. procedure insert_right_bottom(var root,Atree:TNamedIndexItem);
  779. begin
  780. while root.FRight<>nil do
  781. root:=root.FRight;
  782. root.FRight:=Atree;
  783. end;
  784. function delete_from_tree(root:TNamedIndexItem):TNamedIndexItem;
  785. type
  786. leftright=(left,right);
  787. var
  788. lr : leftright;
  789. oldroot : TNamedIndexItem;
  790. begin
  791. oldroot:=nil;
  792. while (root<>nil) and (root.SpeedValue<>SpeedValue) do
  793. begin
  794. oldroot:=root;
  795. if SpeedValue<root.SpeedValue then
  796. begin
  797. root:=root.FRight;
  798. lr:=right;
  799. end
  800. else
  801. begin
  802. root:=root.FLeft;
  803. lr:=left;
  804. end;
  805. end;
  806. while (root<>nil) and (root.FName^<>s) do
  807. begin
  808. oldroot:=root;
  809. if s<root.FName^ then
  810. begin
  811. root:=root.FRight;
  812. lr:=right;
  813. end
  814. else
  815. begin
  816. root:=root.FLeft;
  817. lr:=left;
  818. end;
  819. end;
  820. if root.FLeft<>nil then
  821. begin
  822. { Now the Node pointing to root must point to the left
  823. subtree of root. The right subtree of root must be
  824. connected to the right bottom of the left subtree.}
  825. if lr=left then
  826. oldroot.FLeft:=root.FLeft
  827. else
  828. oldroot.FRight:=root.FLeft;
  829. if root.FRight<>nil then
  830. insert_right_bottom(root.FLeft,root.FRight);
  831. end
  832. else
  833. begin
  834. { There is no left subtree. So we can just replace the Node to
  835. delete with the right subtree.}
  836. if lr=left then
  837. oldroot.FLeft:=root.FRight
  838. else
  839. oldroot.FRight:=root.FRight;
  840. end;
  841. delete_from_tree:=root;
  842. end;
  843. begin
  844. SpeedValue:=GetSpeedValue(s);
  845. n:=FRoot;
  846. if assigned(FHashArray) then
  847. begin
  848. { First, check if the Node to delete directly located under
  849. the hasharray.}
  850. p:=SpeedValue mod hasharraysize;
  851. n:=FHashArray^[p];
  852. if (n<>nil) and (n.SpeedValue=SpeedValue) and
  853. (n.FName^=s) then
  854. begin
  855. { The Node to delete is directly located under the
  856. hasharray. Make the hasharray point to the left
  857. subtree of the Node and place the right subtree on
  858. the right-bottom of the left subtree.}
  859. if n.FLeft<>nil then
  860. begin
  861. FHashArray^[p]:=n.FLeft;
  862. if n.FRight<>nil then
  863. insert_right_bottom(n.FLeft,n.FRight);
  864. end
  865. else
  866. FHashArray^[p]:=n.FRight;
  867. delete:=n;
  868. exit;
  869. end;
  870. end
  871. else
  872. begin
  873. { First check if the Node to delete is the root.}
  874. if (FRoot<>nil) and (n.SpeedValue=SpeedValue) and
  875. (n.FName^=s) then
  876. begin
  877. if n.FLeft<>nil then
  878. begin
  879. FRoot:=n.FLeft;
  880. if n.FRight<>nil then
  881. insert_right_bottom(n.FLeft,n.FRight);
  882. end
  883. else
  884. FRoot:=n.FRight;
  885. delete:=n;
  886. exit;
  887. end;
  888. end;
  889. delete:=delete_from_tree(n);
  890. end;
  891. function Tdictionary.empty:boolean;
  892. var
  893. w : integer;
  894. begin
  895. if assigned(FHashArray) then
  896. begin
  897. empty:=false;
  898. for w:=low(FHashArray^) to high(FHashArray^) do
  899. if assigned(FHashArray^[w]) then
  900. exit;
  901. empty:=true;
  902. end
  903. else
  904. empty:=(FRoot=nil);
  905. end;
  906. procedure Tdictionary.foreach(proc2call:TNamedIndexcallback);
  907. procedure a(p:TNamedIndexItem);
  908. begin
  909. proc2call(p);
  910. if assigned(p.FLeft) then
  911. a(p.FLeft);
  912. if assigned(p.FRight) then
  913. a(p.FRight);
  914. end;
  915. var
  916. i : integer;
  917. begin
  918. if assigned(FHashArray) then
  919. begin
  920. for i:=low(FHashArray^) to high(FHashArray^) do
  921. if assigned(FHashArray^[i]) then
  922. a(FHashArray^[i]);
  923. end
  924. else
  925. if assigned(FRoot) then
  926. a(FRoot);
  927. end;
  928. procedure Tdictionary.foreach_static(proc2call:TNamedIndexStaticCallback);
  929. procedure a(p:TNamedIndexItem);
  930. begin
  931. proc2call(p);
  932. if assigned(p.FLeft) then
  933. a(p.FLeft);
  934. if assigned(p.FRight) then
  935. a(p.FRight);
  936. end;
  937. var
  938. i : integer;
  939. begin
  940. if assigned(FHashArray) then
  941. begin
  942. for i:=low(FHashArray^) to high(FHashArray^) do
  943. if assigned(FHashArray^[i]) then
  944. a(FHashArray^[i]);
  945. end
  946. else
  947. if assigned(FRoot) then
  948. a(FRoot);
  949. end;
  950. function Tdictionary.insert(obj:TNamedIndexItem):TNamedIndexItem;
  951. begin
  952. obj.FSpeedValue:=GetSpeedValue(obj.FName^);
  953. if assigned(FHashArray) then
  954. insert:=insertNode(obj,FHashArray^[obj.SpeedValue mod hasharraysize])
  955. else
  956. insert:=insertNode(obj,FRoot);
  957. end;
  958. function tdictionary.insertNode(NewNode:TNamedIndexItem;var currNode:TNamedIndexItem):TNamedIndexItem;
  959. begin
  960. if currNode=nil then
  961. begin
  962. currNode:=NewNode;
  963. insertNode:=NewNode;
  964. end
  965. { First check SpeedValue, to allow a fast insert }
  966. else
  967. if currNode.SpeedValue>NewNode.SpeedValue then
  968. insertNode:=insertNode(NewNode,currNode.FRight)
  969. else
  970. if currNode.SpeedValue<NewNode.SpeedValue then
  971. insertNode:=insertNode(NewNode,currNode.FLeft)
  972. else
  973. begin
  974. if currNode.FName^>NewNode.FName^ then
  975. insertNode:=insertNode(NewNode,currNode.FRight)
  976. else
  977. if currNode.FName^<NewNode.FName^ then
  978. insertNode:=insertNode(NewNode,currNode.FLeft)
  979. else
  980. begin
  981. if (replace_existing or delete_doubles) and
  982. assigned(currNode) then
  983. begin
  984. NewNode.FLeft:=currNode.FLeft;
  985. NewNode.FRight:=currNode.FRight;
  986. if delete_doubles then
  987. begin
  988. currnode.FLeft:=nil;
  989. currnode.FRight:=nil;
  990. currnode.free;
  991. end;
  992. currNode:=NewNode;
  993. insertNode:=NewNode;
  994. end
  995. else
  996. insertNode:=currNode;
  997. end;
  998. end;
  999. end;
  1000. procedure tdictionary.inserttree(currtree,currroot:TNamedIndexItem);
  1001. begin
  1002. if assigned(currtree) then
  1003. begin
  1004. inserttree(currtree.FLeft,currroot);
  1005. inserttree(currtree.FRight,currroot);
  1006. currtree.FRight:=nil;
  1007. currtree.FLeft:=nil;
  1008. insertNode(currtree,currroot);
  1009. end;
  1010. end;
  1011. function tdictionary.rename(const olds,News : string):TNamedIndexItem;
  1012. var
  1013. spdval : cardinal;
  1014. lasthp,
  1015. hp,hp2,hp3 : TNamedIndexItem;
  1016. begin
  1017. spdval:=GetSpeedValue(olds);
  1018. if assigned(FHashArray) then
  1019. hp:=FHashArray^[spdval mod hasharraysize]
  1020. else
  1021. hp:=FRoot;
  1022. lasthp:=nil;
  1023. while assigned(hp) do
  1024. begin
  1025. if spdval>hp.SpeedValue then
  1026. begin
  1027. lasthp:=hp;
  1028. hp:=hp.FLeft
  1029. end
  1030. else
  1031. if spdval<hp.SpeedValue then
  1032. begin
  1033. lasthp:=hp;
  1034. hp:=hp.FRight
  1035. end
  1036. else
  1037. begin
  1038. if (hp.FName^=olds) then
  1039. begin
  1040. { Get in hp2 the replacer for the root or hasharr }
  1041. hp2:=hp.FLeft;
  1042. hp3:=hp.FRight;
  1043. if not assigned(hp2) then
  1044. begin
  1045. hp2:=hp.FRight;
  1046. hp3:=hp.FLeft;
  1047. end;
  1048. { remove entry from the tree }
  1049. if assigned(lasthp) then
  1050. begin
  1051. if lasthp.FLeft=hp then
  1052. lasthp.FLeft:=hp2
  1053. else
  1054. lasthp.FRight:=hp2;
  1055. end
  1056. else
  1057. begin
  1058. if assigned(FHashArray) then
  1059. FHashArray^[spdval mod hasharraysize]:=hp2
  1060. else
  1061. FRoot:=hp2;
  1062. end;
  1063. { reinsert the hp3 in the tree from hp2 }
  1064. inserttree(hp3,hp2);
  1065. { reset Node with New values }
  1066. hp.FLeft:=nil;
  1067. hp.FRight:=nil;
  1068. stringdispose(hp.FName);
  1069. hp.FName:=stringdup(newS);
  1070. hp.FSpeedValue:=GetSpeedValue(newS);
  1071. { reinsert }
  1072. if assigned(FHashArray) then
  1073. rename:=insertNode(hp,FHashArray^[hp.SpeedValue mod hasharraysize])
  1074. else
  1075. rename:=insertNode(hp,FRoot);
  1076. exit;
  1077. end
  1078. else
  1079. if olds>hp.FName^ then
  1080. begin
  1081. lasthp:=hp;
  1082. hp:=hp.FLeft
  1083. end
  1084. else
  1085. begin
  1086. lasthp:=hp;
  1087. hp:=hp.FRight;
  1088. end;
  1089. end;
  1090. end;
  1091. end;
  1092. function Tdictionary.search(const s:string):TNamedIndexItem;
  1093. begin
  1094. search:=speedsearch(s,GetSpeedValue(s));
  1095. end;
  1096. function Tdictionary.speedsearch(const s:string;SpeedValue:cardinal):TNamedIndexItem;
  1097. var
  1098. NewNode:TNamedIndexItem;
  1099. begin
  1100. if assigned(FHashArray) then
  1101. NewNode:=FHashArray^[SpeedValue mod hasharraysize]
  1102. else
  1103. NewNode:=FRoot;
  1104. while assigned(NewNode) do
  1105. begin
  1106. if SpeedValue>NewNode.SpeedValue then
  1107. NewNode:=NewNode.FLeft
  1108. else
  1109. if SpeedValue<NewNode.SpeedValue then
  1110. NewNode:=NewNode.FRight
  1111. else
  1112. begin
  1113. if (NewNode.FName^=s) then
  1114. begin
  1115. speedsearch:=NewNode;
  1116. exit;
  1117. end
  1118. else
  1119. if s>NewNode.FName^ then
  1120. NewNode:=NewNode.FLeft
  1121. else
  1122. NewNode:=NewNode.FRight;
  1123. end;
  1124. end;
  1125. speedsearch:=nil;
  1126. end;
  1127. {****************************************************************************
  1128. tsingleList
  1129. ****************************************************************************}
  1130. constructor tsingleList.create;
  1131. begin
  1132. First:=nil;
  1133. last:=nil;
  1134. end;
  1135. procedure tsingleList.reset;
  1136. begin
  1137. First:=nil;
  1138. last:=nil;
  1139. end;
  1140. procedure tsingleList.clear;
  1141. var
  1142. hp,hp2 : TNamedIndexItem;
  1143. begin
  1144. hp:=First;
  1145. while assigned(hp) do
  1146. begin
  1147. hp2:=hp;
  1148. hp:=hp.FListNext;
  1149. hp2.free;
  1150. end;
  1151. First:=nil;
  1152. last:=nil;
  1153. end;
  1154. procedure tsingleList.insert(p:TNamedIndexItem);
  1155. begin
  1156. if not assigned(First) then
  1157. First:=p
  1158. else
  1159. last.FListNext:=p;
  1160. last:=p;
  1161. p.FListNext:=nil;
  1162. end;
  1163. {****************************************************************************
  1164. tindexarray
  1165. ****************************************************************************}
  1166. constructor tindexarray.create(Agrowsize:integer);
  1167. begin
  1168. growsize:=Agrowsize;
  1169. size:=0;
  1170. count:=0;
  1171. data:=nil;
  1172. First:=nil;
  1173. noclear:=false;
  1174. end;
  1175. destructor tindexarray.destroy;
  1176. begin
  1177. if assigned(data) then
  1178. begin
  1179. if not noclear then
  1180. clear;
  1181. freemem(data);
  1182. data:=nil;
  1183. end;
  1184. end;
  1185. function tindexarray.search(nr:integer):TNamedIndexItem;
  1186. begin
  1187. if nr<=count then
  1188. search:=data^[nr]
  1189. else
  1190. search:=nil;
  1191. end;
  1192. procedure tindexarray.clear;
  1193. var
  1194. i : integer;
  1195. begin
  1196. for i:=1 to count do
  1197. if assigned(data^[i]) then
  1198. begin
  1199. data^[i].free;
  1200. data^[i]:=nil;
  1201. end;
  1202. count:=0;
  1203. First:=nil;
  1204. end;
  1205. procedure tindexarray.foreach(proc2call : Tnamedindexcallback);
  1206. var
  1207. i : integer;
  1208. begin
  1209. for i:=1 to count do
  1210. if assigned(data^[i]) then
  1211. proc2call(data^[i]);
  1212. end;
  1213. procedure tindexarray.foreach_static(proc2call : Tnamedindexstaticcallback);
  1214. var
  1215. i : integer;
  1216. begin
  1217. for i:=1 to count do
  1218. if assigned(data^[i]) then
  1219. proc2call(data^[i]);
  1220. end;
  1221. procedure tindexarray.grow(gsize:integer);
  1222. var
  1223. osize : integer;
  1224. begin
  1225. osize:=size;
  1226. inc(size,gsize);
  1227. reallocmem(data,size*4);
  1228. fillchar(data^[osize+1],gsize*4,0);
  1229. end;
  1230. procedure tindexarray.deleteindex(p:TNamedIndexItem);
  1231. var
  1232. i : integer;
  1233. begin
  1234. i:=p.Findexnr;
  1235. { update counter }
  1236. if i=count then
  1237. dec(count);
  1238. { update Linked List }
  1239. while (i>0) do
  1240. begin
  1241. dec(i);
  1242. if (i>0) and assigned(data^[i]) then
  1243. begin
  1244. data^[i].FindexNext:=data^[p.Findexnr].FindexNext;
  1245. break;
  1246. end;
  1247. end;
  1248. if i=0 then
  1249. First:=p.FindexNext;
  1250. data^[p.FIndexnr]:=nil;
  1251. { clear entry }
  1252. p.FIndexnr:=-1;
  1253. p.FIndexNext:=nil;
  1254. end;
  1255. procedure tindexarray.delete(var p:TNamedIndexItem);
  1256. begin
  1257. deleteindex(p);
  1258. p.free;
  1259. p:=nil;
  1260. end;
  1261. procedure tindexarray.insert(p:TNamedIndexItem);
  1262. var
  1263. i : integer;
  1264. begin
  1265. if p.FIndexnr=-1 then
  1266. begin
  1267. inc(count);
  1268. p.FIndexnr:=count;
  1269. end;
  1270. if p.FIndexnr>count then
  1271. count:=p.FIndexnr;
  1272. if count>size then
  1273. grow(((count div growsize)+1)*growsize);
  1274. Assert(not assigned(data^[p.FIndexnr]) or (p=data^[p.FIndexnr]));
  1275. data^[p.FIndexnr]:=p;
  1276. { update Linked List backward }
  1277. i:=p.FIndexnr;
  1278. while (i>0) do
  1279. begin
  1280. dec(i);
  1281. if (i>0) and assigned(data^[i]) then
  1282. begin
  1283. data^[i].FIndexNext:=p;
  1284. break;
  1285. end;
  1286. end;
  1287. if i=0 then
  1288. First:=p;
  1289. { update Linked List forward }
  1290. i:=p.FIndexnr;
  1291. while (i<=count) do
  1292. begin
  1293. inc(i);
  1294. if (i<=count) and assigned(data^[i]) then
  1295. begin
  1296. p.FIndexNext:=data^[i];
  1297. exit;
  1298. end;
  1299. end;
  1300. if i>count then
  1301. p.FIndexNext:=nil;
  1302. end;
  1303. {****************************************************************************
  1304. tdynamicarray
  1305. ****************************************************************************}
  1306. constructor tdynamicarray.create(Ablocksize:integer);
  1307. begin
  1308. FPosn:=0;
  1309. FPosnblock:=nil;
  1310. FFirstblock:=nil;
  1311. FLastblock:=nil;
  1312. Fblocksize:=Ablocksize;
  1313. grow;
  1314. end;
  1315. destructor tdynamicarray.destroy;
  1316. var
  1317. hp : pdynamicblock;
  1318. begin
  1319. while assigned(FFirstblock) do
  1320. begin
  1321. hp:=FFirstblock;
  1322. FFirstblock:=FFirstblock^.Next;
  1323. freemem(hp,blocksize+dynamicblockbasesize);
  1324. end;
  1325. end;
  1326. function tdynamicarray.size:integer;
  1327. begin
  1328. if assigned(FLastblock) then
  1329. size:=FLastblock^.pos+FLastblock^.used
  1330. else
  1331. size:=0;
  1332. end;
  1333. procedure tdynamicarray.grow;
  1334. var
  1335. nblock : pdynamicblock;
  1336. begin
  1337. Getmem(nblock,blocksize+dynamicblockbasesize);
  1338. if not assigned(FFirstblock) then
  1339. begin
  1340. FFirstblock:=nblock;
  1341. FPosnblock:=nblock;
  1342. nblock^.pos:=0;
  1343. end
  1344. else
  1345. begin
  1346. FLastblock^.Next:=nblock;
  1347. nblock^.pos:=FLastblock^.pos+FLastblock^.used;
  1348. end;
  1349. nblock^.used:=0;
  1350. nblock^.Next:=nil;
  1351. fillchar(nblock^.data,blocksize,0);
  1352. FLastblock:=nblock;
  1353. end;
  1354. procedure tdynamicarray.align(i:integer);
  1355. var
  1356. j : integer;
  1357. begin
  1358. j:=(FPosn mod i);
  1359. if j<>0 then
  1360. begin
  1361. j:=i-j;
  1362. if FPosnblock^.used+j>blocksize then
  1363. begin
  1364. dec(j,blocksize-FPosnblock^.used);
  1365. FPosnblock^.used:=blocksize;
  1366. grow;
  1367. FPosnblock:=FLastblock;
  1368. end;
  1369. inc(FPosnblock^.used,j);
  1370. inc(FPosn,j);
  1371. end;
  1372. end;
  1373. procedure tdynamicarray.seek(i:integer);
  1374. begin
  1375. if (i<FPosnblock^.pos) or (i>=FPosnblock^.pos+blocksize) then
  1376. begin
  1377. { set FPosnblock correct if the size is bigger then
  1378. the current block }
  1379. if FPosnblock^.pos>i then
  1380. FPosnblock:=FFirstblock;
  1381. while assigned(FPosnblock) do
  1382. begin
  1383. if FPosnblock^.pos+blocksize>i then
  1384. break;
  1385. FPosnblock:=FPosnblock^.Next;
  1386. end;
  1387. { not found ? then increase blocks }
  1388. if not assigned(FPosnblock) then
  1389. begin
  1390. { the current FLastblock is now also fully used }
  1391. FLastblock^.used:=blocksize;
  1392. repeat
  1393. grow;
  1394. FPosnblock:=FLastblock;
  1395. until FPosnblock^.pos+blocksize>=i;
  1396. end;
  1397. end;
  1398. FPosn:=i;
  1399. if FPosn mod blocksize>FPosnblock^.used then
  1400. FPosnblock^.used:=FPosn mod blocksize;
  1401. end;
  1402. procedure tdynamicarray.write(const d;len:integer);
  1403. var
  1404. p : pchar;
  1405. i,j : integer;
  1406. begin
  1407. p:=pchar(@d);
  1408. while (len>0) do
  1409. begin
  1410. i:=FPosn mod blocksize;
  1411. if i+len>=blocksize then
  1412. begin
  1413. j:=blocksize-i;
  1414. move(p^,FPosnblock^.data[i],j);
  1415. inc(p,j);
  1416. inc(FPosn,j);
  1417. dec(len,j);
  1418. FPosnblock^.used:=blocksize;
  1419. if assigned(FPosnblock^.Next) then
  1420. FPosnblock:=FPosnblock^.Next
  1421. else
  1422. begin
  1423. grow;
  1424. FPosnblock:=FLastblock;
  1425. end;
  1426. end
  1427. else
  1428. begin
  1429. move(p^,FPosnblock^.data[i],len);
  1430. inc(p,len);
  1431. inc(FPosn,len);
  1432. i:=FPosn mod blocksize;
  1433. if i>FPosnblock^.used then
  1434. FPosnblock^.used:=i;
  1435. len:=0;
  1436. end;
  1437. end;
  1438. end;
  1439. procedure tdynamicarray.writestr(const s:string);
  1440. begin
  1441. write(s[1],length(s));
  1442. end;
  1443. function tdynamicarray.read(var d;len:integer):integer;
  1444. var
  1445. p : pchar;
  1446. i,j,res : integer;
  1447. begin
  1448. res:=0;
  1449. p:=pchar(@d);
  1450. while (len>0) do
  1451. begin
  1452. i:=FPosn mod blocksize;
  1453. if i+len>=FPosnblock^.used then
  1454. begin
  1455. j:=FPosnblock^.used-i;
  1456. move(FPosnblock^.data[i],p^,j);
  1457. inc(p,j);
  1458. inc(FPosn,j);
  1459. inc(res,j);
  1460. dec(len,j);
  1461. if assigned(FPosnblock^.Next) then
  1462. FPosnblock:=FPosnblock^.Next
  1463. else
  1464. break;
  1465. end
  1466. else
  1467. begin
  1468. move(FPosnblock^.data[i],p^,len);
  1469. inc(p,len);
  1470. inc(FPosn,len);
  1471. inc(res,len);
  1472. len:=0;
  1473. end;
  1474. end;
  1475. read:=res;
  1476. end;
  1477. procedure tdynamicarray.readstream(f:TCStream;maxlen:longint);
  1478. var
  1479. i,left : integer;
  1480. begin
  1481. if maxlen=-1 then
  1482. maxlen:=maxlongint;
  1483. repeat
  1484. left:=blocksize-FPosnblock^.used;
  1485. if left>maxlen then
  1486. left:=maxlen;
  1487. i:=f.Read(FPosnblock^.data[FPosnblock^.used],left);
  1488. dec(maxlen,i);
  1489. inc(FPosnblock^.used,i);
  1490. if FPosnblock^.used=blocksize then
  1491. begin
  1492. if assigned(FPosnblock^.Next) then
  1493. FPosnblock:=FPosnblock^.Next
  1494. else
  1495. begin
  1496. grow;
  1497. FPosnblock:=FLastblock;
  1498. end;
  1499. end;
  1500. until (i<left) or (maxlen=0);
  1501. end;
  1502. procedure tdynamicarray.writestream(f:TCStream);
  1503. var
  1504. hp : pdynamicblock;
  1505. begin
  1506. hp:=FFirstblock;
  1507. while assigned(hp) do
  1508. begin
  1509. f.Write(hp^.data,hp^.used);
  1510. hp:=hp^.Next;
  1511. end;
  1512. end;
  1513. end.
  1514. {
  1515. $Log$
  1516. Revision 1.9 2001-11-18 18:43:13 peter
  1517. * overloading supported in child classes
  1518. * fixed parsing of classes with private and virtual and overloaded
  1519. so it is compatible with delphi
  1520. Revision 1.8 2001/11/05 14:16:25 jonas
  1521. * reduced memory usage by about 10% and increased speed by about 15%
  1522. Revision 1.7 2001/05/04 19:50:04 peter
  1523. * fixed dynamicarray.seek() with pos equals multiple of $10000
  1524. Revision 1.6 2001/04/13 01:22:06 peter
  1525. * symtable change to classes
  1526. * range check generation and errors fixed, make cycle DEBUG=1 works
  1527. * memory leaks fixed
  1528. Revision 1.5 2001/03/13 18:45:06 peter
  1529. * fixed some memory leaks
  1530. Revision 1.4 2001/03/05 21:40:01 peter
  1531. * fixed tdynamicarray.readstream
  1532. Revision 1.3 2000/12/29 21:57:27 peter
  1533. * 'classified' tdictionary, but leave it within an define
  1534. Revision 1.2 2000/12/25 00:07:25 peter
  1535. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1536. tlinkedlist objects)
  1537. Revision 1.1 2000/12/24 12:25:31 peter
  1538. + cstreams unit
  1539. * dynamicarray object to class
  1540. }