home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / ada_1 / Examples_ada_shelltest < prev    next >
Encoding:
Text File  |  1994-10-14  |  2.3 KB  |  82 lines

  1. -- ++
  2. -- Program to test the shell sort generic.
  3. -- An array of pointers to records is sorted via shell sort. The pointers
  4. -- are swapped rather than swapping the record for speed.
  5. -- --
  6.  
  7. with Calendar;
  8. with Integer_IO;
  9. with Positive_IO;
  10. with Random;
  11. with Shell_Sort;
  12. with Text_IO;
  13. procedure Shell_Sort_Test is
  14.  
  15.    Count : Positive;
  16.    Num   : Positive;
  17.  
  18.    procedure Shell_Test ( Num : in Positive; Count : in Positive ) is
  19.       type Element is
  20.      record
  21.         Key1 : Integer;
  22.         Key2 : Integer;
  23.      end record;
  24.       type Key_Index is new Positive range 1 .. Num;
  25.       subtype Index is Positive range 1 .. Num;
  26.       type Sort_Array is array (Key_Index range <>) of Element;
  27.       type Sort_Array_Pointers is array (Index range <>) of Key_Index;
  28.  
  29.       Keys         : Sort_Array(Key_Index);
  30.       Key_Pointers : Sort_Array_Pointers(Index);
  31.       Time       : Calendar.Time;
  32.       CPU       : Duration;
  33.  
  34.       function "<" ( L : in Key_Index; R : in Key_Index ) return Boolean is
  35.       begin
  36.          return ( Keys(L).Key1 < Keys(R).Key1 ) OR ELSE
  37.         ( ( Keys(L).Key1 = Keys(R).Key1 ) AND THEN
  38.           ( Keys(L).Key2 < Keys(R).Key2 ) );
  39.       end "<";
  40.       pragma INLINE ("<");
  41.  
  42.       package Sort_Test is new Shell_Sort ( Item  => Key_Index,
  43.                         Index => Index,
  44.                         Items => Sort_Array_Pointers,
  45.                         "<"   => "<" );
  46.    begin
  47.       for I in Key_Index loop
  48.      Keys(i).Key1 := Random.Random MOD 360;
  49.      Keys(i).Key2 := Random.Random MOD 1000;
  50.       end loop;
  51.       
  52.       time := Calendar.Clock;
  53.       for J in 1 .. Count loop
  54.          for I in Index loop
  55.         Key_pointers(I) := Key_Index(I);
  56.      end loop;
  57.      Sort_Test.Sort ( Key_pointers );
  58.       end loop;
  59.       CPU := Calendar."-" ( Calendar.Clock, time);
  60.       Text_IO.New_Line;
  61.       Text_IO.Put ( "Time to sort " );
  62.       Integer_IO.Put ( Num );
  63.       Text_IO.Put ( " items = " );
  64.       Integer_IO.Put ( Integer(CPU*1000)/count );
  65.       Text_IO.Put_Line ( "ms" );    
  66.  
  67.       for I in Index loop
  68.      Integer_IO.Put ( Keys(Key_pointers(I)).Key1 );
  69.      Integer_IO.Put ( Keys(Key_pointers(I)).Key2 );
  70.      Text_IO.New_Line;
  71.       end loop;
  72.  
  73.    end Shell_Test;
  74.  
  75. begin
  76.    Text_IO.Put ( "Number of elements in array to be sorted ? " );
  77.    Positive_IO.Get ( Num );
  78.    Text_IO.Put ( "Number of sorts to perform ? " );
  79.    Positive_IO.Get ( Count );
  80.    Shell_Test ( Num, Count );
  81. end Shell_Sort_Test;
  82.