cobjects.pas 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. This module provides some basic objects
  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 cobjects;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. cutils;
  23. const
  24. { the real size will be [-hasharray..hasharray] ! }
  25. hasharraysize = 2047;
  26. type
  27. pfileposinfo = ^tfileposinfo;
  28. tfileposinfo = record
  29. line : longint;
  30. column : word;
  31. fileindex : word;
  32. end;
  33. pmemdebug = ^tmemdebug;
  34. tmemdebug = object
  35. constructor init(const s:string);
  36. destructor done;
  37. procedure show;
  38. private
  39. startmem : longint;
  40. infostr : string[40];
  41. end;
  42. plinkedlist_item = ^tlinkedlist_item;
  43. tlinkedlist_item = object
  44. next,previous : plinkedlist_item;
  45. { does nothing }
  46. constructor init;
  47. destructor done;virtual;
  48. function getcopy:plinkedlist_item;virtual;
  49. end;
  50. pstring_item = ^tstring_item;
  51. tstring_item = object(tlinkedlist_item)
  52. str : pstring;
  53. constructor init(const s : string);
  54. destructor done;virtual;
  55. end;
  56. { this implements a double linked list }
  57. plinkedlist = ^tlinkedlist;
  58. tlinkedlist = object
  59. first,last : plinkedlist_item;
  60. constructor init;
  61. destructor done;
  62. { disposes the items of the list }
  63. procedure clear;
  64. { concats a new item at the end }
  65. procedure concat(p : plinkedlist_item);
  66. { inserts a new item at the begin }
  67. procedure insert(p : plinkedlist_item);
  68. { inserts another list at the begin and make this list empty }
  69. procedure insertlist(p : plinkedlist);
  70. { concats another list at the end and make this list empty }
  71. procedure concatlist(p : plinkedlist);
  72. procedure concatlistcopy(p : plinkedlist);
  73. { removes p from the list (p isn't disposed) }
  74. { it's not tested if p is in the list ! }
  75. procedure remove(p : plinkedlist_item);
  76. { is the linkedlist empty ? }
  77. function empty:boolean;
  78. { items in the list }
  79. function count:longint;
  80. end;
  81. { some help data types }
  82. pstringqueueitem = ^tstringqueueitem;
  83. tstringqueueitem = object
  84. data : pstring;
  85. next : pstringqueueitem;
  86. end;
  87. { String Queue}
  88. PStringQueue=^TStringQueue;
  89. TStringQueue=object
  90. first,last : PStringqueueItem;
  91. constructor Init;
  92. destructor Done;
  93. function Empty:boolean;
  94. function Get:string;
  95. function Find(const s:string):PStringqueueItem;
  96. function Delete(const s:string):boolean;
  97. procedure Insert(const s:string);
  98. procedure Concat(const s:string);
  99. procedure Clear;
  100. end;
  101. { containeritem }
  102. pcontaineritem = ^tcontaineritem;
  103. tcontaineritem = object
  104. next : pcontaineritem;
  105. constructor init;
  106. destructor done;virtual;
  107. end;
  108. { container }
  109. pcontainer = ^tcontainer;
  110. tcontainer = object
  111. root,
  112. last : pcontaineritem;
  113. constructor init;
  114. destructor done;
  115. { true when the container is empty }
  116. function empty:boolean;
  117. { amount of strings in the container }
  118. function count:longint;
  119. { inserts a string }
  120. procedure insert(item:pcontaineritem);
  121. { gets a string }
  122. function get:pcontaineritem;
  123. { deletes all items }
  124. procedure clear;
  125. end;
  126. { containeritem }
  127. pstringcontaineritem = ^tstringcontaineritem;
  128. tstringcontaineritem = object(tcontaineritem)
  129. data : pstring;
  130. file_info : tfileposinfo;
  131. constructor init(const s:string);
  132. constructor Init_TokenInfo(const s:string;const pos:tfileposinfo);
  133. destructor done;virtual;
  134. end;
  135. { string container }
  136. pstringcontainer = ^tstringcontainer;
  137. tstringcontainer = object(tcontainer)
  138. doubles : boolean; { if this is set to true, doubles are allowed }
  139. constructor init;
  140. constructor init_no_double;
  141. procedure insert(const s : string);
  142. procedure insert_with_tokeninfo(const s : string;const file_info : tfileposinfo);
  143. { gets a string }
  144. function get : string;
  145. function get_with_tokeninfo(var file_info : tfileposinfo) : string;
  146. { true if string is in the container }
  147. function find(const s:string):boolean;
  148. end;
  149. { namedindexobect for use with dictionary and indexarray }
  150. Pnamedindexobject=^Tnamedindexobject;
  151. Tnamedindexobject=object
  152. { indexarray }
  153. indexnr : longint;
  154. indexnext : Pnamedindexobject;
  155. { dictionary }
  156. _name : Pstring;
  157. _valuename : Pstring; { uppercase name }
  158. left,right : Pnamedindexobject;
  159. speedvalue : longint;
  160. { singlelist }
  161. listnext : Pnamedindexobject;
  162. constructor init;
  163. constructor initname(const n:string);
  164. destructor done;virtual;
  165. procedure setname(const n:string);virtual;
  166. function name:string;virtual;
  167. end;
  168. Pdictionaryhasharray=^Tdictionaryhasharray;
  169. Tdictionaryhasharray=array[-hasharraysize..hasharraysize] of Pnamedindexobject;
  170. Tnamedindexcallback = procedure(p:Pnamedindexobject);
  171. Pdictionary=^Tdictionary;
  172. Tdictionary=object
  173. noclear : boolean;
  174. replace_existing : boolean;
  175. constructor init;
  176. destructor done;virtual;
  177. procedure usehash;
  178. procedure clear;
  179. function delete(const s:string):Pnamedindexobject;
  180. function empty:boolean;
  181. procedure foreach(proc2call:Tnamedindexcallback);
  182. function insert(obj:Pnamedindexobject):Pnamedindexobject;
  183. function rename(const olds,news : string):Pnamedindexobject;
  184. function search(const s:string):Pnamedindexobject;
  185. function speedsearch(const s:string;speedvalue:longint):Pnamedindexobject;
  186. private
  187. root : Pnamedindexobject;
  188. hasharray : Pdictionaryhasharray;
  189. procedure cleartree(obj:Pnamedindexobject);
  190. function insertnode(newnode:Pnamedindexobject;var currnode:Pnamedindexobject):Pnamedindexobject;
  191. procedure inserttree(currtree,currroot:Pnamedindexobject);
  192. end;
  193. psinglelist=^tsinglelist;
  194. tsinglelist=object
  195. first,
  196. last : Pnamedindexobject;
  197. constructor init;
  198. destructor done;
  199. procedure reset;
  200. procedure clear;
  201. procedure insert(p:Pnamedindexobject);
  202. end;
  203. const
  204. dynamicblockbasesize = 12;
  205. type
  206. pdynamicblock = ^tdynamicblock;
  207. tdynamicblock = record
  208. pos,
  209. used : longint;
  210. next : pdynamicblock;
  211. data : array[0..high(longint)-20] of byte;
  212. end;
  213. pdynamicarray = ^tdynamicarray;
  214. tdynamicarray = object
  215. blocksize : longint;
  216. firstblock,
  217. lastblock : pdynamicblock;
  218. constructor init(Ablocksize:longint);
  219. destructor done;
  220. function size:longint;
  221. procedure align(i:longint);
  222. procedure seek(i:longint);
  223. procedure write(const d;len:longint);
  224. function read(var d;len:longint):longint;
  225. procedure blockwrite(var f:file);
  226. private
  227. posn : longint;
  228. posnblock : pdynamicblock;
  229. procedure grow;
  230. end;
  231. tindexobjectarray=array[1..16000] of Pnamedindexobject;
  232. Pnamedindexobjectarray=^tindexobjectarray;
  233. pindexarray=^tindexarray;
  234. tindexarray=object
  235. noclear : boolean;
  236. first : Pnamedindexobject;
  237. count : longint;
  238. constructor init(Agrowsize:longint);
  239. destructor done;
  240. procedure clear;
  241. procedure foreach(proc2call : Tnamedindexcallback);
  242. procedure deleteindex(p:Pnamedindexobject);
  243. procedure delete(var p:Pnamedindexobject);
  244. procedure insert(p:Pnamedindexobject);
  245. function search(nr:longint):Pnamedindexobject;
  246. private
  247. growsize,
  248. size : longint;
  249. data : Pnamedindexobjectarray;
  250. procedure grow(gsize:longint);
  251. end;
  252. {$ifdef fixLeaksOnError}
  253. PStackItem = ^TStackItem;
  254. TStackItem = record
  255. next: PStackItem;
  256. data: pointer;
  257. end;
  258. PStack = ^TStack;
  259. TStack = object
  260. constructor init;
  261. destructor done;
  262. procedure push(p: pointer);
  263. function pop: pointer;
  264. function top: pointer;
  265. function isEmpty: boolean;
  266. private
  267. head: PStackItem;
  268. end;
  269. {$endif fixLeaksOnError}
  270. implementation
  271. {*****************************************************************************
  272. Memory debug
  273. *****************************************************************************}
  274. constructor tmemdebug.init(const s:string);
  275. begin
  276. infostr:=s;
  277. {$ifdef Delphi}
  278. startmem:=0;
  279. {$else}
  280. startmem:=memavail;
  281. {$endif Delphi}
  282. end;
  283. procedure tmemdebug.show;
  284. {$ifndef Delphi}
  285. var
  286. l : longint;
  287. {$endif}
  288. begin
  289. {$ifndef Delphi}
  290. write('memory [',infostr,'] ');
  291. l:=memavail;
  292. if l>startmem then
  293. writeln(l-startmem,' released')
  294. else
  295. writeln(startmem-l,' allocated');
  296. {$endif Delphi}
  297. end;
  298. destructor tmemdebug.done;
  299. begin
  300. show;
  301. end;
  302. {*****************************************************************************
  303. Stack
  304. *****************************************************************************}
  305. {$ifdef fixLeaksOnError}
  306. constructor TStack.init;
  307. begin
  308. head := nil;
  309. end;
  310. procedure TStack.push(p: pointer);
  311. var s: PStackItem;
  312. begin
  313. new(s);
  314. s^.data := p;
  315. s^.next := head;
  316. head := s;
  317. end;
  318. function TStack.pop: pointer;
  319. var s: PStackItem;
  320. begin
  321. pop := top;
  322. if assigned(head) then
  323. begin
  324. s := head^.next;
  325. dispose(head);
  326. head := s;
  327. end
  328. end;
  329. function TStack.top: pointer;
  330. begin
  331. if not isEmpty then
  332. top := head^.data
  333. else top := NIL;
  334. end;
  335. function TStack.isEmpty: boolean;
  336. begin
  337. isEmpty := head = nil;
  338. end;
  339. destructor TStack.done;
  340. var temp: PStackItem;
  341. begin
  342. while head <> nil do
  343. begin
  344. temp := head^.next;
  345. dispose(head);
  346. head := temp;
  347. end;
  348. end;
  349. {$endif fixLeaksOnError}
  350. {****************************************************************************
  351. TStringQueue
  352. ****************************************************************************}
  353. constructor TStringQueue.Init;
  354. begin
  355. first:=nil;
  356. last:=nil;
  357. end;
  358. function TStringQueue.Empty:boolean;
  359. begin
  360. Empty:=(first=nil);
  361. end;
  362. function TStringQueue.Get:string;
  363. var
  364. newnode : pstringqueueitem;
  365. begin
  366. if first=nil then
  367. begin
  368. Get:='';
  369. exit;
  370. end;
  371. Get:=first^.data^;
  372. stringdispose(first^.data);
  373. newnode:=first;
  374. first:=first^.next;
  375. dispose(newnode);
  376. end;
  377. function TStringQueue.Find(const s:string):PStringqueueItem;
  378. var
  379. p : PStringqueueItem;
  380. begin
  381. p:=first;
  382. while assigned(p) do
  383. begin
  384. if p^.data^=s then
  385. break;
  386. p:=p^.next;
  387. end;
  388. Find:=p;
  389. end;
  390. function TStringQueue.Delete(const s:string):boolean;
  391. var
  392. prev,p : PStringqueueItem;
  393. begin
  394. Delete:=false;
  395. prev:=nil;
  396. p:=first;
  397. while assigned(p) do
  398. begin
  399. if p^.data^=s then
  400. begin
  401. if p=last then
  402. last:=prev;
  403. if assigned(prev) then
  404. prev^.next:=p^.next
  405. else
  406. first:=p^.next;
  407. dispose(p);
  408. Delete:=true;
  409. exit;
  410. end;
  411. prev:=p;
  412. p:=p^.next;
  413. end;
  414. end;
  415. procedure TStringQueue.Insert(const s:string);
  416. var
  417. newnode : pstringqueueitem;
  418. begin
  419. new(newnode);
  420. newnode^.next:=first;
  421. newnode^.data:=stringdup(s);
  422. first:=newnode;
  423. if last=nil then
  424. last:=newnode;
  425. end;
  426. procedure TStringQueue.Concat(const s:string);
  427. var
  428. newnode : pstringqueueitem;
  429. begin
  430. new(newnode);
  431. newnode^.next:=nil;
  432. newnode^.data:=stringdup(s);
  433. if first=nil then
  434. first:=newnode
  435. else
  436. last^.next:=newnode;
  437. last:=newnode;
  438. end;
  439. procedure TStringQueue.Clear;
  440. var
  441. newnode : pstringqueueitem;
  442. begin
  443. while (first<>nil) do
  444. begin
  445. newnode:=first;
  446. stringdispose(first^.data);
  447. first:=first^.next;
  448. dispose(newnode);
  449. end;
  450. last:=nil;
  451. end;
  452. destructor TStringQueue.Done;
  453. begin
  454. Clear;
  455. end;
  456. {****************************************************************************
  457. TContainerItem
  458. ****************************************************************************}
  459. constructor TContainerItem.Init;
  460. begin
  461. end;
  462. destructor TContainerItem.Done;
  463. begin
  464. end;
  465. {****************************************************************************
  466. TStringContainerItem
  467. ****************************************************************************}
  468. constructor TStringContainerItem.Init(const s:string);
  469. begin
  470. inherited Init;
  471. data:=stringdup(s);
  472. file_info.fileindex:=0;
  473. file_info.line:=0;
  474. file_info.column:=0;
  475. end;
  476. constructor TStringContainerItem.Init_TokenInfo(const s:string;const pos:tfileposinfo);
  477. begin
  478. inherited Init;
  479. data:=stringdup(s);
  480. file_info:=pos;
  481. end;
  482. destructor TStringContainerItem.Done;
  483. begin
  484. stringdispose(data);
  485. end;
  486. {****************************************************************************
  487. TCONTAINER
  488. ****************************************************************************}
  489. constructor tcontainer.init;
  490. begin
  491. root:=nil;
  492. last:=nil;
  493. end;
  494. destructor tcontainer.done;
  495. begin
  496. clear;
  497. end;
  498. function tcontainer.empty:boolean;
  499. begin
  500. empty:=(root=nil);
  501. end;
  502. function tcontainer.count:longint;
  503. var
  504. i : longint;
  505. p : pcontaineritem;
  506. begin
  507. i:=0;
  508. p:=root;
  509. while assigned(p) do
  510. begin
  511. p:=p^.next;
  512. inc(i);
  513. end;
  514. count:=i;
  515. end;
  516. procedure tcontainer.insert(item:pcontaineritem);
  517. begin
  518. item^.next:=nil;
  519. if root=nil then
  520. root:=item
  521. else
  522. last^.next:=item;
  523. last:=item;
  524. end;
  525. procedure tcontainer.clear;
  526. var
  527. newnode : pcontaineritem;
  528. begin
  529. newnode:=root;
  530. while assigned(newnode) do
  531. begin
  532. root:=newnode^.next;
  533. dispose(newnode,done);
  534. newnode:=root;
  535. end;
  536. last:=nil;
  537. root:=nil;
  538. end;
  539. function tcontainer.get:pcontaineritem;
  540. begin
  541. if root=nil then
  542. get:=nil
  543. else
  544. begin
  545. get:=root;
  546. root:=root^.next;
  547. end;
  548. end;
  549. {****************************************************************************
  550. TSTRINGCONTAINER
  551. ****************************************************************************}
  552. constructor tstringcontainer.init;
  553. begin
  554. inherited init;
  555. doubles:=true;
  556. end;
  557. constructor tstringcontainer.init_no_double;
  558. begin
  559. inherited init;
  560. doubles:=false;
  561. end;
  562. procedure tstringcontainer.insert(const s : string);
  563. var
  564. newnode : pstringcontaineritem;
  565. begin
  566. if (s='') or
  567. ((not doubles) and find(s)) then
  568. exit;
  569. new(newnode,init(s));
  570. inherited insert(newnode);
  571. end;
  572. procedure tstringcontainer.insert_with_tokeninfo(const s : string; const file_info : tfileposinfo);
  573. var
  574. newnode : pstringcontaineritem;
  575. begin
  576. if (not doubles) and find(s) then
  577. exit;
  578. new(newnode,init_tokeninfo(s,file_info));
  579. inherited insert(newnode);
  580. end;
  581. function tstringcontainer.get : string;
  582. var
  583. p : pstringcontaineritem;
  584. begin
  585. p:=pstringcontaineritem(inherited get);
  586. if p=nil then
  587. get:=''
  588. else
  589. begin
  590. get:=p^.data^;
  591. dispose(p,done);
  592. end;
  593. end;
  594. function tstringcontainer.get_with_tokeninfo(var file_info : tfileposinfo) : string;
  595. var
  596. p : pstringcontaineritem;
  597. begin
  598. p:=pstringcontaineritem(inherited get);
  599. if p=nil then
  600. begin
  601. get_with_tokeninfo:='';
  602. file_info.fileindex:=0;
  603. file_info.line:=0;
  604. file_info.column:=0;
  605. end
  606. else
  607. begin
  608. get_with_tokeninfo:=p^.data^;
  609. file_info:=p^.file_info;
  610. dispose(p,done);
  611. end;
  612. end;
  613. function tstringcontainer.find(const s:string):boolean;
  614. var
  615. newnode : pstringcontaineritem;
  616. begin
  617. find:=false;
  618. newnode:=pstringcontaineritem(root);
  619. while assigned(newnode) do
  620. begin
  621. if newnode^.data^=s then
  622. begin
  623. find:=true;
  624. exit;
  625. end;
  626. newnode:=pstringcontaineritem(newnode^.next);
  627. end;
  628. end;
  629. {****************************************************************************
  630. TLINKEDLIST_ITEM
  631. ****************************************************************************}
  632. constructor tlinkedlist_item.init;
  633. begin
  634. previous:=nil;
  635. next:=nil;
  636. end;
  637. destructor tlinkedlist_item.done;
  638. begin
  639. end;
  640. function tlinkedlist_item.getcopy:plinkedlist_item;
  641. var
  642. l : longint;
  643. p : plinkedlist_item;
  644. begin
  645. l:=sizeof(self);
  646. getmem(p,l);
  647. move(self,p^,l);
  648. getcopy:=p;
  649. end;
  650. {****************************************************************************
  651. TSTRING_ITEM
  652. ****************************************************************************}
  653. constructor tstring_item.init(const s : string);
  654. begin
  655. str:=stringdup(s);
  656. end;
  657. destructor tstring_item.done;
  658. begin
  659. stringdispose(str);
  660. inherited done;
  661. end;
  662. {****************************************************************************
  663. TLINKEDLIST
  664. ****************************************************************************}
  665. constructor tlinkedlist.init;
  666. begin
  667. first:=nil;
  668. last:=nil;
  669. end;
  670. destructor tlinkedlist.done;
  671. begin
  672. clear;
  673. end;
  674. procedure tlinkedlist.clear;
  675. var
  676. newnode : plinkedlist_item;
  677. begin
  678. newnode:=first;
  679. while assigned(newnode) do
  680. begin
  681. first:=newnode^.next;
  682. dispose(newnode,done);
  683. newnode:=first;
  684. end;
  685. end;
  686. procedure tlinkedlist.insertlist(p : plinkedlist);
  687. begin
  688. { empty list ? }
  689. if not(assigned(p^.first)) then
  690. exit;
  691. p^.last^.next:=first;
  692. { we have a double linked list }
  693. if assigned(first) then
  694. first^.previous:=p^.last;
  695. first:=p^.first;
  696. if not(assigned(last)) then
  697. last:=p^.last;
  698. { p becomes empty }
  699. p^.first:=nil;
  700. p^.last:=nil;
  701. end;
  702. procedure tlinkedlist.concat(p : plinkedlist_item);
  703. begin
  704. if not(assigned(first)) then
  705. begin
  706. first:=p;
  707. p^.previous:=nil;
  708. p^.next:=nil;
  709. end
  710. else
  711. begin
  712. last^.next:=p;
  713. p^.previous:=last;
  714. p^.next:=nil;
  715. end;
  716. last:=p;
  717. end;
  718. procedure tlinkedlist.insert(p : plinkedlist_item);
  719. begin
  720. if not(assigned(first)) then
  721. begin
  722. last:=p;
  723. p^.previous:=nil;
  724. p^.next:=nil;
  725. end
  726. else
  727. begin
  728. first^.previous:=p;
  729. p^.previous:=nil;
  730. p^.next:=first;
  731. end;
  732. first:=p;
  733. end;
  734. procedure tlinkedlist.remove(p : plinkedlist_item);
  735. begin
  736. if not(assigned(p)) then
  737. exit;
  738. if (first=p) and (last=p) then
  739. begin
  740. first:=nil;
  741. last:=nil;
  742. end
  743. else if first=p then
  744. begin
  745. first:=p^.next;
  746. if assigned(first) then
  747. first^.previous:=nil;
  748. end
  749. else if last=p then
  750. begin
  751. last:=last^.previous;
  752. if assigned(last) then
  753. last^.next:=nil;
  754. end
  755. else
  756. begin
  757. p^.previous^.next:=p^.next;
  758. p^.next^.previous:=p^.previous;
  759. end;
  760. p^.next:=nil;
  761. p^.previous:=nil;
  762. end;
  763. procedure tlinkedlist.concatlist(p : plinkedlist);
  764. begin
  765. if not(assigned(p^.first)) then
  766. exit;
  767. if not(assigned(first)) then
  768. first:=p^.first
  769. else
  770. begin
  771. last^.next:=p^.first;
  772. p^.first^.previous:=last;
  773. end;
  774. last:=p^.last;
  775. { make p empty }
  776. p^.last:=nil;
  777. p^.first:=nil;
  778. end;
  779. procedure tlinkedlist.concatlistcopy(p : plinkedlist);
  780. var
  781. newnode,newnode2 : plinkedlist_item;
  782. begin
  783. newnode:=p^.first;
  784. while assigned(newnode) do
  785. begin
  786. newnode2:=newnode^.getcopy;
  787. if assigned(newnode2) then
  788. begin
  789. if not(assigned(first)) then
  790. begin
  791. first:=newnode2;
  792. newnode2^.previous:=nil;
  793. newnode2^.next:=nil;
  794. end
  795. else
  796. begin
  797. last^.next:=newnode2;
  798. newnode2^.previous:=last;
  799. newnode2^.next:=nil;
  800. end;
  801. last:=newnode2;
  802. end;
  803. newnode:=newnode^.next;
  804. end;
  805. end;
  806. function tlinkedlist.empty:boolean;
  807. begin
  808. empty:=(first=nil);
  809. end;
  810. function tlinkedlist.count:longint;
  811. var
  812. i : longint;
  813. hp : plinkedlist_item;
  814. begin
  815. hp:=first;
  816. i:=0;
  817. while assigned(hp) do
  818. begin
  819. inc(i);
  820. hp:=hp^.next;
  821. end;
  822. count:=i;
  823. end;
  824. {****************************************************************************
  825. Tnamedindexobject
  826. ****************************************************************************}
  827. constructor Tnamedindexobject.init;
  828. begin
  829. { index }
  830. indexnr:=-1;
  831. indexnext:=nil;
  832. { dictionary }
  833. left:=nil;
  834. right:=nil;
  835. _name:=nil;
  836. speedvalue:=-1;
  837. { list }
  838. listnext:=nil;
  839. end;
  840. constructor Tnamedindexobject.initname(const n:string);
  841. begin
  842. { index }
  843. indexnr:=-1;
  844. indexnext:=nil;
  845. { dictionary }
  846. left:=nil;
  847. right:=nil;
  848. speedvalue:=-1;
  849. _name:=stringdup(n);
  850. { list }
  851. listnext:=nil;
  852. end;
  853. destructor Tnamedindexobject.done;
  854. begin
  855. stringdispose(_name);
  856. end;
  857. procedure Tnamedindexobject.setname(const n:string);
  858. begin
  859. if speedvalue=-1 then
  860. begin
  861. if assigned(_name) then
  862. stringdispose(_name);
  863. _name:=stringdup(n);
  864. end;
  865. end;
  866. function Tnamedindexobject.name:string;
  867. begin
  868. if assigned(_name) then
  869. name:=_name^
  870. else
  871. name:='';
  872. end;
  873. {****************************************************************************
  874. TDICTIONARY
  875. ****************************************************************************}
  876. constructor Tdictionary.init;
  877. begin
  878. root:=nil;
  879. hasharray:=nil;
  880. noclear:=false;
  881. replace_existing:=false;
  882. end;
  883. procedure Tdictionary.usehash;
  884. begin
  885. if not(assigned(root)) and
  886. not(assigned(hasharray)) then
  887. begin
  888. new(hasharray);
  889. fillchar(hasharray^,sizeof(hasharray^),0);
  890. end;
  891. end;
  892. destructor Tdictionary.done;
  893. begin
  894. if not noclear then
  895. clear;
  896. if assigned(hasharray) then
  897. dispose(hasharray);
  898. end;
  899. procedure Tdictionary.cleartree(obj:Pnamedindexobject);
  900. begin
  901. if assigned(obj^.left) then
  902. cleartree(obj^.left);
  903. if assigned(obj^.right) then
  904. cleartree(obj^.right);
  905. dispose(obj,done);
  906. obj:=nil;
  907. end;
  908. procedure Tdictionary.clear;
  909. var
  910. w : longint;
  911. begin
  912. if assigned(root) then
  913. cleartree(root);
  914. if assigned(hasharray) then
  915. for w:=-hasharraysize to hasharraysize do
  916. if assigned(hasharray^[w]) then
  917. cleartree(hasharray^[w]);
  918. end;
  919. function Tdictionary.delete(const s:string):Pnamedindexobject;
  920. var p,speedvalue:longint;
  921. n:Pnamedindexobject;
  922. procedure insert_right_bottom(var root,Atree:Pnamedindexobject);
  923. begin
  924. while root^.right<>nil do
  925. root:=root^.right;
  926. root^.right:=Atree;
  927. end;
  928. function delete_from_tree(root:Pnamedindexobject):Pnamedindexobject;
  929. type leftright=(left,right);
  930. var lr:leftright;
  931. oldroot:Pnamedindexobject;
  932. begin
  933. oldroot:=nil;
  934. while (root<>nil) and (root^.speedvalue<>speedvalue) do
  935. begin
  936. oldroot:=root;
  937. if speedvalue<root^.speedvalue then
  938. begin
  939. root:=root^.right;
  940. lr:=right;
  941. end
  942. else
  943. begin
  944. root:=root^.left;
  945. lr:=left;
  946. end;
  947. end;
  948. while (root<>nil) and (root^._name^<>s) do
  949. begin
  950. oldroot:=root;
  951. if s<root^._name^ then
  952. begin
  953. root:=root^.right;
  954. lr:=right;
  955. end
  956. else
  957. begin
  958. root:=root^.left;
  959. lr:=left;
  960. end;
  961. end;
  962. if root^.left<>nil then
  963. begin
  964. {Now the node pointing to root must point to the left
  965. subtree of root. The right subtree of root must be
  966. connected to the right bottom of the left subtree.}
  967. if lr=left then
  968. oldroot^.left:=root^.left
  969. else
  970. oldroot^.right:=root^.left;
  971. if root^.right<>nil then
  972. insert_right_bottom(root^.left,root^.right);
  973. end
  974. else
  975. {There is no left subtree. So we can just replace the node to
  976. delete with the right subtree.}
  977. if lr=left then
  978. oldroot^.left:=root^.right
  979. else
  980. oldroot^.right:=root^.right;
  981. delete_from_tree:=root;
  982. end;
  983. begin
  984. speedvalue:=getspeedvalue(s);
  985. n:=root;
  986. if assigned(hasharray) then
  987. begin
  988. {First, check if the node to delete directly located under
  989. the hasharray.}
  990. p:=speedvalue mod hasharraysize;
  991. n:=hasharray^[p];
  992. if (n<>nil) and (n^.speedvalue=speedvalue) and
  993. (n^._name^=s) then
  994. begin
  995. {The node to delete is directly located under the
  996. hasharray. Make the hasharray point to the left
  997. subtree of the node and place the right subtree on
  998. the right-bottom of the left subtree.}
  999. if n^.left<>nil then
  1000. begin
  1001. hasharray^[p]:=n^.left;
  1002. if n^.right<>nil then
  1003. insert_right_bottom(n^.left,n^.right);
  1004. end
  1005. else
  1006. hasharray^[p]:=n^.right;
  1007. delete:=n;
  1008. exit;
  1009. end;
  1010. end
  1011. else
  1012. begin
  1013. {First check if the node to delete is the root.}
  1014. if (root<>nil) and (n^.speedvalue=speedvalue)
  1015. and (n^._name^=s) then
  1016. begin
  1017. if n^.left<>nil then
  1018. begin
  1019. root:=n^.left;
  1020. if n^.right<>nil then
  1021. insert_right_bottom(n^.left,n^.right);
  1022. end
  1023. else
  1024. root:=n^.right;
  1025. delete:=n;
  1026. exit;
  1027. end;
  1028. end;
  1029. delete:=delete_from_tree(n);
  1030. end;
  1031. function Tdictionary.empty:boolean;
  1032. var
  1033. w : longint;
  1034. begin
  1035. if assigned(hasharray) then
  1036. begin
  1037. empty:=false;
  1038. for w:=-hasharraysize to hasharraysize do
  1039. if assigned(hasharray^[w]) then
  1040. exit;
  1041. empty:=true;
  1042. end
  1043. else
  1044. empty:=(root=nil);
  1045. end;
  1046. procedure Tdictionary.foreach(proc2call:Tnamedindexcallback);
  1047. procedure a(p:Pnamedindexobject);
  1048. begin
  1049. proc2call(p);
  1050. if assigned(p^.left) then
  1051. a(p^.left);
  1052. if assigned(p^.right) then
  1053. a(p^.right);
  1054. end;
  1055. var
  1056. i : longint;
  1057. begin
  1058. if assigned(hasharray) then
  1059. begin
  1060. for i:=-hasharraysize to hasharraysize do
  1061. if assigned(hasharray^[i]) then
  1062. a(hasharray^[i]);
  1063. end
  1064. else
  1065. if assigned(root) then
  1066. a(root);
  1067. end;
  1068. function Tdictionary.insert(obj:Pnamedindexobject):Pnamedindexobject;
  1069. begin
  1070. obj^.speedvalue:=getspeedvalue(obj^._name^);
  1071. if assigned(hasharray) then
  1072. insert:=insertnode(obj,hasharray^[obj^.speedvalue mod hasharraysize])
  1073. else
  1074. insert:=insertnode(obj,root);
  1075. end;
  1076. function tdictionary.insertnode(newnode:Pnamedindexobject;var currnode:Pnamedindexobject):Pnamedindexobject;
  1077. begin
  1078. if currnode=nil then
  1079. begin
  1080. currnode:=newnode;
  1081. insertnode:=newnode;
  1082. end
  1083. { first check speedvalue, to allow a fast insert }
  1084. else
  1085. if currnode^.speedvalue>newnode^.speedvalue then
  1086. insertnode:=insertnode(newnode,currnode^.right)
  1087. else
  1088. if currnode^.speedvalue<newnode^.speedvalue then
  1089. insertnode:=insertnode(newnode,currnode^.left)
  1090. else
  1091. begin
  1092. if currnode^._name^>newnode^._name^ then
  1093. insertnode:=insertnode(newnode,currnode^.right)
  1094. else
  1095. if currnode^._name^<newnode^._name^ then
  1096. insertnode:=insertnode(newnode,currnode^.left)
  1097. else
  1098. begin
  1099. if replace_existing and
  1100. assigned(currnode) then
  1101. begin
  1102. newnode^.left:=currnode^.left;
  1103. newnode^.right:=currnode^.right;
  1104. currnode:=newnode;
  1105. insertnode:=newnode;
  1106. end
  1107. else
  1108. insertnode:=currnode;
  1109. end;
  1110. end;
  1111. end;
  1112. procedure tdictionary.inserttree(currtree,currroot:Pnamedindexobject);
  1113. begin
  1114. if assigned(currtree) then
  1115. begin
  1116. inserttree(currtree^.left,currroot);
  1117. inserttree(currtree^.right,currroot);
  1118. currtree^.right:=nil;
  1119. currtree^.left:=nil;
  1120. insertnode(currtree,currroot);
  1121. end;
  1122. end;
  1123. function tdictionary.rename(const olds,news : string):Pnamedindexobject;
  1124. var
  1125. spdval : longint;
  1126. lasthp,
  1127. hp,hp2,hp3 : Pnamedindexobject;
  1128. begin
  1129. spdval:=getspeedvalue(olds);
  1130. if assigned(hasharray) then
  1131. hp:=hasharray^[spdval mod hasharraysize]
  1132. else
  1133. hp:=root;
  1134. lasthp:=nil;
  1135. while assigned(hp) do
  1136. begin
  1137. if spdval>hp^.speedvalue then
  1138. begin
  1139. lasthp:=hp;
  1140. hp:=hp^.left
  1141. end
  1142. else
  1143. if spdval<hp^.speedvalue then
  1144. begin
  1145. lasthp:=hp;
  1146. hp:=hp^.right
  1147. end
  1148. else
  1149. begin
  1150. if (hp^.name=olds) then
  1151. begin
  1152. { get in hp2 the replacer for the root or hasharr }
  1153. hp2:=hp^.left;
  1154. hp3:=hp^.right;
  1155. if not assigned(hp2) then
  1156. begin
  1157. hp2:=hp^.right;
  1158. hp3:=hp^.left;
  1159. end;
  1160. { remove entry from the tree }
  1161. if assigned(lasthp) then
  1162. begin
  1163. if lasthp^.left=hp then
  1164. lasthp^.left:=hp2
  1165. else
  1166. lasthp^.right:=hp2;
  1167. end
  1168. else
  1169. begin
  1170. if assigned(hasharray) then
  1171. hasharray^[spdval mod hasharraysize]:=hp2
  1172. else
  1173. root:=hp2;
  1174. end;
  1175. { reinsert the hp3 in the tree from hp2 }
  1176. inserttree(hp3,hp2);
  1177. { reset node with new values }
  1178. stringdispose(hp^._name);
  1179. hp^._name:=stringdup(news);
  1180. hp^.speedvalue:=getspeedvalue(news);
  1181. hp^.left:=nil;
  1182. hp^.right:=nil;
  1183. { reinsert }
  1184. if assigned(hasharray) then
  1185. rename:=insertnode(hp,hasharray^[hp^.speedvalue mod hasharraysize])
  1186. else
  1187. rename:=insertnode(hp,root);
  1188. exit;
  1189. end
  1190. else
  1191. if olds>hp^.name then
  1192. begin
  1193. lasthp:=hp;
  1194. hp:=hp^.left
  1195. end
  1196. else
  1197. begin
  1198. lasthp:=hp;
  1199. hp:=hp^.right;
  1200. end;
  1201. end;
  1202. end;
  1203. end;
  1204. function Tdictionary.search(const s:string):Pnamedindexobject;
  1205. begin
  1206. search:=speedsearch(s,getspeedvalue(s));
  1207. end;
  1208. function Tdictionary.speedsearch(const s:string;speedvalue:longint):Pnamedindexobject;
  1209. var
  1210. newnode:Pnamedindexobject;
  1211. begin
  1212. if assigned(hasharray) then
  1213. newnode:=hasharray^[speedvalue mod hasharraysize]
  1214. else
  1215. newnode:=root;
  1216. while assigned(newnode) do
  1217. begin
  1218. if speedvalue>newnode^.speedvalue then
  1219. newnode:=newnode^.left
  1220. else
  1221. if speedvalue<newnode^.speedvalue then
  1222. newnode:=newnode^.right
  1223. else
  1224. begin
  1225. if (newnode^._name^=s) then
  1226. begin
  1227. speedsearch:=newnode;
  1228. exit;
  1229. end
  1230. else
  1231. if s>newnode^._name^ then
  1232. newnode:=newnode^.left
  1233. else
  1234. newnode:=newnode^.right;
  1235. end;
  1236. end;
  1237. speedsearch:=nil;
  1238. end;
  1239. {****************************************************************************
  1240. tsinglelist
  1241. ****************************************************************************}
  1242. constructor tsinglelist.init;
  1243. begin
  1244. first:=nil;
  1245. last:=nil;
  1246. end;
  1247. destructor tsinglelist.done;
  1248. begin
  1249. end;
  1250. procedure tsinglelist.reset;
  1251. begin
  1252. first:=nil;
  1253. last:=nil;
  1254. end;
  1255. procedure tsinglelist.clear;
  1256. var
  1257. hp,hp2 : pnamedindexobject;
  1258. begin
  1259. hp:=first;
  1260. while assigned(hp) do
  1261. begin
  1262. hp2:=hp;
  1263. hp:=hp^.listnext;
  1264. dispose(hp2,done);
  1265. end;
  1266. first:=nil;
  1267. last:=nil;
  1268. end;
  1269. procedure tsinglelist.insert(p:Pnamedindexobject);
  1270. begin
  1271. if not assigned(first) then
  1272. first:=p
  1273. else
  1274. last^.listnext:=p;
  1275. last:=p;
  1276. p^.listnext:=nil;
  1277. end;
  1278. {****************************************************************************
  1279. tdynamicarray
  1280. ****************************************************************************}
  1281. constructor tdynamicarray.init(Ablocksize:longint);
  1282. begin
  1283. posn:=0;
  1284. posnblock:=nil;
  1285. firstblock:=nil;
  1286. lastblock:=nil;
  1287. blocksize:=Ablocksize;
  1288. grow;
  1289. end;
  1290. function tdynamicarray.size:longint;
  1291. begin
  1292. if assigned(lastblock) then
  1293. size:=lastblock^.pos+lastblock^.used
  1294. else
  1295. size:=0;
  1296. end;
  1297. procedure tdynamicarray.grow;
  1298. var
  1299. nblock : pdynamicblock;
  1300. begin
  1301. getmem(nblock,blocksize+dynamicblockbasesize);
  1302. if not assigned(firstblock) then
  1303. begin
  1304. firstblock:=nblock;
  1305. posnblock:=nblock;
  1306. nblock^.pos:=0;
  1307. end
  1308. else
  1309. begin
  1310. lastblock^.next:=nblock;
  1311. nblock^.pos:=lastblock^.pos+lastblock^.used;
  1312. end;
  1313. nblock^.used:=0;
  1314. nblock^.next:=nil;
  1315. fillchar(nblock^.data,blocksize,0);
  1316. lastblock:=nblock;
  1317. end;
  1318. procedure tdynamicarray.align(i:longint);
  1319. var
  1320. j : longint;
  1321. begin
  1322. j:=(posn mod i);
  1323. if j<>0 then
  1324. begin
  1325. j:=i-j;
  1326. if posnblock^.used+j>blocksize then
  1327. begin
  1328. dec(j,blocksize-posnblock^.used);
  1329. posnblock^.used:=blocksize;
  1330. grow;
  1331. posnblock:=lastblock;
  1332. end;
  1333. inc(posnblock^.used,j);
  1334. inc(posn,j);
  1335. end;
  1336. end;
  1337. procedure tdynamicarray.seek(i:longint);
  1338. begin
  1339. if (i<posnblock^.pos) or (i>posnblock^.pos+blocksize) then
  1340. begin
  1341. { set posnblock correct if the size is bigger then
  1342. the current block }
  1343. if posnblock^.pos>i then
  1344. posnblock:=firstblock;
  1345. while assigned(posnblock) do
  1346. begin
  1347. if posnblock^.pos+blocksize>i then
  1348. break;
  1349. posnblock:=posnblock^.next;
  1350. end;
  1351. { not found ? then increase blocks }
  1352. if not assigned(posnblock) then
  1353. begin
  1354. { the current lastblock is now also fully used }
  1355. lastblock^.used:=blocksize;
  1356. repeat
  1357. grow;
  1358. posnblock:=lastblock;
  1359. until posnblock^.pos+blocksize>=i;
  1360. end;
  1361. end;
  1362. posn:=i;
  1363. if posn mod blocksize>posnblock^.used then
  1364. posnblock^.used:=posn mod blocksize;
  1365. end;
  1366. procedure tdynamicarray.write(const d;len:longint);
  1367. var
  1368. p : pchar;
  1369. i,j : longint;
  1370. begin
  1371. p:=pchar(@d);
  1372. while (len>0) do
  1373. begin
  1374. i:=posn mod blocksize;
  1375. if i+len>=blocksize then
  1376. begin
  1377. j:=blocksize-i;
  1378. move(p^,posnblock^.data[i],j);
  1379. inc(p,j);
  1380. inc(posn,j);
  1381. dec(len,j);
  1382. posnblock^.used:=blocksize;
  1383. if assigned(posnblock^.next) then
  1384. posnblock:=posnblock^.next
  1385. else
  1386. begin
  1387. grow;
  1388. posnblock:=lastblock;
  1389. end;
  1390. end
  1391. else
  1392. begin
  1393. move(p^,posnblock^.data[i],len);
  1394. inc(p,len);
  1395. inc(posn,len);
  1396. i:=posn mod blocksize;
  1397. if i>posnblock^.used then
  1398. posnblock^.used:=i;
  1399. len:=0;
  1400. end;
  1401. end;
  1402. end;
  1403. function tdynamicarray.read(var d;len:longint):longint;
  1404. var
  1405. p : pchar;
  1406. i,j,res : longint;
  1407. begin
  1408. res:=0;
  1409. p:=pchar(@d);
  1410. while (len>0) do
  1411. begin
  1412. i:=posn mod blocksize;
  1413. if i+len>=posnblock^.used then
  1414. begin
  1415. j:=posnblock^.used-i;
  1416. move(posnblock^.data[i],p^,j);
  1417. inc(p,j);
  1418. inc(posn,j);
  1419. inc(res,j);
  1420. dec(len,j);
  1421. if assigned(posnblock^.next) then
  1422. posnblock:=posnblock^.next
  1423. else
  1424. break;
  1425. end
  1426. else
  1427. begin
  1428. move(posnblock^.data[i],p^,len);
  1429. inc(p,len);
  1430. inc(posn,len);
  1431. inc(res,len);
  1432. len:=0;
  1433. end;
  1434. end;
  1435. read:=res;
  1436. end;
  1437. procedure tdynamicarray.blockwrite(var f:file);
  1438. var
  1439. hp : pdynamicblock;
  1440. begin
  1441. hp:=firstblock;
  1442. while assigned(hp) do
  1443. begin
  1444. system.blockwrite(f,hp^.data,hp^.used);
  1445. hp:=hp^.next;
  1446. end;
  1447. end;
  1448. destructor tdynamicarray.done;
  1449. var
  1450. hp : pdynamicblock;
  1451. begin
  1452. while assigned(firstblock) do
  1453. begin
  1454. hp:=firstblock;
  1455. firstblock:=firstblock^.next;
  1456. freemem(hp,blocksize+dynamicblockbasesize);
  1457. end;
  1458. end;
  1459. {****************************************************************************
  1460. tindexarray
  1461. ****************************************************************************}
  1462. constructor tindexarray.init(Agrowsize:longint);
  1463. begin
  1464. growsize:=Agrowsize;
  1465. size:=0;
  1466. count:=0;
  1467. data:=nil;
  1468. first:=nil;
  1469. noclear:=false;
  1470. end;
  1471. destructor tindexarray.done;
  1472. begin
  1473. if assigned(data) then
  1474. begin
  1475. if not noclear then
  1476. clear;
  1477. freemem(data,size*4);
  1478. data:=nil;
  1479. end;
  1480. end;
  1481. function tindexarray.search(nr:longint):Pnamedindexobject;
  1482. begin
  1483. if nr<=count then
  1484. search:=data^[nr]
  1485. else
  1486. search:=nil;
  1487. end;
  1488. procedure tindexarray.clear;
  1489. var
  1490. i : longint;
  1491. begin
  1492. for i:=1 to count do
  1493. if assigned(data^[i]) then
  1494. begin
  1495. dispose(data^[i],done);
  1496. data^[i]:=nil;
  1497. end;
  1498. count:=0;
  1499. first:=nil;
  1500. end;
  1501. procedure tindexarray.foreach(proc2call : Tnamedindexcallback);
  1502. var
  1503. i : longint;
  1504. begin
  1505. for i:=1 to count do
  1506. if assigned(data^[i]) then
  1507. proc2call(data^[i]);
  1508. end;
  1509. procedure tindexarray.grow(gsize:longint);
  1510. var
  1511. osize : longint;
  1512. begin
  1513. osize:=size;
  1514. inc(size,gsize);
  1515. reallocmem(data,size*4);
  1516. fillchar(data^[osize+1],gsize*4,0);
  1517. end;
  1518. procedure tindexarray.deleteindex(p:Pnamedindexobject);
  1519. var
  1520. i : longint;
  1521. begin
  1522. i:=p^.indexnr;
  1523. { update counter }
  1524. if i=count then
  1525. dec(count);
  1526. { update linked list }
  1527. while (i>0) do
  1528. begin
  1529. dec(i);
  1530. if (i>0) and assigned(data^[i]) then
  1531. begin
  1532. data^[i]^.indexnext:=data^[p^.indexnr]^.indexnext;
  1533. break;
  1534. end;
  1535. end;
  1536. if i=0 then
  1537. first:=p^.indexnext;
  1538. data^[p^.indexnr]:=nil;
  1539. { clear entry }
  1540. p^.indexnr:=-1;
  1541. p^.indexnext:=nil;
  1542. end;
  1543. procedure tindexarray.delete(var p:Pnamedindexobject);
  1544. begin
  1545. deleteindex(p);
  1546. dispose(p,done);
  1547. p:=nil;
  1548. end;
  1549. procedure tindexarray.insert(p:Pnamedindexobject);
  1550. var
  1551. i : longint;
  1552. begin
  1553. if p^.indexnr=-1 then
  1554. begin
  1555. inc(count);
  1556. p^.indexnr:=count;
  1557. end;
  1558. if p^.indexnr>count then
  1559. count:=p^.indexnr;
  1560. if count>size then
  1561. grow(((count div growsize)+1)*growsize);
  1562. {$ifdef Delphi}
  1563. Assert(not assigned(data^[p^.indexnr]) or (p=data^[p^.indexnr]));
  1564. {$endif}
  1565. data^[p^.indexnr]:=p;
  1566. { update linked list backward }
  1567. i:=p^.indexnr;
  1568. while (i>0) do
  1569. begin
  1570. dec(i);
  1571. if (i>0) and assigned(data^[i]) then
  1572. begin
  1573. data^[i]^.indexnext:=p;
  1574. break;
  1575. end;
  1576. end;
  1577. if i=0 then
  1578. first:=p;
  1579. { update linked list forward }
  1580. i:=p^.indexnr;
  1581. while (i<=count) do
  1582. begin
  1583. inc(i);
  1584. if (i<=count) and assigned(data^[i]) then
  1585. begin
  1586. p^.indexnext:=data^[i];
  1587. exit;
  1588. end;
  1589. end;
  1590. if i>count then
  1591. p^.indexnext:=nil;
  1592. end;
  1593. end.
  1594. {
  1595. $Log$
  1596. Revision 1.18 2000-11-04 14:25:19 florian
  1597. + merged Attila's changes for interfaces, not tested yet
  1598. Revision 1.17 2000/11/03 19:41:06 jonas
  1599. * fixed bug in tdynamicarray.align (merged)
  1600. Revision 1.16 2000/10/31 22:02:46 peter
  1601. * symtable splitted, no real code changes
  1602. Revision 1.15 2000/10/14 10:14:46 peter
  1603. * moehrendorf oct 2000 rewrite
  1604. Revision 1.14 2000/09/24 21:19:50 peter
  1605. * delphi compile fixes
  1606. Revision 1.13 2000/09/24 15:06:12 peter
  1607. * use defines.inc
  1608. Revision 1.12 2000/08/27 20:19:38 peter
  1609. * store strings with case in ppu, when an internal symbol is created
  1610. a '$' is prefixed so it's not automatic uppercased
  1611. Revision 1.11 2000/08/27 16:11:50 peter
  1612. * moved some util functions from globals,cobjects to cutils
  1613. * splitted files into finput,fmodule
  1614. Revision 1.10 2000/08/19 18:44:27 peter
  1615. * new tdynamicarray implementation using blocks instead of
  1616. reallocmem (merged)
  1617. Revision 1.9 2000/08/16 18:33:53 peter
  1618. * splitted namedobjectitem.next into indexnext and listnext so it
  1619. can be used in both lists
  1620. * don't allow "word = word" type definitions (merged)
  1621. Revision 1.8 2000/08/13 08:41:57 peter
  1622. * fixed typo in tsinglelist.clear (merged)
  1623. Revision 1.7 2000/08/12 15:34:22 peter
  1624. + usedasmsymbollist to check and reset only the used symbols (merged)
  1625. Revision 1.6 2000/08/10 12:20:44 jonas
  1626. * reallocmem is now also used under Delphi (merged from fixes branch)
  1627. Revision 1.5 2000/08/09 12:09:45 jonas
  1628. * tidexarray and tdynamicarray now use reallocmem() under FPC for
  1629. growing (merged from fixes branch)
  1630. Revision 1.4 2000/08/06 19:42:40 peter
  1631. * removed note
  1632. Revision 1.3 2000/08/02 19:49:58 peter
  1633. * first things for default parameters
  1634. Revision 1.2 2000/07/13 11:32:38 michael
  1635. + removed logs
  1636. }