В Ada вы можете указать, по каким индексам итерироваться:
-- Declarations
Start : Index_Type;
Finish : Index_Type;
-- Usage
Start := -- Pick your start
Finish := -- Pick your end
for Index in Start .. Finish loop
-- do your stuff
end loop;
-- Example
with Ada.Text_IO; use Ada.Text_IO;
procedure Test is
type Index_Type is (Red, Blue, Green);
type Array_Type is array(Index_Type range <>) of Integer;
My_Array : Array_Type(Index_Type'Range) := (1,2,3);
Start, Finish : Index_Type;
begin
Start := Blue;
Finish := Green;
for Index in Start .. Finish loop
Put_Line(My_Array(Index)'Image);
end loop;
Put_Line("Hello World");
end Test;
, где Start и End могут быть любым типом индекса, который вы хотите. Или вы можете просто перебрать все из них, если хотите, и позволить компилятору определить, какой из них является первым и последним.
Это работает для любого типа, который может быть индексом массива (Enumerations, Integers, et c.).
Для любого типа индекса вы можете делать такие вещи, как:
Index_Type'First
Index_Type'Last
Index_Type'Succ(value)
Index_Type'Pred(value)
My_Array'Length
My_Array'Range
среди многих других. Это должно позволить вам делать то, что вам нужно для математического анализа (опять же, независимо от типа индекса). См. Некоторые примеры ниже
for Index in My_Array'Range loop
if Index /= My_Array'First then
-- do stuff here
end if;
end loop;
if My_Array'First /= Index_Type'Last then
for Index in Index_Type'Succ(My_Array'First) .. My_Array'Last loop
-- Do your stuff
end loop;
end if;