-
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.
#include <stdint.h> | ||
|
||
/** | ||
* @brief Disjoint-set node structure |
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.
As this is part of sys directory, what about adding sys prefix to make it clear what the name space is. So I propose that these are called sys_uf_...
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.
or maybe sys_set_*
? uf
is not very descriptive on its purpose.
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.
Yes, that would be even better
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.
I got the idea for the name from this:
zephyr/include/zephyr/sys/rb.h
Lines 119 to 127 in 413b789
/** | |
* @brief Insert node into tree | |
*/ | |
void rb_insert(struct rbtree *tree, struct rbnode *node); | |
/** | |
* @brief Remove node from tree | |
*/ | |
void rb_remove(struct rbtree *tree, struct rbnode *node); |
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.
IIRC, that's before we started adding namespace for various subsys. You can grep and see that most under sys/
has sys_
prefix.
As for naming with set
instead of uf
, we may add other set operations in the future (e.g. difference and subtraction) so that naming should not be restrictive.
f37ab3c
to
8583e82
Compare
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 (`uf_makeset`, `uf_union` and `uf_find`). Signed-off-by: James Roy <[email protected]>
|
/** | ||
* @brief Initialize a disjoint-set. | ||
*/ | ||
static inline void uf_makeset(struct uf_node *node) | ||
{ | ||
node->parent = node; | ||
node->rank = 0; | ||
} | ||
|
||
/** | ||
* @brief Find the root of the disjoint-set. | ||
*/ | ||
struct uf_node *uf_find(struct uf_node *node); | ||
|
||
/** | ||
* @brief Merge two nodes into the same disjoint-set. | ||
*/ | ||
void uf_union(struct uf_node *node1, struct uf_node *node2); |
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.
Needs full Doxygen comments, including parameters and return values
* Add a set of Disjoint-set functions (:c:func:`uf_find`, :c:func:`uf_union`) to | ||
handle queries between sets. They are declared in :zephyr_file:`include/zephyr/sys/uf.h`. |
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.
* Add a set of Disjoint-set functions (:c:func:`uf_find`, :c:func:`uf_union`) to | |
handle queries between sets. They are declared in :zephyr_file:`include/zephyr/sys/uf.h`. | |
* :c:struct:`uf_node` | |
* :c:func:`uf_makeset` | |
* :c:func:`uf_find` | |
* :c:func:`uf_union` |
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.
*/ | ||
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.
Add a set of Disjoint-set functions (
find
,union
) to handle queries between sets.