xref: /linux-6.15/include/linux/union_find.h (revision 93c8332c)
1*93c8332cSXavier /* SPDX-License-Identifier: GPL-2.0 */
2*93c8332cSXavier #ifndef __LINUX_UNION_FIND_H
3*93c8332cSXavier #define __LINUX_UNION_FIND_H
4*93c8332cSXavier /**
5*93c8332cSXavier  * union_find.h - union-find data structure implementation
6*93c8332cSXavier  *
7*93c8332cSXavier  * This header provides functions and structures to implement the union-find
8*93c8332cSXavier  * data structure. The union-find data structure is used to manage disjoint
9*93c8332cSXavier  * sets and supports efficient union and find operations.
10*93c8332cSXavier  *
11*93c8332cSXavier  * See Documentation/core-api/union_find.rst for documentation and samples.
12*93c8332cSXavier  */
13*93c8332cSXavier 
14*93c8332cSXavier struct uf_node {
15*93c8332cSXavier 	struct uf_node *parent;
16*93c8332cSXavier 	unsigned int rank;
17*93c8332cSXavier };
18*93c8332cSXavier 
19*93c8332cSXavier /* This macro is used for static initialization of a union-find node. */
20*93c8332cSXavier #define UF_INIT_NODE(node)	{.parent = &node, .rank = 0}
21*93c8332cSXavier 
22*93c8332cSXavier /**
23*93c8332cSXavier  * uf_node_init - Initialize a union-find node
24*93c8332cSXavier  * @node: pointer to the union-find node to be initialized
25*93c8332cSXavier  *
26*93c8332cSXavier  * This function sets the parent of the node to itself and
27*93c8332cSXavier  * initializes its rank to 0.
28*93c8332cSXavier  */
uf_node_init(struct uf_node * node)29*93c8332cSXavier static inline void uf_node_init(struct uf_node *node)
30*93c8332cSXavier {
31*93c8332cSXavier 	node->parent = node;
32*93c8332cSXavier 	node->rank = 0;
33*93c8332cSXavier }
34*93c8332cSXavier 
35*93c8332cSXavier /* find the root of a node */
36*93c8332cSXavier struct uf_node *uf_find(struct uf_node *node);
37*93c8332cSXavier 
38*93c8332cSXavier /* Merge two intersecting nodes */
39*93c8332cSXavier void uf_union(struct uf_node *node1, struct uf_node *node2);
40*93c8332cSXavier 
41*93c8332cSXavier #endif /* __LINUX_UNION_FIND_H */
42