home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 19 / CD_ASCQ_19_010295.iso / dos / prg / pas / swag / oop.swg / 0010_OOP-STRG.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  6KB  |  288 lines

  1. {
  2. LARRY HADLEY
  3.  
  4. >Right now, I have an Array of Pointers that point to the beginning
  5. >of each page.  The entire File is loaded into memory using BlockRead.
  6. >To jump to a page, it checks the current page number, jumps to that
  7. >offset (as specified by the Page Array) and dumps the contents
  8. >to the screen Until it reaches the bottom.
  9.  
  10.  I think I see. You have a monolithic block of memory...problem!
  11.  
  12. > There are a lot of ways to do it. One way would be to store the
  13. > File as Arrays of *Pointers* to Strings...this would allow 64k of
  14. > *sentences*, not just 64k of Text. It's a Variation on the old
  15.  
  16.    Actually, this is wrong. Since TP use 4 Byte Pointers, you can
  17.    only <g> store 16k of sentences in a single Array, but even
  18.    though that should still be plenty, you can use linked lists to
  19.    overcome that limitation!
  20.  
  21. >I have an Array of Pointers to the offset of each page.  Could you
  22. >provide a short code fragment?
  23.  
  24.    Instead of treating the Pointers as offsets, you should be using
  25.    them as actual data collections.
  26.  
  27. {
  28.  *****************************************************************
  29.  
  30.  Strings Unit With StrArray Object. Manage linked lists of Strings
  31.  transparently.
  32.  
  33.  By Larry Hadley - May be used freely, provided credit is given
  34.  wherever this code is used.
  35.  
  36.  *****************************************************************
  37. }
  38. Unit Strings;
  39.  
  40. Interface
  41.  
  42. Type
  43.   PString = ^String;
  44.  
  45.   PStringList = ^StringList;
  46.   StringList  = Record
  47.     P    : PString;
  48.     Next : PStringList;
  49.   end;
  50.  
  51.   pStrArray = ^oStrArray;
  52.   oStrArray = Object
  53.     Root   : PStringList;
  54.     total  : Word;
  55.     eolist : Boolean; {end of list - only valid after calling At,
  56.                        AtInsert, and AtDelete}
  57.     Constructor Init;
  58.     Destructor  Done;
  59.  
  60.     Procedure Insert(s : String);
  61.     Procedure Delete;
  62.     Function  At(item : Word) : PString;
  63.     Procedure AtInsert(item : Word; s : String);
  64.     Procedure AtDelete(item : Word);
  65.     Function  First : PString;
  66.     Function  Last  : PString;
  67.  
  68.   Private
  69.     Procedure NewNode(N : PStringList);
  70.     Function  AllocateS(s : String) : PString;
  71.     Procedure DeallocateS(Var P : PString);
  72.   end;
  73.  
  74. Implementation
  75.  
  76. Constructor oStrArray.Init;
  77. begin
  78.   Root   := NIL;
  79.   total  := 0;
  80.   eolist := False;
  81. end;
  82.  
  83. Destructor oStrArray.Done;
  84. Var
  85.   T : PStringList;
  86. begin
  87.   While Root <> NIL do
  88.   begin
  89.     T := Root^.Next;
  90.     if Root^.P <> NIL then
  91.       DeallocateS(Root^.P);
  92.     Dispose(Root);
  93.     Root := T;
  94.   end;
  95. end;
  96.  
  97. Procedure oStrArray.Insert(s : String);
  98. Var
  99.   T, T1 : PStringList;
  100. begin
  101.   NewNode(T1);
  102.   T1^.P := AllocateS(s);
  103.   Inc(total);
  104.   if Root <> NIL then
  105.   begin
  106.     T := Root;
  107.     While T^.Next <> NIL do
  108.        T := T^.Next;
  109.     T^.Next := T1;
  110.   end
  111.   else
  112.     Root := T1;
  113. end;
  114.  
  115. Procedure oStrArray.Delete;
  116. Var
  117.   T, T1 : PStringList;
  118. begin
  119.   T := Root;
  120.   if T <> NIL then
  121.   While T^.Next <> NIL do
  122.   begin
  123.      T1 := T;
  124.      T  := T^.Next;
  125.   end;
  126.   T1^.Next := T^.Next;
  127.   if T^.P <> NIL then
  128.     DeallocateS(T^.P);
  129.   Dispose(T);
  130.   Dec(total);
  131. end;
  132.  
  133. Function oStrArray.At(item : Word) : PString;
  134. Var
  135.   count : Word;
  136.   T     : PStringList;
  137. begin
  138.   if item>total then
  139.     eolist := True
  140.   else
  141.     eolist := False;
  142.   count := 1; {1 based offset}
  143.   T := Root;
  144.   While (count < item) and (T^.Next <> NIL) do
  145.   begin
  146.     T := T^.Next;
  147.     Inc(count);
  148.   end;
  149.   At := T^.P;
  150. end;
  151.  
  152. Procedure oStrArray.AtInsert(item : Word; s : String);
  153. Var
  154.   count : Word;
  155.   T, T1 : PStringList;
  156. begin
  157.   if item > total then
  158.     eolist := True
  159.   else
  160.     eolist := False;
  161.   NewNode(T1);
  162.   T1^.P := AllocateS(s);
  163.   Inc(total);
  164.   count := 1;
  165.   if Root <> NIL then
  166.   begin
  167.     T := Root;
  168.     While (count < Item) and (T^.Next <> NIL) do
  169.     begin
  170.       T := T^.Next;
  171.       Inc(count);
  172.     end;
  173.     T1^.Next := T^.Next;
  174.     T^.Next  := T1;
  175.   end
  176.   else
  177.     Root := T1;
  178. end;
  179.  
  180. Procedure oStrArray.AtDelete(item : Word);
  181. Var
  182.   count : Word;
  183.   T, T1 : PStringList;
  184. begin
  185.   if item > total then { don't delete if item bigger than list total -
  186.                        explicit only! }
  187.   begin
  188.     eolist := True;
  189.     Exit;
  190.   end
  191.   else
  192.     eolist := False;
  193.  
  194.   count := 1;
  195.   T     := Root;
  196.   T1    := NIL;
  197.  
  198.   While (count < item) and (T^.Next <> NIL) do
  199.   begin
  200.     T1 := T;
  201.     T  := T^.Next;
  202.     Inc(count);
  203.   end;
  204.   if T1 = NIL then
  205.     Root := Root^.Next
  206.   else
  207.     T1^.Next := T^.Next;
  208.   DeallocateS(T^.P);
  209.   Dispose(T);
  210.   Dec(total);
  211. end;
  212.  
  213. Function oStrArray.First : PString;
  214. begin
  215.   First := Root^.P;
  216. end;
  217.  
  218. Function oStrArray.Last : PString;
  219. Var
  220.   T : PStringList;
  221. begin
  222.   T := Root;
  223.   if T <> NIL then
  224.   While T^.Next <> NIL do
  225.     T := T^.Next;
  226.   Last := T^.P;
  227. end;
  228.  
  229. Procedure oStrArray.NewNode(N : PStringList);
  230. Var
  231.   T : PStringList;
  232. begin
  233.   New(T);
  234.   T^.Next := NIL;
  235.   T^.P := NIL;
  236.   if N = NIL then
  237.     N := T
  238.   else
  239.   begin
  240.     T^.Next := N^.Next;
  241.     N^.Next := T;
  242.   end;
  243. end;
  244.  
  245. Function oStrArray.AllocateS(s : String) : PString;
  246. Var
  247.   P : PString;
  248. begin
  249.   GetMem(P, Ord(s[0]) + 1);
  250.   P^ := s;
  251.   AllocateS := P;
  252. end;
  253.  
  254. Procedure oStrArray.DeallocateS(Var P : PString);
  255. begin
  256.   FreeMem(P, Ord(P^[0]) + 1);
  257.   P := NIL;  {for error checking}
  258. end;
  259.  
  260. end. {Unit StringS}
  261.  
  262.  
  263. {
  264. Code fragment :
  265.  
  266. Var
  267.   TextList : pStrArray;
  268.  
  269. ...
  270.  
  271.   New(TextList, Init);
  272.  
  273. ...
  274.  
  275.   Repeat
  276.     ReadLn(TextFile, s);
  277.     TextList^.Insert(s);
  278.   Until Eof(TextFile) or LowMemory;
  279.  
  280. ...
  281.  
  282.   For Loop := 1 to PageLen do
  283.   if Not(TextList^.eolist) then
  284.     Writeln(TextList^At(PageTop + Loop)^);
  285. ...
  286.  
  287. etc.
  288. }