From fb4c048c866d51b30a3270139dfd259d90d0ee5c Mon Sep 17 00:00:00 2001 From: JasonZhou404 Date: Mon, 3 Dec 2018 20:11:14 -0800 Subject: [PATCH] Planning: overload = for IndexedList --- modules/planning/common/indexed_list.h | 18 ++++++++++++++++++ modules/planning/common/indexed_list_test.cc | 14 ++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/modules/planning/common/indexed_list.h b/modules/planning/common/indexed_list.h index 836a45e337..8b1d20e40a 100644 --- a/modules/planning/common/indexed_list.h +++ b/modules/planning/common/indexed_list.h @@ -85,6 +85,24 @@ class IndexedList { */ const std::vector& Items() const { return object_list_; } + /** + * @brief List all the items in the container. + * @return the unordered_map of ids and objects in the container. + */ + const std::unordered_map& Dict() const { return object_dict_; } + + /** + * @brief Copy the container with objects. + */ + IndexedList& operator=(const IndexedList& other) { + this->object_list_.clear(); + this->object_dict_.clear(); + for (const auto& item : other.Dict()) { + Add(item.first, item.second); + } + return *this; + } + private: std::vector object_list_; std::unordered_map object_dict_; diff --git a/modules/planning/common/indexed_list_test.cc b/modules/planning/common/indexed_list_test.cc index d3f052cac7..6f4ff60b57 100644 --- a/modules/planning/common/indexed_list_test.cc +++ b/modules/planning/common/indexed_list_test.cc @@ -67,5 +67,19 @@ TEST(IndexedList, Find) { ASSERT_EQ(nullptr, object.Find(2)); } +TEST(IndexedList, Copy) { + StringIndexedList b_object; + b_object.Add(1, "one"); + b_object.Add(2, "two"); + StringIndexedList a_object; + a_object.Add(3, "three"); + a_object.Add(4, "four"); + a_object = b_object; + ASSERT_NE(nullptr, a_object.Find(1)); + ASSERT_NE(nullptr, a_object.Find(2)); + ASSERT_EQ(nullptr, a_object.Find(3)); + ASSERT_EQ(nullptr, a_object.Find(4)); +} + } // namespace planning } // namespace apollo -- GitLab