Apollo  v5.5.0
Open source self driving car software
disjoint_set.h
Go to the documentation of this file.
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();
53  int find(int x);
54  void join(int x, int y);
55  int size(int x) const { return _elts[x].size; }
56  int num_sets() const { return _num; }
57 
58  private:
59  uni_elt *_elts;
60  int _num;
61 };
62 
63 Universe::Universe(int elements) {
64  _elts = new uni_elt[elements];
65  _num = elements;
66  for (int i = 0; i < elements; i++) {
67  _elts[i].rank = 0;
68  _elts[i].size = 1;
69  _elts[i].p = i;
70  }
71 }
72 
73 Universe::~Universe() { delete[] _elts; }
74 
75 int Universe::find(int x) {
76  int y = x;
77  while (y != _elts[y].p) {
78  y = _elts[y].p;
79  }
80  _elts[x].p = y;
81  return y;
82 }
83 
84 void Universe::join(int x, int y) {
85  if (_elts[x].rank > _elts[y].rank) {
86  _elts[y].p = x;
87  _elts[x].size += _elts[y].size;
88  } else {
89  _elts[x].p = y;
90  _elts[y].size += _elts[x].size;
91  if (_elts[x].rank == _elts[y].rank) {
92  _elts[y].rank++;
93  }
94  }
95  _num--;
96 }
97 
98 } // namespace lidar
99 } // namespace perception
100 } // namespace apollo
Universe(int elements)
Definition: disjoint_set.h:63
int rank
Definition: disjoint_set.h:44
Definition: disjoint_set.h:43
Definition: blob.h:72
int find(int x)
Definition: disjoint_set.h:75
void join(int x, int y)
Definition: disjoint_set.h:84
int size(int x) const
Definition: disjoint_set.h:55
int size
Definition: disjoint_set.h:46
Definition: disjoint_set.h:49
int num_sets() const
Definition: disjoint_set.h:56
~Universe()
Definition: disjoint_set.h:73
int p
Definition: disjoint_set.h:45