home *** CD-ROM | disk | FTP | other *** search
/ Shareware 1 2 the Maxx / sw_1.zip / sw_1 / PROGRAM / SRTKIT11.ZIP / SORTKIT.DOC < prev    next >
Text File  |  1992-03-07  |  14KB  |  338 lines

  1.  
  2.  ╔══════════════════════════════════════════════════════════════════════════╗
  3.  ║                                                                          ║
  4.  ║                                 ISoft D&M                                ║
  5.  ║                                 POB. 5517                                ║
  6.  ║                           Coralville IA 52241                            ║
  7.  ║                                   U.S.A                                  ║
  8.  ║                                                                          ║
  9.  ╚══════════════════════════════════════════════════════════════════════════╝ 
  10.  
  11. *******************************************************************************
  12. *                                   sortKit V1.1                              *
  13. * sortKit Documentation file, Last Update : Mar. 07, 1992.                    *
  14. *******************************************************************************
  15.  
  16. File List
  17. ---------
  18.  
  19.  This package contains the following files :
  20.  
  21.  MERGSORT.PAS   - External sort object - source file.
  22.  QUICKSRT.PAS   - Internal sort object - source file.
  23.  COMBSORT.PAS   - Combined internal + external sort object - source file.
  24.  SORTKIT.DOC    - This file.
  25.  SORTKIT.REG    - Registration file.
  26.  PROGRAMS.TXT   - ISoft D&M shareware products description.
  27.  
  28. Why Register
  29. ------------
  30.  
  31.   sortKit is a shareware product, if you find this product valuable,
  32.  please register it. This section describes the reasons you should register.
  33.  
  34.   By registering sortKit you will receive a printed manual with FULL technical
  35.  documentation, loads of program samples printed, and on disk, and the latest
  36.  sortKit version. By registering you will help us to create the next version
  37.  of sortKit that will support faster smarter sorts, and support for PARADOX
  38.  sorts.
  39.  
  40.   Registered sortKit users get full no-rotality usage permission, and the
  41.  complete RFFSORT sort utility program package.
  42.  
  43. Whats new
  44. ---------
  45.  
  46.   - Added better documentation in source files.
  47.   - From this version sortKit is distributed by 
  48.     ISoft D&M, P.O.B 5517, Coralville IA 52241, U.S.A
  49.  
  50. *******************************************************************************
  51. *                                Introduction                                 *
  52. *******************************************************************************
  53.  
  54.   This package provides a Turbo-Pascal 6.0 (and 5.5 probably), OOPS Sort
  55.  library that includes an internal sort (quick sort), and external sort
  56.  (merge sort), and a combined internal and external sort (combined sort).
  57.  
  58.   With this package you can create simple to use sort objects that only need
  59.  to define a virtual compare function, to sort data fast, in memory, or 
  60.  huge data files using a fast internal and external sort algorithm. 
  61.  
  62. *******************************************************************************
  63. *                                Internal Sort                                *
  64. *******************************************************************************
  65.  
  66.   The unit quicksrt.pas defines an internal quicksort object. In order to
  67.  perform fast sort, the quickSort object uses an array of pointers to the
  68.  sorted data, and when it needs to swap data elements, it only swaps their
  69.  pointers in the array of pointers. 
  70.  
  71.   In order to define a sort that will sort YOUR data you should define a 
  72.  descendent of the quickSort object that has one virtual function, the 
  73.  compare function. This function receives 2 pointers - a, and b, and 
  74.  returns 1 if the data pointed by a is "bigger" then the data pointed by
  75.  b, it returns 0 if the data pointed by a is "equal" to the data pointed
  76.  by b, and it returns 2, otherwise. 
  77.  
  78.   Let us assume that we want to create a simple textSort object that sorts
  79.  strings. The following definition will explain the way to create such an
  80.  object :
  81.  
  82.  textSort = object(quickSort)
  83.  
  84.         function compare(a, b : pointer) : byte; virtual;
  85.  
  86.  end; { textSort object definition }
  87.  
  88.  function textSort.compare(a, b : pointer) : byte;
  89.  type
  90.         strPtr = ^ string;
  91.  begin
  92.         if (strPtr(a)^ > strPtr(b)^) then
  93.                 compare := 1
  94.         else if (strPtr(a)^ = strPtr(b)^) then
  95.                 compare := 0
  96.         else
  97.                 compare := 2;
  98.  end; {textSort.compare}
  99.  
  100.  In order to use this object we must perform the following steps : 
  101.  
  102.  1. Declare a variable of the required sort type.
  103.  2. Declare a variable of type arrayOfPointersPtr.
  104.  3. Allocate space from the heap to the variables declared in step 2, in the
  105.     amount of numberOfElementsToSort * sizeOf(pointer).
  106.  4. Assign to the array defined, the pointers to the data elements to be sorted.
  107.  5. Initialize the sort object.
  108.  6. Call the sort object's doYourJob method.
  109.  
  110.  Let us assume that we want to sort an array of 200 strings that we have in
  111.  memory, in a string array defined as :
  112.  
  113.  var
  114.         stringArray : array [1.200] of string;
  115.  
  116.  We will use the following code :
  117.  
  118.  var
  119.         myInternalSort : textSort;                      { step 1 }
  120.         pointers       : arrayOfPointersPtr;            { step 2 }
  121.  begin
  122.         getmem(pointers, 200 * sizeOf(pointer));        { step 3 }
  123.         {$R-} {This is important !, otherwise we will get range errors }
  124.         for i := 1 to 200 do 
  125.                 pointers[i] := @stringArray[i];         { step 4 }
  126.         myInternalSort.init(200, pointers, 256);        { step 5 }
  127.         myInternalSort.doYourJob;                       { step 6 }
  128.   ... { some more code .. }
  129.  
  130.   Notice - At the end of this procedure stringArray is NOT sorted, but 
  131.            pointers is a pointer to a sorted array of pointers. If we
  132.            would like to print the sorted array we will use a procedure 
  133.            such as :
  134.  
  135.         for i := 1 to 200 do
  136.                 writeln(strPtr(pointers[i])^);
  137.  
  138. *******************************************************************************
  139. *                                External Sort                                *
  140. *******************************************************************************
  141.  
  142.   The unit mergsort.pas defines an external mergesort object. This sort is
  143.  used as the basis of file sort routines.
  144.  
  145.   In order to define a sort that will sort YOUR data files you should define
  146.  a descendent of mergeSort that has one virtual function, the compare
  147.  function. This function compares to pointer variables named block1 and
  148.  block2 and returns 1 if the data pointed by block1 is "bigger" then the
  149.  data pointed by block2, 0 if the data pointed by block1 is "equal" to the
  150.  data pointed by block2, and 2, otherwise.
  151.  
  152.   Let us assume that we have a file of myRecord, where myRecord is defined
  153.  as : 
  154.  
  155.  type
  156.         myRecord = record
  157.                 myNum : word;
  158.                 myStatus : integer;
  159.                 myName : string[20];
  160.         end; { myRecord definition }
  161.  
  162.   We will have to use the following code in order to define a sort object 
  163.  that will sort according to the myNum field : 
  164.  
  165.  type
  166.         myExternalSort = object(mergeSort)
  167.  
  168.                 function compare : byte; virtual;
  169.  
  170.         end; { myExternalSort object definition }
  171.  
  172.  function myExternalSort.compare : byte;
  173.  type
  174.         myRecordPtr = ^ myRecord;
  175.  begin
  176.         if (myRecordPtr(block1)^.myNum > myRecordPtr(block2)^.myNum) then
  177.                 compare := 1
  178.         else if (myRecordPtr(block1)^.myNum = myRecordPtr(block2)^.myNum) then
  179.                 compare := 0
  180.         else
  181.                 compare := 2;
  182.  end; {myExternalSort.compare}
  183.  
  184.  In order to use this object we must perform the following steps :
  185.   
  186.  1. Declare a variable of the required sort type.
  187.  2. Initialize the sort object.
  188.  3. Call the sort object's doYourJob method.
  189.  4. Call the sort object's done destructor.
  190.  
  191.  Let us assume we want to sort the file ORGFIL.DTA, which is a file of myRecord,
  192.  to the file SRTFIL.DTA. The code we will use will be :
  193.  
  194.  var
  195.         fileSort : myExternalSort;                      { step 1 }
  196.  begin
  197.         fileSort.init('ORGFIL.DTA', sizeOf(myRecord), 
  198.                       '', 'SRTFIL.DTA');                { step 2 }
  199.         fileSort.doYourJob;                             { step 3 }
  200.         fileSort.done;                                  { step 4 }
  201.   .. { some more code .. }
  202.  
  203.   Notice - The 3rd parameter of the init constructor is the temporary
  204.  path to use during the sort. If we have a RAM disk, large enough to hold
  205.  two files the size of the original file, in drive d:, we can replace
  206.  step 2 with the following code :
  207.  
  208.         fileSort.init('ORGFIL.DTA', sizeOf(myRecord), 
  209.                       'D:\', 'SRTFIL.DTA');             { step 2 }
  210.  
  211.  And the sort will be much faster. Notice - The output file will be on
  212.  drive D:\ at the end of the sort.
  213.  
  214. *******************************************************************************
  215. *                                Combined Sort                                *
  216. *******************************************************************************
  217.  
  218.   A combined sort is an external merge sort that uses an internal quick sort     
  219.  object descendent at the first phase of the sort. Because of this feature
  220.  a combined sort is faster then then the "classic" merge sort - internal sort 
  221.  is fast, there are fewer passes on the temporary files, and thus less I/O
  222.  operations are performed. The only problem with combined sort is the added
  223.  complexity to define and code it. I have tried to minimize this problem
  224.  in the implementation of the combinedSort object provided in the combsort.pas
  225.  unit file. 
  226.  
  227.   Let us use the same example we used in the merge sort section, and try to
  228.  sort the same data file using a combined sort. In order to achieve this
  229.  goal we will have to follow the followin procedure : 
  230.  
  231.  1. Declare a variable of the required sort type, where the sort object is
  232.     a descendent of the combinedSort object, and it is defined the same way
  233.     the merge sort descendent was defined.
  234.  2. Declare a pointer internal sort variable, which performs a compare 
  235.     function similar to the external sort compare function.
  236.  3. Initialize the internal sort, with a nil parameter for the array of 
  237.     pointers.
  238.  4. Initalize the variable declared in step 1 passing the variable declared
  239.     in step 2 as a parameter.
  240.  5. Call the external sort doYourJob method.
  241.  6. Call the external sort done destructor.
  242.  
  243.  Given the same example as the merge sort file the code needed will be :
  244.  
  245.  type
  246.         myInternalSortPtr = ^ myInternalSort;
  247.         myInternalSort = object(quickSort)
  248.  
  249.                 function compare(a, b : pointer) : byte; virtual;
  250.  
  251.         end; { myInternalSort object definition }
  252.  
  253.         myCombinedSort = object(combinedSort)
  254.  
  255.                 function compare : byte; virtual;
  256.  
  257.         end; { myCombinedSort object definition }
  258.  
  259.  var
  260.         misp : myInternalSortPtr;
  261.         mcs  : myCombinedSort;
  262.  
  263.  function doCompare(p1, p2 : pointer) : byte; 
  264.  { this function will compare for both the internal and external sorts }
  265.  type
  266.         myRecordPtr = ^ myRecord;
  267.  begin
  268.         if (myRecordPtr(p1)^.myNum > myRecordPtr(p2)^.myNum) then
  269.                 doCompare := 1
  270.         else if (myRecordPtr(p1)^.myNum = myRecordPtr(p2)^.myNum) then
  271.                 doCompare := 0
  272.         else
  273.                 doCompare := 2;
  274.  end; {doCompare}
  275.  
  276.  function myCombinedSort.compare : byte;
  277.  begin
  278.         compare := doCompare(block1, block2);
  279.  end; {myCombinedSort.compare}
  280.  
  281.  function myInternalSort.compare(a, b : pointer) : byte;
  282.  begin
  283.         compare := doCompare(a, b);
  284.  end; {myInternalSort.compare}
  285.  
  286.  begin
  287.         new(misp, init(1, nil, sizeof(myRecord))); 
  288.         { notice that the first  parameter is not important, put 1 always }
  289.         { notice that the second parameter is not important, put nil }
  290.         myCombinedSort.init('ORGFIL.DTA', sizeOf(myRecord), 
  291.                             'D:\', 'SRTFIL.DTA', misp);
  292.         myCombinedSort.doYourJob;
  293.         myCombinedSort.done;
  294.         ... { rest of code .. }
  295.  
  296. *******************************************************************************
  297. *                                  Warranty                                   *
  298. *******************************************************************************
  299.  
  300.   There is no warranty what so ever, This software package is supplied as is,
  301.  The distributer (ISoft D&M), or the author (Loewy Ron), are not,
  302.  and will not be responsible for any damages, lost profits, 
  303.  or inconveniences caused by the use, or inability to use this package. 
  304.  The use of the package is at your own risk. 
  305.  By using (or attempting to use) the package you agree to this.
  306.  
  307. *******************************************************************************
  308. *                                   General                                   *
  309. *******************************************************************************
  310.  
  311.   sortKit is distributed by ISoft D&M, P.O.B. 5517 CORALVILLE IA 52241, U.S.A.
  312.   
  313.   sortKit is (c) copyrighted by Loewy Ron, 1991, 92.
  314.  
  315.   mouseLib is a shareware package, please register your copy. 
  316.   To register your copy of mouseLib please refer to the supplied
  317.   SORTKIT.REG file. 
  318.  
  319.   Other programs distributed by ISoft D&M are described in the supplied  
  320.   PROGRAMS.TXT file.
  321.  
  322. *******************************************************************************
  323. *                                   Contact                                   *
  324. *******************************************************************************
  325.  
  326.   Please contact :
  327.  
  328.   ISoft D&M,  
  329.   P.O.B 5517
  330.   Coralville IA 52241,
  331.   U.S.A
  332.  
  333. *******************************************************************************
  334. *                                   Credits                                   *
  335. *******************************************************************************
  336.  
  337.   Turbo-Pascal and Paradox are copyrights of Borland International.
  338.