Apollo 10.0
自动驾驶开放平台
range_utils.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 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#pragma once
18
19#include <vector>
20
21namespace apollo {
22namespace routing {
23
24template <typename T>
25int BinarySearchForSLarger(const std::vector<T>& sorted_vec, double value_s) {
26 if (sorted_vec.empty()) {
27 return -1;
28 }
29 int start_index = 0;
30 int end_index = static_cast<int>(sorted_vec.size()) - 1;
31 double internal_s = 0.0;
32 int middle_index = 0;
33 while (end_index - start_index > 1) {
34 middle_index = (start_index + end_index) / 2;
35 internal_s = sorted_vec[middle_index].StartS();
36 if (internal_s > value_s) {
37 end_index = middle_index;
38 } else {
39 start_index = middle_index;
40 }
41 }
42 double end_s = sorted_vec[start_index].EndS();
43 if (value_s <= end_s) {
44 return start_index;
45 }
46 return end_index;
47}
48
49template <typename T>
50int BinarySearchForSSmaller(const std::vector<T>& sorted_vec, double value_s) {
51 if (sorted_vec.empty()) {
52 return -1;
53 }
54 int start_index = 0;
55 int end_index = static_cast<int>(sorted_vec.size()) - 1;
56 double internal_s = 0.0;
57 int middle_index = 0;
58 while (end_index - start_index > 1) {
59 middle_index = (start_index + end_index) / 2;
60 internal_s = sorted_vec[middle_index].EndS();
61 if (internal_s > value_s) {
62 end_index = middle_index;
63 } else {
64 start_index = middle_index;
65 }
66 }
67 double start_s = sorted_vec[end_index].StartS();
68 if (value_s > start_s) {
69 return end_index;
70 }
71 return start_index;
72}
73
74template <typename T>
75int BinarySearchCheckValidSIndex(const std::vector<T>& sorted_vec, int index,
76 double value_s) {
77 if (index == -1) {
78 return -1;
79 }
80 double start_s = sorted_vec[index].StartS();
81 double end_s = sorted_vec[index].EndS();
82 static const double distance_error = 0.02;
83 if (start_s <= value_s + distance_error &&
84 end_s >= value_s - distance_error) {
85 return index;
86 }
87 return -1;
88}
89
90template <typename T>
91int BinarySearchForStartS(const std::vector<T>& sorted_vec, double value_s) {
92 int index = BinarySearchForSLarger(sorted_vec, value_s);
93 return BinarySearchCheckValidSIndex(sorted_vec, index, value_s);
94}
95
96template <typename T>
97int BinarySearchForEndS(const std::vector<T>& sorted_vec, double value_s) {
98 int index = BinarySearchForSSmaller(sorted_vec, value_s);
99 return BinarySearchCheckValidSIndex(sorted_vec, index, value_s);
100}
101
102} // namespace routing
103} // namespace apollo
int BinarySearchForSSmaller(const std::vector< T > &sorted_vec, double value_s)
Definition range_utils.h:50
int BinarySearchForSLarger(const std::vector< T > &sorted_vec, double value_s)
Definition range_utils.h:25
int BinarySearchForEndS(const std::vector< T > &sorted_vec, double value_s)
Definition range_utils.h:97
int BinarySearchForStartS(const std::vector< T > &sorted_vec, double value_s)
Definition range_utils.h:91
int BinarySearchCheckValidSIndex(const std::vector< T > &sorted_vec, int index, double value_s)
Definition range_utils.h:75
class register implement
Definition arena_queue.h:37