From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,7e81a70d49e1dad0 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Newsgroups: comp.lang.ada From: Preben Randhol Subject: Re: Adding functions to generic package References: <429891d3$1@news.broadpark.no> Organization: PVV User-Agent: slrn/0.9.8.1 (Debian) NNTP-Posting-Host: 65.80-202-208.nextgentel.com X-Original-NNTP-Posting-Host: 65.80-202-208.nextgentel.com Message-ID: <4298b608$1@news.broadpark.no> Date: 28 May 2005 20:18:48 +0200 X-Trace: news.broadpark.no 1117304328 65.80-202-208.nextgentel.com (28 May 2005 20:18:48 +0200) Path: g2news1.google.com!news3.google.com!news.glorb.com!newspeer2.se.telia.net!se.telia.net!fi.sn.net!newsfeed2.fi.sn.net!feeder2.news.jippii.net!feeder1.news.jippii.net!news.net.hanse.com!nntp.gblx.net!nntp3.phx1!news.broadpark.no Xref: g2news1.google.com comp.lang.ada:11196 Date: 2005-05-28T20:18:48+02:00 List-Id: On 2005-05-28, Matthew Heaney wrote: > Preben Randhol 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; -----------------------