[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20241007152833.2282199-3-visitorckw@gmail.com>
Date: Mon, 7 Oct 2024 23:28:29 +0800
From: Kuan-Wei Chiu <visitorckw@...il.com>
To: xavier_qy@....com,
longman@...hat.com,
lizefan.x@...edance.com,
tj@...nel.org,
hannes@...xchg.org,
mkoutny@...e.com,
akpm@...ux-foundation.org
Cc: jserv@...s.ncku.edu.tw,
linux-kernel@...r.kernel.org,
cgroups@...r.kernel.org,
Kuan-Wei Chiu <visitorckw@...il.com>
Subject: [PATCH v2 2/6] lib/union_find: Change uf_union() return type to bool
Modify the uf_union() function to return a bool indicating whether a
merge occurred. If the two nodes belong to the same set, the function
returns false, indicating no merge took place. Otherwise, it completes
the merge and returns true. This change allows the caller to easily
determine the number of distinct groups by tracking successful merges,
enhancing the usability of the union-find implementation.
Signed-off-by: Kuan-Wei Chiu <visitorckw@...il.com>
---
include/linux/union_find.h | 2 +-
lib/union_find.c | 8 ++++++--
2 files changed, 7 insertions(+), 3 deletions(-)
diff --git a/include/linux/union_find.h b/include/linux/union_find.h
index cfd49263c138..45c1a6fc6574 100644
--- a/include/linux/union_find.h
+++ b/include/linux/union_find.h
@@ -36,6 +36,6 @@ static inline void uf_node_init(struct uf_node *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);
+bool uf_union(struct uf_node *node1, struct uf_node *node2);
#endif /* __LINUX_UNION_FIND_H */
diff --git a/lib/union_find.c b/lib/union_find.c
index c9fd30b8059c..a20678da0220 100644
--- a/lib/union_find.c
+++ b/lib/union_find.c
@@ -31,14 +31,16 @@ EXPORT_SYMBOL(uf_find);
*
* This function merges the sets containing node1 and node2, by comparing
* the ranks to keep the tree balanced.
+ *
+ * Returns true if the sets were merged, false if they were already in the same set.
*/
-void uf_union(struct uf_node *node1, struct uf_node *node2)
+bool uf_union(struct uf_node *node1, struct uf_node *node2)
{
struct uf_node *root1 = uf_find(node1);
struct uf_node *root2 = uf_find(node2);
if (root1 == root2)
- return;
+ return false;
if (root1->rank < root2->rank) {
root1->parent = root2;
@@ -48,5 +50,7 @@ void uf_union(struct uf_node *node1, struct uf_node *node2)
root2->parent = root1;
root1->rank++;
}
+
+ return true;
}
EXPORT_SYMBOL(uf_union);
--
2.34.1
Powered by blists - more mailing lists