-- Demonstrates creating and calling a generic array sort
with ada.text_io; use ada.text_io;
with ada.float_text_io; use ada.float_text_io;
with Ada.Containers.Generic_Array_Sort;
use Ada.Containers;
procedure generic_sort is
type Flt_Array is array(Natural range <>) of Float;
procedure put_line(a: Flt_Array) is
begin
for f of a loop
put(f, 5, 1, 0);
end loop;
new_line;
end put_line;
procedure fsort is new Ada.Containers.Generic_Array_Sort(
Index_Type => Natural,
Element_Type => Float,
Array_Type => Flt_Array);
procedure revfsort is new Ada.Containers.Generic_Array_Sort(
Index_Type => Natural,
Element_Type => Float,
Array_Type => Flt_Array,
"<" => ">");
-- Use greater than for comparison to sort in reverse
-- Procedure fsort uses default "<"
---------------------------------------------------
type Char_Array is array(Natural range <>) of Character;
procedure put_line(a: Char_Array) is
begin
for c of a loop
put(c);
end loop;
new_line;
end put_line;
procedure sort is new
Ada.Containers.Generic_Array_Sort(Natural, Character, Char_Array);
---------------------------------------------------
subtype String10 is String(1 .. 10);
type String_Array is array(Natural range <>) of String10;
procedure put_line(a: String_Array) is
begin
for c of a loop
put(c);
end loop;
new_line;
end put_line;
procedure sort is new
Generic_Array_Sort(Natural, String10, String_Array);
-- Allowed because of use Ada.Containers
---------------------------------------------------
my_floats: Flt_Array(1 .. 4) := (20.0, 30.0, 10.0, 25.0);
my_chars : Char_Array(1 .. 4) := ('j', 'p', 'b', 'n');
begin
put_line(my_floats);
revfsort(my_floats);
put_line(my_floats);
put_line(my_chars);
sort(my_chars);
put_line(my_chars);
end generic_sort;
-- Output
-- 20.0 30.0 10.0 25.0
-- 30.0 25.0 20.0 10.0
-- jpbn
-- bjnp