Commit 010ab8a3 authored by Philip Carns's avatar Philip Carns

remove unused interval merge and rb trees

parent 068648d8
......@@ -88,10 +88,6 @@ src_libcodes_base_a_SOURCES = \
src/modelconfig/txt_configfile.c \
codes/tools.h \
src/util/tools.c \
src/util/interval-merge-tree.h \
src/util/interval-merge-tree.c \
src/util/red-black-tree.h \
src/util/red-black-tree.c \
src/util/codes-callbacks.h \
src/util/codes-callbacks.c \
src/util/codes_mapping.c \
......
#include "util/interval-merge-tree.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <pthread.h>
#include <assert.h>
/* rb tree sentinel */
static size_t interval_merge_tree_sentinel_key_key = 0;
static size_t interval_merge_tree_sentinel_key_value = 0;
static interval_merge_tree_key_t interval_merge_tree_sentinel_key =
{ &interval_merge_tree_sentinel_key_key, &interval_merge_tree_sentinel_key_value };
static interval_merge_tree_node_t interval_merge_tree_sentinel =
{ {&interval_merge_tree_sentinel_key_key, &interval_merge_tree_sentinel_key_value}, NULL, NULL,
NULL, 0, 0, {0, 0}, NULL, NULL, RB_TREE_COLOR_BLACK, 0, NULL };
#define INTERVAL_TREE_LL_POOL_SIZE 4096
#define INTERVAL_TREE_NODE_POOL_SIZE 4096
static size_t interval_merge_tree_ll_pool_size = 0;
static size_t interval_merge_tree_node_pool_size = 0;
static size_t ll_pool_next = 0;
static size_t node_pool_next = 0;
static interval_merge_tree_node_t *interval_merge_tree_node_pool = NULL;
static interval_merge_tree_node_t **interval_merge_tree_node_pool_index = NULL;
static interval_merge_tree_node_t *interval_merge_tree_node_pool_min = NULL;
static interval_merge_tree_node_t *interval_merge_tree_node_pool_max = NULL;
static interval_merge_tree_interval_ll_t *interval_merge_tree_ll_pool = NULL;
static interval_merge_tree_interval_ll_t **interval_merge_tree_ll_pool_index = NULL;
static interval_merge_tree_interval_ll_t *interval_merge_tree_ll_pool_min = NULL;
static interval_merge_tree_interval_ll_t *interval_merge_tree_ll_pool_max = NULL;
static inline int interval_merge_tree_create_ll_pool(
size_t num)
{
size_t i = 0;
interval_merge_tree_ll_pool =
(interval_merge_tree_interval_ll_t *) malloc(sizeof(interval_merge_tree_interval_ll_t) *
num);
interval_merge_tree_ll_pool_index =
(interval_merge_tree_interval_ll_t **) malloc(sizeof(interval_merge_tree_interval_ll_t *) *
num);
interval_merge_tree_ll_pool_size = num;
ll_pool_next = 0;
interval_merge_tree_ll_pool_min = &(interval_merge_tree_ll_pool[0]);
interval_merge_tree_ll_pool_max = &(interval_merge_tree_ll_pool[num - 1]);
for(i = 0; i < num; i++)
{
interval_merge_tree_ll_pool_index[i] = &(interval_merge_tree_ll_pool[i]);
}
return 0;
}
static inline int interval_merge_tree_destroy_ll_pool(
)
{
free(interval_merge_tree_ll_pool);
interval_merge_tree_ll_pool = NULL;
free(interval_merge_tree_ll_pool_index);
interval_merge_tree_ll_pool_index = NULL;
ll_pool_next = 0;
interval_merge_tree_ll_pool_size = 0;
interval_merge_tree_ll_pool_min = NULL;
interval_merge_tree_ll_pool_max = NULL;
return 0;
}
static inline interval_merge_tree_interval_ll_t *interval_merge_tree_alloc_ll(
)
{
if(ll_pool_next < interval_merge_tree_ll_pool_size)
{
return interval_merge_tree_ll_pool_index[ll_pool_next++];
}
else
{
return (interval_merge_tree_interval_ll_t *)
malloc(sizeof(interval_merge_tree_interval_ll_t));
}
return NULL;
}
static inline int interval_merge_tree_dealloc_ll(
interval_merge_tree_interval_ll_t * node)
{
if(node >= interval_merge_tree_ll_pool_min && node <= interval_merge_tree_ll_pool_max)
{
interval_merge_tree_ll_pool_index[--ll_pool_next] = node;
}
else
{
free(node);
}
return 0;
}
static inline int interval_merge_tree_create_node_pool(
size_t num)
{
size_t i = 0;
interval_merge_tree_node_pool =
(interval_merge_tree_node_t *) malloc(sizeof(interval_merge_tree_node_t) * num);
interval_merge_tree_node_pool_index =
(interval_merge_tree_node_t **) malloc(sizeof(interval_merge_tree_node_t *) * num);
interval_merge_tree_node_pool_size = num;
node_pool_next = 0;
interval_merge_tree_node_pool_min = &(interval_merge_tree_node_pool[0]);
interval_merge_tree_node_pool_max = &(interval_merge_tree_node_pool[num - 1]);
for(i = 0; i < num; i++)
{
interval_merge_tree_node_pool_index[i] = &(interval_merge_tree_node_pool[i]);
}
return 0;
}
static inline int interval_merge_tree_destroy_node_pool(
)
{
free(interval_merge_tree_node_pool);
interval_merge_tree_node_pool = NULL;
free(interval_merge_tree_node_pool_index);
interval_merge_tree_node_pool_index = NULL;
interval_merge_tree_node_pool_size = 0;
node_pool_next = 0;
interval_merge_tree_node_pool_min = NULL;
interval_merge_tree_node_pool_max = NULL;
return 0;
}
static inline interval_merge_tree_node_t *interval_merge_tree_alloc_node(
)
{
if(node_pool_next < interval_merge_tree_node_pool_size)
{
return interval_merge_tree_node_pool_index[node_pool_next++];
}
else
{
return (interval_merge_tree_node_t *) malloc(sizeof(interval_merge_tree_node_t));
}
return NULL;
}
static inline int interval_merge_tree_dealloc_node(
interval_merge_tree_node_t * node)
{
if(node >= interval_merge_tree_node_pool_min && node <= interval_merge_tree_node_pool_max)
{
interval_merge_tree_node_pool_index[--node_pool_next] = node;
}
else
{
free(node);
}
return 0;
}
static inline interval_merge_tree_interval_ll_t *interval_merge_tree_interval_ll_create(
)
{
interval_merge_tree_interval_ll_t *ll =
(interval_merge_tree_interval_ll_t *) malloc(sizeof(interval_merge_tree_interval_ll_t));
//interval_merge_tree_interval_ll_t * ll = interval_merge_tree_alloc_ll();
ll->next = NULL;
ll->data = NULL;
ll->key = NULL;
return ll;
}
inline int interval_merge_tree_interval_ll_destroy(
interval_merge_tree_interval_ll_t * ll)
{
interval_merge_tree_interval_ll_t *cur = ll;
interval_merge_tree_interval_ll_t *next = NULL;
while(cur)
{
next = cur->next;
cur->next = NULL;
cur->data = NULL;
free(cur);
//interval_merge_tree_dealloc_ll(cur);
cur = next;
}
return 0;
}
/* copy the n2 list into the n1 list */
static inline int interval_merge_tree_interval_ll_merge(
interval_merge_tree_node_t * n1,
interval_merge_tree_node_t ** n2)
{
interval_merge_tree_interval_ll_t *l2_head = (*n2)->ll_head;
interval_merge_tree_interval_ll_t *l2_tail = (*n2)->ll_tail;
if(l2_head && l2_tail)
{
if(n1->ll_head && n1->ll_tail)
{
/* set the next value after n1 tail to n2 head */
n1->ll_tail->next = l2_head;
n1->ll_tail = l2_tail;
l2_tail->next = NULL;
(*n2)->ll_head = NULL;
(*n2)->ll_tail = NULL;
}
else
{
/* if the current node list was empty, set it to the l2 values */
n1->ll_head = l2_head;
n1->ll_tail = l2_tail;
l2_tail->next = NULL;
(*n2)->ll_head = NULL;
(*n2)->ll_tail = NULL;
}
}
return 0;
}
inline int interval_merge_tree_interval_ll_init(
interval_merge_tree_node_t * node,
void *data)
{
/* create the link list for the node and update the head with the data */
node->ll_head = interval_merge_tree_interval_ll_create();
node->ll_head->data = data;
node->ll_head->key = node->key.key;
node->ll_tail = node->ll_head;
return 0;
}
/* determine the max value for an interval or node */
inline static size_t interval_merge_tree_interval_max(
interval_merge_tree_node_t * node)
{
if(node->interval.end > node->left->max)
{
if(node->interval.end > node->right->max)
{
return node->interval.end;
}
else /* if(i->end <= node->right->max) */
{
return node->right->max;
}
}
else /* if(i->end <= node->left->max) */
{
if(node->left->max > node->right->max)
{
return node->left->max;
}
else /* if(node->left->max <= node->left->max) */
{
return node->right->max;
}
}
/* should not get here */
return 0;
}
/* create an empty node */
inline interval_merge_tree_node_t *interval_merge_tree_create_node(
)
{
interval_merge_tree_node_t *node = NULL;
/* try to get a node from the node pool first */
#if 0
node = interval_merge_tree_node_pool_alloc_node();
if(!node)
#endif
{
node = (interval_merge_tree_node_t *) malloc(sizeof(interval_merge_tree_node_t));
//node = interval_merge_tree_alloc_node();
node->node_pool = 0;
}
/* set child and parent pointers to NULL */
node->left = &interval_merge_tree_sentinel;
node->right = &interval_merge_tree_sentinel;
node->parent = &interval_merge_tree_sentinel;
/* set the key to NULL */
node->key.key = NULL;
node->key.value = NULL;
/* set to a non-valid color */
node->color = RB_TREE_COLOR_NONE;
/* create the interval */
node->interval.start = 0;
node->interval.end = 0;
/* set the max interval value */
node->max = 0;
/* set the order stat size to 0 */
node->size = 0;
/* create the link list for intervals... set the head to the tail */
node->ll_head = NULL;
node->ll_tail = node->ll_head;
/* init the consumer to null */
node->consumer = NULL;
return node;
}
/* destroy a node */
inline int interval_merge_tree_destroy_node(
interval_merge_tree_node_t * node)
{
if(node && node != &interval_merge_tree_sentinel)
{
node->left = NULL;
node->right = NULL;
/* destroy the key */
node->key.key = NULL;
node->key.value = NULL;
/* do not attempt to destroy the parent node ... just set to NULL */
node->parent = NULL;
/* set the color to 0 */
node->color = 0;
/* set the max interval value */
node->max = 0;
/* destroy the interval */
node->interval.start = 0;
node->interval.end = 0;
/* erase the pointers to, but do not destroy, the link list */
node->ll_head = NULL;
node->ll_tail = NULL;
/* destroy the node */
#if 0
if(node->node_pool)
{
interval_merge_tree_node_pool_free_node(node);
}
else
#endif
{
free(node);
//interval_merge_tree_dealloc_node(node);
}
}
return 0;
}
/* recycle a node */
inline int interval_merge_tree_recycle_node(
interval_merge_tree_node_t * node)
{
if(node && node != &interval_merge_tree_sentinel)
{
node->left = NULL;
node->right = NULL;
/* destroy the key */
node->key.key = NULL;
node->key.value = NULL;
/* do not attempt to destroy the parent node ... just set to NULL */
node->parent = NULL;
/* set the color to 0 */
node->color = 0;
/* set the max interval value */
node->max = 0;
/* destroy the interval */
//interval_merge_tree_destroy_interval(node->interval);
node->interval.start = 0;
node->interval.end = 0;
/* erase the pointers to, but do not destroy, the link list */
node->ll_head = NULL;
node->ll_tail = NULL;
}
return 0;
}
/* destroy a node and it's children... but not the node parent */
int interval_merge_tree_destroy_tree(
interval_merge_tree_node_t * node)
{
if(node && node != &interval_merge_tree_sentinel)
{
/* destroy the left node */
if(node->left && node->left != &interval_merge_tree_sentinel)
{
interval_merge_tree_destroy_tree(node->left);
node->left = NULL;
}
/* destory the right node */
if(node->right && node->right != &interval_merge_tree_sentinel)
{
interval_merge_tree_destroy_tree(node->right);
node->right = NULL;
}
/* destroy the key */
node->key.key = NULL;
node->key.value = NULL;
/* do not attempt to destroy the parent node ... just set to NULL */
node->parent = NULL;
/* set the color to 0 */
node->color = 0;
/* set the max interval value */
node->max = 0;
/* destroy the interval */
node->interval.start = 0;
node->interval.end = 0;
/* cleanup the interval link list */
interval_merge_tree_interval_ll_destroy(node->ll_head);
node->ll_head = NULL;
node->ll_tail = NULL;
/* destroy the node */
#if 0
if(node->node_pool)
{
interval_merge_tree_node_pool_free_node(node);
}
else
#endif
{
free(node);
//interval_merge_tree_dealloc_node(node);
}
}
/* cleanup the mem pools */
//interval_merge_tree_destroy_ll_pool();
//interval_merge_tree_destroy_node_pool();
return 0;
}
/* rotate the nodes to the left */
static inline int interval_merge_tree_rotate_left(
interval_merge_tree_node_t ** root,
interval_merge_tree_node_t * x,
int op)
{
interval_merge_tree_node_t *y = x->right;
x->right = y->left;
if(y->left && y->left != &interval_merge_tree_sentinel)
{
y->left->parent = x;
}
y->parent = x->parent;
if(!x->parent || x->parent == &interval_merge_tree_sentinel)
{
*root = y;
}
else
{
if(x == x->parent->left)
{
x->parent->left = y;
}
else
{
x->parent->right = y;
}
}
y->left = x;
x->parent = y;
/* update the size based on the operation */
if(op == RB_TREE_INSERT)
{
if(x != &interval_merge_tree_sentinel && y != &interval_merge_tree_sentinel)
{
y->size = x->size;
assert(x->right);
assert(x->left);
x->size = x->left->size + x->right->size + 1;
}
}
else
{
if(x != &interval_merge_tree_sentinel && y != &interval_merge_tree_sentinel)
{
y->size = x->size;
assert(x->right);
assert(x->left);
x->size = x->left->size - x->right->size - 1;
}
}
/* update the max interval */
y->max = interval_merge_tree_interval_max(y);
x->max = interval_merge_tree_interval_max(x);
return 0;
}
/* rotate the nodes to the right */
static inline int interval_merge_tree_rotate_right(
interval_merge_tree_node_t ** root,
interval_merge_tree_node_t * y,
int op)
{
interval_merge_tree_node_t *x = y->left;
y->left = x->right;
if(x->right && x->right != &interval_merge_tree_sentinel)
{
x->right->parent = y;
}
x->parent = y->parent;
if(!y->parent || y->parent == &interval_merge_tree_sentinel)
{
*root = x;
}
else
{
if(y == y->parent->right)
{
y->parent->right = x;
}
else
{
y->parent->left = x;
}
}
x->right = y;
y->parent = x;
/* update the size based on the operation */
if(op == RB_TREE_INSERT)
{
if(x != &interval_merge_tree_sentinel && y != &interval_merge_tree_sentinel)
{
x->size = y->size;
assert(y->left);
assert(y->right);
y->size = y->left->size + y->right->size + 1;
}
}
else
{
if(x != &interval_merge_tree_sentinel && y != &interval_merge_tree_sentinel)
{
x->size = y->size;
assert(y->left);
assert(y->right);
y->size = y->left->size - y->right->size - 1;
}
}
/* update the max interval */
x->max = interval_merge_tree_interval_max(x);
y->max = interval_merge_tree_interval_max(y);
return 0;
}
/* function to make sure the red-black tree properties are followed after inserts */
static int interval_merge_tree_insert_rules(
interval_merge_tree_node_t ** root,
interval_merge_tree_node_t * node)
{
interval_merge_tree_node_t *y = NULL;
while(node->parent->color == RB_TREE_COLOR_RED)
{
if(node->parent == node->parent->parent->left)
{
y = node->parent->parent->right;
if(y->color == RB_TREE_COLOR_RED)
{
node->parent->color = RB_TREE_COLOR_BLACK;
y->color = RB_TREE_COLOR_BLACK;
node->parent->parent->color = RB_TREE_COLOR_RED;
node = node->parent->parent;
}
else
{
if(node == node->parent->right)
{
node = node->parent;
interval_merge_tree_rotate_left(root, node, RB_TREE_INSERT);
}
node->parent->color = RB_TREE_COLOR_BLACK;
node->parent->parent->color = RB_TREE_COLOR_RED;
interval_merge_tree_rotate_right(root, node->parent->parent, RB_TREE_INSERT);
}
}
else
{
y = node->parent->parent->left;
if(y->color == RB_TREE_COLOR_RED)
{
node->parent->color = RB_TREE_COLOR_BLACK;
y->color = RB_TREE_COLOR_BLACK;
node->parent->parent->color = RB_TREE_COLOR_RED;
node = node->parent->parent;
}
else
{
if(node == node->parent->left)
{
node = node->parent;
interval_merge_tree_rotate_right(root, node, RB_TREE_INSERT);
}
node->parent->color = RB_TREE_COLOR_BLACK;
node->parent->parent->color = RB_TREE_COLOR_RED;
interval_merge_tree_rotate_left(root, node->parent->parent, RB_TREE_INSERT);
}
}
}
(*root)->color = RB_TREE_COLOR_BLACK;
return 0;
}
/* insert a node into the rb tree */
int interval_merge_tree_insert(
interval_merge_tree_node_t ** node,
interval_merge_tree_key_t key,
interval_merge_tree_node_t ** root)
{
interval_merge_tree_node_t *y = &interval_merge_tree_sentinel;
interval_merge_tree_node_t *x = *root;
/* if x is null, make it a sentinel */
if(!x)
{
x = &interval_merge_tree_sentinel;
}
/* if this is a new tree */
if(!*root)
{
/* create new memory pools */
//interval_merge_tree_create_ll_pool(INTERVAL_TREE_LL_POOL_SIZE);
//interval_merge_tree_create_node_pool(INTERVAL_TREE_NODE_POOL_SIZE);
}
if(!*node)
{
*node = interval_merge_tree_create_node();
/* update the size of the new node */
(*node)->size = 1;
/* set the node max to the end of the interval */
(*node)->max = (*node)->interval.end;
}
/* set the key */
(*node)->key = key;
while(x != &interval_merge_tree_sentinel)
{
y = x;
/* this is a duplicate... don't insert it */
if(*(size_t *) key.key == *(size_t *) x->key.key)
{
return 1;
}
else
{
/* update the size of the path */
x->size += 1;
/* update the max variable for all nodes along the path */
if((*node)->max > x->max)
{
x->max = (*node)->max;
}
if(*(size_t *) key.key < *(size_t *) x->key.key)
{
x = x->left;
}