Blender  V2.93
util_disjoint_set.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __UTIL_DISJOINT_SET_H__
18 #define __UTIL_DISJOINT_SET_H__
19 
20 #include "util_array.h"
21 #include <utility>
22 
24 
25 class DisjointSet {
26  private:
27  array<size_t> parents;
28  array<size_t> ranks;
29 
30  public:
31  DisjointSet(size_t size) : parents(size), ranks(size)
32  {
33  for (size_t i = 0; i < size; i++) {
34  parents[i] = i;
35  ranks[i] = 0;
36  }
37  }
38 
39  size_t find(size_t x)
40  {
41  size_t root = x;
42  while (parents[root] != root) {
43  root = parents[root];
44  }
45  while (parents[x] != root) {
46  size_t parent = parents[x];
47  parents[x] = root;
48  x = parent;
49  }
50  return root;
51  }
52 
53  void join(size_t x, size_t y)
54  {
55  size_t x_root = find(x);
56  size_t y_root = find(y);
57 
58  if (x_root == y_root) {
59  return;
60  }
61 
62  if (ranks[x_root] < ranks[y_root]) {
63  std::swap(x_root, y_root);
64  }
65  parents[y_root] = x_root;
66 
67  if (ranks[x_root] == ranks[y_root]) {
68  ranks[x_root]++;
69  }
70  }
71 };
72 
74 
75 #endif /* __UTIL_DISJOINT_SET_H__ */
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
void join(size_t x, size_t y)
DisjointSet(size_t size)
size_t find(size_t x)
#define CCL_NAMESPACE_END