I am looking for suggestions for a more efficient algorithm to determine if an array contains all the values from 1 through the length of the array. The solution I have devised works correctly using Ada2012.
------------------------------------------------------------------
-- Build a function to determine whether an array contains --
-- all the values from 1 through the length of the array. --
------------------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
procedure Sequence_test is
type Sequence_Array is array(Positive range <>) of Integer;
function Is_Sequence(Item : Sequence_Array) return Boolean is
Flags : Array(Positive range 1..Item'Length) of Boolean := (Others => False);
begin
for Num of Item loop
if Num in Flags'Range then
Flags(Num) := True;
else
exit;
end if;
end loop;
return (for all P of Flags => P = True);
end Is_Sequence;
A : Sequence_Array := (1,2,3,4,5,6);
B : Sequence_Array := (6,5,4,3,2,1);
C : Sequence_Array := (1,1,1,6,6,6);
D : Sequence_Array := (1,2,3,4,6);
E : Sequence_Array := (6,1,5,2,4,3);
F : Sequence_Array := (1,1,1,2,3,4,5,9,10,11);
begin
Put_Line("A is " & Boolean'Image(Is_Sequence(A)));
Put_Line("B is " & Boolean'Image(Is_Sequence(B)));
Put_Line("C is " & Boolean'Image(Is_Sequence(C)));
Put_Line("D is " & Boolean'Image(Is_Sequence(D)));
Put_Line("E is " & Boolean'Image(Is_Sequence(E)));
Put_Line("F slice is " & Boolean'Image(Is_Sequence(F(3..7))));
end Sequence_test;
The output of my program is
A is TRUE
B is TRUE
C is FALSE
D is FALSE
E is TRUE
F slice is TRUE