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
18
19namespace apollo {
20namespace perception {
21namespace algorithm {
22
23Universe::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
32void 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
42int 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
59void 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
void Join(const int x, const int y)
void Reset(const int elements_num)
class register implement
Definition arena_queue.h:37