home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / arrays / generics / genlist / l_heaps.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-07-24  |  3.5 KB  |  133 lines

  1. Unit L_Heaps;
  2. {$O+,S-}
  3.  
  4. { While its VERY ineffecient to use the L_Heap for Integers,
  5.   I will do so here anyway since this is only an example Unit
  6.   intended to demonstrate the declaration of application-level
  7.   versions of the L_Heap. }
  8.  
  9. { NOTE: L_Heaps are "Indexed" from 1 to CurrentLength }
  10. {       CurrentLength = 0 equals Empty List           }
  11.  
  12. INTERFACE
  13.  
  14. Uses LGenHeap,DoubLink,NodeSort;
  15.  
  16. {
  17. All L_Heaps additionally inherit the following Methods:
  18.  
  19.   (* GenList Methods *)
  20.                              Procedure Create;
  21.                              Procedure Destroy;
  22.                              Procedure ReWind; (* "GoTo" Element 1 instantly *)
  23.                                                (* used to speed up listhead *)
  24.                                                (* accesses. *)
  25.  
  26.                              Function CurrentLength; (* LongInt *)
  27.                              Function ElemSize;      (* Word *)
  28.  
  29.                              Function Node_N (N : LongInt) : D_Ptr;
  30.  
  31.                            (* Note on Node_N: It is used only in order
  32.                               to simplify the Swap operation, and I can
  33.                               think of no other useful application, but
  34.                               maybe you can.  The D_Ptr is defined in
  35.                               Unit DoubLink. *)
  36.  
  37.   (* L_Heap Methods *)
  38.                              Procedure Swap (I,J : LongInt);
  39.                              Procedure SiftDown (I,J : LongInt);
  40.                              Procedure BuidHeap;
  41.                              Procedure ChangeSort (NewSort : NodeSortFunc);
  42.                              Procedure Sort;
  43.                              Procedure HeapSort;
  44. }
  45.  
  46. Type
  47.   L_IntHeap = Object (L_Heap)
  48.  
  49.                    { Re-Defining these methods re-introduces Types and   }
  50.                    { most importantly, Type-Checking to the Generic      }
  51.                    { (typeless) L_Heap, as well as making the calls more }
  52.                    { "natural" for the actual applications Heaps.        }
  53.  
  54.                    { Follow this pattern when defining your descendant Heaps }
  55.  
  56.  
  57.                    Procedure Init;
  58.  
  59.         (* Linked-List Methods *)
  60.  
  61.                    Procedure Append (I : Integer);
  62.  
  63.                    Procedure Insert (I : Integer; After : LongInt);
  64.  
  65.                    Procedure SiftUp (I : Integer);
  66.  
  67.         (* Array-Like Methods *)
  68.  
  69.                    Procedure Accept (I : Integer; Index : LongInt);
  70.  
  71.                    Function Retrieve (Index : LongInt) : Integer;
  72.  
  73.         (* General Methods *)
  74.  
  75.                    Procedure Copy (From : L_IntHeap);
  76.  
  77.             End;
  78.  
  79.  
  80. IMPLEMENTATION
  81.  
  82. Procedure L_IntHeap.Init;
  83. Begin
  84.   L_Heap.Init (IntGreater,SizeOf(Integer))
  85. End;
  86.  
  87. Procedure L_IntHeap.Append (I : Integer);
  88. Var
  89.   J : Integer;
  90. Begin
  91.   J := I;
  92.   L_Heap.Append (J,SizeOf(Integer))
  93. End;
  94.  
  95. Procedure L_IntHeap.Insert (I : Integer; After : LongInt);
  96. Var
  97.   J : Integer;
  98. Begin
  99.   J := I;
  100.   InsertAfter_N (J,After,SizeOf(Integer))
  101. End;
  102.  
  103. Procedure L_IntHeap.SiftUp (I : Integer);
  104. Var
  105.   J : Integer;
  106. Begin
  107.   J := I;
  108.   L_Heap.SiftUp (J,SizeOf(Integer))
  109. End;
  110.  
  111. Procedure L_IntHeap.Accept (I : Integer; Index : LongInt);
  112. Var
  113.   J : Integer;
  114. Begin
  115.   J := I;
  116.   ReSetElement_N (J,Index,SizeOf(Integer))
  117. End;
  118.  
  119. Function L_IntHeap.Retrieve (Index : LongInt) : Integer;
  120. Var
  121.   J : Integer;
  122. Begin
  123.   GetElement_N (J,Index,SizeOf(Integer));
  124.   Retrieve := J
  125. End;
  126.  
  127. Procedure L_IntHeap.Copy (From : L_IntHeap);
  128. Begin
  129.   L_Heap.Copy (From)
  130. End;
  131.  
  132. BEGIN
  133. END.