Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #384263
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Recursion, Yo |
| Date | 2024-04-10 10:14 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <86cyqx16p2.fsf@linuxsc.com> (permalink) |
| References | <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org> <uv2u2a$41j5$1@dont-email.me> <87edbestmg.fsf@bsb.me.uk> <uv4r9e$mdd3$1@dont-email.me> |
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
> On Tue, 09 Apr 2024 11:44:23 +0100, Ben Bacarisse wrote:
>
>> It's significant (or at least note worthy) that the code in the
>> original post was not ISO C ...
>
> Interesting that GCC?s C compiler allows nested routine definitions,
> but the C++ compiler does not.
>
>> and re-writing it in ISO C would show up some of the issues involved
>> ...
>
> Ask and you shall receive ...
>
> /*
> Generate permutations of a list of items.
> Pass a list of arbitary words as command arguments, and this
> program will print out all possible orderings of them.
> */
>
> #include <iso646.h>
> #include <stdbool.h>
> #include <stdlib.h>
> #include <stdio.h>
>
> typedef void (*permute_action)
> (
> unsigned int nrwords,
> const char * const * words,
> void * arg
> );
>
> struct permute_ctx
> {
> unsigned int nrwords;
> const char * const * words;
> permute_action action;
> void * action_arg;
> const char ** permbuf;
> bool * used;
> } /*permute_ctx*/;
>
> static void permute1
> (
> struct permute_ctx * ctx,
> unsigned int depth
> )
> {
> if (depth < ctx->nrwords)
> {
> for (unsigned int i = 0; i < ctx->nrwords; ++i)
> {
> if (not ctx->used[i])
> {
> ctx->permbuf[depth] = ctx->words[i];
> ctx->used[i] = true;
> permute1(ctx, depth + 1);
> ctx->used[i] = false;
> } /*if*/
> } /*for*/
> }
> else
> {
> ctx->action(ctx->nrwords, ctx->permbuf, ctx->action_arg);
> } /*if*/
> } /*permute1*/
>
> void permute
> (
> unsigned int nrwords,
> const char * const * words,
> permute_action action,
> void * action_arg
> )
> {
> if (nrwords > 0)
> {
> struct permute_ctx ctx;
> ctx.nrwords = nrwords;
> ctx.words = words;
> ctx.action = action;
> ctx.action_arg = action_arg;
> ctx.permbuf = (const char **)malloc(nrwords * sizeof(char *));
> ctx.used = (bool *)malloc(nrwords);
> for (unsigned int i = 0; i < nrwords; ++i)
> {
> ctx.used[i] = false;
> } /*for*/
>
> permute1(&ctx, 0);
>
> free(ctx.permbuf);
> free(ctx.used);
> } /*if*/
> } /*permute*/
>
> [...]
More complicated than it needs to be. Besides that, in a beauty
contest this coding style would lose out even to Frankenstein.
Simpler:
#include <stdlib.h>
#include <string.h>
typedef void *Voidp;
typedef struct array_CB_s ArrayCB;
struct array_CB_s {
void (*run)( ArrayCB *cb, unsigned n, const Voidp[ n ] );
};
static void permute_r( unsigned n, Voidp[n], unsigned, ArrayCB * );
static void voidp_exchange( Voidp [], unsigned, unsigned );
void
permute( unsigned n, const Voidp in_array[n], ArrayCB *cb ){
for( Voidp *pva = malloc( n * sizeof *pva ); pva; pva = 0 ){
memcpy( pva, in_array, n * sizeof *pva );
permute_r( n, pva, n, cb );
free( pva );
}
}
void
permute_r( unsigned n, Voidp values[n], unsigned k, ArrayCB *cb ){
for( unsigned i = 0; i < k; i++ ){
voidp_exchange( values, n-k, n-k+i );
permute_r( n, values, k-1, cb );
voidp_exchange( values, n-k, n-k+i );
}
if( k == 1 ) cb->run( cb, n, values );
}
void
voidp_exchange( Voidp values[], unsigned i, unsigned j ){
if( i != j ){
Voidp pi = values[i], pj = values[j];
values[i] = pj, values[j] = pi;
}
}
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar
Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-07 02:58 +0000
Re: Recursion, Yo fir <fir@grunge.pl> - 2024-04-07 11:52 +0200
Re: Recursion, Yo Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-09 10:25 +0200
Re: Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-09 08:43 +0000
Re: Recursion, Yo Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-09 14:27 +0200
Re: Recursion, Yo Ben Bacarisse <ben.usenet@bsb.me.uk> - 2024-04-09 15:27 +0100
Re: Recursion, Yo Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-09 21:55 +0200
Re: Recursion, Yo Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-04-09 13:31 -0700
Re: Recursion, Yo Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-10 08:30 +0200
Re: Recursion, Yo bart <bc@freeuk.com> - 2024-04-09 15:02 +0100
Re: Recursion, Yo Ben Bacarisse <ben.usenet@bsb.me.uk> - 2024-04-09 11:44 +0100
Re: Recursion, Yo Ben Bacarisse <ben.usenet@bsb.me.uk> - 2024-04-09 13:25 +0100
Re: Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-10 01:50 +0000
Re: Recursion, Yo "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-04-09 20:54 -0700
Re: Recursion, Yo David Brown <david.brown@hesbynett.no> - 2024-04-10 09:11 +0200
Re: Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-10 07:52 +0000
Re: Recursion, Yo David Brown <david.brown@hesbynett.no> - 2024-04-10 11:18 +0200
Re: Recursion, Yo bart <bc@freeuk.com> - 2024-04-10 13:42 +0100
Re: Recursion, Yo David Brown <david.brown@hesbynett.no> - 2024-04-10 16:46 +0200
Re: Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-11 00:18 +0000
Re: Recursion, Yo Kaz Kylheku <643-408-1753@kylheku.com> - 2024-04-11 00:54 +0000
Heh heh... (Was: Recursion, Yo) gazelle@shell.xmission.com (Kenny McCormack) - 2024-04-11 08:00 +0000
Re: Heh heh... (Was: Recursion, Yo) Kaz Kylheku <643-408-1753@kylheku.com> - 2024-04-11 15:13 +0000
Re: Heh heh... (Was: Recursion, Yo) gazelle@shell.xmission.com (Kenny McCormack) - 2024-04-11 19:06 +0000
Re: Recursion, Yo Kaz Kylheku <643-408-1753@kylheku.com> - 2024-04-11 15:04 +0000
Re: Recursion, Yo David Brown <david.brown@hesbynett.no> - 2024-04-11 20:47 +0200
Re: Recursion, Yo scott@slp53.sl.home (Scott Lurndal) - 2024-04-11 15:15 +0000
Re: Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-12 02:31 +0000
Re: Recursion, Yo Kaz Kylheku <643-408-1753@kylheku.com> - 2024-04-12 03:51 +0000
Re: Recursion, Yo cross@spitfire.i.gajendra.net (Dan Cross) - 2024-04-12 12:51 +0000
Re: Recursion, Yo Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-12 07:32 +0200
Re: Recursion, Yo David Brown <david.brown@hesbynett.no> - 2024-04-12 09:30 +0200
Re: Recursion, Yo Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-12 15:03 +0200
Re: Recursion, Yo David Brown <david.brown@hesbynett.no> - 2024-04-12 16:51 +0200
Re: Recursion, Yo Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-13 19:17 +0200
Re: Recursion, Yo David Brown <david.brown@hesbynett.no> - 2024-04-13 20:04 +0200
Re: Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-13 00:10 +0000
Re: Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-12 07:34 +0000
Re: Recursion, Yo Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-12 14:35 +0200
Re: Recursion, Yo bart <bc@freeuk.com> - 2024-04-12 16:42 +0100
Re: Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-13 00:13 +0000
Re: Recursion, Yo Michael S <already5chosen@yahoo.com> - 2024-04-13 20:33 +0300
Re: Recursion, Yo Ben Bacarisse <ben.usenet@bsb.me.uk> - 2024-04-14 00:23 +0100
Re: Recursion, Yo Michael S <already5chosen@yahoo.com> - 2024-04-14 03:29 +0300
Re: Recursion, Yo Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-15 15:07 +0200
Re: Recursion, Yo Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-04-15 08:46 -0700
Re: Recursion, Yo Ben Bacarisse <ben.usenet@bsb.me.uk> - 2024-04-15 17:47 +0100
Re: Recursion, Yo Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-04-15 12:00 -0700
Re: Recursion, Yo bart <bc@freeuk.com> - 2024-04-15 20:24 +0100
Re: Recursion, Yo Ben Bacarisse <ben.usenet@bsb.me.uk> - 2024-04-15 21:37 +0100
Re: Recursion, Yo Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-15 22:39 +0200
Re: Recursion, Yo Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-15 22:54 +0200
Re: Recursion, Yo Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-04-15 14:25 -0700
Re: Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-14 00:29 +0000
Re: Recursion, Yo Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-04-13 20:05 -0700
Re: Recursion, Yo Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-13 19:37 +0200
Re: Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-14 00:28 +0000
Re: Recursion, Yo Ben Bacarisse <ben.usenet@bsb.me.uk> - 2024-04-14 11:17 +0100
Re: Recursion, Yo bart <bc@freeuk.com> - 2024-04-14 13:51 +0100
Re: Recursion, Yo Ben Bacarisse <ben.usenet@bsb.me.uk> - 2024-04-14 22:47 +0100
Re: Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-14 22:44 +0000
Re: Recursion, Yo "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-04-14 20:35 -0700
Re: Recursion, Yo Ben Bacarisse <ben.usenet@bsb.me.uk> - 2024-04-15 17:50 +0100
Re: Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-15 22:30 +0000
Re: Recursion, Yo Ben Bacarisse <ben.usenet@bsb.me.uk> - 2024-04-17 18:09 +0100
Re: Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-17 22:12 +0000
Re: Recursion, Yo Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-22 18:06 +0200
Re: Recursion, Yo Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-15 20:36 +0200
Re: Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-15 22:32 +0000
Re: Recursion, Yo Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-22 17:41 +0200
Re: Recursion, Yo Michael S <already5chosen@yahoo.com> - 2024-04-16 23:11 +0300
Re: Recursion, Yo Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-04-19 08:26 -0700
Re: Recursion, Yo scott@slp53.sl.home (Scott Lurndal) - 2024-04-19 18:25 +0000
Re: Recursion, Yo Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-04-19 12:15 -0700
Re: Recursion, Yo Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-04-20 05:55 -0700
Re: Recursion, Yo bart <bc@freeuk.com> - 2024-04-19 19:34 +0100
Re: Recursion, Yo Ben Bacarisse <ben.usenet@bsb.me.uk> - 2024-04-20 02:11 +0100
Re: Recursion, Yo Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-04-19 18:24 -0700
Re: Recursion, Yo Kaz Kylheku <643-408-1753@kylheku.com> - 2024-04-20 15:35 +0000
Re: Recursion, Yo Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-04-19 12:18 -0700
Re: Recursion, Yo Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-04-20 06:03 -0700
Re: Recursion, Yo Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-22 17:49 +0200
Re: Recursion, Yo Ben Bacarisse <ben.usenet@bsb.me.uk> - 2024-04-15 17:50 +0100
Re: Recursion, Yo bart <bc@freeuk.com> - 2024-04-12 10:38 +0100
Re: Recursion, Yo Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-12 15:07 +0200
Re: Recursion, Yo bart <bc@freeuk.com> - 2024-04-12 16:38 +0100
Re: Recursion, Yo Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-04-12 19:06 -0700
Re: Recursion, Yo Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-04-13 07:46 -0700
Re: Recursion, Yo scott@slp53.sl.home (Scott Lurndal) - 2024-04-11 14:36 +0000
Re: Recursion, Yo David Brown <david.brown@hesbynett.no> - 2024-04-11 10:15 +0200
Re: Recursion, Yo Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-04-11 12:37 -0700
Re: Recursion, Yo David Brown <david.brown@hesbynett.no> - 2024-04-12 09:38 +0200
Re: Recursion, Yo fir <fir@grunge.pl> - 2024-04-14 20:34 +0200
Re: Recursion, Yo Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-10 15:18 +0200
Re: Recursion, Yo scott@slp53.sl.home (Scott Lurndal) - 2024-04-10 14:23 +0000
Re: Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-11 00:16 +0000
Re: Recursion, Yo scott@slp53.sl.home (Scott Lurndal) - 2024-04-11 14:34 +0000
Re: Recursion, Yo Ben Bacarisse <ben.usenet@bsb.me.uk> - 2024-04-12 00:04 +0100
Re: Recursion, Yo Kaz Kylheku <643-408-1753@kylheku.com> - 2024-04-10 17:19 +0000
Re: Recursion, Yo David Brown <david.brown@hesbynett.no> - 2024-04-10 21:19 +0200
Re: Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-11 00:54 +0000
Re: Recursion, Yo Kaz Kylheku <643-408-1753@kylheku.com> - 2024-04-11 02:06 +0000
Re: Recursion, Yo David Brown <david.brown@hesbynett.no> - 2024-04-11 10:23 +0200
Re: Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-11 08:34 +0000
Re: Recursion, Yo Kaz Kylheku <643-408-1753@kylheku.com> - 2024-04-10 17:10 +0000
Re: Recursion, Yo Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-04-10 10:14 -0700
Re: Recursion, Yo Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-09 08:44 +0000
csiph-web