diff options
| author | Xavier <[email protected]> | 2024-07-04 14:24:43 +0800 |
|---|---|---|
| committer | Tejun Heo <[email protected]> | 2024-07-30 13:04:36 -1000 |
| commit | 93c8332c8373fee415bd79f08d5ba4ba7ca5ad15 (patch) | |
| tree | de7441511fcc624ed96229dfff371e7c0d4d83d4 /include/linux | |
| parent | 4a711dd910d0135b3d262f85612f7e17e0feb989 (diff) | |
Union-Find: add a new module in kernel library
This patch implements a union-find data structure in the kernel library,
which includes operations for allocating nodes, freeing nodes,
finding the root of a node, and merging two nodes.
Signed-off-by: Xavier <[email protected]>
Signed-off-by: Tejun Heo <[email protected]>
Diffstat (limited to 'include/linux')
| -rw-r--r-- | include/linux/union_find.h | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/include/linux/union_find.h b/include/linux/union_find.h new file mode 100644 index 000000000000..cfd49263c138 --- /dev/null +++ b/include/linux/union_find.h @@ -0,0 +1,41 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __LINUX_UNION_FIND_H +#define __LINUX_UNION_FIND_H +/** + * union_find.h - union-find data structure implementation + * + * This header provides functions and structures to implement the union-find + * data structure. The union-find data structure is used to manage disjoint + * sets and supports efficient union and find operations. + * + * See Documentation/core-api/union_find.rst for documentation and samples. + */ + +struct uf_node { + struct uf_node *parent; + unsigned int rank; +}; + +/* This macro is used for static initialization of a union-find node. */ +#define UF_INIT_NODE(node) {.parent = &node, .rank = 0} + +/** + * uf_node_init - Initialize a union-find node + * @node: pointer to the union-find node to be initialized + * + * This function sets the parent of the node to itself and + * initializes its rank to 0. + */ +static inline void uf_node_init(struct uf_node *node) +{ + node->parent = node; + node->rank = 0; +} + +/* find the root of a node */ +struct uf_node *uf_find(struct uf_node *node); + +/* Merge two intersecting nodes */ +void uf_union(struct uf_node *node1, struct uf_node *node2); + +#endif /* __LINUX_UNION_FIND_H */ |