From efcf016ee626b95c41b31e4c3829de1da688dd08 Mon Sep 17 00:00:00 2001 From: Arjo Chakravarty Date: Thu, 6 Oct 2022 23:53:31 +0800 Subject: [PATCH] Adds an API to retrieve keys. (#446) This is useful for when we perform visualization. Signed-off-by: Arjo Chakravarty Co-authored-by: Michael Carroll --- io/include/gz/common/DataFrame.hh | 12 ++++++++++++ io/src/DataFrame_TEST.cc | 6 +++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/io/include/gz/common/DataFrame.hh b/io/include/gz/common/DataFrame.hh index bc340015e..fda99d060 100644 --- a/io/include/gz/common/DataFrame.hh +++ b/io/include/gz/common/DataFrame.hh @@ -68,6 +68,18 @@ namespace gz return this->storage.at(_key); } + /// \brief Retrieve all keys + /// \return A vector with keys + public: const std::vector Keys() const + { + std::vector keyList; + for (auto &[k, _]: this->storage) + { + keyList.push_back(k); + } + return keyList; + } + /// \brief Data frame storage private: std::unordered_map storage; }; diff --git a/io/src/DataFrame_TEST.cc b/io/src/DataFrame_TEST.cc index 5418d6879..fd2e8a215 100644 --- a/io/src/DataFrame_TEST.cc +++ b/io/src/DataFrame_TEST.cc @@ -66,6 +66,9 @@ TEST(DataFrameTests, SimpleCSV) temperatureSession.value(), position); ASSERT_TRUE(temperature.has_value()); EXPECT_DOUBLE_EQ(25.1, temperature.value()); + auto keys = df.Keys(); + ASSERT_EQ(keys.size(), 1); + ASSERT_EQ(keys[0], "temperature"); } ///////////////////////////////////////////////// @@ -87,7 +90,8 @@ TEST(DataFrameTests, ComplexCSV) EXPECT_TRUE(df.Has("temperature")); EXPECT_TRUE(df.Has("humidity")); ASSERT_TRUE(df.Has("pressure")); - + auto keys = df.Keys(); + ASSERT_EQ(keys.size(), 3); const DataT &pressureData = df["pressure"]; auto pressureSession = pressureData.CreateSession(); const math::Vector3d position{36.80079505, -121.789472517, 0.8};