home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / arrays / generics / genlist / genlist.doc < prev    next >
Encoding:
Text File  |  1989-07-28  |  11.7 KB  |  277 lines

  1. {
  2. ******  The List and L_Heap Objects (and Descendants) *****
  3.  
  4.  
  5. Design and Implementation ---  Eric C. Wentz
  6.  
  7.                                1-703-860-3807
  8.  
  9.                                2374 Antiqua Court
  10.                                Reston, Va. 22091
  11.  
  12.  
  13. Last Modification Date : 7-28-89
  14.  
  15.  
  16. ********************************************************************
  17.  
  18.  
  19. Design Objective(s):
  20.  
  21.        (1) {List}
  22.  
  23.            Implementation of a fully Generic Doubly-Linked List Object,
  24.            Capable of all List functions, for any DataType.
  25.  
  26.        (2) {L_Heap}
  27.  
  28.            Implementation of a fully Dynamic Generic Heap Object, capable
  29.            of all Heap functions for any Datatype.
  30.  
  31. Limitations:
  32.  
  33.            Individual Nodes in both are limited to the maximum size which
  34.            GetMem can allocate to a single variable, or 65521 bytes.
  35.  
  36. ***************************************************************************
  37. Implementation {GenList} :
  38. ***************************************************************************
  39.  
  40. Unit GenList;  {Generic Fully Dynamic Doubly-Linked List w/ArrayLike extras}
  41.  
  42. { Unit GenList introduces the LinkedList equivalent of the FlexArray datatype }
  43. { Lists are allocated dynamically on the Heap, but as they are allocated on a }
  44. { node-by-node basis, they can grow to occupy the entire Heap.  This is their }
  45. { only superiority over the FlexArray type.  FlexArray's will perform quite a }
  46. { bit faster for all operations, and take up much less overhead (since there  }
  47. { are TWO pointers associated with each element in a List).  Nevertheless,    }
  48. { for Lists/Arrays of LARGE data structures, or simply huge lists of smaller  }
  49. { structures, Lists should prove quite handy.                                 }
  50.  
  51. { All Lists (and descendant objects) MUST be CREATEd before any other         }
  52. { operations are performed upon them.  Using CREATE to re-initialize          }
  53. { may well lead to system crashes, so DON'T do it!  By using DESTROY and      }
  54. { then INITting again, you can re-use the List type for whatever purpose      }
  55. { deemed appropriate.  As ListS use Untyped Variables to achieve Genericity,  }
  56. { you MUST tell the List how many bytes to expect -- use SizeOf.              }
  57. { Procedures RESETELEMENT_N and GETELEMENT_N provide Array-like access to     }
  58. { Established Lists, and DO check current bounds of List.  Attempting to      }
  59. { Access beyond these bounds is an error and will terminate the program!      }
  60. { Procedures APPEND, DELETE and INSERTAFTER_N are more ListSpecific .         }
  61. { APPEND is used to add new elements to the "end" of the List.  Insertion at  }
  62. { a particular index is accomplished with INSERTAFTER_N, and deletion of a    }
  63. { given element is similarly handled by DELETE.  REMEMBER: For DELETE and     }
  64. { methods ending in "_N", it is a program-terminating error to attempt to     }
  65. { access the List outside of it's (Current) boundaries!! Procedure LISTCOPY   }
  66. { will copy one list to another (if enough memory) and is of necessity very   }
  67. { slow. Finally, Functions ElemSize and CurrentLength provide important info  }
  68. { as to (Current) List status.  Procedure ReWind is provided only to speed up }
  69. { those List Accesses which will be sequential, starting from the "Top", and  }
  70. { is generally not particularly useful or needed.  It merely goes directly to }
  71. { the "Top", where the "_N" methods must run down the pointers to reach the   }
  72. { "Top".  Calling it once will suffice to speed up sequential accesses.       }
  73. { Note, here I am using Top in the sense of a FIFO Queue, NOT as in a Stack!  }
  74.  
  75. { LAST MOD: Added Function Node_N for more direct access to Nodes, as needed  }
  76. { in Unit GenHeap, which defines the Heap structured variant of this List.    }
  77.  
  78. { NOTE: Lists (as defined here) are "indexed" 1..CurrentLength }
  79. {       CurrentLength = 0 equates to Empty List                }
  80.  
  81. INTERFACE  {NOT complete, but all needed information is here}
  82.  
  83. Type
  84.   List = Object
  85.     (**********************************************************************)
  86.     { GENERAL NOTE: Every time the parameter "Size" is specified, you MUST }
  87.     {               use SizeOf(<inputvariable>) to ensure correctness!!    }
  88.     (**********************************************************************)
  89.  
  90.     Procedure Create;{ List Must be CREATEd EXACTLY once, and before all else }
  91.  
  92.     Procedure Init (Size : Word);  { Once CREATEd, you may Re-Use a List }
  93.     Procedure Destroy;             { by DESTROYing and then Re-INITting  }
  94.  
  95.     Function CurrentLength : LongInt;   { Report Current Length of List }
  96.     Function ElemSize : Word;           { Report Size of List Elements  }
  97.     Function Current : LongInt;         { Where is "Cursor" ?           }
  98.     Procedure ReWind;                   { Go To the "Top" of the List   }
  99.  
  100.     Procedure Append (Var InData; Size : Word); { Add InData to END of List  }
  101.                                                 { automatically "grows" List }
  102.  
  103.     Procedure GetElement_N (Var OutData; N : LongInt; Size : Word);
  104.  
  105.                            { Return value of Nth member of List    }
  106.                            { legal N's are from 1..CurrentLength   }
  107.                            { Only legal size is equal to ElemSize. }
  108.  
  109.     Procedure ReSetElement_N (Var InData; N : LongInt; Size : Word);
  110.  
  111.                            { Re-Assign value of Nth member of List }
  112.                            { legal N's are form 1..CurrentLength   }
  113.                            { Only legal size is equal to ElemSize. }
  114.  
  115.     Procedure Delete (N : LongInt); { Delete Nth Element }
  116.  
  117.     Procedure InsertAfter_N (Var InData; N : LongInt; Size : Word);
  118.  
  119.                            { Insert new value into list AFTER Nth element }
  120.  
  121.     Procedure Copy (L : List); { Target List MUST be CREATEd or DESTROYed }
  122.  
  123.     Function Node_N (N : LongInt) : DPtr; { Returns a Pointer to the Nth Node }
  124.                                         { See Unit LGenHeap for correct usage }
  125.   End;
  126.  
  127.  
  128. IMPLEMENTATION { Not Reproduced }
  129.  
  130. ***************************************************************************
  131. Implementation {LGenHeap} :
  132. ***************************************************************************
  133.  
  134. Unit LGenHeap; {Generic Fully Dynamic Doubly-Linked Heap}
  135.  
  136. INTERFACE { NOT the entire Interface, but all needed definitions are here }
  137.  
  138. {Introduces the Heap variant of the List Object}
  139.  
  140. Type
  141.   L_Heap = Object (List)
  142.  
  143.                 Greater : NodeSortFunc;
  144.  
  145.                 Procedure Init (SortBy : NodeSortFunc; Size : Word);
  146.  
  147.                                    { Size refers to ElementSize! }
  148.  
  149.                 Procedure SiftDown (I,J : LongInt);
  150.  
  151.                                    { While I can think of No reason to  }
  152.                                    { Use SiftDown externally, there may }
  153.                                    { be a reason, so I have exported it }
  154.  
  155.                 Procedure SiftUp (Var Elem; Size : Word);
  156.  
  157.                                  { SiftUp can be used in place of Append }
  158.                                  { In order to Create/Maintain a Heap as }
  159.                                  { a Heap while adding elements, thus    }
  160.                                  { allowing the use of Sort instead of   }
  161.                                  { HeapSort which structures a Heap by   }
  162.                                  { using BuildHeap.                      }
  163.  
  164.                 Procedure BuildHeap;
  165.  
  166.                                  { Creates the Heap structure from }
  167.                                  { the ground up.                  }
  168.  
  169.                 Procedure ChangeSort (NewSort : NodeSortFunc);
  170.  
  171.                           { Permits redefinition of collating sequence }
  172.                           { for already established Heaps.             }
  173.  
  174.                 Procedure Sort;
  175.  
  176.                           { Sorts a Heap into Ascending order    }
  177.                           { Assumes HEAP is built or maintained. }
  178.  
  179.                 Procedure HeapSort;
  180.  
  181.                           { Sorts a Heap into Ascending order     }
  182.                           { Assumes nothing about Heap structure. }
  183.  
  184.                 Procedure Copy (H : L_Heap)
  185.  
  186.              End;
  187.  
  188.  
  189. IMPLEMENTATION { Not Reproduced }
  190.  
  191. **************************************************************************
  192.  { UNIT DEPENDENCIES }
  193. **************************************************************************
  194.  
  195.         FlexPntr
  196.            |    \
  197.            |     \
  198.            |      \
  199.            |       \
  200.         GenDatum    \
  201.            |        |
  202.            |        |
  203.            |        |
  204.            |        |
  205.         DoubLink____|___
  206.            |\\      |   \
  207.            | \\     |    \
  208.            |  \\    |     \
  209.            |   \\   |      \
  210.            |    \\  |       \
  211.         GenList  \\ |        \
  212.         /     \   \\|         \
  213.        /       \  | \  /----NodeSort
  214.       /         \ | |\/      |
  215.      /           \| |/\      |
  216.   Lists        LGenHeap\     |
  217.                     \   \    |
  218.                      \   \   |
  219.                       \   \  |
  220.                        \   \ |
  221.                         L_Heaps
  222.  
  223.  
  224. ****************************************************************************
  225.  { ERROR MESSAGES }
  226. ****************************************************************************
  227.  
  228. POSSIBLE ERROR MESSAGES: (Unit GenList)
  229.  
  230.            Attempted to Insert wrong size data Element.
  231.            Attempted GetElement with wrong size variable.
  232.            Attempted to index past current end of List.
  233.            Attempted ReSetElement with wrong size variable.
  234.            Attempted to delete past current bounds of List.
  235.            Attempted to insert past current bounds of List.
  236.            Attempted InsertAfter with wrong size data Element.
  237.            Attempted Function Node_N beyond Current Length.
  238.            **** OUT OF MEMORY ERROR ****
  239.  
  240.  
  241. POSSIBLE ERROR MESSAGES: (Unit GenDatum)
  242.  
  243.            Attempted Set_Data with Incompatible Data Size.
  244.            Attempted Get_Data with Incompatible Data Size.
  245.            Attempted Destroy on Non-Initialized Datum.
  246.            Attempted Init on Initialized or UnCREATEd Datum.
  247.            **** OUT OF MEMORY ****
  248.  
  249.  
  250.   { To be honest, I must add that I may very easily have overlooked something.
  251.     If you find such an error, please write and let me know!!! }
  252.  
  253.  
  254. ******************************************************************************
  255.  { PROPRIETARY CODE }
  256. ******************************************************************************
  257.  
  258. The FlexPntr Unit.
  259.  
  260. ALL the code in it was either directly taken from an article in
  261. PROGRAMMERS JOURNAL by Tom Swann or inspired by the same article
  262. (Programming on the Variant Express PJ V6.5 Sept/Oct 1988), and is of such a
  263. fundamental nature that no changes are really possible or desireable in ANY
  264. context.  I have distributed it as a TPU simply because Turbo has no other
  265. method available for PRIVATE declarations (ala ADA).  Therefore, since I did
  266. not really originate the code, and since there is no reason to ever change
  267. any of it, I will NOT distribute the source code for any reason.  Those
  268. incureably interested individuals should look up the above article, paying
  269. particular attention to Mr. Swann's definitions of IntPtr and IntRec, which
  270. I have renamed "FlexPtr" and "FlxArray" respectively. NOTE: the array which
  271. Mr. Swann defines within the IntRec is referenced with field name "Flex" in
  272. my version of the construct, and is an array of Bytes, not Integers.
  273.  
  274. ******************************************************************************
  275. ******************************************************************************
  276. ******************************************************************************
  277. }