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/*
18Copyright (C) 2006 Pedro Felzenszwalb
19
20This program is free software; you can redistribute it and/or modify
21it under the terms of the GNU General Public License as published by
22the Free Software Foundation; either version 2 of the License, or
23(at your option) any later version.
24
25This program is distributed in the hope that it will be useful,
26but WITHOUT ANY WARRANTY; without even the implied warranty of
27MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28GNU General Public License for more details.
29
30You should have received a copy of the GNU General Public License
31along with this program; if not, write to the Free Software
32Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33*/
34
35#pragma once
36
37namespace apollo {
38namespace perception {
39namespace lidar {
40
41// disjoint-set forests using union-by-rank and path compression (sort of).
42
43typedef struct {
44 int rank;
45 int p;
46 int size;
47} uni_elt;
48
49class Universe {
50public:
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
85private:
86 uni_elt *_elts;
87 int _num;
88};
89
90Universe::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
101 delete[] _elts;
102}
103
104int 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
113void 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
int find(int x)
Find parent of x
int num_sets() const
Get set number
void join(int x, int y)
Join set x and set y
int size(int x) const
Get size of set x
class register implement
Definition arena_queue.h:37