17 #ifndef CARTOGRAPHER_MAPPING_ID_H_ 18 #define CARTOGRAPHER_MAPPING_ID_H_ 33 #include "cartographer/mapping/proto/pose_graph.pb.h" 34 #include "glog/logging.h" 62 return std::forward_as_tuple(trajectory_id, node_index) ==
69 return std::forward_as_tuple(trajectory_id, node_index) <
73 void ToProto(proto::NodeId* proto)
const {
74 proto->set_trajectory_id(trajectory_id);
75 proto->set_node_index(node_index);
90 return std::forward_as_tuple(trajectory_id, submap_index) ==
97 return std::forward_as_tuple(trajectory_id, submap_index) <
102 proto->set_trajectory_id(trajectory_id);
103 proto->set_submap_index(submap_index);
111 template <
typename IteratorType>
114 Range(
const IteratorType& begin,
const IteratorType& end)
115 : begin_(begin), end_(end) {}
117 IteratorType
begin()
const {
return begin_; }
118 IteratorType
end()
const {
return end_; }
129 template <
typename IdType,
typename DataType>
145 using pointer = std::unique_ptr<const IdDataReference>;
149 : current_trajectory_(
150 map_by_id.trajectories_.lower_bound(trajectory_id)),
151 end_trajectory_(map_by_id.trajectories_.end()) {
152 if (current_trajectory_ != end_trajectory_) {
153 current_data_ = current_trajectory_->second.data_.begin();
154 AdvanceToValidDataIterator();
159 : current_trajectory_(map_by_id.trajectories_.find(id.trajectory_id)),
160 end_trajectory_(map_by_id.trajectories_.end()) {
161 if (current_trajectory_ != end_trajectory_) {
164 if (current_data_ == current_trajectory_->second.data_.end()) {
165 current_trajectory_ = end_trajectory_;
171 CHECK(current_trajectory_ != end_trajectory_);
173 IdType{current_trajectory_->first, current_data_->first},
174 current_data_->second};
178 return common::make_unique<const IdDataReference>(this->
operator*());
182 CHECK(current_trajectory_ != end_trajectory_);
184 AdvanceToValidDataIterator();
189 while (current_trajectory_ == end_trajectory_ ||
190 current_data_ == current_trajectory_->second.data_.begin()) {
191 --current_trajectory_;
192 current_data_ = current_trajectory_->second.data_.end();
199 if (current_trajectory_ == end_trajectory_ ||
211 CHECK(current_trajectory_ != end_trajectory_);
212 while (current_data_ == current_trajectory_->second.data_.end()) {
213 ++current_trajectory_;
214 if (current_trajectory_ == end_trajectory_) {
217 current_data_ = current_trajectory_->second.data_.begin();
235 typename std::map<int, MapByIndex>::const_iterator current_trajectory)
236 : current_trajectory_(current_trajectory) {}
238 int operator*()
const {
return current_trajectory_->first; }
241 ++current_trajectory_;
246 --current_trajectory_;
255 return !operator==(it);
263 IdType
Append(
const int trajectory_id,
const DataType& data) {
264 CHECK_GE(trajectory_id, 0);
265 auto& trajectory = trajectories_[trajectory_id];
266 CHECK(trajectory.can_append_);
268 trajectory.data_.empty() ? 0 : trajectory.data_.rbegin()->first + 1;
269 trajectory.data_.emplace(index, data);
270 return IdType{trajectory_id, index};
275 ConstIterator
find(
const IdType&
id)
const {
276 return ConstIterator(*
this,
id);
280 void Insert(
const IdType&
id,
const DataType& data) {
281 CHECK_GE(
id.trajectory_id, 0);
282 CHECK_GE(GetIndex(
id), 0);
283 auto& trajectory = trajectories_[
id.trajectory_id];
284 trajectory.can_append_ =
false;
285 CHECK(trajectory.data_.emplace(GetIndex(
id), data).second);
290 auto& trajectory = trajectories_.at(
id.trajectory_id);
291 const auto it = trajectory.data_.find(GetIndex(
id));
292 CHECK(it != trajectory.data_.end());
293 if (std::next(it) == trajectory.data_.end()) {
298 trajectory.can_append_ =
false;
300 trajectory.data_.erase(it);
301 if (trajectory.data_.empty()) {
302 trajectories_.erase(
id.trajectory_id);
307 return trajectories_.count(
id.trajectory_id) != 0 &&
308 trajectories_.at(
id.trajectory_id).data_.count(GetIndex(
id)) != 0;
311 const DataType&
at(
const IdType&
id)
const {
312 return trajectories_.at(
id.trajectory_id).data_.at(GetIndex(
id));
315 DataType&
at(
const IdType&
id) {
316 return trajectories_.at(
id.trajectory_id).data_.at(GetIndex(
id));
321 return ConstIterator(*
this, trajectory_id);
324 return BeginOfTrajectory(trajectory_id + 1);
329 return trajectories_.count(trajectory_id)
330 ? trajectories_.at(trajectory_id).data_.size()
337 for (
const auto& item : trajectories_) {
338 size += item.second.data_.size();
346 EndOfTrajectory(trajectory_id));
352 ConstTrajectoryIterator(trajectories_.begin()),
353 ConstTrajectoryIterator(trajectories_.end()));
356 ConstIterator
begin()
const {
return BeginOfTrajectory(0); }
357 ConstIterator
end()
const {
358 return BeginOfTrajectory(std::numeric_limits<int>::max());
361 bool empty()
const {
return begin() == end(); }
369 if (SizeOfTrajectoryOrZero(trajectory_id) == 0) {
370 return EndOfTrajectory(trajectory_id);
373 const std::map<int, DataType>& trajectory =
374 trajectories_.at(trajectory_id).data_;
376 return EndOfTrajectory(trajectory_id);
378 auto left = trajectory.begin();
379 auto right = std::prev(trajectory.end());
380 while (left != right) {
381 const int middle = left->first + (right->first - left->first) / 2;
382 const auto lower_bound_middle = trajectory.lower_bound(middle);
384 left = std::next(lower_bound_middle);
386 right = lower_bound_middle;
390 return ConstIterator(*
this, IdType{trajectory_id, left->first});
395 bool can_append_ =
true;
408 #endif // CARTOGRAPHER_MAPPING_ID_H_ std::unique_ptr< const IdDataReference > operator->() const
bool operator==(const ConstTrajectoryIterator &it) const
bool operator<(const NodeId &other) const
ConstIterator & operator++()
ConstIterator end() const
std::map< int, MapByIndex >::const_iterator current_trajectory_
ConstTrajectoryIterator & operator--()
bool operator!=(const NodeId &other) const
bool operator==(const ConstIterator &it) const
void Trim(const IdType &id)
bool operator<(const SubmapId &other) const
std::map< int, DataType > data_
common::Time GetTime(const T &t)
const DataType & at(const IdType &id) const
void ToProto(proto::SubmapId *proto) const
std::bidirectional_iterator_tag iterator_category
size_t SizeOfTrajectoryOrZero(const int trajectory_id) const
void Insert(const IdType &id, const DataType &data)
static int GetIndex(const NodeId &id)
Range< ConstIterator > trajectory(const int trajectory_id) const
UniversalTimeScaleClock::time_point Time
std::map< int, DataType >::const_iterator current_data_
bool operator!=(const ConstIterator &it) const
ConstIterator(const MapById &map_by_id, const int trajectory_id)
bool Contains(const IdType &id) const
IdType Append(const int trajectory_id, const DataType &data)
ConstIterator EndOfTrajectory(const int trajectory_id) const
IteratorType begin() const
std::map< int, MapByIndex > trajectories_
bool operator==(const NodeId &other) const
std::bidirectional_iterator_tag iterator_category
ConstTrajectoryIterator & operator++()
void ToProto(proto::NodeId *proto) const
bool operator!=(const ConstTrajectoryIterator &it) const
Range< ConstTrajectoryIterator > trajectory_ids() const
ConstIterator begin() const
bool operator==(const SubmapId &other) const
static int GetIndex(const SubmapId &id)
ConstIterator find(const IdType &id) const
DataType & at(const IdType &id)
std::map< int, MapByIndex >::const_iterator current_trajectory_
bool operator!=(const SubmapId &other) const
ConstIterator(const MapById &map_by_id, const IdType &id)
ConstIterator & operator--()
std::ostream & operator<<(std::ostream &os, const NodeId &v)
ConstIterator BeginOfTrajectory(const int trajectory_id) const
ConstTrajectoryIterator(typename std::map< int, MapByIndex >::const_iterator current_trajectory)
void AdvanceToValidDataIterator()
ConstIterator lower_bound(const int trajectory_id, const common::Time time) const
std::unique_ptr< const IdDataReference > pointer
auto GetTimeImpl(const T &t, int) -> decltype(t.time())
IdDataReference operator*() const
Range(const IteratorType &begin, const IteratorType &end)
std::map< int, MapByIndex >::const_iterator end_trajectory_