CARLA
RoadElementSet.h
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
9 #include "carla/ListView.h"
10 #include "carla/NonCopyable.h"
11 
12 #include <iterator>
13 #include <memory>
14 #include <vector>
15 #include <algorithm>
16 #include <type_traits>
17 
18 namespace carla {
19 namespace road {
20 
21  /// A set of elements ordered by its position on the road.
22  template <typename T>
24  public:
25 
26  using mapped_type = T;
27 
28  using key_type = double;
29 
30  RoadElementSet() = default;
31 
32  /// Explicit move constructor.
33  template <typename InputTypeT>
34  RoadElementSet(std::vector<InputTypeT> &&range)
35  : _vec([](auto &&input) {
36  static_assert(!std::is_const<InputTypeT>::value, "Input type cannot be const");
37  std::sort(std::begin(input), std::end(input), LessComp());
38  return decltype(_vec){
39  std::make_move_iterator(std::begin(input)),
40  std::make_move_iterator(std::end(input))};
41  }(std::move(range))) {}
42 
43  /// Return all values from the set.
44  const std::vector<mapped_type> &GetAll() const {
45  return _vec;
46  }
47 
48  /// Return a reversed list of elements that have key
49  /// value GetDistance() <= s.
50  auto GetReverseSubset(const key_type k) const {
51  return MakeListView(
52  std::make_reverse_iterator(std::upper_bound(_vec.begin(), _vec.end(), k, LessComp())),
53  _vec.rend());
54  }
55 
56  /// Return a list of elements that have a key value in the range [min_k, max_k]
57  auto GetSubsetInRange(const key_type min_k, const key_type max_k) const {
58  auto low_bound = (std::lower_bound(_vec.begin(), _vec.end(), min_k, LessComp()));
59  auto up_bound = (std::upper_bound(_vec.begin(), _vec.end(), max_k, LessComp()));
60  return MakeListView((low_bound), (up_bound));
61  }
62 
63  auto GetReverseSubsetInRange(const key_type min_k, const key_type max_k) const {
64  auto low_bound = (std::lower_bound(_vec.begin(), _vec.end(), min_k, LessComp()));
65  auto up_bound = (std::upper_bound(low_bound, _vec.end(), max_k, LessComp()));
66  return MakeListView(std::make_reverse_iterator(up_bound), std::make_reverse_iterator(low_bound));
67  }
68 
69  bool empty() const {
70  return _vec.empty();
71  }
72 
73  size_t size() const {
74  return _vec.size();
75  }
76 
77  auto begin() const {
78  return _vec.begin();
79  }
80 
81  auto end() const {
82  return _vec.end();
83  }
84 
85  private:
86 
87  static key_type GetDistance(const key_type key) {
88  return key;
89  }
90 
91  template <typename ValueT>
92  static key_type GetDistance(const ValueT &value) {
93  return value.GetDistance();
94  }
95 
96  template <typename ValueT>
97  static key_type GetDistance(const ValueT *value) {
98  return value->GetDistance();
99  }
100 
101  template <typename ValueT>
102  static key_type GetDistance(const std::unique_ptr<ValueT> &value) {
103  return value->GetDistance();
104  }
105 
106  struct LessComp {
107  using is_transparent = void;
108  template <typename LhsT, typename RhsT>
110  const LhsT &a,
111  const RhsT &b) const {
112  return GetDistance(a) < GetDistance(b);
113  }
114  };
115 
116  std::vector<mapped_type> _vec;
117  };
118 
119 } // road
120 } // carla
bool operator()(const LhsT &a, const RhsT &b) const
static key_type GetDistance(const std::unique_ptr< ValueT > &value)
auto GetSubsetInRange(const key_type min_k, const key_type max_k) const
Return a list of elements that have a key value in the range [min_k, max_k].
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
RoadElementSet(std::vector< InputTypeT > &&range)
Explicit move constructor.
const std::vector< mapped_type > & GetAll() const
Return all values from the set.
A set of elements ordered by its position on the road.
Inherit (privately) to suppress copy construction and assignment.
std::vector< mapped_type > _vec
auto GetReverseSubset(const key_type k) const
Return a reversed list of elements that have key value GetDistance() <= s.
auto GetReverseSubsetInRange(const key_type min_k, const key_type max_k) const
static key_type GetDistance(const ValueT &value)
static key_type GetDistance(const ValueT *value)
static auto MakeListView(Iterator begin, Iterator end)
void sort(I begin, I end, const Pred &pred)
Definition: pugixml.cpp:7444
static key_type GetDistance(const key_type key)