Apollo  6.0
Open source self driving car software
string_compare.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 #pragma once
17 #include <algorithm>
18 #include <iostream>
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 namespace apollo {
24 namespace perception {
25 namespace benchmark {
26 
27 static void string_split(const std::string& str, char c,
28  std::vector<std::string>* strs) {
29  strs->clear();
30  std::string term;
31  for (auto& cur_char : str) {
32  if (cur_char != c) {
33  term += cur_char;
34  continue;
35  }
36  // find one
37  if (!term.empty()) {
38  strs->push_back(term);
39  term.clear();
40  }
41  }
42  if (!term.empty()) {
43  strs->push_back(term);
44  }
45 }
46 
47 static bool string_compare_by_length(const std::string& lhs,
48  const std::string& rhs) {
49  if (lhs.length() < rhs.length()) {
50  return true;
51  } else if (lhs.length() == rhs.length()) {
52  return lhs <= rhs;
53  } else {
54  return false;
55  }
56 }
57 
58 static void sort_strings_by_split_length(
59  const std::vector<std::string>& strs,
60  std::vector<std::size_t>* sorted_indices) {
61  struct StringHelper {
62  std::string str;
63  std::vector<std::string> splits;
64  std::size_t id;
65  };
66  std::vector<StringHelper> helper(strs.size());
67  for (std::size_t i = 0; i < strs.size(); ++i) {
68  helper[i].str = strs.at(i);
69  string_split(helper[i].str, '/', &helper[i].splits);
70  helper[i].id = i;
71  }
72  std::sort(helper.begin(), helper.end(),
73  [](const StringHelper& lhs, const StringHelper& rhs) {
74  if (lhs.splits.size() < rhs.splits.size()) {
75  return true;
76  } else if (lhs.splits.size() > rhs.splits.size()) {
77  return false;
78  }
79  for (std::size_t i = 0; i < lhs.splits.size(); ++i) {
80  if (lhs.splits[i] == rhs.splits[i]) {
81  continue;
82  } else {
83  return string_compare_by_length(lhs.splits[i], rhs.splits[i]);
84  }
85  }
86  return true;
87  });
88  sorted_indices->resize(strs.size());
89  for (std::size_t i = 0; i < strs.size(); ++i) {
90  sorted_indices->at(i) = helper[i].id;
91  }
92 }
93 
94 template <typename T>
95 void shuffle_by_indices(std::vector<T>* src,
96  const std::vector<std::size_t>& indices) {
97  if (src->size() != indices.size()) {
98  return;
99  }
100  std::vector<T> dst;
101  dst.reserve(indices.size());
102  for (auto& id : indices) {
103  dst.push_back(std::move(src->at(id)));
104  }
105  src->clear();
106  for (auto& data : dst) {
107  src->push_back(std::move(data));
108  }
109 }
110 
111 } // namespace benchmark
112 } // namespace perception
113 } // namespace apollo
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void shuffle_by_indices(std::vector< T > *src, const std::vector< std::size_t > &indices)
Definition: string_compare.h:95