Generics

Introduction

We have covered generics in Oxygene for Java elsewhere. The syntax in Lazarus is slightly different. We have converted our demonstration of the insertion sort for integers to a generic version which we have implemented for both strings and integers. The code of both source files follows a screenshot of the final lines of the output.

End of Output

End of output

Code of GenericSortDemo.pas

program GenericSortDemo;

uses
   Classes, SysUtils, uSorter;
type
  TIntegerSorter = specialize TSorter<integer>;
  TStringSorter =  specialize TSorter<string>;
var
  IntegerSorter : TIntegerSorter;
  IntegerArray : array[1..UPPER_BOUND] of integer = (503, 087, 512, 061, 908, 170, 897, 275,
                                                     653, 426, 154, 509, 612, 677, 765, 703);

  StringSorter : TStringSorter;
  StringArray : array[1..UPPER_BOUND] of string = ('and', 'ton', 'ore', 'but', 'the', 'tor', 'rot', 'dip',
                                                   'off', 'too', 'two', 'one', 'mud', 'dim', 'sit', 'its');
begin
  IntegerSorter := TIntegerSorter.Create;
  writeln('Initial array:');
  IntegerSorter.Items := IntegerArray;
  IntegerSorter.Display;
  IntegerSorter.InsertionSort;

  StringSorter := TStringSorter.Create;
  writeln('Initial array:');
  StringSorter.Items := StringArray;
  StringSorter.Display;
  StringSorter.InsertionSort;
  readln;
end.

Code of uSorter.pas

unit uSorter;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils;

const
  UPPER_BOUND = 16;

type
  generic TSorter<T> = class
  private
    Items: array[1..UPPER_BOUND] of T;
  public
    procedure Display;
    procedure InsertionSort;
  end;

implementation

procedure TSorter.Display;
var
  Count : integer;
begin
  for Count := 1 to UPPER_BOUND do
    write(Items[Count] : 4);
  writeln;
end;

procedure TSorter.InsertionSort;
var
  Inserted : Boolean;
  Current, Insert : integer;
  Temp : T;
begin
  if UPPER_BOUND < 2 then
    begin
      writeln('No sorting possible');
    end
  else
    begin
      for Insert := 2 to UPPER_BOUND do
        begin
          Inserted := False;
          Current := Insert;
          Temp := Items[Insert];
          while not inserted do
            begin
                if Temp > Items[Current - 1] then
                  begin
                    Inserted := True; //Position found;
                    Items[Current] := Temp;
                    writeln(#13#10,'Sorted up to index ', Insert,
                        '. ', Temp,' inserted at position ', Current,':');
                    Display;
                  end
               else
                 begin
                   //Shift up integer one place
                   Items[Current] := Items[Current - 1];
                   dec(Current);
                   if Current = 1 then
                     begin
                       Items[1] := Temp;
                       writeln(#13#10,'Sorted up to index ', Insert,
                        '. ', Temp, ' inserted at position 1:');
                       Inserted := True;
                       Display;
                     end;
                 end;
            end;//while
        end;//for
     end;//if
end;//proc

end.

 

Programming - a skill for life!

Developments in Pascal, Oxygen for Java and Smart Mobile Studio