-- combinations.adb instantiate with Item_Type and Vector_Type -- -- type Vector_Type is array(integer range <>) of Item_Type; -- procedure My_Combinations is new Combinations(Item_Type, Vector_Type); -- -- usage: -- package My_Comb is new Combinations(max_size, item_type, vector_type); -- -- First := True; -- Last := False; -- while not last loop -- My_Comb.Comb(N, M, Vn, Vm, First, Last); -- -- use the generated Vm array -- end loop; -- -- N is for n things n! / ((n-m)! m!) combinations -- M is for m at a time -- Vn must be the N items of Vector_Type, unchanged by combinations -- Vm must be of size M items of Vector_type, -- the returned combination each call -- First must be true the first call, then set to false internally -- Last is false if more combinations to come. -- Last is True for the final combination with Ada.Text_Io; use Ada.Text_Io; package body Combinations is procedure Comb(N : Integer; M: Integer; Vn : in Vector_Type; Vm : in out Vector_Type; First : in out Boolean; Last : in out Boolean) is J : Integer; Debug : Boolean := False; Empty : Integer := -1; begin -- Vm(Vm'First+I) := Vn(Vn'First+I); if M<1 or M>N then Put_Line("bad parameters to combination, N:="&Integer'Image(N)& ", M:="&Integer'Image(M)); Last := True; return; end if; if First then First := False; for I in 0..N-1 loop if I