diff options
author | Ronnie Sahlberg <ronnie_sahlberg@ozemail.com.au> | 2006-03-18 06:15:39 +0000 |
---|---|---|
committer | Ronnie Sahlberg <ronnie_sahlberg@ozemail.com.au> | 2006-03-18 06:15:39 +0000 |
commit | c4c2ce26069c0ad5525311561a3aeb4d5d488656 (patch) | |
tree | ca6d6c3eb30bf3e476296e7132cdc12a8a875e86 | |
parent | 87e4a20bbbe3d17b10e414d30ca47e9e9aea3843 (diff) |
add new se_tree_lookup32_less_than_or_equal() call
svn path=/trunk/; revision=17663
-rw-r--r-- | epan/emem.c | 88 | ||||
-rw-r--r-- | epan/emem.h | 8 |
2 files changed, 96 insertions, 0 deletions
diff --git a/epan/emem.c b/epan/emem.c index 5ba7463659..fdc08a0054 100644 --- a/epan/emem.c +++ b/epan/emem.c @@ -851,6 +851,94 @@ se_tree_lookup32(se_tree_t *se_tree, guint32 key) return NULL; } +void * +se_tree_lookup32_less_than_or_equal(se_tree_t *se_tree, guint32 key) +{ + se_tree_node_t *node; + + node=se_tree->tree; + + if(!node){ + return NULL; + } + + + while(node){ + if(key==node->key32){ + return node->data; + } + if(key<node->key32){ + if(node->left){ + node=node->left; + continue; + } else { + break; + } + } + if(key>node->key32){ + if(node->right){ + node=node->right; + continue; + } else { + break; + } + } + } + + + /* If we are still at the root of the tree this means that this node + * is either smaller thant the search key and then we return this + * node or else there is no smaller key availabel and then + * we return NULL. + */ + if(!node->parent){ + if(key>node->key32){ + return node->data; + } else { + return NULL; + } + } + + if(node->parent->left==node){ + /* left child */ + + if(key>node->key32){ + /* if this is a left child and its key is smaller than + * the search key, then this is the node we want. + */ + return node->data; + } else { + /* if this is a left child and its key is bigger than + * the search key, we have to check if any + * of our ancestors are smaller than the search key. + */ + while(node){ + if(key>node->key32){ + return node->data; + } + node=node->parent; + } + return NULL; + } + } else { + /* right child */ + + if(node->key32<key){ + /* if this is the right child and its key is smaller + * than the search key then this is the one we want. + */ + return node->data; + } else { + /* if this is the right child and its key is larger + * than the search key then our parent is the one we + * want. + */ + return node->parent->data; + } + } + +} + static inline se_tree_node_t * se_tree_parent(se_tree_node_t *node) diff --git a/epan/emem.h b/epan/emem.h index efbe7b9f4a..45d3afbf7a 100644 --- a/epan/emem.h +++ b/epan/emem.h @@ -211,6 +211,14 @@ void se_tree_insert32(se_tree_t *se_tree, guint32 key, void *data); */ void *se_tree_lookup32(se_tree_t *se_tree, guint32 key); +/* This function will look up a node in the tree indexed by a guint32 integer + * value. + * The function will return the node that has the largest key that is + * equal to or smaller than the search key, or NULL if no such key was + * found. + */ +void *se_tree_lookup32_less_than_or_equal(se_tree_t *se_tree, guint32 key); + /* This function is similar to the se_tree_create() call but with the * difference that when the se memory is release everything including the |