mirror of https://github.com/getdnsapi/getdns.git
Map rbtree symbols
This commit is contained in:
parent
5f3de12644
commit
ec685e900d
|
@ -40,8 +40,8 @@
|
|||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "util/log.h"
|
||||
#include "util/fptr_wlist.h"
|
||||
#include "log.h"
|
||||
#include "fptr_wlist.h"
|
||||
#include "util/rbtree.h"
|
||||
|
||||
/** Node colour black */
|
||||
|
@ -50,7 +50,7 @@
|
|||
#define RED 1
|
||||
|
||||
/** the NULL node, global alloc */
|
||||
_getdns_rbnode_t _getdns_rbtree_null_node = {
|
||||
rbnode_type rbtree_null_node = {
|
||||
RBTREE_NULL, /* Parent. */
|
||||
RBTREE_NULL, /* Left. */
|
||||
RBTREE_NULL, /* Right. */
|
||||
|
@ -59,13 +59,14 @@ _getdns_rbnode_t _getdns_rbtree_null_node = {
|
|||
};
|
||||
|
||||
/** rotate subtree left (to preserve redblack property) */
|
||||
static void _getdns_rbtree_rotate_left(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node);
|
||||
static void rbtree_rotate_left(rbtree_type *rbtree, rbnode_type *node);
|
||||
/** rotate subtree right (to preserve redblack property) */
|
||||
static void _getdns_rbtree_rotate_right(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node);
|
||||
static void rbtree_rotate_right(rbtree_type *rbtree, rbnode_type *node);
|
||||
/** Fixup node colours when insert happened */
|
||||
static void _getdns_rbtree_insert_fixup(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node);
|
||||
static void rbtree_insert_fixup(rbtree_type *rbtree, rbnode_type *node);
|
||||
/** Fixup node colours when delete happened */
|
||||
static void _getdns_rbtree_delete_fixup(_getdns_rbtree_t* rbtree, _getdns_rbnode_t* child, _getdns_rbnode_t* child_parent);
|
||||
static void rbtree_delete_fixup(rbtree_type* rbtree, rbnode_type* child,
|
||||
rbnode_type* child_parent);
|
||||
|
||||
/*
|
||||
* Creates a new red black tree, initializes and returns a pointer to it.
|
||||
|
@ -73,25 +74,25 @@ static void _getdns_rbtree_delete_fixup(_getdns_rbtree_t* rbtree, _getdns_rbnode
|
|||
* Return NULL on failure.
|
||||
*
|
||||
*/
|
||||
_getdns_rbtree_t *
|
||||
_getdns_rbtree_create (int (*cmpf)(const void *, const void *))
|
||||
rbtree_type *
|
||||
rbtree_create (int (*cmpf)(const void *, const void *))
|
||||
{
|
||||
_getdns_rbtree_t *rbtree;
|
||||
rbtree_type *rbtree;
|
||||
|
||||
/* Allocate memory for it */
|
||||
rbtree = (_getdns_rbtree_t *) malloc(sizeof(_getdns_rbtree_t));
|
||||
rbtree = (rbtree_type *) malloc(sizeof(rbtree_type));
|
||||
if (!rbtree) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Initialize it */
|
||||
_getdns_rbtree_init(rbtree, cmpf);
|
||||
rbtree_init(rbtree, cmpf);
|
||||
|
||||
return rbtree;
|
||||
}
|
||||
|
||||
void
|
||||
_getdns_rbtree_init(_getdns_rbtree_t *rbtree, int (*cmpf)(const void *, const void *))
|
||||
rbtree_init(rbtree_type *rbtree, int (*cmpf)(const void *, const void *))
|
||||
{
|
||||
/* Initialize it */
|
||||
rbtree->root = RBTREE_NULL;
|
||||
|
@ -104,9 +105,9 @@ _getdns_rbtree_init(_getdns_rbtree_t *rbtree, int (*cmpf)(const void *, const vo
|
|||
*
|
||||
*/
|
||||
static void
|
||||
_getdns_rbtree_rotate_left(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node)
|
||||
rbtree_rotate_left(rbtree_type *rbtree, rbnode_type *node)
|
||||
{
|
||||
_getdns_rbnode_t *right = node->right;
|
||||
rbnode_type *right = node->right;
|
||||
node->right = right->left;
|
||||
if (right->left != RBTREE_NULL)
|
||||
right->left->parent = node;
|
||||
|
@ -131,9 +132,9 @@ _getdns_rbtree_rotate_left(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node)
|
|||
*
|
||||
*/
|
||||
static void
|
||||
_getdns_rbtree_rotate_right(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node)
|
||||
rbtree_rotate_right(rbtree_type *rbtree, rbnode_type *node)
|
||||
{
|
||||
_getdns_rbnode_t *left = node->left;
|
||||
rbnode_type *left = node->left;
|
||||
node->left = left->right;
|
||||
if (left->right != RBTREE_NULL)
|
||||
left->right->parent = node;
|
||||
|
@ -154,9 +155,9 @@ _getdns_rbtree_rotate_right(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node)
|
|||
}
|
||||
|
||||
static void
|
||||
_getdns_rbtree_insert_fixup(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node)
|
||||
rbtree_insert_fixup(rbtree_type *rbtree, rbnode_type *node)
|
||||
{
|
||||
_getdns_rbnode_t *uncle;
|
||||
rbnode_type *uncle;
|
||||
|
||||
/* While not at the root and need fixing... */
|
||||
while (node != rbtree->root && node->parent->color == RED) {
|
||||
|
@ -179,12 +180,12 @@ _getdns_rbtree_insert_fixup(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node)
|
|||
/* Are we the right child? */
|
||||
if (node == node->parent->right) {
|
||||
node = node->parent;
|
||||
_getdns_rbtree_rotate_left(rbtree, node);
|
||||
rbtree_rotate_left(rbtree, node);
|
||||
}
|
||||
/* Now we're the left child, repaint and rotate... */
|
||||
node->parent->color = BLACK;
|
||||
node->parent->parent->color = RED;
|
||||
_getdns_rbtree_rotate_right(rbtree, node->parent->parent);
|
||||
rbtree_rotate_right(rbtree, node->parent->parent);
|
||||
}
|
||||
} else {
|
||||
uncle = node->parent->parent->left;
|
||||
|
@ -204,12 +205,12 @@ _getdns_rbtree_insert_fixup(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node)
|
|||
/* Are we the right child? */
|
||||
if (node == node->parent->left) {
|
||||
node = node->parent;
|
||||
_getdns_rbtree_rotate_right(rbtree, node);
|
||||
rbtree_rotate_right(rbtree, node);
|
||||
}
|
||||
/* Now we're the right child, repaint and rotate... */
|
||||
node->parent->color = BLACK;
|
||||
node->parent->parent->color = RED;
|
||||
_getdns_rbtree_rotate_left(rbtree, node->parent->parent);
|
||||
rbtree_rotate_left(rbtree, node->parent->parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -223,17 +224,17 @@ _getdns_rbtree_insert_fixup(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node)
|
|||
* Returns NULL on failure or the pointer to the newly added node
|
||||
* otherwise.
|
||||
*/
|
||||
_getdns_rbnode_t *
|
||||
_getdns_rbtree_insert (_getdns_rbtree_t *rbtree, _getdns_rbnode_t *data)
|
||||
rbnode_type *
|
||||
rbtree_insert (rbtree_type *rbtree, rbnode_type *data)
|
||||
{
|
||||
/* XXX Not necessary, but keeps compiler quiet... */
|
||||
int r = 0;
|
||||
|
||||
/* We start at the root of the tree */
|
||||
_getdns_rbnode_t *node = rbtree->root;
|
||||
_getdns_rbnode_t *parent = RBTREE_NULL;
|
||||
rbnode_type *node = rbtree->root;
|
||||
rbnode_type *parent = RBTREE_NULL;
|
||||
|
||||
fptr_ok(fptr_whitelist__getdns_rbtree_cmp(rbtree->cmp));
|
||||
fptr_ok(fptr_whitelist_rbtree_cmp(rbtree->cmp));
|
||||
/* Lets find the new parent... */
|
||||
while (node != RBTREE_NULL) {
|
||||
/* Compare two keys, do we have a duplicate? */
|
||||
|
@ -267,7 +268,7 @@ _getdns_rbtree_insert (_getdns_rbtree_t *rbtree, _getdns_rbnode_t *data)
|
|||
}
|
||||
|
||||
/* Fix up the red-black properties... */
|
||||
_getdns_rbtree_insert_fixup(rbtree, data);
|
||||
rbtree_insert_fixup(rbtree, data);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
@ -276,12 +277,12 @@ _getdns_rbtree_insert (_getdns_rbtree_t *rbtree, _getdns_rbnode_t *data)
|
|||
* Searches the red black tree, returns the data if key is found or NULL otherwise.
|
||||
*
|
||||
*/
|
||||
_getdns_rbnode_t *
|
||||
_getdns_rbtree_search (_getdns_rbtree_t *rbtree, const void *key)
|
||||
rbnode_type *
|
||||
rbtree_search (rbtree_type *rbtree, const void *key)
|
||||
{
|
||||
_getdns_rbnode_t *node;
|
||||
rbnode_type *node;
|
||||
|
||||
if (_getdns_rbtree_find_less_equal(rbtree, key, &node)) {
|
||||
if (rbtree_find_less_equal(rbtree, key, &node)) {
|
||||
return node;
|
||||
} else {
|
||||
return NULL;
|
||||
|
@ -295,13 +296,14 @@ static void swap_int8(uint8_t* x, uint8_t* y)
|
|||
}
|
||||
|
||||
/** helpers for delete: swap node pointers */
|
||||
static void swap_np(_getdns_rbnode_t** x, _getdns_rbnode_t** y)
|
||||
static void swap_np(rbnode_type** x, rbnode_type** y)
|
||||
{
|
||||
_getdns_rbnode_t* t = *x; *x = *y; *y = t;
|
||||
rbnode_type* t = *x; *x = *y; *y = t;
|
||||
}
|
||||
|
||||
/** Update parent pointers of child trees of 'parent' */
|
||||
static void change_parent_ptr(_getdns_rbtree_t* rbtree, _getdns_rbnode_t* parent, _getdns_rbnode_t* old, _getdns_rbnode_t* new)
|
||||
static void change_parent_ptr(rbtree_type* rbtree, rbnode_type* parent,
|
||||
rbnode_type* old, rbnode_type* new)
|
||||
{
|
||||
if(parent == RBTREE_NULL)
|
||||
{
|
||||
|
@ -315,30 +317,31 @@ static void change_parent_ptr(_getdns_rbtree_t* rbtree, _getdns_rbnode_t* parent
|
|||
if(parent->right == old) parent->right = new;
|
||||
}
|
||||
/** Update parent pointer of a node 'child' */
|
||||
static void change_child_ptr(_getdns_rbnode_t* child, _getdns_rbnode_t* old, _getdns_rbnode_t* new)
|
||||
static void change_child_ptr(rbnode_type* child, rbnode_type* old,
|
||||
rbnode_type* new)
|
||||
{
|
||||
if(child == RBTREE_NULL) return;
|
||||
log_assert(child->parent == old || child->parent == new);
|
||||
if(child->parent == old) child->parent = new;
|
||||
}
|
||||
|
||||
_getdns_rbnode_t*
|
||||
_getdns_rbtree_delete(_getdns_rbtree_t *rbtree, const void *key)
|
||||
rbnode_type*
|
||||
rbtree_delete(rbtree_type *rbtree, const void *key)
|
||||
{
|
||||
_getdns_rbnode_t *to_delete;
|
||||
_getdns_rbnode_t *child;
|
||||
if((to_delete = _getdns_rbtree_search(rbtree, key)) == 0) return 0;
|
||||
rbnode_type *to_delete;
|
||||
rbnode_type *child;
|
||||
if((to_delete = rbtree_search(rbtree, key)) == 0) return 0;
|
||||
rbtree->count--;
|
||||
|
||||
/* make sure we have at most one non-leaf child */
|
||||
if(to_delete->left != RBTREE_NULL && to_delete->right != RBTREE_NULL)
|
||||
{
|
||||
/* swap with smallest from right subtree (or largest from left) */
|
||||
_getdns_rbnode_t *smright = to_delete->right;
|
||||
rbnode_type *smright = to_delete->right;
|
||||
while(smright->left != RBTREE_NULL)
|
||||
smright = smright->left;
|
||||
/* swap the smright and to_delete elements in the tree,
|
||||
* but the _getdns_rbnode_t is first part of user data struct
|
||||
* but the rbnode_type is first part of user data struct
|
||||
* so cannot just swap the keys and data pointers. Instead
|
||||
* readjust the pointers left,right,parent */
|
||||
|
||||
|
@ -390,7 +393,7 @@ _getdns_rbtree_delete(_getdns_rbtree_t *rbtree, const void *key)
|
|||
/* change child to BLACK, removing a RED node is no problem */
|
||||
if(child!=RBTREE_NULL) child->color = BLACK;
|
||||
}
|
||||
else _getdns_rbtree_delete_fixup(rbtree, child, to_delete->parent);
|
||||
else rbtree_delete_fixup(rbtree, child, to_delete->parent);
|
||||
|
||||
/* unlink completely */
|
||||
to_delete->parent = RBTREE_NULL;
|
||||
|
@ -400,9 +403,10 @@ _getdns_rbtree_delete(_getdns_rbtree_t *rbtree, const void *key)
|
|||
return to_delete;
|
||||
}
|
||||
|
||||
static void _getdns_rbtree_delete_fixup(_getdns_rbtree_t* rbtree, _getdns_rbnode_t* child, _getdns_rbnode_t* child_parent)
|
||||
static void rbtree_delete_fixup(rbtree_type* rbtree, rbnode_type* child,
|
||||
rbnode_type* child_parent)
|
||||
{
|
||||
_getdns_rbnode_t* sibling;
|
||||
rbnode_type* sibling;
|
||||
int go_up = 1;
|
||||
|
||||
/* determine sibling to the node that is one-black short */
|
||||
|
@ -422,8 +426,8 @@ static void _getdns_rbtree_delete_fixup(_getdns_rbtree_t* rbtree, _getdns_rbnode
|
|||
child_parent->color = RED;
|
||||
sibling->color = BLACK;
|
||||
if(child_parent->right == child)
|
||||
_getdns_rbtree_rotate_right(rbtree, child_parent);
|
||||
else _getdns_rbtree_rotate_left(rbtree, child_parent);
|
||||
rbtree_rotate_right(rbtree, child_parent);
|
||||
else rbtree_rotate_left(rbtree, child_parent);
|
||||
/* new sibling after rotation */
|
||||
if(child_parent->right == child) sibling = child_parent->left;
|
||||
else sibling = child_parent->right;
|
||||
|
@ -468,7 +472,7 @@ static void _getdns_rbtree_delete_fixup(_getdns_rbtree_t* rbtree, _getdns_rbnode
|
|||
{
|
||||
sibling->color = RED;
|
||||
sibling->right->color = BLACK;
|
||||
_getdns_rbtree_rotate_left(rbtree, sibling);
|
||||
rbtree_rotate_left(rbtree, sibling);
|
||||
/* new sibling after rotation */
|
||||
if(child_parent->right == child) sibling = child_parent->left;
|
||||
else sibling = child_parent->right;
|
||||
|
@ -480,7 +484,7 @@ static void _getdns_rbtree_delete_fixup(_getdns_rbtree_t* rbtree, _getdns_rbnode
|
|||
{
|
||||
sibling->color = RED;
|
||||
sibling->left->color = BLACK;
|
||||
_getdns_rbtree_rotate_right(rbtree, sibling);
|
||||
rbtree_rotate_right(rbtree, sibling);
|
||||
/* new sibling after rotation */
|
||||
if(child_parent->right == child) sibling = child_parent->left;
|
||||
else sibling = child_parent->right;
|
||||
|
@ -493,21 +497,22 @@ static void _getdns_rbtree_delete_fixup(_getdns_rbtree_t* rbtree, _getdns_rbnode
|
|||
{
|
||||
log_assert(sibling->left->color == RED);
|
||||
sibling->left->color = BLACK;
|
||||
_getdns_rbtree_rotate_right(rbtree, child_parent);
|
||||
rbtree_rotate_right(rbtree, child_parent);
|
||||
}
|
||||
else
|
||||
{
|
||||
log_assert(sibling->right->color == RED);
|
||||
sibling->right->color = BLACK;
|
||||
_getdns_rbtree_rotate_left(rbtree, child_parent);
|
||||
rbtree_rotate_left(rbtree, child_parent);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
_getdns_rbtree_find_less_equal(_getdns_rbtree_t *rbtree, const void *key, _getdns_rbnode_t **result)
|
||||
rbtree_find_less_equal(rbtree_type *rbtree, const void *key,
|
||||
rbnode_type **result)
|
||||
{
|
||||
int r;
|
||||
_getdns_rbnode_t *node;
|
||||
rbnode_type *node;
|
||||
|
||||
log_assert(result);
|
||||
|
||||
|
@ -515,7 +520,7 @@ _getdns_rbtree_find_less_equal(_getdns_rbtree_t *rbtree, const void *key, _getdn
|
|||
node = rbtree->root;
|
||||
|
||||
*result = NULL;
|
||||
fptr_ok(fptr_whitelist__getdns_rbtree_cmp(rbtree->cmp));
|
||||
fptr_ok(fptr_whitelist_rbtree_cmp(rbtree->cmp));
|
||||
|
||||
/* While there are children... */
|
||||
while (node != RBTREE_NULL) {
|
||||
|
@ -540,19 +545,19 @@ _getdns_rbtree_find_less_equal(_getdns_rbtree_t *rbtree, const void *key, _getdn
|
|||
* Finds the first element in the red black tree
|
||||
*
|
||||
*/
|
||||
_getdns_rbnode_t *
|
||||
_getdns_rbtree_first (_getdns_rbtree_t *rbtree)
|
||||
rbnode_type *
|
||||
rbtree_first (rbtree_type *rbtree)
|
||||
{
|
||||
_getdns_rbnode_t *node;
|
||||
rbnode_type *node;
|
||||
|
||||
for (node = rbtree->root; node->left != RBTREE_NULL; node = node->left);
|
||||
return node;
|
||||
}
|
||||
|
||||
_getdns_rbnode_t *
|
||||
_getdns_rbtree_last (_getdns_rbtree_t *rbtree)
|
||||
rbnode_type *
|
||||
rbtree_last (rbtree_type *rbtree)
|
||||
{
|
||||
_getdns_rbnode_t *node;
|
||||
rbnode_type *node;
|
||||
|
||||
for (node = rbtree->root; node->right != RBTREE_NULL; node = node->right);
|
||||
return node;
|
||||
|
@ -562,10 +567,10 @@ _getdns_rbtree_last (_getdns_rbtree_t *rbtree)
|
|||
* Returns the next node...
|
||||
*
|
||||
*/
|
||||
_getdns_rbnode_t *
|
||||
_getdns_rbtree_next (_getdns_rbnode_t *node)
|
||||
rbnode_type *
|
||||
rbtree_next (rbnode_type *node)
|
||||
{
|
||||
_getdns_rbnode_t *parent;
|
||||
rbnode_type *parent;
|
||||
|
||||
if (node->right != RBTREE_NULL) {
|
||||
/* One right, then keep on going left... */
|
||||
|
@ -581,10 +586,10 @@ _getdns_rbtree_next (_getdns_rbnode_t *node)
|
|||
return node;
|
||||
}
|
||||
|
||||
_getdns_rbnode_t *
|
||||
_getdns_rbtree_previous(_getdns_rbnode_t *node)
|
||||
rbnode_type *
|
||||
rbtree_previous(rbnode_type *node)
|
||||
{
|
||||
_getdns_rbnode_t *parent;
|
||||
rbnode_type *parent;
|
||||
|
||||
if (node->left != RBTREE_NULL) {
|
||||
/* One left, then keep on going right... */
|
||||
|
@ -602,19 +607,20 @@ _getdns_rbtree_previous(_getdns_rbnode_t *node)
|
|||
|
||||
/** recursive descent traverse */
|
||||
static void
|
||||
_getdns_traverse_post(void (*func)(_getdns_rbnode_t*, void*), void* arg, _getdns_rbnode_t* node)
|
||||
traverse_post(void (*func)(rbnode_type*, void*), void* arg, rbnode_type* node)
|
||||
{
|
||||
if(!node || node == RBTREE_NULL)
|
||||
return;
|
||||
/* recurse */
|
||||
_getdns_traverse_post(func, arg, node->left);
|
||||
_getdns_traverse_post(func, arg, node->right);
|
||||
traverse_post(func, arg, node->left);
|
||||
traverse_post(func, arg, node->right);
|
||||
/* call user func */
|
||||
(*func)(node, arg);
|
||||
}
|
||||
|
||||
void
|
||||
_getdns_traverse_postorder(_getdns_rbtree_t* tree, void (*func)(_getdns_rbnode_t*, void*), void* arg)
|
||||
traverse_postorder(rbtree_type* tree, void (*func)(rbnode_type*, void*),
|
||||
void* arg)
|
||||
{
|
||||
_getdns_traverse_post(func, arg, tree->root);
|
||||
traverse_post(func, arg, tree->root);
|
||||
}
|
||||
|
|
|
@ -1,192 +1,50 @@
|
|||
/**
|
||||
*
|
||||
* \file rbtree.h
|
||||
* /brief Alternative symbol names for unbound's rbtree.h
|
||||
*
|
||||
*/
|
||||
/*
|
||||
* rbtree.h -- generic red-black tree
|
||||
*
|
||||
* Copyright (c) 2001-2007, NLnet Labs. All rights reserved.
|
||||
*
|
||||
* This software is open source.
|
||||
* Copyright (c) 2017, NLnet Labs, the getdns team
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the names of the copyright holders nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* Neither the name of the NLNET LABS nor the names of its contributors may
|
||||
* be used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
||||
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file
|
||||
* Red black tree. Implementation taken from NSD 3.0.5, adjusted for use
|
||||
* in unbound (memory allocation, logging and so on).
|
||||
*/
|
||||
|
||||
#ifndef UTIL_RBTREE_H_
|
||||
#define UTIL_RBTREE_H_
|
||||
|
||||
/**
|
||||
* This structure must be the first member of the data structure in
|
||||
* the rbtree. This allows easy casting between an _getdns_rbnode_t and the
|
||||
* user data (poor man's inheritance).
|
||||
*/
|
||||
typedef struct _getdns_rbnode_t _getdns_rbnode_t;
|
||||
/**
|
||||
* The _getdns_rbnode_t struct definition.
|
||||
*/
|
||||
struct _getdns_rbnode_t {
|
||||
/** parent in rbtree, RBTREE_NULL for root */
|
||||
_getdns_rbnode_t *parent;
|
||||
/** left node (smaller items) */
|
||||
_getdns_rbnode_t *left;
|
||||
/** right node (larger items) */
|
||||
_getdns_rbnode_t *right;
|
||||
/** pointer to sorting key */
|
||||
const void *key;
|
||||
/** colour of this node */
|
||||
uint8_t color;
|
||||
};
|
||||
|
||||
/** The nullpointer, points to empty node */
|
||||
#define RBTREE_NULL &_getdns_rbtree_null_node
|
||||
/** the global empty node */
|
||||
extern _getdns_rbnode_t _getdns_rbtree_null_node;
|
||||
|
||||
/** An entire red black tree */
|
||||
typedef struct _getdns_rbtree_t _getdns_rbtree_t;
|
||||
/** definition for tree struct */
|
||||
struct _getdns_rbtree_t {
|
||||
/** The root of the red-black tree */
|
||||
_getdns_rbnode_t *root;
|
||||
|
||||
/** The number of the nodes in the tree */
|
||||
size_t count;
|
||||
|
||||
/**
|
||||
* Key compare function. <0,0,>0 like strcmp.
|
||||
* Return 0 on two NULL ptrs.
|
||||
*/
|
||||
int (*cmp) (const void *, const void *);
|
||||
};
|
||||
|
||||
/**
|
||||
* Create new tree (malloced) with given key compare function.
|
||||
* @param cmpf: compare function (like strcmp) takes pointers to two keys.
|
||||
* @return: new tree, empty.
|
||||
*/
|
||||
_getdns_rbtree_t *_getdns_rbtree_create(int (*cmpf)(const void *, const void *));
|
||||
|
||||
/**
|
||||
* Init a new tree (malloced by caller) with given key compare function.
|
||||
* @param rbtree: uninitialised memory for new tree, returned empty.
|
||||
* @param cmpf: compare function (like strcmp) takes pointers to two keys.
|
||||
*/
|
||||
void _getdns_rbtree_init(_getdns_rbtree_t *rbtree, int (*cmpf)(const void *, const void *));
|
||||
|
||||
/**
|
||||
* Insert data into the tree.
|
||||
* @param rbtree: tree to insert to.
|
||||
* @param data: element to insert.
|
||||
* @return: data ptr or NULL if key already present.
|
||||
*/
|
||||
_getdns_rbnode_t *_getdns_rbtree_insert(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *data);
|
||||
|
||||
/**
|
||||
* Delete element from tree.
|
||||
* @param rbtree: tree to delete from.
|
||||
* @param key: key of item to delete.
|
||||
* @return: node that is now unlinked from the tree. User to delete it.
|
||||
* returns 0 if node not present
|
||||
*/
|
||||
_getdns_rbnode_t *_getdns_rbtree_delete(_getdns_rbtree_t *rbtree, const void *key);
|
||||
|
||||
/**
|
||||
* Find key in tree. Returns NULL if not found.
|
||||
* @param rbtree: tree to find in.
|
||||
* @param key: key that must match.
|
||||
* @return: node that fits or NULL.
|
||||
*/
|
||||
_getdns_rbnode_t *_getdns_rbtree_search(_getdns_rbtree_t *rbtree, const void *key);
|
||||
|
||||
/**
|
||||
* Find, but match does not have to be exact.
|
||||
* @param rbtree: tree to find in.
|
||||
* @param key: key to find position of.
|
||||
* @param result: set to the exact node if present, otherwise to element that
|
||||
* precedes the position of key in the tree. NULL if no smaller element.
|
||||
* @return: true if exact match in result. Else result points to <= element,
|
||||
* or NULL if key is smaller than the smallest key.
|
||||
*/
|
||||
int _getdns_rbtree_find_less_equal(_getdns_rbtree_t *rbtree, const void *key,
|
||||
_getdns_rbnode_t **result);
|
||||
|
||||
/**
|
||||
* Returns first (smallest) node in the tree
|
||||
* @param rbtree: tree
|
||||
* @return: smallest element or NULL if tree empty.
|
||||
*/
|
||||
_getdns_rbnode_t *_getdns_rbtree_first(_getdns_rbtree_t *rbtree);
|
||||
|
||||
/**
|
||||
* Returns last (largest) node in the tree
|
||||
* @param rbtree: tree
|
||||
* @return: largest element or NULL if tree empty.
|
||||
*/
|
||||
_getdns_rbnode_t *_getdns_rbtree_last(_getdns_rbtree_t *rbtree);
|
||||
|
||||
/**
|
||||
* Returns next larger node in the tree
|
||||
* @param rbtree: tree
|
||||
* @return: next larger element or NULL if no larger in tree.
|
||||
*/
|
||||
_getdns_rbnode_t *_getdns_rbtree_next(_getdns_rbnode_t *rbtree);
|
||||
|
||||
/**
|
||||
* Returns previous smaller node in the tree
|
||||
* @param rbtree: tree
|
||||
* @return: previous smaller element or NULL if no previous in tree.
|
||||
*/
|
||||
_getdns_rbnode_t *_getdns_rbtree_previous(_getdns_rbnode_t *rbtree);
|
||||
|
||||
/**
|
||||
* Call with node=variable of struct* with _getdns_rbnode_t as first element.
|
||||
* with type is the type of a pointer to that struct.
|
||||
*/
|
||||
#define RBTREE_FOR(node, type, rbtree) \
|
||||
for(node=(type)_getdns_rbtree_first(rbtree); \
|
||||
(_getdns_rbnode_t*)node != RBTREE_NULL; \
|
||||
node = (type)_getdns_rbtree_next((_getdns_rbnode_t*)node))
|
||||
|
||||
/**
|
||||
* Call function for all elements in the redblack tree, such that
|
||||
* leaf elements are called before parent elements. So that all
|
||||
* elements can be safely free()d.
|
||||
* Note that your function must not remove the nodes from the tree.
|
||||
* Since that may trigger rebalances of the rbtree.
|
||||
* @param tree: the tree
|
||||
* @param func: function called with element and user arg.
|
||||
* The function must not alter the rbtree.
|
||||
* @param arg: user argument.
|
||||
*/
|
||||
void _getdns_traverse_postorder(_getdns_rbtree_t* tree, void (*func)(_getdns_rbnode_t*, void*),
|
||||
void* arg);
|
||||
|
||||
#endif /* UTIL_RBTREE_H_ */
|
||||
#ifndef RBTREE_H_SYMBOLS
|
||||
#define RBTREE_H_SYMBOLS
|
||||
#define rbnode_type _getdns_rbnode_t
|
||||
#define rbtree_null_node _getdns_rbtree_null_node
|
||||
#define rbtree_type _getdns_rbtree_t
|
||||
#define rbtree_create _getdns_rbtree_create
|
||||
#define rbtree_init _getdns_rbtree_init
|
||||
#define rbtree_insert _getdns_rbtree_insert
|
||||
#define rbtree_delete _getdns_rbtree_delete
|
||||
#define rbtree_search _getdns_rbtree_search
|
||||
#define rbtree_find_less_equal _getdns_rbtree_find_less_equal
|
||||
#define rbtree_first _getdns_rbtree_first
|
||||
#define rbtree_last _getdns_rbtree_last
|
||||
#define rbtree_next _getdns_rbtree_next
|
||||
#define rbtree_previous _getdns_rbtree_previous
|
||||
#define traverse_postorder _getdns_traverse_postorder
|
||||
#include "util/ub/rbtree.h"
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,192 @@
|
|||
/*
|
||||
* rbtree.h -- generic red-black tree
|
||||
*
|
||||
* Copyright (c) 2001-2007, NLnet Labs. All rights reserved.
|
||||
*
|
||||
* This software is open source.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* Neither the name of the NLNET LABS nor the names of its contributors may
|
||||
* be used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
||||
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file
|
||||
* Red black tree. Implementation taken from NSD 3.0.5, adjusted for use
|
||||
* in unbound (memory allocation, logging and so on).
|
||||
*/
|
||||
|
||||
#ifndef UTIL_RBTREE_H_
|
||||
#define UTIL_RBTREE_H_
|
||||
|
||||
/**
|
||||
* This structure must be the first member of the data structure in
|
||||
* the rbtree. This allows easy casting between an rbnode_type and the
|
||||
* user data (poor man's inheritance).
|
||||
*/
|
||||
typedef struct rbnode_type rbnode_type;
|
||||
/**
|
||||
* The rbnode_type struct definition.
|
||||
*/
|
||||
struct rbnode_type {
|
||||
/** parent in rbtree, RBTREE_NULL for root */
|
||||
rbnode_type *parent;
|
||||
/** left node (smaller items) */
|
||||
rbnode_type *left;
|
||||
/** right node (larger items) */
|
||||
rbnode_type *right;
|
||||
/** pointer to sorting key */
|
||||
const void *key;
|
||||
/** colour of this node */
|
||||
uint8_t color;
|
||||
};
|
||||
|
||||
/** The nullpointer, points to empty node */
|
||||
#define RBTREE_NULL &rbtree_null_node
|
||||
/** the global empty node */
|
||||
extern rbnode_type rbtree_null_node;
|
||||
|
||||
/** An entire red black tree */
|
||||
typedef struct rbtree_type rbtree_type;
|
||||
/** definition for tree struct */
|
||||
struct rbtree_type {
|
||||
/** The root of the red-black tree */
|
||||
rbnode_type *root;
|
||||
|
||||
/** The number of the nodes in the tree */
|
||||
size_t count;
|
||||
|
||||
/**
|
||||
* Key compare function. <0,0,>0 like strcmp.
|
||||
* Return 0 on two NULL ptrs.
|
||||
*/
|
||||
int (*cmp) (const void *, const void *);
|
||||
};
|
||||
|
||||
/**
|
||||
* Create new tree (malloced) with given key compare function.
|
||||
* @param cmpf: compare function (like strcmp) takes pointers to two keys.
|
||||
* @return: new tree, empty.
|
||||
*/
|
||||
rbtree_type *rbtree_create(int (*cmpf)(const void *, const void *));
|
||||
|
||||
/**
|
||||
* Init a new tree (malloced by caller) with given key compare function.
|
||||
* @param rbtree: uninitialised memory for new tree, returned empty.
|
||||
* @param cmpf: compare function (like strcmp) takes pointers to two keys.
|
||||
*/
|
||||
void rbtree_init(rbtree_type *rbtree, int (*cmpf)(const void *, const void *));
|
||||
|
||||
/**
|
||||
* Insert data into the tree.
|
||||
* @param rbtree: tree to insert to.
|
||||
* @param data: element to insert.
|
||||
* @return: data ptr or NULL if key already present.
|
||||
*/
|
||||
rbnode_type *rbtree_insert(rbtree_type *rbtree, rbnode_type *data);
|
||||
|
||||
/**
|
||||
* Delete element from tree.
|
||||
* @param rbtree: tree to delete from.
|
||||
* @param key: key of item to delete.
|
||||
* @return: node that is now unlinked from the tree. User to delete it.
|
||||
* returns 0 if node not present
|
||||
*/
|
||||
rbnode_type *rbtree_delete(rbtree_type *rbtree, const void *key);
|
||||
|
||||
/**
|
||||
* Find key in tree. Returns NULL if not found.
|
||||
* @param rbtree: tree to find in.
|
||||
* @param key: key that must match.
|
||||
* @return: node that fits or NULL.
|
||||
*/
|
||||
rbnode_type *rbtree_search(rbtree_type *rbtree, const void *key);
|
||||
|
||||
/**
|
||||
* Find, but match does not have to be exact.
|
||||
* @param rbtree: tree to find in.
|
||||
* @param key: key to find position of.
|
||||
* @param result: set to the exact node if present, otherwise to element that
|
||||
* precedes the position of key in the tree. NULL if no smaller element.
|
||||
* @return: true if exact match in result. Else result points to <= element,
|
||||
* or NULL if key is smaller than the smallest key.
|
||||
*/
|
||||
int rbtree_find_less_equal(rbtree_type *rbtree, const void *key,
|
||||
rbnode_type **result);
|
||||
|
||||
/**
|
||||
* Returns first (smallest) node in the tree
|
||||
* @param rbtree: tree
|
||||
* @return: smallest element or NULL if tree empty.
|
||||
*/
|
||||
rbnode_type *rbtree_first(rbtree_type *rbtree);
|
||||
|
||||
/**
|
||||
* Returns last (largest) node in the tree
|
||||
* @param rbtree: tree
|
||||
* @return: largest element or NULL if tree empty.
|
||||
*/
|
||||
rbnode_type *rbtree_last(rbtree_type *rbtree);
|
||||
|
||||
/**
|
||||
* Returns next larger node in the tree
|
||||
* @param rbtree: tree
|
||||
* @return: next larger element or NULL if no larger in tree.
|
||||
*/
|
||||
rbnode_type *rbtree_next(rbnode_type *rbtree);
|
||||
|
||||
/**
|
||||
* Returns previous smaller node in the tree
|
||||
* @param rbtree: tree
|
||||
* @return: previous smaller element or NULL if no previous in tree.
|
||||
*/
|
||||
rbnode_type *rbtree_previous(rbnode_type *rbtree);
|
||||
|
||||
/**
|
||||
* Call with node=variable of struct* with rbnode_type as first element.
|
||||
* with type is the type of a pointer to that struct.
|
||||
*/
|
||||
#define RBTREE_FOR(node, type, rbtree) \
|
||||
for(node=(type)rbtree_first(rbtree); \
|
||||
(rbnode_type*)node != RBTREE_NULL; \
|
||||
node = (type)rbtree_next((rbnode_type*)node))
|
||||
|
||||
/**
|
||||
* Call function for all elements in the redblack tree, such that
|
||||
* leaf elements are called before parent elements. So that all
|
||||
* elements can be safely free()d.
|
||||
* Note that your function must not remove the nodes from the tree.
|
||||
* Since that may trigger rebalances of the rbtree.
|
||||
* @param tree: the tree
|
||||
* @param func: function called with element and user arg.
|
||||
* The function must not alter the rbtree.
|
||||
* @param arg: user argument.
|
||||
*/
|
||||
void traverse_postorder(rbtree_type* tree, void (*func)(rbnode_type*, void*),
|
||||
void* arg);
|
||||
|
||||
#endif /* UTIL_RBTREE_H_ */
|
Loading…
Reference in New Issue