WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
unionfind.h
Go to the documentation of this file.
1/* Copyright (C) 2013-2016, The Regents of The University of Michigan.
2All rights reserved.
3This software was developed in the APRIL Robotics Lab under the
4direction of Edwin Olson, ebolson@umich.edu. This software may be
5available under alternative licensing terms; contact the address above.
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
81. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
102. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23The views and conclusions contained in the software and documentation are those
24of the authors and should not be interpreted as representing official policies,
25either expressed or implied, of the Regents of The University of Michigan.
26*/
27
28#pragma once
29
30#include <string.h>
31#include <stdint.h>
32#include <stdlib.h>
33
34typedef struct unionfind unionfind_t;
35
37{
38 uint32_t maxid;
39
40 // Parent node for each. Initialized to 0xffffffff
41 uint32_t *parent;
42
43 // The size of the tree excluding the root
44 uint32_t *size;
45};
46
47static inline unionfind_t *unionfind_create(uint32_t maxid)
48{
49 unionfind_t *uf = (unionfind_t*) calloc(1, sizeof(unionfind_t));
50 uf->maxid = maxid;
51 uf->parent = (uint32_t *) malloc((maxid+1) * sizeof(uint32_t) * 2);
52 memset(uf->parent, 0xff, (maxid+1) * sizeof(uint32_t));
53 uf->size = uf->parent + (maxid+1);
54 memset(uf->size, 0, (maxid+1) * sizeof(uint32_t));
55 return uf;
56}
57
58static inline void unionfind_destroy(unionfind_t *uf)
59{
60 free(uf->parent);
61 free(uf);
62}
63
64/*
65static inline uint32_t unionfind_get_representative(unionfind_t *uf, uint32_t id)
66{
67 // base case: a node is its own parent
68 if (uf->parent[id] == id)
69 return id;
70
71 // otherwise, recurse
72 uint32_t root = unionfind_get_representative(uf, uf->parent[id]);
73
74 // short circuit the path. [XXX This write prevents tail recursion]
75 uf->parent[id] = root;
76
77 return root;
78}
79*/
80
81// this one seems to be every-so-slightly faster than the recursive
82// version above.
83static inline uint32_t unionfind_get_representative(unionfind_t *uf, uint32_t id)
84{
85 uint32_t root = uf->parent[id];
86 // unititialized node, so set to self
87 if (root == 0xffffffff) {
88 uf->parent[id] = id;
89 return id;
90 }
91
92 // chase down the root
93 while (uf->parent[root] != root) {
94 root = uf->parent[root];
95 }
96
97 // go back and collapse the tree.
98 while (uf->parent[id] != root) {
99 uint32_t tmp = uf->parent[id];
100 uf->parent[id] = root;
101 id = tmp;
102 }
103
104 return root;
105}
106
107static inline uint32_t unionfind_get_set_size(unionfind_t *uf, uint32_t id)
108{
109 uint32_t repid = unionfind_get_representative(uf, id);
110 return uf->size[repid] + 1;
111}
112
113static inline uint32_t unionfind_connect(unionfind_t *uf, uint32_t aid, uint32_t bid)
114{
115 uint32_t aroot = unionfind_get_representative(uf, aid);
116 uint32_t broot = unionfind_get_representative(uf, bid);
117
118 if (aroot == broot)
119 return aroot;
120
121 // we don't perform "union by rank", but we perform a similar
122 // operation (but probably without the same asymptotic guarantee):
123 // We join trees based on the number of *elements* (as opposed to
124 // rank) contained within each tree. I.e., we use size as a proxy
125 // for rank. In my testing, it's often *faster* to use size than
126 // rank, perhaps because the rank of the tree isn't that critical
127 // if there are very few nodes in it.
128 uint32_t asize = uf->size[aroot] + 1;
129 uint32_t bsize = uf->size[broot] + 1;
130
131 // optimization idea: We could shortcut some or all of the tree
132 // that is grafted onto the other tree. Pro: those nodes were just
133 // read and so are probably in cache. Con: it might end up being
134 // wasted effort -- the tree might be grafted onto another tree in
135 // a moment!
136 if (asize > bsize) {
137 uf->parent[broot] = aroot;
138 uf->size[aroot] += bsize;
139 return aroot;
140 } else {
141 uf->parent[aroot] = broot;
142 uf->size[broot] += asize;
143 return broot;
144 }
145}
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or and nanopb were all modified for use in Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable or merely the Work and Derivative Works thereof Contribution shall mean any work of including the original version of the Work and any modifications or additions to that Work or Derivative Works that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner For the purposes of this submitted means any form of or written communication sent to the Licensor or its including but not limited to communication on electronic mailing source code control and issue tracking systems that are managed or on behalf the Licensor for the purpose of discussing and improving the but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as Not a Contribution Contributor shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work Grant of Copyright License Subject to the terms and conditions of this each Contributor hereby grants to You a non no royalty free
Definition ThirdPartyNotices.txt:164
Definition unionfind.h:37
uint32_t * size
Definition unionfind.h:44
uint32_t maxid
Definition unionfind.h:38
uint32_t * parent
Definition unionfind.h:41
static uint32_t unionfind_connect(unionfind_t *uf, uint32_t aid, uint32_t bid)
Definition unionfind.h:113
static unionfind_t * unionfind_create(uint32_t maxid)
Definition unionfind.h:47
static uint32_t unionfind_get_representative(unionfind_t *uf, uint32_t id)
Definition unionfind.h:83
static void unionfind_destroy(unionfind_t *uf)
Definition unionfind.h:58
static uint32_t unionfind_get_set_size(unionfind_t *uf, uint32_t id)
Definition unionfind.h:107