Apollo
11.0
自动驾驶开放平台
disjoint_set.cc
浏览该文件的文档.
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
17
#include "
modules/perception/common/algorithm/graph/disjoint_set.h
"
18
19
namespace
apollo
{
20
namespace
perception {
21
namespace
algorithm {
22
23
Universe::Universe
(
const
int
elements_num)
24
: elts_(elements_num), sets_num_(elements_num) {
25
for
(
int
i = 0; i < elements_num; ++i) {
26
elts_[i].rank = 0;
27
elts_[i].size = 1;
28
elts_[i].p = i;
29
}
30
}
31
32
void
Universe::Reset
(
const
int
elements_num) {
33
sets_num_ = elements_num;
34
elts_.resize(elements_num);
35
for
(
int
i = 0; i < elements_num; ++i) {
36
elts_[i].rank = 0;
37
elts_[i].size = 1;
38
elts_[i].p = i;
39
}
40
}
41
42
int
Universe::Find
(
const
int
x) {
43
int
y = x;
44
while
(y != elts_[y].p) {
45
y = elts_[y].p;
46
}
47
int
w = x;
48
while
(
true
) {
49
const
int
z = elts_[w].p;
50
if
(z == w) {
51
break
;
52
}
53
elts_[w].p = y;
54
w = z;
55
}
56
return
y;
57
}
58
59
void
Universe::Join
(
const
int
x,
const
int
y) {
60
if
(elts_[x].rank > elts_[y].rank) {
61
elts_[y].p = x;
62
elts_[x].size += elts_[y].size;
63
}
else
{
64
elts_[x].p = y;
65
elts_[y].size += elts_[x].size;
66
if
(elts_[x].rank == elts_[y].rank) {
67
++elts_[y].rank;
68
}
69
}
70
--sets_num_;
71
}
72
73
}
// namespace algorithm
74
}
// namespace perception
75
}
// namespace apollo
apollo::perception::algorithm::Universe::Join
void Join(const int x, const int y)
Definition
disjoint_set.cc:59
apollo::perception::algorithm::Universe::Universe
Universe()=default
apollo::perception::algorithm::Universe::Reset
void Reset(const int elements_num)
Definition
disjoint_set.cc:32
apollo::perception::algorithm::Universe::Find
int Find(const int x)
Definition
disjoint_set.cc:42
disjoint_set.h
apollo
class register implement
Definition
arena_queue.h:37
modules
perception
common
algorithm
graph
disjoint_set.cc