-
Notifications
You must be signed in to change notification settings - Fork 7.7k
sys: Add Disjoint-set (union-find) data structure #93300
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New features should have test cases added.
f37ab3c
to
8583e82
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Design note on the pointer handling.
Also count me on team sys_set_
. "uf
" is an expression of deep exasperation.
include/zephyr/sys/uf.h
Outdated
*/ | ||
struct uf_node { | ||
/** @cond INTERNAL_HIDDEN */ | ||
struct uf_node *parent; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks suspiciously like an slist, no? Not like linked list handling is that big a deal, but every byte of code savings helps.
de9abc2
to
f7d79b5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rruuaanng - just a couple of nits about naming
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rruuaanng - please also add a testcase.yaml file.
It may be worth it to add incremental tests to each iteration in the one test function to validate certain assumptions about the disjoint set.
E.g. rank at each stage, parent properties, and so on.
ZTEST(set, test_find_and_union) | ||
{ | ||
sys_set_makeset(&data_list[0].ufnode, 5); | ||
sys_set_makeset(&data_list[1].ufnode, 4); | ||
sys_set_makeset(&data_list[2].ufnode, 3); | ||
sys_set_makeset(&data_list[3].ufnode, 2); | ||
sys_set_makeset(&data_list[4].ufnode, 1); | ||
|
||
sys_set_union(&data_list[0].ufnode, &data_list[1].ufnode); | ||
sys_set_union(&data_list[1].ufnode, &data_list[2].ufnode); | ||
sys_set_union(&data_list[2].ufnode, &data_list[3].ufnode); | ||
sys_set_union(&data_list[3].ufnode, &data_list[4].ufnode); | ||
|
||
struct sys_set_node *root_node = sys_set_find(&data_list[4].ufnode); | ||
struct user_data *root_data = CONTAINER_OF(root_node, struct user_data, ufnode); | ||
|
||
zassert_equal(root_data->n, data_list[0].n, "Root set data n are not equal"); | ||
zassert_equal(root_data->ufnode.rank, root_node->rank, "Root set are not equal"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You will need to rename ufnode to node everywhere
Add a set of Disjoint-set functions (`find`, `union`) to handle queries between sets. Signed-off-by: James Roy <[email protected]>
Add unit tests for functions (`sys_set_makeset`, `sys_set_union` and `sys_set_find`). Signed-off-by: James Roy <[email protected]>
|
Add a set of Disjoint-set functions (
find
,union
) to handle queries between sets.