along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: rbl.c,v 1.1.2.5 2000/11/19 02:04:29 guus Exp $
+ $Id: rbl.c,v 1.1.2.12 2000/11/24 23:12:59 guus Exp $
*/
+#include "config.h"
+
+#include <stdlib.h>
#include <xalloc.h>
#include "rbl.h"
+#include <system.h>
/* Allocate a new rbl node */
rbl_t *new_rbl()
/* Free a rbl node */
void free_rbl(rbl_t *rbl)
{
+ if(rbl->data && rbl->tree->delete)
+ rbl->tree->delete(rbl->data);
free(rbl);
}
}
/* Search closest match in the tree */
-rbl_t *rbl_search_closest(rbltree_t *tree, void *data)
+rbl_t *rbl_search_closest_rbl(rbltree_t *tree, void *data)
{
rbl_t *rbl, *next;
int result;
-
+
next = rbl = tree->top;
while(next)
result = tree->compare(data, rbl->data);
-// fprintf(stderr, "comparing %s with %s = %d\n", rbl->data, data, result);
-
if(result < 0)
next = rbl->left;
else if(result > 0)
return rbl;
}
+/* Search closest match in the tree */
+rbl_t *rbl_search_closest_greater_rbl(rbltree_t *tree, void *data)
+{
+ rbl_t *rbl;
+
+ rbl = rbl_search_closest_rbl(tree, data);
+
+ if(rbl)
+ {
+ if(tree->compare(data, rbl->data) > 0)
+ rbl = rbl->next;
+ }
+
+ return rbl;
+}
+
+/* Search closest match in the tree */
+rbl_t *rbl_search_closest_smaller_rbl(rbltree_t *tree, void *data)
+{
+ rbl_t *rbl;
+
+ rbl = rbl_search_closest_rbl(tree, data);
+
+ if(rbl)
+ {
+ if(tree->compare(data, rbl->data) < 0)
+ rbl = rbl->next;
+ }
+
+ return rbl;
+}
+
+void *rbl_search_closest(rbltree_t *tree, void *data)
+{
+ rbl_t *rbl;
+
+ rbl = rbl_search_closest_rbl(tree, data);
+
+ if(rbl)
+ return rbl->data;
+ else
+ return NULL;
+}
+
+void *rbl_search_closest_greater(rbltree_t *tree, void *data)
+{
+ rbl_t *rbl;
+
+ rbl = rbl_search_closest_greater_rbl(tree, data);
+
+ if(rbl)
+ return rbl->data;
+ else
+ return NULL;
+}
+
+void *rbl_search_closest_smaller(rbltree_t *tree, void *data)
+{
+ rbl_t *rbl;
+
+ rbl = rbl_search_closest_smaller_rbl(tree, data);
+
+ if(rbl)
+ return rbl->data;
+ else
+ return NULL;
+}
+
/* Search exact match or return NULL pointer */
-rbl_t *rbl_search(rbltree_t *tree, void *data)
+rbl_t *rbl_search_rbl(rbltree_t *tree, void *data)
{
- rbl_t *rbl, *next;
+ rbl_t *rbl;
int result;
+
+ rbl = tree->top;
- next = rbl = tree->top;
-
- while(next)
+ while(rbl)
{
- rbl = next;
-
- result = tree->compare(rbl->data, data);
+ result = tree->compare(data, rbl->data);
if(result < 0)
- next = rbl->left;
+ rbl = rbl->left;
else if(result > 0)
- next = rbl->right;
+ rbl = rbl->right;
else
return rbl;
}
-
+
return NULL;
}
+void *rbl_search(rbltree_t *tree, void *data)
+{
+ rbl_t *rbl;
+
+ rbl = rbl_search_rbl(tree, data);
+
+ if(rbl)
+ return rbl->data;
+ else
+ return NULL;
+}
+
/* Red-black tree operations taken from Introduction to Algorithms,
Cormen, Leiserson & Rivest, chapter 14.
*/
if(tree->top)
{
- closest = rbl_search_closest(tree, rbl->data);
+ closest = rbl_search_closest_rbl(tree, rbl->data);
result = tree->compare(rbl->data, closest->data);
if(result < 0)
{
rbl_t *x, *y;
/* Binary tree delete */
-
+
if(rbl->left && rbl->right)
y = rbl->next;
else
/* Red-black part of delete */
- if(y->color == RBL_BLACK)
+ if(y->color == RBL_BLACK && x)
rbl_delete_fixup(x);
-
+
return rbl;
}
{
rbl_t *rbl;
- rbl = rbl_search(tree, data);
+ rbl = rbl_search_rbl(tree, data);
if(rbl)
- return rbl_unlink_rbl(rbl);
- else
- return NULL;
+ rbl_unlink_rbl(rbl);
+
+ return rbl;
}
/* Unlink node and free it */
void rbl_delete_rbl(rbl_t *rbl)
{
- free_rbl(rbl_unlink_rbl(rbl));
+ rbl_unlink_rbl(rbl);
+ free_rbl(rbl);
}
/* Search node in tree, unlink and free it */
void rbl_delete(rbltree_t *tree, void *data)
{
- free_rbl(rbl_unlink(tree, data));
+ rbl_t *rbl;
+
+ rbl = rbl_unlink(tree, data);
+
+ if(rbl)
+ free_rbl(rbl);
+}
+
+/* Optimized unlinking for a complete tree */
+void rbl_unlink_rbltree(rbltree_t *tree)
+{
+ rbl_t *rbl, *next;
+
+ for(rbl = tree->head; rbl; rbl = next)
+ {
+ next = rbl->next;
+ rbl->tree = NULL;
+ rbl->parent = NULL;
+ rbl->left = NULL;
+ rbl->right = NULL;
+ rbl->prev = NULL;
+ rbl->next = NULL;
+ }
+
+ tree->top = NULL;
+ tree->head = NULL;
+ tree->tail = NULL;
+}
+
+/* Optimized deletion for a complete tree */
+void rbl_delete_rbltree(rbltree_t *tree)
+{
+ rbl_t *rbl, *next;
+
+ for(rbl = tree->head; rbl; rbl = next)
+ {
+ next = rbl->next;
+ free_rbl(rbl);
+ }
+
+ tree->top = NULL;
+ tree->head = NULL;
+ tree->tail = NULL;
}
/* Do action for each list entry (in order)
{
rbl_t *rbl, *next;
+ for(rbl = tree->head; rbl; rbl = next)
+ {
+ next = rbl->next;
+ action(rbl->data);
+ }
+}
+
+void rbl_foreach_rbl(rbltree_t *tree, rbl_action_rbl_t action)
+{
+ rbl_t *rbl, *next;
+
for(rbl = tree->head; rbl; rbl = next)
{
next = rbl->next;