• Arrays with discriminated task components

    From AdaMagica@21:1/5 to All on Sat Dec 24 03:44:27 2022
    I've got a task type with a discriminant:

    type Index is range 1 .. N;

    task type T (D: Index);

    Now I want an array of these tasks, where each task knows its identity (the index) via the discriminant, an iterated_component_association:

    Arr: array (Index) of T := (for I in Index => ???);

    How can I do this?

    This works with access, but I find this extremely ugly:

    Arr: array (Index) of access T := (for I in Index => new T (I));

    Alternatively, I could use the traditional method with a Start entry with the index as parameter:

    task type T is
    entry Start (D: Index);
    end T;

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Niklas Holsti@21:1/5 to AdaMagica on Sat Dec 24 20:05:19 2022
    On 2022-12-24 13:44, AdaMagica wrote:
    I've got a task type with a discriminant:

    type Index is range 1 .. N;

    task type T (D: Index);

    Now I want an array of these tasks, where each task knows its
    identity (the index) via the discriminant, an
    iterated_component_association: >
    Arr: array (Index) of T := (for I in Index => ???);

    How can I do this?


    One way is to give the discrimant a default value that is a function
    call that returns a new identifier on each call:

    Next_Index : Index := Index'First;
    -- The value returned by the next call of New_Index.

    function New_Index return Index
    -- Returns a unique Index value (up to N).
    is
    Result : constant Index := Next_Index;
    begin
    if Next_Index < Index'Last then
    Next_Index := Next_Index + 1;
    -- else report error?
    end if;
    return Result;
    end New_Index;

    task type T (D: Index := New_Index);

    Then you can declare the array without any initial value:

    Arr: array (Index) of T;

    and the initialization of each task in the array makes its own call to New_Index and gets its own identifier value.

    A bit sneaky, but has the advantage that it extends automatically to
    two arrays of tasks, or one array and some separate single declarations
    of tasks, etc.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jeffrey R.Carter@21:1/5 to Niklas Holsti on Sat Dec 24 23:41:34 2022
    On 2022-12-24 19:05, Niklas Holsti wrote:
    On 2022-12-24 13:44, AdaMagica wrote:

    Now I want an array of these tasks, where each task knows its identity (the >> index) via the discriminant, an
    iterated_component_association: >
    Arr: array (Index) of T := (for I in Index => ???);

    One way is to give the discrimant a default value that is a function call that
    returns a new identifier on each call:

    No, this does not guarantee that the task's discriminant is its index in the array, which is a requirement of the question.

    --
    Jeff Carter
    "My mind is aglow with whirling, transient nodes of
    thought, careening through a cosmic vapor of invention."
    Blazing Saddles
    85

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Niklas Holsti@21:1/5 to Jeffrey R.Carter on Sun Dec 25 17:32:43 2022
    On 2022-12-25 0:41, Jeffrey R.Carter wrote:
    On 2022-12-24 19:05, Niklas Holsti wrote:
    On 2022-12-24 13:44, AdaMagica wrote:

    Now I want an array of these tasks, where each task knows its
    identity (the index) via the discriminant, an
    iterated_component_association: >
    Arr: array (Index) of T := (for I in Index => ???);

    One way is to give the discrimant a default value that is a function
    call that returns a new identifier on each call:

    No, this does not guarantee that the task's discriminant is its index in
    the array, which is a requirement of the question.


    Right, I did not take that requirement into account, apologies for my inattention. Indeed the tasks in the array are initialized in an
    arbitrary order, not necessarily in increasing index order.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Niklas Holsti@21:1/5 to AdaMagica on Sun Dec 25 18:16:56 2022
    On 2022-12-24 13:44, AdaMagica wrote:
    I've got a task type with a discriminant:

    type Index is range 1 .. N;

    task type T (D: Index);

    Now I want an array of these tasks, where each task knows its
    identity (the index) via the discriminant, an iterated_component_association:

    Arr: array (Index) of T := (for I in Index => ???);

    How can I do this?

    This works with access, but I find this extremely ugly:

    Arr: array (Index) of access T := (for I in Index => new T (I));

    Alternatively, I could use the traditional method with a Start entry with the index as parameter:

    task type T is
    entry Start (D: Index);
    end T;


    This seems to work with gnat, but I'm not entirely sure if it is legal
    (could there be a conflict between the default value of the task
    discriminant, which is the same for all tasks in the array, and the
    actual discriminants which are different for each task in the array?):

    N : constant := 10;

    type Index is range 1 .. N;

    task type T (D: Index := Index'First);
    -- A default value for D is needed to make the type constrained, as
    -- required by the Arr declaration below.

    function New_T (I : in Index)
    return T
    is
    begin
    return TI : T (D => I)
    do
    null;
    end return;
    end New_T;

    Arr: array (Index) of T := (for I in Index => New_T(I));

    Whether this is any less ugly than the heap allocation method is doubtful.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From AdaMagica@21:1/5 to All on Mon Dec 26 08:39:23 2022
    Thanx, Niklas and Jeffrey. I just didn't think of the generator function. Christoph

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)