Apollo
11.0
自动驾驶开放平台
disjoint_set.h
浏览该文件的文档.
1
/******************************************************************************
2
* Copyright 2019 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
/*
18
Copyright (C) 2006 Pedro Felzenszwalb
19
20
This program is free software; you can redistribute it and/or modify
21
it under the terms of the GNU General Public License as published by
22
the Free Software Foundation; either version 2 of the License, or
23
(at your option) any later version.
24
25
This program is distributed in the hope that it will be useful,
26
but WITHOUT ANY WARRANTY; without even the implied warranty of
27
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28
GNU General Public License for more details.
29
30
You should have received a copy of the GNU General Public License
31
along with this program; if not, write to the Free Software
32
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33
*/
34
35
#pragma once
36
37
namespace
apollo
{
38
namespace
perception {
39
namespace
lidar {
40
41
// disjoint-set forests using union-by-rank and path compression (sort of).
42
43
typedef
struct
{
44
int
rank
;
45
int
p
;
46
int
size
;
47
}
uni_elt
;
48
49
class
Universe
{
50
public
:
51
explicit
Universe
(
int
elements);
52
~Universe
();
59
int
find
(
int
x);
66
void
join
(
int
x,
int
y);
73
int
size
(
int
x)
const
{
74
return
_elts[x].
size
;
75
}
81
int
num_sets
()
const
{
82
return
_num;
83
}
84
85
private
:
86
uni_elt
*_elts;
87
int
_num;
88
};
89
90
Universe::Universe
(
int
elements) {
91
_elts =
new
uni_elt
[elements];
92
_num = elements;
93
for
(
int
i = 0; i < elements; i++) {
94
_elts[i].
rank
= 0;
95
_elts[i].
size
= 1;
96
_elts[i].
p
= i;
97
}
98
}
99
100
Universe::~Universe
() {
101
delete
[] _elts;
102
}
103
104
int
Universe::find
(
int
x) {
105
int
y = x;
106
while
(y != _elts[y].p) {
107
y = _elts[y].
p
;
108
}
109
_elts[x].
p
= y;
110
return
y;
111
}
112
113
void
Universe::join
(
int
x,
int
y) {
114
if
(_elts[x].rank > _elts[y].rank) {
115
_elts[y].
p
= x;
116
_elts[x].
size
+= _elts[y].
size
;
117
}
else
{
118
_elts[x].
p
= y;
119
_elts[y].
size
+= _elts[x].
size
;
120
if
(_elts[x].rank == _elts[y].rank) {
121
_elts[y].
rank
++;
122
}
123
}
124
_num--;
125
}
126
127
}
// namespace lidar
128
}
// namespace perception
129
}
// namespace apollo
apollo::perception::lidar::Universe
Definition
disjoint_set.h:49
apollo::perception::lidar::Universe::Universe
Universe(int elements)
Definition
disjoint_set.h:90
apollo::perception::lidar::Universe::find
int find(int x)
Find parent of x
Definition
disjoint_set.h:104
apollo::perception::lidar::Universe::num_sets
int num_sets() const
Get set number
Definition
disjoint_set.h:81
apollo::perception::lidar::Universe::join
void join(int x, int y)
Join set x and set y
Definition
disjoint_set.h:113
apollo::perception::lidar::Universe::size
int size(int x) const
Get size of set x
Definition
disjoint_set.h:73
apollo::perception::lidar::Universe::~Universe
~Universe()
Definition
disjoint_set.h:100
apollo
class register implement
Definition
arena_queue.h:37
apollo::perception::lidar::uni_elt
Definition
disjoint_set.h:43
apollo::perception::lidar::uni_elt::size
int size
Definition
disjoint_set.h:46
apollo::perception::lidar::uni_elt::rank
int rank
Definition
disjoint_set.h:44
apollo::perception::lidar::uni_elt::p
int p
Definition
disjoint_set.h:45
modules
perception
lidar_segmentation
segmentor
ncut_segmentation
common
graph_felzenszwalb
disjoint_set.h