Apollo 11.0
自动驾驶开放平台
disjoint_set.h
浏览该文件的文档.
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
18namespace apollo {
19namespace perception {
20namespace lidar {
21
22template <class T>
24 x->parent = x;
25 x->node_rank = 0;
26}
27
28template <class T>
30 if (x->parent != x) {
31 x->parent = DisjointSetFindRecursive(x->parent);
32 }
33 return x->parent;
34}
35
36template <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
54template <class T>
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
66template <class T>
67void DisjointSetMerge(T* x, const T* y) {}
68
69template <class T>
70void 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
void DisjointSetUnion(T *x, T *y)
void DisjointSetMerge(T *x, const T *y)
class register implement
Definition arena_queue.h:37