Re: Adding functions to generic package
- From: Preben Randhol <randhol+valid_for_reply_from_news@xxxxxxx>
- Date: 28 May 2005 20:18:48 +0200
On 2005-05-28, Matthew Heaney <matthewjheaney@xxxxxxxxxxxxx> wrote:
> Preben Randhol <randhol+cla@xxxxxxx> writes:
> If you want to take advantage of the representation of the list
> container type, then you would have to create a child. Otherwise, if
> your algorithm can be implemented in terms of the already-existing
> (primitive) operations of the type, then it doesn't have to be a child.
>
> You could do something like:
>
> with Charles.Lists.Double.Unbounded;
>
> generic
> with package P is new Charles.Lists.Double.Unbounded (<>);
> use P;
> package Generic_List_Ops is
> procedure Randomize (Container : in out Container_Type);
> procedure Move (...);
> end;
I see.
> If these are truly generic algorithms, then you could implement them as,
> well, generic algorithms, and then either use those as is, or use them
> to implement the generic package above.
>
> What do Randomize and Move do? If you have a tentative implementation,
> then post it (or just mail it to me) and we can figure what is the best
> option for you.
It should be called Randomize_Container and be equivelent to the
Reverse_Container procedure except that it makes the order of the
elements random. I guess this doesn't seem to make sense for a List, but
I need it for a program that is asking questions from a list.
What Move does it to move element number 2 to number 4. If you have a
list perhaps the user wants to rearrange the order and drags one element
to another place in the list (I'm not talking GUI-wise). Then I noticed
it is a bit cumbersome not to have a move routine.
The procedures are give below:
-----------------------
procedure Move
(Container : in out Container_Type;
From : Natural;
To : Natural)
is
From_Iterator : Iterator_Type;
To_Iterator : Iterator_Type;
Iter : Iterator_Type := First (Container);
Last_Element : Natural;
begin
if To > From then
Last_Element := To;
elsif From > To then
Last_Element := From;
end if;
if To /= From and then Last_Element <= Length (Container) then
for I in 1 .. Last_Element loop
if I = To then
To_Iterator := Iter;
elsif I = From then
From_Iterator := Iter;
end if;
Increment (Iter);
end loop;
if To = Length (Container) then
Append (Container, Element (From_Iterator));
elsif To > From then
Insert (Container, Succ (To_Iterator),
Element (From_Iterator));
elsif To < From then
Insert (Container, To_Iterator,
Element (From_Iterator));
end if;
Delete (Container, From_Iterator);
end if;
end Move;
-----------------------
procedure Randomise_Container
(Container : in out Container_Type;
Loops : Natural)
is
subtype Container_Range is Integer range 1 .. Length (Container);
package Random_Container is new
Ada.Numerics.Discrete_Random (Container_Range);
use Random_Container;
Seed : Generator;
Number : Natural;
begin
Reset (Seed);
for I in 1 .. loops loop
Reset (Seed);
Reset (Seed);
for Current in Container_Range loop
Number := Random (Seed);
Move (Container, From => Current, To => Number);
end loop;
end loop;
end Randomise_Container;
-----------------------
.
- Follow-Ups:
- Re: Adding functions to generic package
- From: Matthew Heaney
- Re: Adding functions to generic package
- From: Preben Randhol
- Re: Adding functions to generic package
- References:
- Adding functions to generic package
- From: Preben Randhol
- Re: Adding functions to generic package
- From: Matthew Heaney
- Adding functions to generic package
- Prev by Date: Re: Adding functions to generic package
- Next by Date: Re: Adding functions to generic package
- Previous by thread: Re: Adding functions to generic package
- Next by thread: Re: Adding functions to generic package
- Index(es):