CARLA
 
载入中...
搜索中...
未找到
RoadElementSet.h
浏览该文件的文档.
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#include "carla/ListView.h" // 包含 ListView 头文件,可能用于处理列表视图。
9#include "carla/NonCopyable.h" // 包含不可拷贝类的定义。
10
11#include <iterator> // 包含迭代器相关的标准库。
12#include <memory> // 包含智能指针相关的标准库。
13#include <vector> // 包含动态数组(向量)的标准库。
14#include <algorithm> // 包含算法(如排序、查找等)的标准库。
15#include <type_traits> // 包含类型特征的标准库。
16
17namespace carla { // 定义命名空间 carla
18namespace road { // 定义命名空间 road
19
20 /// 表示按道路位置排序的元素集合。
21 template <typename T>
22 class RoadElementSet : private MovableNonCopyable { // 模板类 RoadElementSet,继承不可拷贝的类
23 public:
24
25 using mapped_type = T; // 映射类型为模板参数 T
26
27 using key_type = double; // 键类型为 double
28
29 RoadElementSet() = default; // 默认构造函数
30
31 /// 显式移动构造函数。
32 template <typename InputTypeT>
33 RoadElementSet(std::vector<InputTypeT> &&range)
34 : _vec([](auto &&input) { // 初始化 _vec,使用 lambda 表达式
35 static_assert(!std::is_const<InputTypeT>::value, "Input type cannot be const"); // 检查输入类型不能是常量
36 std::sort(std::begin(input), std::end(input), LessComp()); // 按照 LessComp 排序
37 return decltype(_vec){ // 返回 _vec 的移动迭代器
38 std::make_move_iterator(std::begin(input)),
39 std::make_move_iterator(std::end(input))};
40 }(std::move(range))) {} // 移动输入范围并初始化
41
42 /// 返回集合中的所有值。
43 const std::vector<mapped_type> &GetAll() const { // 返回 _vec 的常量引用
44 return _vec; // 返回所有元素
45 }
46
47 /// 返回键值小于等于 s 的元素的逆序列表。
48 auto GetReverseSubset(const key_type k) const { // 以 k 为界获取逆序子集
49 return MakeListView(
50 std::make_reverse_iterator(std::upper_bound(_vec.begin(), _vec.end(), k, LessComp())), // 找到大于 k 的第一个元素并反转
51 _vec.rend()); // 返回逆序迭代器的范围
52 }
53
54 /// 返回键值在 [min_k, max_k] 范围内的元素列表。
55 auto GetSubsetInRange(const key_type min_k, const key_type max_k) const { // 获取范围内的子集
56 auto low_bound = (std::lower_bound(_vec.begin(), _vec.end(), min_k, LessComp())); // 找到大于等于 min_k 的下界
57 auto up_bound = (std::upper_bound(_vec.begin(), _vec.end(), max_k, LessComp())); // 找到大于 max_k 的上界
58 return MakeListView((low_bound), (up_bound)); // 返回该范围内的视图
59 }
60
61 /// 返回键值在 [min_k, max_k] 范围内的元素的逆序列表。
62 auto GetReverseSubsetInRange(const key_type min_k, const key_type max_k) const { // 获取范围内的逆序子集
63 auto low_bound = (std::lower_bound(_vec.begin(), _vec.end(), min_k, LessComp())); // 找到大于等于 min_k 的下界
64 auto up_bound = (std::upper_bound(low_bound, _vec.end(), max_k, LessComp())); // 找到大于 max_k 的上界
65 return MakeListView(std::make_reverse_iterator(up_bound), std::make_reverse_iterator(low_bound)); // 返回逆序视图
66 }
67
68 bool empty() const { // 检查集合是否为空
69 return _vec.empty(); // 返回 _vec 是否为空
70 }
71
72 size_t size() const { // 获取集合大小
73 return _vec.size(); // 返回 _vec 的大小
74 }
75
76 auto begin() const { // 返回开始迭代器
77 return _vec.begin(); // 返回 _vec 的开始迭代器
78 }
79
80 auto end() const { // 返回结束迭代器
81 return _vec.end(); // 返回 _vec 的结束迭代器
82 }
83
84 private:
85
86 static key_type GetDistance(const key_type key) { // 获取键值的距离
87 return key; // 直接返回键值
88 }
89
90 template <typename ValueT>
91 static key_type GetDistance(const ValueT &value) { // 获取元素的距离(值类型)
92 return value.GetDistance(); // 调用元素的 GetDistance 方法
93 }
94
95 template <typename ValueT>
96 static key_type GetDistance(const ValueT *value) { // 获取元素的距离(指针类型)
97 return value->GetDistance(); // 调用指针指向元素的 GetDistance 方法
98 }
99
100 template <typename ValueT>
101 static key_type GetDistance(const std::unique_ptr<ValueT> &value) { // 获取元素的距离(智能指针类型)
102 return value->GetDistance(); // 调用智能指针指向元素的 GetDistance 方法
103 }
104
105 struct LessComp { // 定义一个比较结构体
106 using is_transparent = void; // 使比较器支持透明比较
107 template <typename LhsT, typename RhsT>
108 bool operator()( // 重载比较运算符
109 const LhsT &a,
110 const RhsT &b) const {
111 return GetDistance(a) < GetDistance(b); // 使用 GetDistance 比较两个值
112 }
113 };
114
115 std::vector<mapped_type> _vec; // 存储元素的向量
116 };
117
118} // namespace road
119} // namespace carla
这个类用于禁止拷贝构造函数和赋值操作,但允许移动构造函数和赋值操作
表示按道路位置排序的元素集合。
const std::vector< mapped_type > & GetAll() const
返回集合中的所有值。
std::vector< mapped_type > _vec
static key_type GetDistance(const ValueT &value)
static key_type GetDistance(const ValueT *value)
static key_type GetDistance(const key_type key)
auto GetSubsetInRange(const key_type min_k, const key_type max_k) const
返回键值在 [min_k, max_k] 范围内的元素列表。
RoadElementSet(std::vector< InputTypeT > &&range)
显式移动构造函数。
auto GetReverseSubsetInRange(const key_type min_k, const key_type max_k) const
返回键值在 [min_k, max_k] 范围内的元素的逆序列表。
static key_type GetDistance(const std::unique_ptr< ValueT > &value)
auto GetReverseSubset(const key_type k) const
返回键值小于等于 s 的元素的逆序列表。
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
static auto MakeListView(Iterator begin, Iterator end)
bool operator()(const LhsT &a, const RhsT &b) const