From: "Steven Deller" <deller@smsail.com>
Subject: RE: Generation of permutations
Date: Fri, 10 May 2002 21:52:29 -0400
Date: 2002-05-10T21:52:29-04:00 [thread overview]
Message-ID: <mailman.1021085642.13006.comp.lang.ada@ada.eu.org> (raw)
In-Reply-To: <3CCEB2E0.7050701@mail.com>
[-- Attachment #1: Type: text/plain, Size: 1419 bytes --]
Write one way back when starting Ada 83 (1984). Based on Djkstra's
algoritm in A Discipline of Programming. Is this what you want. (Sorry
for the long delay -- I only get to read CLA once every few weeks when
flying on a plane). It only does a next perumutation, but a "previous"
permutation is pretty simple to devise. Use WP's to even prove it :-).
Regards,
Steve Deller
> -----Original Message-----
> From: comp.lang.ada-admin@ada.eu.org
> [mailto:comp.lang.ada-admin@ada.eu.org] On Behalf Of Hyman Rosen
> Sent: Tuesday, April 30, 2002 11:06 AM
> To: comp.lang.ada@ada.eu.org
> Subject: Re: Generation of permutations
>
>
> Ted Dennison wrote:
> > Reinert Korsnes <reinert.korsnes@chello.no> wrote in message
> > news:<aajce7$7jb$1@snipp.uninett.no>...
> >
> >>Are there any good (Ada95) packages for generation of permutations
> >>(of elements in array) ?
> >
> > Sadly, that doesn't come up much...outside of homework assignments.
> > ;-) Are you writing a bogosort?
>
> Whether or not it comes up much, in fact the algorithms
> next_permutation and prev_permutation are part of the C++
> standard library. You can find them online in
<http://www.sgi.com/tech/stl/stl_algo.h>. It should not be too difficult
for the OP to translate them to Ada.
_______________________________________________
comp.lang.ada mailing list
comp.lang.ada@ada.eu.org
http://ada.eu.org/mailman/listinfo/comp.lang.ada
[-- Attachment #2: permuting.a --]
[-- Type: application/octet-stream, Size: 1094 bytes --]
------------------------------------------------------------------------------
-- package PERMUTING
-- Provides a Permute generic package. The generic parameters are an element
-- of any type, an index of discrete type, and an ordering function. The
-- package defines a list data type and procedure. The procedure takes a
-- single argument of list type, and rearranges it to the next lexical
-- permutation. The exception "no_more_permutations" is raised when the
-- input list is lexically the last permutation.
-- The algorithm is derived from that presented in Dijkstra's "A Discipline of
-- Programming".
------------------------------------------------------------------------------
package Permuting is
generic
type Element is private;
type Index is (<>);
with function "<=" ( left, right: Element ) return Boolean is <>;
package Permute is
type List is array (Index range <>) of Element;
procedure Permute ( L: in out List );
No_more_permutations : exception ;
end Permute ;
pragma Share_Body ( Permute , False ) ;
end Permuting ;
[-- Attachment #3: permutingB.a --]
[-- Type: application/octet-stream, Size: 2067 bytes --]
------------------------------------------------------------------------------
-- package body PERMUTING
------------------------------------------------------------------------------
package body Permuting is
package body Permute is
procedure Exchange ( I, J: in out Element ) is
Temp: Element;
begin
Temp := I;
I := J;
J := Temp;
end Exchange;
pragma Inline ( Exchange );
procedure Permute ( L: in out List ) is
I, J : Index ;
begin -- procedure Permute
-- Find the list tail, the largest end sequence that is all in order.
-- Find I, the last list position (largest i) where L(I) < L(I+1)
-- and the list tail is then all elements from L(I+1) on.
I := Index'pred( L'last ) ;
while ( I >= L'first and then L(Index'succ(I)) <= L(I) ) loop
I := Index'pred(I) ;
end loop ;
if -- no such I exists (complete list is monotonically decreasing)
( I < L'first )
then
raise No_more_permutations ; -- because list is already lexically last
end if ;
-- Find J in the tail, such that L(J) is the smallest value
-- for which L(J) > L(I).
-- Note that L(I+1) > L(I) so at least one tail value is
-- larger than L(I), though there are usually more.
-- Note also that the tail is in decreasing order, so we need only
-- search from the end backwards until we find a value that is larger.
J := L'last ;
while ( L(J) <= L(I) ) loop
J := Index'pred(J) ;
end loop ;
Exchange( L(I), L(j) ) ;
-- Put values in the tail into monotonically increasing order.
-- Note that the tail is monotonically decreasing, so we need only
-- reverse the order of values in the tail.
I := Index'succ(I) ;
J := L'last ;
while ( I < J ) loop
Exchange( L(I), L(J) ) ;
I := Index'succ(I) ;
J := Index'pred(J) ;
end loop ;
-- List now contains the next lexical permutation of its values.
end Permute ;
begin -- package Permute
null ;
end Permute ;
begin
null ;
end Permuting ;
next prev parent reply other threads:[~2002-05-11 1:52 UTC|newest]
Thread overview: 88+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-04-29 11:54 Generation of permutations Reinert Korsnes
2002-04-30 13:52 ` Ted Dennison
2002-04-30 14:20 ` Marin David Condic
2002-05-02 12:32 ` Robert Dewar
2002-05-02 15:47 ` Ted Dennison
2002-05-02 16:16 ` Mark Biggar
2002-05-03 13:04 ` Marin David Condic
2002-05-05 0:52 ` Robert Dewar
2002-05-05 23:11 ` tmoran
2002-05-06 2:13 ` Chad R. Meiners
2002-05-06 13:52 ` Stephen Leake
2002-05-09 17:44 ` Darren New
2002-05-09 18:07 ` Stephen Leake
2002-05-09 20:58 ` Darren New
2002-05-09 23:21 ` tmoran
2002-05-09 23:51 ` Darren New
2002-05-10 3:37 ` tmoran
2002-05-10 3:59 ` Darren New
2002-05-10 13:13 ` Robert Dewar
2002-05-25 16:21 ` Robert I. Eachus
2002-05-09 23:24 ` Robert Dewar
2002-05-09 23:48 ` Robert Dewar
2002-05-10 3:37 ` tmoran
2002-05-10 15:10 ` Chad R. Meiners
2002-05-11 4:04 ` Robert Dewar
2002-05-16 1:35 ` Chad R. Meiners
2002-05-11 4:05 ` Robert Dewar
2002-05-06 15:46 ` Wes Groleau
2002-05-06 16:21 ` Chad R. Meiners
2002-05-06 16:33 ` Darren New
2002-05-07 0:06 ` tmoran
2002-05-07 0:26 ` Darren New
2002-05-07 1:56 ` tmoran
2002-05-07 10:39 ` Robert Dewar
2002-05-07 17:25 ` Chad R. Meiners
2002-05-08 2:27 ` Robert Dewar
2002-05-08 8:44 ` Mats Karlssohn
2002-05-07 17:00 ` Wes Groleau
2002-05-06 21:33 ` Robert Dewar
2002-05-06 17:26 ` Marin David Condic
2002-05-07 7:35 ` tmoran
2002-05-07 13:22 ` Marin David Condic
2002-05-08 5:23 ` tmoran
2002-05-08 14:10 ` Marin David Condic
2002-05-09 16:20 ` Darren New
2002-05-09 19:04 ` tmoran
2002-05-08 16:20 ` Darren New
2002-05-08 17:31 ` tmoran
2002-05-08 17:39 ` Chad R. Meiners
2002-05-07 15:34 ` Darren New
2002-05-07 17:44 ` Chad R. Meiners
2002-05-07 19:58 ` tmoran
2002-05-07 21:05 ` Turing-undecidable languages (OT) Chad R. Meiners
2002-05-08 8:24 ` Danx
2002-05-08 17:16 ` Chad R. Meiners
2002-05-10 2:37 ` Robert Dewar
2002-05-08 9:16 ` Dmitry A. Kazakov
2002-05-08 17:18 ` Chad R. Meiners
2002-05-09 20:56 ` Dmitry A.Kazakov
2002-05-09 16:18 ` Chad R. Meiners
2002-05-10 2:52 ` Robert Dewar
2002-05-08 2:17 ` Generation of permutations Robert Dewar
2002-05-03 13:13 ` Ted Dennison
2002-05-03 13:24 ` Lutz Donnerhacke
2002-04-30 15:06 ` Hyman Rosen
2002-05-01 8:40 ` Adrian Hoe
2002-05-01 19:53 ` Hyman Rosen
2002-05-11 1:52 ` Steven Deller [this message]
2002-05-02 16:24 ` Mark Biggar
2002-04-30 17:12 ` Wes Groleau
2002-04-30 22:57 ` Robert Dewar
2002-05-01 0:54 ` tmoran
2002-05-01 9:42 ` Florian Weimer
2002-05-02 12:34 ` Robert Dewar
2002-05-01 12:43 ` Robert Dewar
2002-05-01 15:05 ` TO WHOM IT MAY CONCERN Wes Groleau
2002-05-02 12:27 ` More on copyright, (Re: TO WHOM IT MAY CONCERN) Robert Dewar
2002-05-08 13:56 ` Wes Groleau
2002-05-08 18:01 ` Robert Dewar
2002-05-08 18:31 ` Hyman Rosen
2002-05-09 13:41 ` Wes Groleau
2002-05-01 12:46 ` Generation of permutations Robert Dewar
2002-05-01 18:22 ` OT:Copyright, was " tmoran
2002-05-01 21:56 ` Robert Dewar
2002-05-01 23:45 ` tmoran
2002-05-02 11:58 ` Robert Dewar
2002-05-01 14:55 ` Wes Groleau
2002-05-02 12:41 ` Robert Dewar
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox