R
Remo D.
I've read with much interest the threads on how to create data
containers in C.
I started thinking if simpler ADT (other than the proposed List) could
be used as a starting point.
What about a dynamic vector? I mean a dynamic array with no predefined
size. A possible API could be:
typedef struct {
.... /* appropriate fields to manage the vector data */
} *vec_t;
vec_t vecNew (size_t elemsize);
void *vecSet (vec_t v, size_t index, void *elem);
void *vecGet (vec_t v, size_t index);
size_t vecCnt (vec_t v);
vec_t vecFree (vec_t v);
An open question is if vecSet should create a copy of the element or
memory management should be demanded to the programmer. For simplicy I
would prefer the former (and that's also the reason for the elemsize of
vecNew().
vecCnt would return the highest index where an element has been stored
with vecSet();
This simple API could be augmented with:
void vecQsort (vec_t v, int (*cmp)(void *, void *));
void *vecBsearch (vec_t v, void *key, int (*cmp)(void *, void *));
etc.
That could be the case for other ADT. Once vec has been defined it's
easy to see how to use it to implement stacks, for example.
Comments?
Remo.D
containers in C.
I started thinking if simpler ADT (other than the proposed List) could
be used as a starting point.
What about a dynamic vector? I mean a dynamic array with no predefined
size. A possible API could be:
typedef struct {
.... /* appropriate fields to manage the vector data */
} *vec_t;
vec_t vecNew (size_t elemsize);
void *vecSet (vec_t v, size_t index, void *elem);
void *vecGet (vec_t v, size_t index);
size_t vecCnt (vec_t v);
vec_t vecFree (vec_t v);
An open question is if vecSet should create a copy of the element or
memory management should be demanded to the programmer. For simplicy I
would prefer the former (and that's also the reason for the elemsize of
vecNew().
vecCnt would return the highest index where an element has been stored
with vecSet();
This simple API could be augmented with:
void vecQsort (vec_t v, int (*cmp)(void *, void *));
void *vecBsearch (vec_t v, void *key, int (*cmp)(void *, void *));
etc.
That could be the case for other ADT. Once vec has been defined it's
easy to see how to use it to implement stacks, for example.
Comments?
Remo.D