home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / o / opuniq.zip / OPUNIQUE.PAS < prev    next >
Pascal/Delphi Source File  |  1992-10-14  |  6KB  |  217 lines

  1. {$S-,R-,V-,I-,B-,F+,O-,A-}
  2.  
  3. {Conditional defines that may affect this unit}
  4. {$I OPDEFINE.INC}
  5.  
  6. {*********************************************************}
  7. {*                  OPUNIQUE.PAS 1.03                    *}
  8. {*      Copyright (c) TurboPower Software 1987,1989.     *}
  9. {*                 All rights reserved.                  *}
  10. {*********************************************************}
  11.  
  12. unit OpUnique;
  13.   {-String array that stores only unique copies of strings}
  14.  
  15. interface
  16.  
  17. uses
  18.   OpString,
  19.   OpConst, {!!.20}
  20.   OpRoot,
  21.   OpTree;
  22.  
  23. type
  24.   IndexTreeNodePtr = ^IndexTreeNode;
  25.   IndexTreeNode =
  26.     object(TreeNode)
  27.       itnIndex : Word;  {Index into StringArray for the string}
  28.  
  29.       constructor Init(Index : Word);
  30.         {-Set initial index}
  31.     end;
  32.  
  33.   IndexTreePtr = ^IndexTree;
  34.   IndexTree =
  35.     object(Tree)
  36.       itSP : StringArrayPtr;
  37.  
  38.       constructor Init(SAP : StringArrayPtr);
  39.         {-Initialize and connect to StringArray}
  40.       function Compare(Key1, Key2 : Pointer) : CompareType; virtual;
  41.         {-Compare two keys, returning Less, Equal, Greater}
  42.       function GetKey(N : TreeNodePtr) : Pointer; virtual;
  43.         {-Return a pointer to the key value for node N}
  44.     end;
  45.  
  46.   UniqueStringArrayPtr = ^UniqueStringArray;
  47.   UniqueStringArray =
  48.     object(StringArray)
  49.       usTP : IndexTreePtr; {Pointer to IndexTree}
  50.  
  51.       constructor Init(StrMax, Amount : Word);
  52.         {-Allocate space for StrMax strings in Amount space}
  53.       destructor Done; virtual;
  54.         {-Deallocate array}
  55.       function AddString(St : String) : Word; virtual;
  56.         {-Add a new string, returning its index, or 0 if error}
  57.       procedure RemoveString(Which : Word); virtual;
  58.         {-Remove specified string from array and pack character table}
  59.       procedure RemoveStringByName(St : String);
  60.         {-Remove named string}
  61.       procedure Clear;
  62.         {-Remove all strings from array}
  63.       function GetTreePtr : IndexTreePtr;
  64.         {-Return address of associated IndexTree}
  65.  
  66.     {$IFDEF UseStreams}
  67.       constructor Load(var S : IdStream);
  68.         {-Load a binary packed array from a stream. NOT SUPPORTED}
  69.       procedure Store(var S : IdStream);
  70.         {-Write a packed array to a stream. NOT SUPPORTED}
  71.     {$ENDIF}
  72.     end;
  73.  
  74.   {====================================================================}
  75.  
  76. implementation
  77.  
  78.   constructor IndexTreeNode.Init(Index : Word);
  79.     {-Set initial index}
  80.   begin
  81.     if not TreeNode.Init then
  82.       Fail;
  83.     itnIndex := Index;
  84.   end;
  85.  
  86.   {--------------------------------------------------------------------}
  87.  
  88.   constructor IndexTree.Init(SAP : StringArrayPtr);
  89.     {-Initialize and connect to StringArray}
  90.   begin
  91.     if not Tree.Init then
  92.       Fail;
  93.     itSP := SAP;
  94.   end;
  95.  
  96.   function IndexTree.Compare(Key1, Key2 : Pointer) : CompareType;
  97.     {-Compare two keys, returning Less, Equal, Greater}
  98.   begin
  99.     Compare := CompString(StringPtr(Key1)^, StringPtr(Key2)^);
  100.   end;
  101.  
  102.   function IndexTree.GetKey(N : TreeNodePtr) : Pointer;
  103.     {-Return a pointer to the key value for node N}
  104.   begin
  105.     GetKey := itSP^.GetStringPtr(IndexTreeNodePtr(N)^.itnIndex);
  106.   end;
  107.  
  108.   {--------------------------------------------------------------------}
  109.  
  110.   constructor UniqueStringArray.Init(StrMax, Amount : Word);
  111.     {-Allocate space for StrMax strings in Amount space}
  112.   begin
  113.     usTP := nil;
  114.  
  115.     if not StringArray.Init(StrMax, Amount) then
  116.       Fail;
  117.  
  118.     new(usTP, Init(@Self));
  119.     if usTP = nil then begin
  120.       Done;
  121.       InitStatus := epFatal+ecOutOfMemory;
  122.       Fail;
  123.     end;
  124.   end;
  125.  
  126.   destructor UniqueStringArray.Done;
  127.     {-Deallocate array}
  128.   begin
  129.     if usTP <> nil then
  130.       Dispose(usTP, Done);
  131.     StringArray.Done;
  132.   end;
  133.  
  134.   function UniqueStringArray.AddString(St : String) : Word;
  135.     {-Add a new string, returning its index, or 0 if error}
  136.   var
  137.     ITNP : IndexTreeNodePtr;
  138.     Index : Word;
  139.   begin
  140.     ITNP :=IndexTreeNodePtr(usTP^.Find(@St));
  141.     if ITNP = nil then begin
  142.       {String is not in the array, add it now}
  143.       Index := StringArray.AddString(St);
  144.       if Index = 0 then begin
  145.         AddString := 0;
  146.         Exit;
  147.       end;
  148.       {Create new tree node for the string}
  149.       new(ITNP, Init(Index));
  150.       if ITNP = nil then begin
  151.         AddString := 0;
  152.         Exit;
  153.       end;
  154.       {Insert it into binary tree}
  155.       usTP^.Insert(ITNP);
  156.     end;
  157.     {Return the index}
  158.     AddString := ITNP^.itnIndex;
  159.   end;
  160.  
  161.   procedure UniqueStringArray.RemoveString(Which : Word);
  162.     {-Remove specified string from array and pack character table}
  163.   var
  164.     ITNP : IndexTreeNodePtr;
  165.     Index : Word;
  166.   begin
  167.     ITNP :=IndexTreeNodePtr(usTP^.Find(GetStringPtr(Which)));
  168.     if ITNP <> nil then begin
  169.       usTP^.Remove(ITNP);
  170.       Dispose(ITNP, Done);
  171.     end;
  172.     StringArray.RemoveString(Which);
  173.   end;
  174.  
  175.   procedure UniqueStringArray.RemoveStringByName(St : String);
  176.     {-Remove named string}
  177.   var
  178.     ITNP : IndexTreeNodePtr;
  179.     Index : Word;
  180.   begin
  181.     ITNP :=IndexTreeNodePtr(usTP^.Find(@St));
  182.     if ITNP <> nil then begin
  183.       Index := ITNP^.itnIndex;
  184.       usTP^.Remove(ITNP);
  185.       Dispose(ITNP, Done);
  186.       StringArray.RemoveString(Index);
  187.     end;
  188.   end;
  189.  
  190.   procedure UniqueStringArray.Clear;
  191.     {-Remove all strings from array}
  192.   begin
  193.     StringArray.Clear;
  194.     usTP^.Clear;
  195.   end;
  196.  
  197.   function UniqueStringArray.GetTreePtr : IndexTreePtr;
  198.     {-Return address of associated IndexTree}
  199.   begin
  200.     GetTreePtr := usTP;
  201.   end;
  202.  
  203. {$IFDEF UseStreams}
  204.   constructor UniqueStringArray.Load(var S : IdStream);
  205.     {-Load a binary packed array from a stream. NOT SUPPORTED}
  206.   begin
  207.     Fail;
  208.   end;
  209.  
  210.   procedure UniqueStringArray.Store(var S : IdStream);
  211.     {-Write a packed array to a stream. NOT SUPPORTED}
  212.   begin
  213.   end;
  214. {$ENDIF}
  215.  
  216. end.
  217.