mirror of
https://gitlab.com/mbunkus/mkvtoolnix.git
synced 2025-01-07 18:53:24 +00:00
list utils: implementation of first_of
This commit is contained in:
parent
d5920630bd
commit
6f80025fe6
@ -14,6 +14,8 @@
|
||||
#ifndef MTX_COMMON_LIST_UTILS_H
|
||||
#define MTX_COMMON_LIST_UTILS_H
|
||||
|
||||
#include <boost/optional.hpp>
|
||||
|
||||
namespace mtx {
|
||||
|
||||
template<typename T>
|
||||
@ -73,6 +75,22 @@ none_of(std::function<bool(T const &)> pred,
|
||||
return !any_of<T, Trest...>(pred, val, rest...);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
boost::optional<T>
|
||||
first_of(std::function<bool(T const &)> pred,
|
||||
T const &val) {
|
||||
return pred(val) ? val : boost::optional<T>{};
|
||||
}
|
||||
|
||||
template<typename T,
|
||||
typename... Trest>
|
||||
boost::optional<T>
|
||||
first_of(std::function<bool(T const &)> pred,
|
||||
T const &val,
|
||||
Trest... rest) {
|
||||
return pred(val) ? val : first_of(pred, rest...);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // MTX_COMMON_LIST_UTILS_H
|
||||
|
@ -6,6 +6,21 @@
|
||||
|
||||
namespace {
|
||||
|
||||
TEST(ListUtils, first_of) {
|
||||
EXPECT_FALSE(mtx::first_of<int>([](int val) { return val > 43; }, 42));
|
||||
|
||||
EXPECT_TRUE(!!mtx::first_of<int>([](int val) { return val > 43; }, 48));
|
||||
EXPECT_TRUE(!!mtx::first_of<int>([](int val) { return val > 43; }, 42, 54));
|
||||
EXPECT_TRUE(!!mtx::first_of<int>([](int val) { return val > 43; }, 42, 41, 40, 39, 38, 12345));
|
||||
|
||||
EXPECT_EQ(48, mtx::first_of<int>([](int val) { return val > 43; }, 48));
|
||||
EXPECT_EQ(54, mtx::first_of<int>([](int val) { return val > 43; }, 42, 54).get());
|
||||
EXPECT_EQ(12345, mtx::first_of<int>([](int val) { return val > 43; }, 42, 41, 40, 39, 38, 12345).get());
|
||||
|
||||
EXPECT_TRUE(!!mtx::first_of<void *>([](void *p) { return !!p; }, static_cast<void *>(nullptr), reinterpret_cast<void *>(0x12345)));
|
||||
EXPECT_EQ(reinterpret_cast<void *>(0x12345), mtx::first_of<void *>([](void *p) { return !!p; }, static_cast<void *>(nullptr), reinterpret_cast<void *>(0x12345)));
|
||||
}
|
||||
|
||||
TEST(ListUtils, included_in) {
|
||||
EXPECT_TRUE(mtx::included_in(42, 42));
|
||||
EXPECT_TRUE(mtx::included_in(42, 54, 42, 48));
|
||||
|
Loading…
Reference in New Issue
Block a user