Blender  V2.93
BLI_disjoint_set.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
25 #include "BLI_array.hh"
26 
27 namespace blender {
28 
29 class DisjointSet {
30  private:
31  Array<int64_t> parents_;
32  Array<int64_t> ranks_;
33 
34  public:
38  DisjointSet(int64_t size) : parents_(size), ranks_(size, 0)
39  {
40  BLI_assert(size >= 0);
41  for (int64_t i = 0; i < size; i++) {
42  parents_[i] = i;
43  }
44  }
45 
51  {
52  int64_t root1 = this->find_root(x);
53  int64_t root2 = this->find_root(y);
54 
55  /* x and y are in the same set already. */
56  if (root1 == root2) {
57  return;
58  }
59 
60  /* Implement union by rank heuristic. */
61  if (ranks_[root1] < ranks_[root2]) {
62  std::swap(root1, root2);
63  }
64  parents_[root2] = root1;
65 
66  if (ranks_[root1] == ranks_[root2]) {
67  ranks_[root1]++;
68  }
69  }
70 
75  {
76  int64_t root1 = this->find_root(x);
77  int64_t root2 = this->find_root(y);
78  return root1 == root2;
79  }
80 
85  {
86  /* Find root by following parents. */
87  int64_t root = x;
88  while (parents_[root] != root) {
89  root = parents_[root];
90  }
91 
92  /* Compress path. */
93  while (parents_[x] != root) {
94  int64_t parent = parents_[x];
95  parents_[x] = root;
96  x = parent;
97  }
98 
99  return root;
100  }
101 };
102 
103 } // namespace blender
#define BLI_assert(a)
Definition: BLI_assert.h:58
void swap(T &a, T &b)
Definition: Common.h:33
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
int64_t find_root(int64_t x)
void join(int64_t x, int64_t y)
DisjointSet(int64_t size)
bool in_same_set(int64_t x, int64_t y)
__int64 int64_t
Definition: stdint.h:92