Ada 95 Quality and Style Guide Chapter 8

Chapter 8: Reusability - TOC - 8.3 ADAPTABILITY

8.3.4 Using Generic Units to Encapsulate Algorithms


  • Use generic units to encapsulate algorithms independently of data type.

  • example

    This is the specification of a generic sort procedure:

       type Element is private;
       type Data    is array (Positive range <>) of Element;
       with function Should_Precede (Left  : in     Element;
                                     Right : in     Element)
              return Boolean is <>;
     with procedure Swap (Left  : in out Element;
                            Right : in out Element) is <>;
    procedure Generic_Sort (Data_To_Sort : in out Data);

    The generic body looks just like a regular procedure body and can make full use of the generic formal parameters in implementing the sort algorithm:

    procedure Generic_Sort (Data_To_Sort : in out Data) is
       for I in Data_To_Sort'Range loop
             if Should_Precede (Data_To_Sort(J), Data_To_Sort(I)) then
                Swap(Data_To_Sort(I), Data_To_Sort(J));
             end if;
       end loop;
    end Generic_Sort;

    The generic procedure can be instantiated as:

       type Integer_Array is array (Positive range <>) of Integer;
       function Should_Precede (Left  : in     Integer;
                                Right : in     Integer)
         return Boolean;
       procedure Swap (Left  : in out Integer;
                       Right : in out Integer);
       procedure Sort is
          new Generic_Sort (Element => Integer,
                            Data    => Integer_Array);


       subtype String_80    is String (1 .. 80);
       type    String_Array is array (Positive range <>) of String_80;
       function Should_Precede (Left  : in     String_80;
                                Right : in     String_80)
         return Boolean;
       procedure Swap (Left  : in out String_80;
                       Right : in out String_80);
       procedure Sort is
          new Generic_Sort (Element => String_80,
                            Data    => String_Array);

    and called as:

       Integer_Array_1 : Integer_Array (1 .. 100);
       Sort (Integer_Array_1);


       String_Array_1  : String_Array  (1 .. 100);
       Sort (String_Array_1);


    A sort algorithm can be described independently of the data type being sorted. This generic procedure takes the Element data type as a generic limited private type parameter so that it assumes as little as possible about the data type of the objects actually being operated on. It also takes Data as a generic formal parameter so that instantiations can have entire arrays passed to them for sorting. Finally, it explicitly requires the two operators that it needs to do the sort: Should_Precede and Swap. The sort algorithm is encapsulated without reference to any data type. The generic can be instantiated to sort an array of any data type.

    < Previous Page Search Contents Index Next Page >
    1 2 3 4 5 6 7 8 9 10 11
    Appendix References Bibliography