Renamed ternary_insert() to ternary_insert_replace() and added the ability

to replace existing data (without a memory leak.) Added two DEFINES
ternary_insert() and ternary_replace() to aid in coding.
This commit is contained in:
Robert James Kaes 2001-08-30 16:52:09 +00:00
parent 22bdb8123d
commit a328cefbf0
2 changed files with 16 additions and 5 deletions

View File

@ -1,4 +1,4 @@
/* $Id: ternary.c,v 1.5 2001-05-23 17:59:53 rjkaes Exp $ /* $Id: ternary.c,v 1.6 2001-08-30 16:52:09 rjkaes Exp $
* *
* This module creates a Ternary Search Tree which can store both string * This module creates a Ternary Search Tree which can store both string
* keys, and arbitrary data for each key. It works similar to a hash, and * keys, and arbitrary data for each key. It works similar to a hash, and
@ -294,7 +294,8 @@ int ternary_destroy(TERNARY tno, void (*freeptr)(void *))
* TE_TOOFULL tree is full, so no new elements can be added. * TE_TOOFULL tree is full, so no new elements can be added.
* Exceptions: none * Exceptions: none
*/ */
int ternary_insert(TERNARY tno, const char *s, void *data) int ternary_insert_replace(TERNARY tno, const char *s, void *data,
short int replace)
{ {
int cur; /* index of current tree */ int cur; /* index of current tree */
Ttree *tree; /* pointer to tree structure */ Ttree *tree; /* pointer to tree structure */
@ -314,7 +315,13 @@ int ternary_insert(TERNARY tno, const char *s, void *data)
while ((pp = *p)) { while ((pp = *p)) {
if ((d = *s - pp->splitchar) == 0) { if ((d = *s - pp->splitchar) == 0) {
if (*s++ == 0) { if (*s++ == 0) {
if (!replace)
return TE_EXISTS; return TE_EXISTS;
else {
free(pp->eqkid);
pp->eqkid = (Tnode *)data;
return TE_NONE;
}
} }
p = &(pp->eqkid); p = &(pp->eqkid);
} else if (d < 0) } else if (d < 0)

View File

@ -1,4 +1,4 @@
/* $Id: ternary.h,v 1.2 2000-09-26 04:59:20 rjkaes Exp $ /* $Id: ternary.h,v 1.3 2001-08-30 16:52:09 rjkaes Exp $
* *
* See 'ternary.c' for a detailed description. * See 'ternary.c' for a detailed description.
* *
@ -58,7 +58,11 @@ extern char te_errbuf[256];
extern TERNARY ternary_new(void); extern TERNARY ternary_new(void);
extern int ternary_destroy(TERNARY tno, void (*freeptr)(void *)); extern int ternary_destroy(TERNARY tno, void (*freeptr)(void *));
extern int ternary_insert(TERNARY tno, const char *s, void *data); #define ternary_insert(x, y, z) ternary_insert_replace(x, y, z, 0)
#define ternary_replace(x, y, z) ternary_insert_replace(x, y, z, 1)
extern int ternary_insert_replace(TERNARY tno, const char *s, void *data,
short int replace);
extern int ternary_search(TERNARY tno, const char *s, void **data); extern int ternary_search(TERNARY tno, const char *s, void **data);
#endif #endif