Skip to content

Commit a0ffcff

Browse files
committed
Add a utility method to check if 2 containers have common items or not
1 parent f03e5dc commit a0ffcff

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

hardware_interface/include/hardware_interface/helpers.hpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <map>
2121
#include <string>
2222
#include <unordered_map>
23+
#include <unordered_set>
2324
#include <vector>
2425

2526
namespace ros2_control
@@ -36,7 +37,8 @@ namespace ros2_control
3637
template <typename Container, typename T>
3738
[[nodiscard]] auto get_item_iterator(const Container & container, const T & item)
3839
{
39-
if constexpr (std::is_same_v<Container, std::vector<T>>)
40+
if constexpr (
41+
std::is_same_v<Container, std::vector<T>> || std::is_same_v<Container, std::unordered_set<T>>)
4042
{
4143
return std::find(container.begin(), container.end(), item);
4244
}
@@ -52,10 +54,11 @@ template <typename Container, typename T>
5254
using is_map = std::is_same<Container, std::map<T, typename Container::mapped_type>>;
5355
using is_unordered_map =
5456
std::is_same<Container, std::unordered_map<T, typename Container::mapped_type>>;
57+
using is_unordered_set = std::is_same<Container, std::unordered_set<T>>;
5558
// Handle unsupported container types
5659
static_assert(
57-
is_vector::value || is_map::value || is_unordered_map::value,
58-
"Only std::vector, std::map and std::unordered_map are supported.");
60+
is_vector::value || is_map::value || is_unordered_map::value || is_unordered_set::value,
61+
"Only std::vector, std::unordered_set, std::map and std::unordered_map are supported.");
5962
}
6063
}
6164

@@ -175,6 +178,22 @@ template <typename Container>
175178
return std::adjacent_find(container.cbegin(), container.cend()) == container.cend();
176179
}
177180

181+
// Create a function that checks that two containers do not have any common items
182+
/**
183+
* @brief Check if the two containers have no common items.
184+
* @param container1 The first container to search in.
185+
* @param container2 The second container to search in.
186+
* @return True if the two containers have no common items, false otherwise.
187+
*/
188+
template <typename Container1, typename Container2>
189+
[[nodiscard]] bool has_no_common_items(const Container1 & container1, const Container2 & container2)
190+
{
191+
return std::none_of(
192+
container1.cbegin(), container1.cend(),
193+
[&container2](const typename Container1::value_type & item)
194+
{ return has_item(container2, item); });
195+
}
196+
178197
} // namespace ros2_control
179198

180199
#endif // HARDWARE_INTERFACE__HELPERS_HPP_

hardware_interface/test/test_helpers.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,28 @@ TEST(TestHelper, test_helper_methods)
5454

5555
ASSERT_TRUE(ros2_control::is_unique(vec));
5656
ASSERT_FALSE(ros2_control::is_unique(std::vector<std::string>({"aa", "bb", "cc", "aa"})));
57+
58+
std::vector<std::string> vec1({"aa", "bb", "cc"});
59+
std::vector<std::string> vec2({"dd", "ee"});
60+
std::vector<std::string> vec3({"aa", "bb", "dd"});
61+
62+
std::unordered_set<std::string> set1({"aa", "bb", "cc"});
63+
std::unordered_set<std::string> set2({"dd", "ee"});
64+
std::unordered_set<std::string> set3({"aa", "bb", "dd"});
65+
66+
ASSERT_TRUE(ros2_control::has_no_common_items(vec1, vec2));
67+
ASSERT_TRUE(ros2_control::has_no_common_items(set1, set2));
68+
ASSERT_TRUE(ros2_control::has_no_common_items(vec1, set2));
69+
ASSERT_TRUE(ros2_control::has_no_common_items(vec2, set1));
70+
ASSERT_TRUE(ros2_control::has_no_common_items(set1, vec2));
71+
ASSERT_TRUE(ros2_control::has_no_common_items(set2, vec1));
72+
73+
ASSERT_FALSE(ros2_control::has_no_common_items(vec1, vec3));
74+
ASSERT_FALSE(ros2_control::has_no_common_items(set1, set3));
75+
ASSERT_FALSE(ros2_control::has_no_common_items(vec1, vec1));
76+
ASSERT_FALSE(ros2_control::has_no_common_items(set1, set1));
77+
ASSERT_FALSE(ros2_control::has_no_common_items(vec2, vec2));
78+
ASSERT_FALSE(ros2_control::has_no_common_items(set2, set2));
79+
ASSERT_FALSE(ros2_control::has_no_common_items(vec1, set3));
80+
ASSERT_FALSE(ros2_control::has_no_common_items(set1, vec3));
5781
}

0 commit comments

Comments
 (0)