From e721fe6d8ea12f51112d553ee88c3ddf260ddc87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20Sol=C3=A0?= <jsola@iri.upc.edu> Date: Mon, 22 Jun 2020 13:05:11 +0200 Subject: [PATCH] Add structure comparator structcmp() --- include/core/state_block/state_composite.h | 3 +++ src/state_block/state_composite.cpp | 16 ++++++++++++++++ test/gtest_state_composite.cpp | 13 +++++++++++++ 3 files changed, 32 insertions(+) diff --git a/include/core/state_block/state_composite.h b/include/core/state_block/state_composite.h index c7a125ac0..a58121fef 100644 --- a/include/core/state_block/state_composite.h +++ b/include/core/state_block/state_composite.h @@ -25,6 +25,9 @@ typedef std::string StateStructure; typedef std::unordered_map < char, StateBlockPtr > StateBlockMap; typedef StateBlockMap::const_iterator StateBlockMapCIter; +bool structcmp(const StateStructure& s1, const StateStructure& s2); + + class VectorComposite : public std::unordered_map < char, Eigen::VectorXd > { public: diff --git a/src/state_block/state_composite.cpp b/src/state_block/state_composite.cpp index 0f4efab6f..3b4367c08 100644 --- a/src/state_block/state_composite.cpp +++ b/src/state_block/state_composite.cpp @@ -6,6 +6,22 @@ namespace wolf{ +////// STATE STRUCTURE ////////// + +bool structcmp(const StateStructure& s1, const StateStructure& s2) +{ + if (s1.size() != s2.size()) return false; + + std::set<char> s1_chars, s2_chars; + + for (const auto &c : s1) + s1_chars.emplace(c); + for (const auto &c : s2) + s2_chars.emplace(c); + + return s1_chars == s2_chars; +} + ////// VECTOR COMPOSITE ////////// VectorComposite::VectorComposite(const StateStructure& _structure, const std::list<int>& _sizes) diff --git a/test/gtest_state_composite.cpp b/test/gtest_state_composite.cpp index f4bcea67b..d075886db 100644 --- a/test/gtest_state_composite.cpp +++ b/test/gtest_state_composite.cpp @@ -13,6 +13,19 @@ using namespace wolf; using namespace std; +TEST(StateStructure, operator_equal) +{ + StateStructure POV("POV"), PVO("PVO"), PO("PO"), POVW("POVW"); + + ASSERT_TRUE ( structcmp(POV , PVO ) ); + ASSERT_FALSE( structcmp(POV , PO ) ); + ASSERT_FALSE( structcmp(PO , POV ) ); + ASSERT_FALSE( structcmp(PO , POVW) ); + ASSERT_FALSE( structcmp(POVW , PO ) ); + + ASSERT_TRUE ( structcmp(POVW , "PVOW" ) ); + ASSERT_TRUE ( structcmp(POVW , "WPOV" ) ); +} class StateBlockCompositeInit : public testing::Test { -- GitLab