diff --git a/CMakeLists.txt b/CMakeLists.txt index d61f265..1f55b57 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,8 @@ set(CMAKE_BUILD_TYPE "RelWithDebInfo") include(ConfigureChecks) +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=unused-parameter") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=unused-parameter") include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) include_directories(${CMAKE_CURRENT_BINARY_DIR}) @@ -31,6 +33,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third-party/judy-1.0.5/src) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third-party/tommyds/tommyds) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third-party/xxHash) + add_subdirectory(src) add_subdirectory(third-party) diff --git a/include/target-utils/rbt/rbt.h b/include/target-utils/rbt/rbt.h index 56c8489..3aadc82 100644 --- a/include/target-utils/rbt/rbt.h +++ b/include/target-utils/rbt/rbt.h @@ -33,20 +33,20 @@ typedef enum bf_rbt_node_direction { } bf_rbt_node_direction_t; typedef struct bf_rbt_node_t { - uint32_t priority; + uint32_t key; bool color; struct bf_rbt_node_t *left, *right, *parent; void *data; } bf_rbt_node_t; /*! - * Create a tree node for the priority + * Create a tree node for the key * - * @param priority parsed from match spec + * @param key to be created * @param root parent node of new node to be created * @return pointer to newly created node */ -bf_rbt_node_t *bf_create_rbt_node(uint32_t priority, bf_rbt_node_t *root); +bf_rbt_node_t *bf_create_rbt_node(uint32_t key, bf_rbt_node_t *root); /*! * Perform a right rotation of node @@ -71,7 +71,7 @@ bf_rbt_node_t *bf_left_rotate_rbt_node(bf_rbt_node_t *node, bf_rbt_node_t **rbt_ * * @param parent node based on which direction of rotation is evaluated * @param rbt_head head ptr of rb-tree - * @param key priority of child node + * @param key of child node * @return pointer to node after rotation */ bf_rbt_node_t *bf_perform_rotation(bf_rbt_node_t *parent, bf_rbt_node_t **rbt_head, uint32_t key); @@ -80,11 +80,10 @@ bf_rbt_node_t *bf_perform_rotation(bf_rbt_node_t *parent, bf_rbt_node_t **rbt_he * Fix RBT node colors as part of balancing tree * * @param parent node based on which direction of rotation is evaluated - * @param key priority of child node - * @param key priority of child node + * @param rbt_head head ptr of rb-tree * @return void */ -void bf_color_fix_rbt_nodes(bf_rbt_node_t *parent, bf_rbt_node_t **rbt_head, uint32_t key); +void bf_color_fix_rbt_nodes(bf_rbt_node_t *parent, bf_rbt_node_t **rbt_head); /*! * Retrieve color of neighbor node @@ -127,61 +126,64 @@ bf_rbt_node_t *bf_get_successor_rbt_node(bf_rbt_node_t *root); bf_rbt_node_t *bf_get_predecessor_rbt_node(bf_rbt_node_t *root); /*! - * Insert a tree node into RBT with given priority + * Insert a tree node into RBT with given key * - * @param key priority to be inserted into RBT + * @param key to be inserted into RBT * @param root parent node of new node to be created + * @param rbt_head head ptr of rb-tree * @return pointer to newly created node */ bf_rbt_node_t *bf_insert_rbt_entry(bf_rbt_node_t *root, uint32_t key, bf_rbt_node_t **rbt_head); /*! - * Rtrieve node which is just lesser than or equal to given priority + * Rtrieve node which is just lesser than or equal to given key * - * @param priority for which lower bound has to be found + * @param key for which lower bound has to be found * @param rbt_head head ptr of rb-tree * @return pointer to lower bound node */ -bf_rbt_node_t *bf_get_lower_bound(uint32_t priority, bf_rbt_node_t *rbt_head); +bf_rbt_node_t *bf_get_lower_bound(uint32_t key, bf_rbt_node_t *rbt_head); /*! - * Retrieve node which is just greater than or equal to given priority + * Retrieve node which is just greater than or equal to given key * - * @param priority for which upper bound has to be found + * @param key for which upper bound has to be found * @param rbt_head head ptr of rb-tree * @return pointer to upper bound node */ -bf_rbt_node_t *bf_get_upper_bound(uint32_t priority, bf_rbt_node_t *rbt_head); +bf_rbt_node_t *bf_get_upper_bound(uint32_t key, bf_rbt_node_t *rbt_head); /*! - * Retrieve node with highest priority in rb-tree + * Retrieve node with highest key in rb-tree * * @param rbt_head head ptr of rb-tree - * @return pointer to highest priority node + * @return pointer to highest key node */ -bf_rbt_node_t *bf_get_highest_priority_node(bf_rbt_node_t *rbt_head); +bf_rbt_node_t *bf_get_highest_key_node(bf_rbt_node_t *rbt_head); /*! - * Retrieve node with lowest priority in rb-tree + * Retrieve node with lowest key in rb-tree * * @param rbt_head head ptr of rb-tree - * @return pointer to lowest priority node + * @return pointer to lowest key node */ -bf_rbt_node_t *bf_get_lowest_priority_node(bf_rbt_node_t *rbt_head); +bf_rbt_node_t *bf_get_lowest_key_node(bf_rbt_node_t *rbt_head); /*! * Perform BST node deletion for given key and update color information * - * @param key priority to be deleted from RBT + * @param key to be deleted from RBT + * @param rbt_head head ptr of rb-tree * @param color address where color needs to be updated * @return node which has to be deleted */ bf_rbt_node_t *bf_bst_node_deletion(uint32_t key, bf_rbt_node_t *rbt_head, int *color); /*! - * Remove a tree node from RBT with given priority + * Remove a tree node from RBT with given key * - * @param key priority of node to be deleted from RBT + * @param key of node to be deleted from RBT + * @param rbt_head head ptr of rb-tree * @return status of API call */ int bf_remove_rbt_entry(uint32_t key, bf_rbt_node_t **rbt_head); @@ -190,7 +192,8 @@ int bf_remove_rbt_entry(uint32_t key, bf_rbt_node_t **rbt_head); * Balance RBT after insertion of new node * * @param root parent of the node which got inserted - * @param key priority of inserted node + * @param rbt_head head ptr of rb-tree + * @param key of inserted node * @return void */ void bf_balance_rbt_post_insertion(bf_rbt_node_t *root, bf_rbt_node_t **rbt_head, uint32_t key); @@ -199,6 +202,7 @@ void bf_balance_rbt_post_insertion(bf_rbt_node_t *root, bf_rbt_node_t **rbt_head * Balance RBT after deletion of a node * * @param node node to be deleted from RBT + * @param rbt_head head ptr of rb-tree * @return void */ void bf_balance_rbt_post_deletion(bf_rbt_node_t *node, bf_rbt_node_t **rbt_head); diff --git a/src/rbt/rbt.c b/src/rbt/rbt.c index a50d905..7d4b8cc 100644 --- a/src/rbt/rbt.c +++ b/src/rbt/rbt.c @@ -17,11 +17,11 @@ #include #include -bf_rbt_node_t *bf_create_rbt_node(uint32_t priority, bf_rbt_node_t *root) { +bf_rbt_node_t *bf_create_rbt_node(uint32_t key, bf_rbt_node_t *root) { bf_rbt_node_t *new_node = (bf_rbt_node_t *)bf_sys_calloc(1, sizeof(bf_rbt_node_t)); if (new_node == NULL) return NULL; - new_node->priority = priority; + new_node->key = key; new_node->color = RED; new_node->left = new_node->right = NULL; new_node->parent = root; @@ -46,7 +46,7 @@ bf_rbt_node_t *bf_right_rotate_rbt_node(bf_rbt_node_t *node, bf_rbt_node_t **rbt g_child->parent = node; if (g_parent != NULL) { - if (g_parent->priority < l_child->priority) + if (g_parent->key < l_child->key) g_parent->right = l_child; else g_parent->left = l_child; @@ -76,7 +76,7 @@ bf_rbt_node_t *bf_left_rotate_rbt_node(bf_rbt_node_t *node, bf_rbt_node_t **rbt_ g_child->parent = node; if (g_parent != NULL) { - if (g_parent->priority < r_child->priority) + if (g_parent->key < r_child->key) g_parent->right = r_child; else g_parent->left = r_child; @@ -90,14 +90,14 @@ bf_rbt_node_t *bf_left_rotate_rbt_node(bf_rbt_node_t *node, bf_rbt_node_t **rbt_ bf_rbt_node_t *bf_perform_rotation(bf_rbt_node_t *parent, bf_rbt_node_t **rbt_head, uint32_t key) { bf_rbt_node_t *g_parent = parent->parent; - /* If g_parent->priority < parent->priority, + /* If g_parent->key < parent->key, * then parent is right child to grand parent */ - if (g_parent->priority < parent->priority) { - /* If parent->priority < key, then new node is right child to parent + if (g_parent->key < parent->key) { + /* If parent->key < key, then new node is right child to parent * Perform left rotation */ - if (parent->priority < key) { + if (parent->key < key) { parent = bf_left_rotate_rbt_node(g_parent, rbt_head); } /* If new node is left child to parent. It needs 2 rotations. @@ -108,14 +108,14 @@ bf_rbt_node_t *bf_perform_rotation(bf_rbt_node_t *parent, bf_rbt_node_t **rbt_he parent = bf_left_rotate_rbt_node(g_parent, rbt_head); } } - /* If g_parent->priority > parent->priority, + /* If g_parent->key > parent->key, * then parent is left child to grand parent */ else { - /* If parent->priority < key, then new node is right child to parent + /* If parent->key < key, then new node is right child to parent * Left rotate parent, right rotate grand parent node */ - if (parent->priority < key) { + if (parent->key < key) { parent = bf_left_rotate_rbt_node(parent, rbt_head); parent = bf_right_rotate_rbt_node(g_parent, rbt_head); } @@ -147,14 +147,14 @@ bf_rbt_node_t *bf_get_predecessor_rbt_node(bf_rbt_node_t *root) { return predecessor; } -bf_rbt_node_t *bf_get_lower_bound(uint32_t priority, bf_rbt_node_t *rbt_head) { +bf_rbt_node_t *bf_get_lower_bound(uint32_t key, bf_rbt_node_t *rbt_head) { bf_rbt_node_t *lower_bound = NULL; bf_rbt_node_t *root = rbt_head; while (root != NULL) { - if (root->priority == priority) { + if (root->key == key) { return root; // Exact match found - } else if (root->priority > priority) { + } else if (root->key > key) { root = root->left; } else { lower_bound = root; // Update result and move to the right subtree @@ -165,14 +165,14 @@ bf_rbt_node_t *bf_get_lower_bound(uint32_t priority, bf_rbt_node_t *rbt_head) { return lower_bound; } -bf_rbt_node_t *bf_get_upper_bound(uint32_t priority, bf_rbt_node_t *rbt_head) { +bf_rbt_node_t *bf_get_upper_bound(uint32_t key, bf_rbt_node_t *rbt_head) { bf_rbt_node_t *upper_bound = NULL; bf_rbt_node_t *root = rbt_head; while (root != NULL) { - if (root->priority == priority) { + if (root->key == key) { return root; // Exact match found - } else if (root->priority < priority) { + } else if (root->key < key) { root = root->right; } else { upper_bound = root; // Update result and move to the left subtree @@ -183,7 +183,7 @@ bf_rbt_node_t *bf_get_upper_bound(uint32_t priority, bf_rbt_node_t *rbt_head) { return upper_bound; } -bf_rbt_node_t *bf_get_highest_priority_node(bf_rbt_node_t *rbt_head) { +bf_rbt_node_t *bf_get_highest_key_node(bf_rbt_node_t *rbt_head) { bf_rbt_node_t *current = rbt_head; if (current == NULL) return NULL; @@ -196,7 +196,7 @@ bf_rbt_node_t *bf_get_highest_priority_node(bf_rbt_node_t *rbt_head) { return current; } -bf_rbt_node_t *bf_get_lowest_priority_node(bf_rbt_node_t *rbt_head) { +bf_rbt_node_t *bf_get_lowest_key_node(bf_rbt_node_t *rbt_head) { bf_rbt_node_t *current = rbt_head; if (current == NULL) return NULL; @@ -212,8 +212,8 @@ bf_rbt_node_t *bf_get_lowest_priority_node(bf_rbt_node_t *rbt_head) { bf_rbt_node_direction_t bf_get_rbt_node_direction(bf_rbt_node_t *root) { if (root->parent == NULL) return BF_RBT_ROOT_NODE; - if (root->parent->priority < root->priority || - (root->parent->right != NULL && root->parent->right->priority == root->priority)) + if (root->parent->key < root->key || + (root->parent->right != NULL && root->parent->right->key == root->key)) return BF_RBT_RIGHT_NODE; return BF_RBT_LEFT_NODE; } @@ -222,12 +222,12 @@ bool bf_get_rbt_neigh_color(bf_rbt_node_t *root) { bf_rbt_node_t *parent = root->parent; if (parent == NULL) return BLACK; - if ((parent->right != NULL && parent->right->priority == root->priority) || - parent->priority < root->priority) { + if ((parent->right != NULL && parent->right->key == root->key) || + parent->key < root->key) { if (parent->left != NULL) return parent->left->color; - } else if ((parent->left != NULL && parent->left->priority == root->priority) || - parent->priority > root->priority) { + } else if ((parent->left != NULL && parent->left->key == root->key) || + parent->key > root->key) { if (parent->right != NULL) return parent->right->color; } @@ -238,22 +238,22 @@ bf_rbt_node_t *bf_get_neighbor_rbt_node(bf_rbt_node_t *root) { bf_rbt_node_t *parent = root->parent; if (parent == NULL) return NULL; - if ((parent->right != NULL && parent->right->priority == root->priority) || - parent->priority < root->priority) { + if ((parent->right != NULL && parent->right->key == root->key) || + parent->key < root->key) { return parent->left; - } else if ((parent->left != NULL && parent->left->priority == root->priority) || - parent->priority > root->priority) { + } else if ((parent->left != NULL && parent->left->key == root->key) || + parent->key > root->key) { return parent->right; } return NULL; } -void bf_color_fix_rbt_nodes(bf_rbt_node_t *root, bf_rbt_node_t **rbt_head, uint32_t key) { +void bf_color_fix_rbt_nodes(bf_rbt_node_t *root, bf_rbt_node_t **rbt_head) { if (root->color == BLACK) return; bf_rbt_node_t *parent = root->parent; bf_rbt_node_t *sibling; - if(parent->priority < root->priority) { + if(parent->key < root->key) { sibling = parent->left; } else { sibling = parent->right; @@ -265,7 +265,7 @@ void bf_color_fix_rbt_nodes(bf_rbt_node_t *root, bf_rbt_node_t **rbt_head, uint3 } else { parent->color = RED; //Color fixing should be done from current node to top until reach root node - bf_balance_rbt_post_insertion(parent->parent, rbt_head, root->priority); + bf_balance_rbt_post_insertion(parent->parent, rbt_head, root->key); } } @@ -277,7 +277,7 @@ void bf_balance_rbt_post_insertion(bf_rbt_node_t *root, bf_rbt_node_t **rbt_head * recoloring upper node to balance RB Tree */ if (neigh_color == RED) - bf_color_fix_rbt_nodes(root, rbt_head, key); + bf_color_fix_rbt_nodes(root, rbt_head); /* If neighbor node color is BLACK, perform * rotation to balance the RB Tree */ @@ -302,18 +302,18 @@ bf_rbt_node_t *bf_insert_rbt_entry(bf_rbt_node_t *root, uint32_t key, bf_rbt_nod } while (root != NULL) { prev = root; - if (root->priority < key) + if (root->key < key) root = root->right; - else if (root->priority > key) + else if (root->key > key) root = root->left; else break; } if (root == NULL) root = prev; - if (root->priority == key) { + if (root->key == key) { return root; - } else if (root->priority < key) { + } else if (root->key < key) { root->right = bf_create_rbt_node(key, root); if (root->right == NULL) return NULL; @@ -332,10 +332,10 @@ bf_rbt_node_t *bf_insert_rbt_entry(bf_rbt_node_t *root, uint32_t key, bf_rbt_nod bf_rbt_node_t *bf_bst_node_deletion(uint32_t key, bf_rbt_node_t *rbt_head, int *color) { bf_rbt_node_t *root_node = rbt_head; bf_rbt_node_t *replacement; - while (root_node != NULL && root_node->priority != key) { - if (root_node->priority < key) + while (root_node != NULL && root_node->key != key) { + if (root_node->key < key) root_node = root_node->right; - else if (root_node->priority > key) + else if (root_node->key > key) root_node = root_node->left; } while (root_node != NULL) { @@ -354,7 +354,7 @@ bf_rbt_node_t *bf_bst_node_deletion(uint32_t key, bf_rbt_node_t *rbt_head, int * replacement = bf_get_successor_rbt_node(root_node); if (replacement == NULL) replacement = bf_get_predecessor_rbt_node(root_node); - root_node->priority = replacement->priority; + root_node->key = replacement->key; root_node->data = replacement->data; *color = replacement->color; root_node = replacement;