Added the TE_EXISTS return code, and cleaned up the ternary_destroy

function.
This commit is contained in:
Robert James Kaes 2000-09-26 04:59:20 +00:00
parent a2c3e5ecc9
commit ba5e5fc109
2 changed files with 22 additions and 35 deletions

View File

@ -1,4 +1,4 @@
/* $Id: ternary.c,v 1.1 2000-09-12 00:10:28 rjkaes Exp $ /* $Id: ternary.c,v 1.2 2000-09-26 04:59:20 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
@ -33,7 +33,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "log.h"
#include "ternary.h" #include "ternary.h"
#include "tinyproxy.h"
/* /*
* Macros for the tree structures (limits) * Macros for the tree structures (limits)
@ -224,7 +226,7 @@ TERNARY ternary_new(void)
*/ */
if (TE_ISERROR(token = create_token_ref(cur))) { if (TE_ISERROR(token = create_token_ref(cur))) {
/* error in token generation -- clean up and return */ /* error in token generation -- clean up and return */
free(trees[cur]); safefree(trees[cur]);
trees[cur] = NULL; trees[cur] = NULL;
return token; return token;
} }
@ -239,27 +241,6 @@ TERNARY ternary_new(void)
return token; return token;
} }
/*
* Recursively deletes the branches (and their branches)
*
* Parameter: Tnode *p node on branch
* void(*)(void *) function pointer to free data routine
* Returned: none
* Exceptions: none
*/
static void delete_branch(Tnode *p, void (*freeptr)(void *ptr))
{
if (p) {
delete_branch(p->lokid, freeptr);
delete_branch(p->hikid, freeptr);
if (p->splitchar)
delete_branch(p->eqkid, freeptr);
else
(*freeptr)(p->eqkid);
free(p);
}
}
/* /*
* Delete an exisiting tree * Delete an exisiting tree
* *
@ -274,7 +255,7 @@ static void delete_branch(Tnode *p, void (*freeptr)(void *ptr))
int ternary_destroy(TERNARY tno, void (*freeptr)(void *)) int ternary_destroy(TERNARY tno, void (*freeptr)(void *))
{ {
int cur; /* index of current tree */ int cur; /* index of current tree */
unsigned int i; unsigned int i, j;
/* /*
* Check that tno refers to an existing tree; * Check that tno refers to an existing tree;
@ -283,15 +264,18 @@ int ternary_destroy(TERNARY tno, void (*freeptr)(void *))
if (TE_ISERROR(cur = read_token_ref(tno))) if (TE_ISERROR(cur = read_token_ref(tno)))
return cur; return cur;
/* for (i = 0; i < trees[cur]->freen; i++) {
* Free the tree and reset the array element for (j = 0; j < BUFSIZE; j++) {
*/ Tnode *ptr = (trees[cur]->freearr[i] + j);
delete_branch(trees[cur]->tree_root, freeptr); if (ptr->splitchar == 0)
for (i = 0; i < trees[cur]->freen; i++) (*freeptr)(ptr->eqkid);
free(trees[cur]->freearr[i]); safefree(ptr);
}
}
free(trees[cur]); trees[cur]->token = 0;
trees[cur] = NULL; trees[cur]->tree_root = trees[cur]->buf = NULL;
trees[cur]->bufn = trees[cur]->freen = 0;
return TE_NONE; return TE_NONE;
} }
@ -329,8 +313,10 @@ 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) {
return TE_NONE; DEBUG2("Key exists: %s", s);
return TE_EXISTS;
}
p = &(pp->eqkid); p = &(pp->eqkid);
} else if (d < 0) } else if (d < 0)
p = &(pp->lokid); p = &(pp->lokid);

View File

@ -1,4 +1,4 @@
/* $Id: ternary.h,v 1.1 2000-09-12 00:10:28 rjkaes Exp $ /* $Id: ternary.h,v 1.2 2000-09-26 04:59:20 rjkaes Exp $
* *
* See 'ternary.c' for a detailed description. * See 'ternary.c' for a detailed description.
* *
@ -50,6 +50,7 @@ extern char te_errbuf[256];
#define TE_NOROOM -6 /* can't allocate space (sys err) */ #define TE_NOROOM -6 /* can't allocate space (sys err) */
#define TE_TOOMANYTS -7 /* too many trees in use */ #define TE_TOOMANYTS -7 /* too many trees in use */
#define TE_INTINCON -8 /* internal inconsistency */ #define TE_INTINCON -8 /* internal inconsistency */
#define TE_EXISTS -9 /* key already exists in tree */
/* /*
* Library functions. * Library functions.