Apollo  6.0
Open source self driving car software
range_utils.h
Go to the documentation of this file.
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 
21 namespace apollo {
22 namespace routing {
23 
24 template <typename T>
25 int 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 
49 template <typename T>
50 int 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 
74 template <typename T>
75 int 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 
83  if (start_s <= value_s && end_s >= value_s) {
84  return index;
85  }
86  return -1;
87 }
88 
89 template <typename T>
90 int BinarySearchForStartS(const std::vector<T>& sorted_vec, double value_s) {
91  int index = BinarySearchForSLarger(sorted_vec, value_s);
92  return BinarySearchCheckValidSIndex(sorted_vec, index, value_s);
93 }
94 
95 template <typename T>
96 int BinarySearchForEndS(const std::vector<T>& sorted_vec, double value_s) {
97  int index = BinarySearchForSSmaller(sorted_vec, value_s);
98  return BinarySearchCheckValidSIndex(sorted_vec, index, value_s);
99 }
100 
101 } // namespace routing
102 } // namespace apollo
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
int BinarySearchForEndS(const std::vector< T > &sorted_vec, double value_s)
Definition: range_utils.h:96
int BinarySearchForSSmaller(const std::vector< T > &sorted_vec, double value_s)
Definition: range_utils.h:50
int BinarySearchForStartS(const std::vector< T > &sorted_vec, double value_s)
Definition: range_utils.h:90
int BinarySearchCheckValidSIndex(const std::vector< T > &sorted_vec, int index, double value_s)
Definition: range_utils.h:75
int BinarySearchForSLarger(const std::vector< T > &sorted_vec, double value_s)
Definition: range_utils.h:25