Re: OOP



campyhapper@xxxxxxxxx wrote:
Hi folks,

I tend to prefer C, and of course I know that structs
can be used in C to achieve something like
an object-oriented design. And I prefer C in part
because C++ has, I think, grown into a bit of a
monster wherein readability is sacrificed.
But I wonder, has anyone ever tried to create a
sort of lite version of C++, a C+ if you will, that adds
to C just a few key features and disallows things
like templates, multiple inheritance and the like?

I got the e_type interface for sorting functions from Dann Corbit.
It's a step in the direction towards templates. I like it.
All you have to do is
#define E_TYPE /* array element type */
and
#define GT(A, B) /* a "Greater Than" macro */
and you can sort a one dimensional array of any type.

/* BEGIN output from ptr_sort.c */

Arrays of type struct node { char *string; struct node *next; },
are being sorted by string length.

This is the original order of the test array:
one
two
three
four
five
six
seven
eight
nine
ten

Stable insertionsort:
one
two
six
ten
four
five
nine
three
seven
eight

Nonstable simple selection sort,
four is after nine, and three is after eight.
one
two
six
ten
five
nine
four
eight
three
seven

/* END output from ptr_sort.c */



/* BEGIN ptr_sort.c */
/*
** lencomp() compares pointers to strings,
** according to string length.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define STRUCTURES 1 /* Either 1 or 0 */

#define SORT_FUNCTIONS { \
no_sort, "This is the original order of the test array:",\
si_sort, "Stable insertionsort:", \
ssort, "Nonstable simple selection sort,\nfour " \
"is after nine, and three is after eight.", \
q_sort, "Standard library qsort, " \
"unspecified ordering of equal keys:" \
}
#define NUMBERS { \
{"one"},{ "two"},{"three"},{"four"},{"five"}, \
{"six"},{"seven"},{"eight"},{"nine"},{ "ten"} \
}
#define GT(A, B) (lencomp((A), (B)) > 0)
#define NMEMB(A) (sizeof (A) / sizeof *(A))
#define SORTS (sizeof s_F / sizeof *s_F)
#define str(s) # s
#define xstr(s) str(s)

#if STRUCTURES != 0

#define E_TYPE \
struct node { \
char *string; \
struct node *next; \
}
#define STRING(E) ((E).string)

#else

#define E_TYPE char *
#define STRING(E) (E)

#endif

typedef E_TYPE e_type;

int lencomp(const void *, const void *);
void no_sort(e_type *, size_t);
void si_sort(e_type *, size_t);
void ssort(e_type *, size_t);
void q_sort(e_type *, size_t);

int main(void)
{
size_t element, sort;
struct sf {
void (*function)(e_type *, size_t);
const char *description;
} s_F[] = SORT_FUNCTIONS;
const e_type numbers[] = NUMBERS;
e_type copy[NMEMB(numbers)];

puts("/* BEGIN output from ptr_sort.c */\n");
puts("Arrays of type " xstr(E_TYPE)
",\nare being sorted by string length.\n");
for (sort = 0; sort != NMEMB(s_F); ++sort) {
puts(s_F[sort].description);
memcpy(copy, numbers, sizeof copy);
s_F[sort].function(copy, NMEMB(numbers));
for (element = 0; element != NMEMB(numbers); ++element) {
puts(STRING(copy[element]));
}
putchar('\n');
}
puts("/* END output from ptr_sort.c */");
return 0;
}

int lencomp(const void *a, const void *b)
{
const size_t a_len = strlen(STRING(*(const e_type *)a));
const size_t b_len = strlen(STRING(*(const e_type *)b));

return b_len > a_len ? -1 : b_len != a_len;
}

void no_sort(e_type *array, size_t nmemb)
{
array, nmemb;
}

void si_sort(e_type *array, size_t nmemb)
{
e_type temp, *base, *low, *high;

if (nmemb-- > 1) {
base = array;
do {
low = array++;
if (GT(low, array)) {
high = array;
temp = *high;
do {
*high-- = *low;
if (low == base) {
break;
}
--low;
} while (GT(low, &temp));
*high = temp;
}
} while (--nmemb != 0);
}
}

void ssort(e_type *array, size_t nmemb)
{
e_type *first, *middle, temp;
size_t counter;

while (nmemb-- > 1) {
first = middle = array + 1;
counter = nmemb;
while (--counter != 0) {
++middle;
if (GT(first, middle)) {
first = middle;
}
}
if (GT(array, first)) {
temp = *array;
*array = *first;
*first = temp;
}
++array;
}
}

void q_sort(e_type *array, size_t nmemb)
{
qsort(array, nmemb, sizeof *array, lencomp);
}

/* END ptr_sort.c */





--
pete
.



Relevant Pages

  • Re: is there an alternative to strstr
    ... >> To exploit this fact, you need a different data format, a plain string is ... > Ok I have put the email ids in a sorted array. ... However you can use an array of char pointers and use ... int cmp(const void *v1, const void *v2); ...
    (comp.lang.c)
  • Re: Pointers on string members of structure
    ... because this just points memstr to a fixed string and it is undefined to ... char memstrA; ... string array directly like this and how? ... struct or if the struct member points to the array. ...
    (microsoft.public.vc.language)
  • Re: Pointers on string members of structure
    ... because this just points memstr to a fixed string and it is undefined to try ... char memstrA; ... string array directly like this and how? ... struct or if the struct member points to the array. ...
    (microsoft.public.vc.language)
  • Re: Structures Again?
    ... So if we refer to the code below this paragrah, when the array ... actuall location of were the string is immediately stored. ... const TCHAR * szLabel; ... > should define your struct as ...
    (microsoft.public.vc.language)
  • Re: MVC in C++
    ... No Observer can handle ... > struct Observable ... > void notifyObservers() ... > string version; ...
    (comp.object)