Skip to content

Commit

Permalink
override rb_find by adding compare func
Browse files Browse the repository at this point in the history
in this way, custom compare can be used for getting items. For instance you can compare null terminated string with non-null terminated string by providing custom compare to find()
  • Loading branch information
recp committed Dec 5, 2020
1 parent b11c21d commit 1c3cc76
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
39 changes: 35 additions & 4 deletions include/ds/rb.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ rb_remove(RBTree *tree,
*/
DS_EXPORT
void*
rb_find(RBTree *tree,
void *key);
rb_find(RBTree * __restrict tree, void * __restrict key);

/*!
* @brief find node by key. It returns RBnode you can get value from node
Expand All @@ -152,8 +151,40 @@ rb_find(RBTree *tree,
*/
DS_EXPORT
RBNode*
rb_find_node(RBTree *tree,
void *key);
rb_find_node(RBTree * __restrict tree, void * __restrict key);

/*!
* @brief find value by key with cmp
*
* for instance you can compare null-terminated string key with non-null
* terminated string by providing cmp func for find
*
* @param[in] tree rbtree
* @param[in] key key
*
* @return found value or NULL
*/
DS_EXPORT
void*
rb_find_by(RBTree * __restrict tree, void * __restrict key, const DsCmpFn cmp);

/*!
* @brief find node by key with cmp.
* It returns RBnode you can get value from node
*
* for instance you can compare null-terminated string key with non-null
* terminated string by providing cmp func for find
*
* @param[in] tree rbtree
* @param[in] key key
*
* @return found RBNode or NULL
*/
DS_EXPORT
RBNode*
rb_find_node_by(RBTree * __restrict tree,
void * __restrict key,
const DsCmpFn cmp);

/*!
* @brief get parent node of found node (of key). This will lookup for node
Expand Down
38 changes: 30 additions & 8 deletions src/rb.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,9 +541,9 @@ rb_remove(RBTree *tree, void *key) {

DS_EXPORT
void*
rb_find(RBTree *tree, void *key) {
rb_find(RBTree * __restrict tree, void * __restrict key) {
RBNode *found;
found = rb_find_node(tree, key);
found = rb_find_node_by(tree, key, tree->cmp);
if (found == NULL)
return NULL;

Expand All @@ -552,23 +552,45 @@ rb_find(RBTree *tree, void *key) {

DS_EXPORT
RBNode*
rb_find_node(RBTree *tree, void *key) {
RBNode *iter;
rb_find_node(RBTree * __restrict tree, void * __restrict key) {
return rb_find_node_by(tree, key, tree->cmp);
}

iter = tree->root->chld[RB_RIGHT];
DS_EXPORT
void*
rb_find_by(RBTree * __restrict tree,
void * __restrict key,
const DsCmpFn cmp) {
RBNode *found;
found = rb_find_node_by(tree, key, cmp);
if (found == NULL)
return NULL;

while (iter != tree->nullNode) {
return found->val;
}

DS_EXPORT
RBNode*
rb_find_node_by(RBTree * __restrict tree,
void * __restrict key,
const DsCmpFn cmp) {
RBNode *iter, *nullNode;

iter = tree->root->chld[RB_RIGHT];
nullNode = tree->nullNode;

while (iter != nullNode) {
int cmpRet;

cmpRet = tree->cmp(iter->key, key);
cmpRet = cmp(iter->key, key);

if (cmpRet == 0)
break;

iter = iter->chld[cmpRet < 0];
}

if (!iter || iter == tree->nullNode)
if (!iter || iter == nullNode)
return NULL;

return iter;
Expand Down

0 comments on commit 1c3cc76

Please sign in to comment.