core/sm_trie.cpp
changeset 211 4c77142eaf36
parent 195 625a44869e15
child 216 38b1bdbc5bc9
     1.1 --- a/core/sm_trie.cpp	Wed Dec 13 11:19:46 2006 +0000
     1.2 +++ b/core/sm_trie.cpp	Fri Dec 15 13:38:04 2006 +0000
     1.3 @@ -266,6 +266,56 @@
     1.4  	delete trie;
     1.5  }
     1.6  
     1.7 +bool sm_trie_delete(Trie *trie, const char *key)
     1.8 +{
     1.9 +	unsigned int lastidx = 1;		/* the last node index */
    1.10 +	unsigned int curidx;			/* current node index */
    1.11 +	const char *keyptr = key;		/* input stream at current token */
    1.12 +	TrieNode *node = NULL;			/* current node being processed */
    1.13 +	TrieNode *base = trie->base;
    1.14 +
    1.15 +	if (!*key)
    1.16 +	{
    1.17 +		return false;
    1.18 +	}
    1.19 +
    1.20 +	/* Start traversing at the root node */
    1.21 +	do
    1.22 +	{
    1.23 +		/* Find where the next character is, then advance */
    1.24 +		curidx = base[lastidx].idx;
    1.25 +		node = &base[curidx];
    1.26 +		curidx += charval(*keyptr);
    1.27 +		node = &base[curidx];
    1.28 +		keyptr++;
    1.29 +
    1.30 +		/* Check if this slot is supposed to be empty or is a collision */
    1.31 +		if ((curidx > trie->baseSize) || node->mode == Node_Unused || node->parent != lastidx)
    1.32 +		{
    1.33 +			return false;
    1.34 +		} else if (node->mode == Node_Term) {
    1.35 +			char *term = &trie->stringtab[node->idx];
    1.36 +			if (strcmp(keyptr, term) == 0)
    1.37 +			{
    1.38 +				break;
    1.39 +			}
    1.40 +		}
    1.41 +		lastidx = curidx;
    1.42 +	} while (*keyptr != '\0');
    1.43 +
    1.44 +	assert(node != NULL);
    1.45 +
    1.46 +	if (!node->valset)
    1.47 +	{
    1.48 +		return false;
    1.49 +	}
    1.50 +
    1.51 +	node->valset = false;
    1.52 +	node->value = NULL;
    1.53 +
    1.54 +	return true;
    1.55 +}
    1.56 +
    1.57  bool sm_trie_retrieve(Trie *trie, const char *key, void **value)
    1.58  {
    1.59  	unsigned int lastidx = 1;		/* the last node index */