Reusing the same ADT for other types

I am having trouble getting over this.

Take, for example, I have a red ebony implementation that works with elements:

typedef unsigned long int Key;
struct rbt_node{
    Item item;
    int color;
    Key key;
    struct rbt_node* parent;
    struct rbt_node* left;
    struct rbt_node* right;
};

then in Item.h I define the structure that I will use, for example:

typedef struct _something* Item;

That way, I untie the element held from the tree implementation. The problem occurs if I want to reuse ADT for other types.

For now, I will need to define Item2.h and copy rbt.c / rbt.h to rbt2.c / rbt2.h and change them to use Item2.h and change the function names. Is there a cleaner way?

C , , , , .

:
 rbt_insert(rbt_of_something, something);
rbt_insert(rbt_of_somethingElse, somethingElse);

+3
2

item void*. , / . .

FILE* rbt_fileFromNode(struct rbt_node* node);

, , , rbt_node rbt_tree .

+2

:

#ifdef SOMETHING
typedef struct _something* Item;
#elif SOMETHINGELSE
typedef struct _somethingElse* Item;
#else
#error no definition for Item. Use -D flag to specify Item definition.
#endif

-D .

+3

Source: https://habr.com/ru/post/1771898/


All Articles