Repository navigation
UCS/DATASTRUCT: Interval tree fixes #11213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,8 +9,8 @@ | |
| #endif | ||
|
|
||
| #include "interval_tree.h" | ||
| #include <ucs/debug/assert.h> | ||
| #include <ucs/sys/math.h> | ||
| #include <ucs/datastruct/mpool.h> | ||
|
|
||
|
|
||
| /** | ||
|
|
@@ -23,7 +23,7 @@ ucs_interval_tree_node_create(ucs_interval_tree_t *tree, uint64_t start, | |
| { | ||
| ucs_interval_node_t *node; | ||
|
|
||
| node = tree->ops.alloc_node(sizeof(*node), tree->ops.arg); | ||
| node = ucs_mpool_get(tree->mpool); | ||
| if (node == NULL) { | ||
| return NULL; | ||
| } | ||
|
|
@@ -35,11 +35,11 @@ ucs_interval_tree_node_create(ucs_interval_tree_t *tree, uint64_t start, | |
| return node; | ||
| } | ||
|
|
||
| void ucs_interval_tree_init(ucs_interval_tree_t *tree, | ||
| const ucs_interval_tree_ops_t *ops) | ||
| void ucs_interval_tree_init(ucs_interval_tree_t *tree, ucs_mpool_t *mpool) | ||
| { | ||
| tree->ops = *ops; | ||
| tree->root = NULL; | ||
| tree->mpool = mpool; | ||
| tree->root = NULL; | ||
| tree->single_node = 0; | ||
| } | ||
|
|
||
| static void ucs_interval_tree_cleanup_recursive(ucs_interval_tree_t *tree, | ||
|
|
@@ -51,7 +51,7 @@ static void ucs_interval_tree_cleanup_recursive(ucs_interval_tree_t *tree, | |
|
|
||
| ucs_interval_tree_cleanup_recursive(tree, node->left); | ||
| ucs_interval_tree_cleanup_recursive(tree, node->right); | ||
| tree->ops.free_node(node, tree->ops.arg); | ||
| ucs_mpool_put(node); | ||
| } | ||
|
|
||
| void ucs_interval_tree_cleanup(ucs_interval_tree_t *tree) | ||
|
|
@@ -92,11 +92,11 @@ ucs_interval_tree_remove_node(ucs_interval_tree_t *tree, | |
| /* Found target - handle deletion based on number of children */ | ||
| if (root->left == NULL) { | ||
| temp = root->right; | ||
| tree->ops.free_node(root, tree->ops.arg); | ||
| ucs_mpool_put(root); | ||
| return temp; | ||
| } else if (root->right == NULL) { | ||
| temp = root->left; | ||
| tree->ops.free_node(root, tree->ops.arg); | ||
| ucs_mpool_put(root); | ||
| return temp; | ||
| } | ||
|
|
||
|
|
@@ -205,5 +205,9 @@ ucs_status_t ucs_interval_tree_insert_slow(ucs_interval_tree_t *tree, | |
|
|
||
| /* Insert the merged node into the tree */ | ||
| tree->root = ucs_interval_tree_insert_node(tree->root, new_node); | ||
|
|
||
| /* Update single_node flag: tree is single node if root has no children */ | ||
| tree->single_node = (tree->root->left == NULL) && | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe have tree->num_nodes and do ++ every time we insert to it and -- when remove? |
||
| (tree->root->right == NULL); | ||
| return UCS_OK; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,41 +10,11 @@ | |
| #include <ucs/debug/assert.h> | ||
| #include <ucs/sys/math.h> | ||
| #include <ucs/type/status.h> | ||
| #include <ucs/datastruct/mpool.h> | ||
| #include <stdint.h> | ||
| #include <stddef.h> | ||
|
|
||
|
|
||
| /** | ||
| * Callback for allocating an interval tree node | ||
| * | ||
| * @param [in] size Size of the node to allocate | ||
| * @param [in] arg User-defined argument passed during tree initialization | ||
|
|
||
| * @return Pointer to allocated node, or NULL on failure | ||
| */ | ||
| typedef void *(*ucs_interval_tree_alloc_node_func_t)(size_t size, void *arg); | ||
|
|
||
|
|
||
| /** | ||
| * Callback for deallocating an interval tree node | ||
| * | ||
| * @param [in] node Node to deallocate | ||
| * @param [in] arg User-defined argument passed during tree initialization | ||
| */ | ||
| typedef void (*ucs_interval_tree_free_node_func_t)(void *node, void *arg); | ||
|
|
||
|
|
||
| /** | ||
| * Interval tree node allocation/deallocation operations | ||
| */ | ||
| typedef struct { | ||
| ucs_interval_tree_alloc_node_func_t alloc_node; /* Node allocation callback */ | ||
| ucs_interval_tree_free_node_func_t free_node; /* Node deallocation callback */ | ||
| void *arg; /* User-defined argument for | ||
| * callbacks */ | ||
| } ucs_interval_tree_ops_t; | ||
|
|
||
|
|
||
| /** | ||
| * Interval tree node structure | ||
| */ | ||
|
|
@@ -57,19 +27,19 @@ typedef struct ucs_interval_node { | |
|
|
||
|
|
||
| typedef struct { | ||
| ucs_interval_node_t *root; /**< Root node of the tree */ | ||
| ucs_interval_tree_ops_t ops; /**< Memory management callbacks */ | ||
| ucs_interval_node_t *root; /**< Root node of the tree */ | ||
| ucs_mpool_t *mpool; /**< Memory pool for node allocation */ | ||
| int single_node; /**< Cached flag: 1 if tree has only root node */ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor: fix indent |
||
| } ucs_interval_tree_t; | ||
|
|
||
|
|
||
| /** | ||
| * Initialize an interval tree | ||
| * | ||
| * @param [in] tree Interval tree to initialize | ||
| * @param [in] ops Memory management operations (alloc, free, arg) | ||
| * @param [in] tree Interval tree to initialize | ||
| * @param [in] mpool Memory pool for node allocation | ||
| */ | ||
| void ucs_interval_tree_init(ucs_interval_tree_t *tree, | ||
| const ucs_interval_tree_ops_t *ops); | ||
| void ucs_interval_tree_init(ucs_interval_tree_t *tree, ucs_mpool_t *mpool); | ||
|
|
||
|
|
||
| /** | ||
|
|
@@ -108,13 +78,15 @@ ucs_interval_tree_is_single_node(const ucs_interval_tree_t *tree) | |
| static UCS_F_ALWAYS_INLINE ucs_status_t ucs_interval_tree_insert( | ||
| ucs_interval_tree_t *tree, uint64_t start, uint64_t end) | ||
| { | ||
| ucs_assert(start <= end); | ||
| ucs_interval_node_t *root = tree->root; | ||
|
|
||
| ucs_assertv(start <= end + 1, "start=%lu, end=%lu", start, end); | ||
|
yosefe marked this conversation as resolved.
Resolved
|
||
|
|
||
| /* Fast path: if tree has only root and new interval overlaps/touches it, extend it */ | ||
| if (ucs_interval_tree_is_single_node(tree) && | ||
| (start <= (tree->root->end + 1)) && (tree->root->start <= (end + 1))) { | ||
| tree->root->start = ucs_min(tree->root->start, start); | ||
| tree->root->end = ucs_max(tree->root->end, end); | ||
| if (ucs_likely(tree->single_node) && ucs_likely(start <= (root->end + 1)) && | ||
| ucs_likely(root->start <= (end + 1))) { | ||
| root->start = ucs_min(root->start, start); | ||
| root->end = ucs_max(root->end, end); | ||
| return UCS_OK; | ||
| } | ||
|
|
||
|
|
@@ -135,7 +107,7 @@ static UCS_F_ALWAYS_INLINE int | |
| ucs_interval_tree_is_equal_range(const ucs_interval_tree_t *tree, | ||
| uint64_t start, uint64_t end) | ||
| { | ||
| ucs_assert(start <= end); | ||
| ucs_assertv(start <= end + 1, "start=%lu, end=%lu", start, end); | ||
|
yosefe marked this conversation as resolved.
Resolved
|
||
|
|
||
| return ucs_interval_tree_is_single_node(tree) && | ||
| (tree->root->start == start) && (tree->root->end == end); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.