From 006db4591079ed878cdbbbd75eae22669aceacbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Sch=C3=BCtte?= Date: Fri, 13 Oct 2017 16:44:31 +0200 Subject: [PATCH] Introduce proxy object for range based loops over nodes of trajectory. (#583) --- cartographer/mapping/id.h | 20 ++++++++++++++++++++ cartographer/mapping/id_test.cc | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/cartographer/mapping/id.h b/cartographer/mapping/id.h index 347a243..292969c 100644 --- a/cartographer/mapping/id.h +++ b/cartographer/mapping/id.h @@ -113,6 +113,20 @@ class NestedVectorsById { std::vector> data_; }; +template +class Range { + public: + Range(const IteratorType& begin, const IteratorType& end) + : begin_(begin), end_(end) {} + + IteratorType begin() const { return begin_; } + IteratorType end() const { return end_; } + + private: + IteratorType begin_; + IteratorType end_; +}; + // Reminiscent of std::map, but indexed by 'IdType' which can be 'NodeId' or // 'SubmapId'. template @@ -261,6 +275,12 @@ class MapById { : 0; } + // Returns Range object for range-based loops over the nodes of a trajectory. + Range trajectory(const int trajectory_id) const { + return Range(BeginOfTrajectory(trajectory_id), + EndOfTrajectory(trajectory_id)); + } + ConstIterator begin() const { return BeginOfTrajectory(0); } ConstIterator end() const { return BeginOfTrajectory(std::numeric_limits::max()); diff --git a/cartographer/mapping/id_test.cc b/cartographer/mapping/id_test.cc index 7573c7d..1c93e0e 100644 --- a/cartographer/mapping/id_test.cc +++ b/cartographer/mapping/id_test.cc @@ -59,6 +59,26 @@ TEST(IdTest, MapByIdIterator) { EXPECT_TRUE(expected_id_data.empty()); } +TEST(IdTest, MapByIdTrajectoryRange) { + MapById map_by_id; + map_by_id.Append(7, 2); + map_by_id.Append(42, 3); + map_by_id.Append(0, 0); + map_by_id.Append(0, 1); + + std::deque> expected_data = { + {NodeId{0, 0}, 0}, + {NodeId{0, 1}, 1}, + }; + for (const auto& entry : map_by_id.trajectory(0)) { + EXPECT_EQ(expected_data.front().first, entry.id); + EXPECT_EQ(expected_data.front().second, entry.data); + ASSERT_FALSE(expected_data.empty()); + expected_data.pop_front(); + } + EXPECT_TRUE(expected_data.empty()); +} + TEST(IdTest, MapByIdPrevIterator) { MapById map_by_id; map_by_id.Append(42, 42);