home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / LISTS4.ZIP / LISTS40.DOC < prev    next >
Encoding:
Text File  |  1989-06-25  |  22.7 KB  |  656 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.                                   LISTS version 4.0
  9.  
  10.  
  11.           The LISTS unit is designed to make work with doubly-linked lists
  12.           a whole lot easier.  Now, with Objects and version 4.0 of LISTS,
  13.           LISTS itself has become a whole lot easier.
  14.  
  15.           Please note:  An awful lot of effort has gone into the
  16.           development and testing of LISTS.  If you use LISTS and like it,
  17.           a $15 registration fee is requested.  When registered, you will
  18.           receive the source code to LISTS via CompuServe's EasyPlex.
  19.           Thank you.
  20.  
  21.  
  22.  
  23.                                   Table of Contents
  24.  
  25.                                                                        Page
  26.           Section One: Interface. . . . . . . . . . . . . . . . . . . . 1
  27.           Section Two: ListObj description. . . . . . . . . . . . . . . 2
  28.              F_Entry. . . . . . . . . . . . . . . . . . . . . . . . . . 3
  29.              L_Entry. . . . . . . . . . . . . . . . . . . . . . . . . . 3
  30.              Init . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
  31.              Delete . . . . . . . . . . . . . . . . . . . . . . . . . . 3
  32.              FirstEntry . . . . . . . . . . . . . . . . . . . . . . . . 4
  33.              LastEntry. . . . . . . . . . . . . . . . . . . . . . . . . 4
  34.              EntryAbs . . . . . . . . . . . . . . . . . . . . . . . . . 5
  35.              EntryRel . . . . . . . . . . . . . . . . . . . . . . . . . 5
  36.              MoveEntry. . . . . . . . . . . . . . . . . . . . . . . . . 6
  37.           Section Three: EntryObj description . . . . . . . . . . . . . 7
  38.              P_Entry. . . . . . . . . . . . . . . . . . . . . . . . . . 7
  39.              N_Entry. . . . . . . . . . . . . . . . . . . . . . . . . . 7
  40.              Add. . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
  41.              Insert . . . . . . . . . . . . . . . . . . . . . . . . . . 8
  42.              Remove . . . . . . . . . . . . . . . . . . . . . . . . . . 8
  43.              NextEntry. . . . . . . . . . . . . . . . . . . . . . . . . 9
  44.              PrevEntry. . . . . . . . . . . . . . . . . . . . . . . . . 9
  45.           Examples. . . . . . . . . . . . . . . . . . . . . . . . . .  11
  46.              Static entries . . . . . . . . . . . . . . . . . . . . .  11
  47.              Dynamic entries. . . . . . . . . . . . . . . . . . . . .  12
  48.           Hopes . . . . . . . . . . . . . . . . . . . . . . . . . . .  13
  49.  
  50.  
  51.  
  52.           LISTS documentation                                        Page 2
  53.  
  54.  
  55.  
  56.           Section One: Interface
  57.           ------------ ---------
  58.           The following describes the Interface section of LISTS (the
  59.           routines that can be called from another program.)  Please note
  60.           that there are no Procedures/Functions per se, but objects and
  61.           methods instead.  This switch from previous versions of LISTS
  62.           makes full use of the new capabilities of TP 5.5 and allows for
  63.           much greater flexibility in using LISTS.
  64.  
  65.           INTERFACE
  66.           Type
  67.            EntryPtr  =  ^EntryObj;
  68.  
  69.            ListObj   =  Object
  70.                           F_Entry  :   EntryPtr;
  71.                           L_Entry  :   EntryPtr;
  72.  
  73.                           Constructor Init;
  74.                           Destructor Done; Virtual;
  75.  
  76.                           Function FirstEntry:EntryPtr; Virtual;
  77.                           Function LastEntry:EntryPtr; Virtual;
  78.                           Function EntryAbs(Num:LongInt):EntryPtr; Virtual;
  79.                           Function EntryRel(Num:LongInt):EntryPtr; Virtual;
  80.                           Procedure MoveEntry(Source, Dest:EntryPtr);
  81.                             Virtual;
  82.                         End;
  83.  
  84.            EntryObj  =  Object
  85.                           P_Entry  :   EntryPtr;
  86.                           N_Entry  :   EntryPtr;
  87.  
  88.                           Constructor Add(Var List:ListObj);
  89.                           Constructor Insert(Var List:ListObj;
  90.                                              Loc:EntryPtr);
  91.                           Destructor Remove(Var List:ListObj);
  92.  
  93.                           Function NextEntry:EntryPtr; Virtual;
  94.                           Function PrevEntry:EntryPtr; Virtual;
  95.                         End;
  96.  
  97.           Notice that all the methods are VIRTUAL, thus allowing the
  98.           programmer to customize the routines.
  99.  
  100.  
  101.  
  102.           LISTS documentation                                        Page 3
  103.  
  104.  
  105.  
  106.           Section Two: ListObj Description
  107.           ------------ -------------------
  108.           The ListObj is a data type of Object which include two data
  109.           fields (F_Entry and L_Entry, both of which are of type EntryPtr)
  110.           and seven methods (Init, the constructor, Delete, the destructor,
  111.           FirstEntry, LastEntry, EntryAbs, EntryRel, and MoveEntry).  All
  112.           the methods are virtual to allow for simple extensions and
  113.           modifications.
  114.  
  115.                ------------------------------------------------------------
  116.                F_Entry : EntryPtr
  117.                ------------------------------------------------------------
  118.                This data field is a pointer to the first entry in the list.
  119.                It should never be accessed by the programmer; use the
  120.                method FirstEntry instead.
  121.  
  122.  
  123.  
  124.                ------------------------------------------------------------
  125.                L_Entry : EntryPtr
  126.                ------------------------------------------------------------
  127.                This data field is a pointer to the last entry in the list.
  128.                It should never be accessed by the programmer; use the
  129.                method LastEntry instead.
  130.  
  131.  
  132.  
  133.                ------------------------------------------------------------
  134.                Constructor Init
  135.                ------------------------------------------------------------
  136.                This routine initializes the List for processing by setting
  137.                the data fields to NIL and preparing the Virtual Method
  138.                Table (VMT).
  139.  
  140.                Init must be called prior to ANY activity for the List.
  141.  
  142.                Example
  143.                Var
  144.                   List : ListObj;
  145.  
  146.                Begin
  147.                   List.Init;
  148.                End.
  149.  
  150.  
  151.  
  152.                ------------------------------------------------------------
  153.                Destructor Delete
  154.                ------------------------------------------------------------
  155.                This routine does nothing but release the memory used by the
  156.                Virtual Method Table (VMT).
  157.  
  158.                Example
  159.                Var
  160.  
  161.  
  162.  
  163.           LISTS documentation                                        Page 4
  164.  
  165.  
  166.  
  167.                   List : ListObj;
  168.  
  169.                Begin
  170.                   List.Init;
  171.  
  172.                   ...
  173.  
  174.                   p
  175.                List.Delete;
  176.                End.
  177.  
  178.  
  179.  
  180.                ------------------------------------------------------------
  181.                Function FirstEntry:EntryPtr
  182.                ------------------------------------------------------------
  183.                This function returns a pointer to the first entry in the
  184.                list.  Use this method instead of F_Entry.
  185.  
  186.                This function returns NIL if the List has been Initialized
  187.                and is empty.
  188.  
  189.                Example
  190.                Var
  191.                   List : ListObj;
  192.                   FirstEntryInList : EntryObj;
  193.  
  194.                Begin
  195.                   List.Init;
  196.  
  197.                   ...
  198.  
  199.                   FirstEntryInList:=List.FirstEntry;
  200.                End.
  201.  
  202.  
  203.  
  204.                ------------------------------------------------------------
  205.                Function LastEntry:EntryPtr
  206.                ------------------------------------------------------------
  207.                This function returns a pointer to the first entry in the
  208.                list.  Use this method instead of L_Entry.
  209.  
  210.                This function returns NIL the the List has been Initialized
  211.                and is empty.
  212.  
  213.                Example
  214.                Var
  215.                   List : ListObj;
  216.                   LastEntryInList : EntryObj;
  217.  
  218.                Begin
  219.                   List.Init;
  220.  
  221.  
  222.  
  223.           LISTS documentation                                        Page 5
  224.  
  225.  
  226.  
  227.                   ...
  228.  
  229.                   LastEntryInList:=List.LastEntry;
  230.                End.
  231.  
  232.  
  233.  
  234.                ------------------------------------------------------------
  235.                Function EntryAbs(N:LongInt):EntryPtr;
  236.                ------------------------------------------------------------
  237.                This function returns a pointer to the Nth entry in the List
  238.                starting from the first as number one.
  239.  
  240.                N must be positive.  Negative numbers will return the
  241.                FirstEntry in the List.
  242.  
  243.                If N exceeds the number of Entries in the List, EntryAbs
  244.                returns the LastEntry in the List.
  245.  
  246.                Example
  247.                Var
  248.                   List : ListObj;
  249.                   TenthEntry : EntryObj;
  250.  
  251.                Begin
  252.                   List.Init;
  253.  
  254.                   ...
  255.  
  256.                   TenthEntry:=List.EntryAbs(10);
  257.                End.
  258.  
  259.  
  260.  
  261.                ------------------------------------------------------------
  262.                Function EntryRel(Loc:EntryPtr; N:LongInt):EntryPtr
  263.                ------------------------------------------------------------
  264.                This function returns a pointer to the Nth entry relative to
  265.                Loc.
  266.  
  267.                N may be positive or negative.  If the boundaries of the
  268.                List are exceeded, a positive N value will return the
  269.                LastEntry in the List while a negative N value will return
  270.                the FirstEntry in the List
  271.  
  272.                Example
  273.                Var
  274.                   List : ListObj;
  275.                   TenthFromLast : EntryObj;
  276.  
  277.                Begin
  278.                   List.Init;
  279.  
  280.                   ...
  281.  
  282.  
  283.  
  284.           LISTS documentation                                        Page 6
  285.  
  286.  
  287.  
  288.                   TenthFromLast:=List.EntryRel(List.LastEntry, -10);
  289.                End.
  290.  
  291.  
  292.  
  293.                ------------------------------------------------------------
  294.                Procedure MoveEntry(Source, Dest:EntryPtr)
  295.                ------------------------------------------------------------
  296.                This procedure moves the Source entry to the location of
  297.                immediately preceding Dest.  For example,
  298.                   Var
  299.                      L    :  ListObj;
  300.  
  301.                   Begin
  302.                      L.Init;
  303.  
  304.                      ...
  305.  
  306.                      With L Do
  307.                         MoveEntry(FirstEntry, LastEntry);
  308.                   End.
  309.                will move the FirstEntry in List, L to the next to the last
  310.                position in L.
  311.  
  312.                To move Source to the last position in the List, use NIL as
  313.                the value for Dest, for example,
  314.                   Var
  315.                      L    :  ListObj;
  316.  
  317.                   Begin
  318.                      L.Init;
  319.  
  320.                      ...
  321.  
  322.                      With L Do
  323.                         MoveEntry(FirstEntry, NIL);
  324.                   End.
  325.                Now the FirstEntry in the List has been moved to the
  326.                LastEntry and there is a new FirstEntry.
  327.  
  328.                MoveEntry performs no range checks of any sort and therefore
  329.                it is the programmer's responsibility to ensure that the
  330.                arguments to MoveEntry are both of the same list.
  331.  
  332.  
  333.  
  334.           LISTS documentation                                        Page 7
  335.  
  336.  
  337.  
  338.           Section Three: EntryObj Description
  339.           -------------- --------------------
  340.           Like the ListObj, all the methods in EntryObj are virtual.
  341.  
  342.                ------------------------------------------------------------
  343.                P_Entry : EntryObj
  344.                ------------------------------------------------------------
  345.                Data field pointing to the previous entry in the List.
  346.                FirstEntry^.P_Entry points to NIL.
  347.  
  348.                Should not be accessed by the programmer, use PrevEntry
  349.                instead.
  350.  
  351.  
  352.                ------------------------------------------------------------
  353.                N_Entry : EntryObj
  354.                ------------------------------------------------------------
  355.                Data field pointing to the next entry in the List.
  356.                LastEntry^.N_Entry points to NIL.
  357.  
  358.                Should not be accessed by the programmer, use NextEntry
  359.                instead.
  360.  
  361.  
  362.                ------------------------------------------------------------
  363.                Constructor Add(Var List:ListObj);
  364.                ------------------------------------------------------------
  365.                This routine adds EntryObj into List at the end and sets up
  366.                its own Virtual Method Table.
  367.  
  368.                If the List is empty, both List.FirstEntry and
  369.                List.LastEntry point to the EntryObj.
  370.  
  371.                Example
  372.                Type
  373.                   EntryRec = Object (EntryObj) {The new object is a
  374.                                                 descendant from EntryObj,
  375.                                                 which is defined in LISTS}
  376.                                 I:Integer;
  377.                              End;
  378.  
  379.                Var
  380.                   List : ListObj;
  381.                   Entry: EntryRec;
  382.  
  383.                Begin
  384.                   List.Init;
  385.  
  386.                   Entry.I:=1;
  387.                   Entry.Add(List);
  388.                End;
  389.  
  390.  
  391.  
  392.           LISTS documentation                                        Page 8
  393.  
  394.  
  395.  
  396.                ------------------------------------------------------------
  397.                Constructor Insert(Var List:ListObj; Loc:EntryPtr)
  398.                ------------------------------------------------------------
  399.                Like Add, this routine sets up the Virtual Method Table
  400.                (VMT).  Insert also inserts EntryObj into List immediately
  401.                preceding Loc, if Loc is NIL, EntryObj is added to the end
  402.                of List.
  403.  
  404.                No range checking is performed in Insert, it is the
  405.                programmer's responsibility to ensure that Loc is in the
  406.                same list as List.
  407.  
  408.                Example
  409.                Type
  410.                   EntryRec = Object (EntryObj) {The new object is a
  411.                                                 descendant from EntryObj,
  412.                                                 which is defined in LISTS}
  413.                                 I:Integer;
  414.                              End;
  415.  
  416.                Var
  417.                   List : ListObj;
  418.                   Entry: EntryRec;
  419.  
  420.                Begin
  421.                   List.Init;
  422.  
  423.                   ...
  424.  
  425.                   Entry.I:=2;
  426.                   Entry.Insert(List, List.LastEntry);
  427.                End;
  428.  
  429.  
  430.  
  431.                ------------------------------------------------------------
  432.                Destructor Remove(Var List:ListObj)
  433.                ------------------------------------------------------------
  434.                This routine removes EntryObj from List and frees up the
  435.                memory required by EntryObj's Virtual Method Table.
  436.  
  437.                Example
  438.                Type
  439.                   EntryRec = Object (EntryObj)
  440.                                 I : Integer;
  441.                              End;
  442.  
  443.                Var
  444.                   List : ListObj;
  445.                   Entries :  Array [1..10] of EntryRec;
  446.                   N : Integer;
  447.  
  448.                Begin
  449.                   List.Init;
  450.  
  451.  
  452.  
  453.           LISTS documentation                                        Page 9
  454.  
  455.  
  456.  
  457.                   For N:=1 To 10 Do Begin
  458.                      Entries[N].I:=N;
  459.                      Entries[N].Add(List);
  460.                   End;
  461.  
  462.                   Entries[5].Remove(List);
  463.                End.
  464.  
  465.                At the end of the program, List would look like this:
  466.                   1   2   3   4   6   7   8   9   10
  467.  
  468.  
  469.  
  470.                ------------------------------------------------------------
  471.                Function NextEntry:EntryPtr
  472.                ------------------------------------------------------------
  473.                This routine returns the entry immediately after EntryObj.
  474.  
  475.                If the EntryObj is the LastEntry in the list, a value of NIL
  476.                is returned.
  477.  
  478.                Example
  479.                Type
  480.                   EntryRec = Object (EntryObj)
  481.                                 I : Integer;
  482.                              End;
  483.  
  484.                Var
  485.                   List : ListObj;
  486.                   Entries :  Array [1..10] of EntryRec;
  487.                   N : Integer;
  488.  
  489.                Begin
  490.                   List.Init;
  491.  
  492.                   For N:=1 To 10 Do Begin
  493.                      Entries[N].I:=N;
  494.                      Entries[N].Add(List);
  495.                   End;
  496.  
  497.                   Writeln(EntryRec(Entries[5].NextEntry^).I);
  498.                End.
  499.                This program would output a "6".
  500.  
  501.  
  502.  
  503.                ------------------------------------------------------------
  504.                Function PrevEntry:EntryPtr
  505.                ------------------------------------------------------------
  506.                This routine returns the entry immediately before EntryObj.
  507.  
  508.                If the EntryObj is the FirstEntry in the List, a value of
  509.                NIL is returned.
  510.  
  511.  
  512.  
  513.           LISTS documentation                                       Page 10
  514.  
  515.  
  516.  
  517.                Example
  518.                Type
  519.                   EntryRec = Object (EntryObj)
  520.                                 I : Integer;
  521.                              End;
  522.  
  523.                Var
  524.                   List : ListObj;
  525.                   Entries :  Array [1..10] of EntryRec;
  526.                   N : Integer;
  527.  
  528.                Begin
  529.                   List.Init;
  530.  
  531.                   For N:=1 To 10 Do Begin
  532.                      Entries[N].I:=N;
  533.                      Entries[N].Add(List);
  534.                   End;
  535.  
  536.                   Writeln(EntryRec(Entries[5].PrevEntry^).I);
  537.                End.
  538.                This program would output a "4".
  539.  
  540.  
  541.  
  542.           LISTS documentation                                       Page 11
  543.  
  544.  
  545.  
  546.           Examples
  547.           --------
  548.           The following examples will hopefully make the use of LISTS a bit
  549.           clearer.
  550.  
  551.  
  552.                Static Entries
  553.                --------------
  554.                Occasionally it might be useful to have static entries in a
  555.                List, that is to have the entries declared in the VAR
  556.                section of the program.  The above examples are examples of
  557.                static entries.
  558.  
  559.                To make use of static entries, simply define the TYPE of the
  560.                entry as an object which is descendant of EntryObj, for
  561.                example,
  562.                   Type
  563.                      EntryRec   =   Object (EntryObj) {Descendant from
  564.                                                        EntryObj}
  565.                                        Name:String[30];
  566.                                     End;
  567.  
  568.                Then, in the VAR section, declare variables to be of type
  569.                EntryRec which then can become part of a List.  For example,
  570.                   Var
  571.                      Entry1, Entry2, Entry3  :   EntryRec;
  572.                      NameList                :   ListObj;
  573.                      P                       :   EntryPtr;
  574.  
  575.                   Begin
  576.                      NameList.Init;
  577.  
  578.                      Write('Enter first name:'); Readln(Entry1.Name);
  579.                      Write('Enter second name:'); Readln(Entry2.Name);
  580.                      Write('Entry third name:'); Readln(Entry3.Name);
  581.  
  582.                      Entry1.Add(NameList);
  583.                      Entry2.Add(NameList);
  584.                      Entry3.Add(NameList);
  585.  
  586.                      Writeln('The following is the REVERSED list of
  587.                names.');
  588.  
  589.                      P:=NameList.LastEntry;
  590.                      While Not (P=nil) Do Begin
  591.                         Writeln(EntryRec(P^).Name);
  592.  
  593.                         P:=P^.PrevEntry;
  594.                      End;
  595.                   End.
  596.  
  597.  
  598.  
  599.           LISTS documentation                                       Page 12
  600.  
  601.  
  602.  
  603.                Dynamic entries
  604.                ---------------
  605.                Infinitely more useful than Static entries are Dynamic
  606.                entries.  These entries use the heap to store the
  607.                information contained in the List.
  608.  
  609.                Below is the above example written to use Dynamic entries.
  610.                Notice that in the output section, after displaying the
  611.                name, the program removes the Entry from the List and then
  612.                removes the Entry from the heap.
  613.                   Type
  614.                      EntryRec   =   Object (EntryObj) {Descendant from
  615.                                                        EntryObj}
  616.                                        Name:String[30];
  617.                                     End;
  618.  
  619.                   Var
  620.                      Entry                   :   ^EntryRec;
  621.                      Name                    :   String[30];
  622.                      NameList                :   ListObj;
  623.                      P                       :   EntryPtr;
  624.  
  625.                   Begin
  626.                      NameList.Init;
  627.  
  628.                      Writeln('Enter names, blank line to stop.');
  629.  
  630.                      Repeat
  631.                         Readln(Name);
  632.                         If Not (Name='') Then Begin
  633.                            New(Entry, Add(NameList));
  634.                            Entry^.Name:=Name;
  635.                         End;
  636.                      Until Name='';
  637.  
  638.                      Writeln('The following is the REVERSED list of
  639.                names.');
  640.  
  641.                      P:=NameList.LastEntry;
  642.                      While Not (P=nil) Do Begin
  643.                         Writeln(EntryRec(P^).Name);
  644.  
  645.                         Entry:=P;
  646.                         P:=P^.PrevEntry;
  647.  
  648.                         Dispose(Entry, Remove);
  649.                      End;
  650.                   End.
  651.  
  652.           IMPORTANT NOTE:
  653.           Due to the nature of LISTS, each Entry into the List must be
  654.           descended from EntryObj.
  655.  
  656.  
  657.  
  658.           LISTS documentation                                       Page 13
  659.  
  660.  
  661.  
  662.           Hopes
  663.           -----
  664.           I hope you find LISTS both useful and productive.  If you have
  665.           any questions or comments regarding the program, please address
  666.           them to Mark Addleman via CompuServe (CIS number - 72777, 740)
  667.  
  668.           Thank you very much.
  669.  
  670.