| Deutsch English Français Italiano |
|
<20250304175602.c9fe683d678d3a2ed101a4ac@g{oogle}mail.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Anton Shepelev <anton.txt@g{oogle}mail.com>
Newsgroups: comp.lang.c
Subject: Re: Which code style do you prefer the most?
Date: Tue, 4 Mar 2025 17:56:02 +0300
Organization: A noiseless patient Spider
Lines: 182
Message-ID: <20250304175602.c9fe683d678d3a2ed101a4ac@g{oogle}mail.com>
References: <vpkmq0$21php$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 04 Mar 2025 15:56:04 +0100 (CET)
Injection-Info: dont-email.me; posting-host="35a27bf72a86c6fb6a2c86ddce86583c";
logging-data="2005586"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Jk4YTP/dvqXY21qdKeMwX/f42hFSpcLM="
Cancel-Lock: sha1:YwknxtYlL02mpJbOMF/AcYxVuh0=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
Bytes: 6288
Ar Rakin:
> I've been writing C code for a long time, in different styles, but
> always wanted to know which code style most people prefer. This is all
> about that question. Which one of the following do you prefer the most?
> 1. GNU Style
> 2. Linux Style
I don't like them much. Below is a piece of my code
(previosly shown in this group). It uses tabs for
indentation and spaces for alignment.
-- sort.c --
#include <stdio.h>
#include <limits.h>
#include "sort.h"
/* Partition an array [l..r] around a "pivot" member so that */
/* the elements of the left sub-array do not exceed, and the ones */
/* of the right sub-array are not less than, the pivot. */
static int /* returns the right boundary of the left sub-array */
partition
( void * data, /* pointer to the full array */
int l, /* left boundary of the sub-array */
int r, /* right boundary of the sub-array, l < r */
comp_f comp,
swap_f swap,
void * extra
)
{ int p; /* index of the pivot, element */
p = ( l + r ) / 2; /* Chose the middle element as pivot */
while( 1 )
{ /* advance cursors until they stop at a pair of elements */
/* that should be swapped or meet or go past each other. */
/* The pivot element serves as a sentinel. */
while( comp( data, l, p, extra ) < 0 ) l += 1;
while( comp( data, r, p, extra ) > 0 ) r -= 1;
if( l >= r ) break;
swap( data, l, r ); /* Swap the pair found */
if( l == p ) p = r; /* Update pivot location if it */
else if( r == p ) p = l; /* was changed during the swap */
/* Advance the cursors. If they are adjecent they will cross: */
l += 1; r -= 1;
}
return r;
}
void sort
( void * data,
int len,
comp_f comp,
swap_f swap,
void * extra
)
{ /* In this algorithm, minimun sufficient stack size is Log(len) */
int sl[sizeof(int) * CHAR_BIT]; /* left sub-array boundaries on the stack */
int sr[sizeof(int) * CHAR_BIT]; /* right sub-array boundaries nn the stack */
int d; /* stack depth */
int l, r; /* left and right boundaries of a sub-array */
int lpos, rpos; /* positions of the left and right new sub-arrays on the stack */
int newr; /* the right boundary of the new left sub-array */
d = 1; /* Put the initial sub-array onto the stack */
sl[0] = 0; sr[0] = len - 1;
while( d > 0 )
{ d -= 1; /* Take an element from the stack */
l = sl[d]; r = sr[d];
if( l >= r ) continue; /* Skip a subarray shorter than two */
newr = partition( data, l , r, comp, swap, extra );
/* process the shorter sub-array first to ease the stack: */
lpos = d; rpos = d;
if( newr - l > r - newr - 1 ) rpos += 1;
else lpos += 1;
/* Put partiioned sub-arrays onto the stack: */
sl[lpos] = l; sr[lpos] = newr;
sl[rpos] = newr + 1; sr[rpos] = r;
d += 2;
}
}
-- sort.h --
/* ----------------------- A generic sorting routine ------------------------ */
/* Comparison function:
> 0 <=> data[i] > data[j]
< 0 <=> data[i] < data[j]
== 0 <=> data[i] = data[j] */
typedef int (*comp_f)( const void * data, int i, int j, void * extra );
/* Exchange ith and jth elements: */
typedef void (*swap_f)( void * data, int i, int j );
void sort
( void * data, /* pointer to data to sort */
int len, /* length of data to sort */
comp_f comp, /* comparison function */
swap_f swap, /* swap function */
void * extra /* user-supplied parameter to comp */
);
-- test.c --
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "sort.h"
static void print( int * a, int n )
{ int i;
for( i = 0; i < n; i++ )
printf("%i ", a[i] );
printf("\n");
}
static int intcomp( int a, int b )
{ if( a > b ) return 1;
if( a < b ) return -1;
return 0;
}
static int comp( const void * data, int i, int j, void * extra )
{ return intcomp( ( (int*)data )[i], ( (int*)data )[j] ); }
/* comparison for qsort: */
static int qscomp(const void * a, const void * b)
{ return intcomp( *(int*)a, *(int*)b ); }
static void swap( void * data, int i, int j )
{ int buf;
buf = ( (int*)data )[i];
( (int*)data )[i] = ( (int*)data )[j];
( (int*)data )[j] = buf;
}
#define TESTLEN 500
/* performance test with a random array: */
static void test()
{ int a[TESTLEN], c[TESTLEN];
int i, j;
clock_t start, total;
total = 0;
for( i = 0; i < 1000; i++ )
{ for( j = 0; j < TESTLEN; j++ )
{ a[j] = rand() % 5;
c[j] = a[j];
}
start = clock();
/*qsort( a, testlen, sizeof(int), &qscomp );*/
sort( a, TESTLEN, &comp, &swap, NULL );
total += clock() - start;
for( j = 0; j < TESTLEN-1; j++ )
{ if( a[j] > a[j+1] )
{ print( c, TESTLEN );
print( a, TESTLEN );
return;
}
}
}
printf("Time: %f\n", (float)total / CLOCKS_PER_SEC );
}
/* test with a small hard-coded array: */
#define TMLEN 3
static void testman( void )
{ int ta[TMLEN] = {3,2,1};
print( ta, TMLEN );
sort ( ta, TMLEN, &comp, &swap, NULL );
print( ta, TMLEN );
}
int main( int argc, char** argv )
{ /*testman();
return 0;*/
test();
return 0;
========== REMAINDER OF ARTICLE TRUNCATED ==========