Apollo  6.0
Open source self driving car software
disjoint_set.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
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 #pragma once
17 
18 namespace apollo {
19 namespace perception {
20 namespace lidar {
21 
22 template <class T>
23 void DisjointSetMakeSet(T* x) {
24  x->parent = x;
25  x->node_rank = 0;
26 }
27 
28 template <class T>
30  if (x->parent != x) {
31  x->parent = DisjointSetFindRecursive(x->parent);
32  }
33  return x->parent;
34 }
35 
36 template <class T>
38  T* y = x;
39  while (y->parent != y) {
40  y = y->parent;
41  }
42 
43  T* w = x;
44  T* temp = x;
45  while (w->parent != w) {
46  temp = w->parent;
47  w->parent = y;
48  w = temp;
49  }
50 
51  return y;
52 }
53 
54 template <class T>
55 T* DisjointSetFind(T* x) {
56  T* y = x->parent;
57  if (y == x || y->parent == y) {
58  return y;
59  }
60  T* root = DisjointSetFindLoop(y->parent);
61  x->parent = root;
62  y->parent = root;
63  return root;
64 }
65 
66 template <class T>
67 void DisjointSetMerge(T* x, const T* y) {}
68 
69 template <class T>
70 void DisjointSetUnion(T* x, T* y) {
71  x = DisjointSetFind(x);
72  y = DisjointSetFind(y);
73  if (x == y) {
74  return;
75  }
76  if (x->node_rank < y->node_rank) {
77  x->parent = y;
78  // DisjointSetMerge(y, x);
79  } else if (y->node_rank < x->node_rank) {
80  y->parent = x;
81  // DisjointSetMerge(x, y);
82  } else {
83  y->parent = x;
84  x->node_rank++;
85  // DisjointSetMerge(x, y);
86  }
87 }
88 
89 } // namespace lidar
90 } // namespace perception
91 } // namespace apollo
T * DisjointSetFindLoop(T *x)
Definition: disjoint_set.h:37
void DisjointSetUnion(T *x, T *y)
Definition: disjoint_set.h:70
T * DisjointSetFindRecursive(T *x)
Definition: disjoint_set.h:29
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
T * DisjointSetFind(T *x)
Definition: disjoint_set.h:55
void DisjointSetMakeSet(T *x)
Definition: disjoint_set.h:23
void DisjointSetMerge(T *x, const T *y)
Definition: disjoint_set.h:67