mirror of
https://gitlab.com/mbunkus/mkvtoolnix.git
synced 2025-01-07 18:53:24 +00:00
common: add variadic template list utility functions
This commit is contained in:
parent
1498fa98a4
commit
c4aacc4569
78
src/common/list_utils.h
Normal file
78
src/common/list_utils.h
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
mkvmerge -- utility for splicing together matroska files
|
||||
from component media subtypes
|
||||
|
||||
Distributed under the GPL v2
|
||||
see the file COPYING for details
|
||||
or visit http://www.gnu.org/copyleft/gpl.html
|
||||
|
||||
list utility functions
|
||||
|
||||
Written by Moritz Bunkus <moritz@bunkus.org>.
|
||||
*/
|
||||
|
||||
#ifndef MTX_COMMON_LIST_UTILS_H
|
||||
#define MTX_COMMON_LIST_UTILS_H
|
||||
|
||||
namespace mtx {
|
||||
|
||||
template<typename T>
|
||||
bool
|
||||
includes(T const &needle,
|
||||
T const &val) {
|
||||
return needle == val;
|
||||
}
|
||||
|
||||
template<typename T,
|
||||
typename... Trest>
|
||||
bool
|
||||
includes(T const &needle,
|
||||
T const &val,
|
||||
Trest... rest) {
|
||||
return (needle == val) || includes(needle, rest...);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool
|
||||
any_of(std::function<bool(T const &)> pred,
|
||||
T const &val) {
|
||||
return pred(val);
|
||||
}
|
||||
|
||||
template<typename T,
|
||||
typename... Trest>
|
||||
bool
|
||||
any_of(std::function<bool(T const &)> pred,
|
||||
T const &val,
|
||||
Trest... rest) {
|
||||
return pred(val) || any_of(pred, rest...);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool
|
||||
all_of(std::function<bool(T const &)> pred,
|
||||
T const &val) {
|
||||
return pred(val);
|
||||
}
|
||||
|
||||
template<typename T,
|
||||
typename... Trest>
|
||||
bool
|
||||
all_of(std::function<bool(T const &)> pred,
|
||||
T const &val,
|
||||
Trest... rest) {
|
||||
return pred(val) && all_of(pred, rest...);
|
||||
}
|
||||
|
||||
template<typename T,
|
||||
typename... Trest>
|
||||
bool
|
||||
none_of(std::function<bool(T const &)> pred,
|
||||
T const &val,
|
||||
Trest... rest) {
|
||||
return !any_of<T, Trest...>(pred, val, rest...);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // MTX_COMMON_LIST_UTILS_H
|
57
tests/unit/common/list_utils.cpp
Normal file
57
tests/unit/common/list_utils.cpp
Normal file
@ -0,0 +1,57 @@
|
||||
#include "common/common_pch.h"
|
||||
|
||||
#include "common/list_utils.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace {
|
||||
|
||||
TEST(ListUtils, includes) {
|
||||
EXPECT_TRUE(mtx::includes(42, 42));
|
||||
EXPECT_TRUE(mtx::includes(42, 54, 42, 48));
|
||||
EXPECT_TRUE(mtx::includes(42, 42, 54, 48));
|
||||
EXPECT_TRUE(mtx::includes(42, 54, 48, 42));
|
||||
|
||||
EXPECT_FALSE(mtx::includes(23, 42));
|
||||
EXPECT_FALSE(mtx::includes(23, 54, 42, 48));
|
||||
EXPECT_FALSE(mtx::includes(23, 42, 54, 48));
|
||||
EXPECT_FALSE(mtx::includes(23, 54, 48, 42));
|
||||
}
|
||||
|
||||
TEST(ListUtils, any_of) {
|
||||
EXPECT_TRUE(mtx::any_of<int>([](int val) { return val < 43; }, 42));
|
||||
EXPECT_TRUE(mtx::any_of<int>([](int val) { return val < 43; }, 54, 42, 48));
|
||||
EXPECT_TRUE(mtx::any_of<int>([](int val) { return val < 43; }, 42, 54, 48));
|
||||
EXPECT_TRUE(mtx::any_of<int>([](int val) { return val < 43; }, 54, 48, 42));
|
||||
|
||||
EXPECT_FALSE(mtx::any_of<int>([](int val) { return val > 64; }, 42));
|
||||
EXPECT_FALSE(mtx::any_of<int>([](int val) { return val > 64; }, 54, 42, 48));
|
||||
EXPECT_FALSE(mtx::any_of<int>([](int val) { return val > 64; }, 42, 54, 48));
|
||||
EXPECT_FALSE(mtx::any_of<int>([](int val) { return val > 64; }, 54, 48, 42));
|
||||
}
|
||||
|
||||
TEST(ListUtils, all_of) {
|
||||
EXPECT_TRUE(mtx::all_of<int>([](int val) { return val < 64; }, 42));
|
||||
EXPECT_TRUE(mtx::all_of<int>([](int val) { return val < 64; }, 54, 42, 48));
|
||||
EXPECT_TRUE(mtx::all_of<int>([](int val) { return val < 64; }, 42, 54, 48));
|
||||
EXPECT_TRUE(mtx::all_of<int>([](int val) { return val < 64; }, 54, 48, 42));
|
||||
|
||||
EXPECT_FALSE(mtx::all_of<int>([](int val) { return val < 42; }, 42));
|
||||
EXPECT_FALSE(mtx::all_of<int>([](int val) { return val < 42; }, 54, 42, 48));
|
||||
EXPECT_FALSE(mtx::all_of<int>([](int val) { return val < 42; }, 42, 54, 48));
|
||||
EXPECT_FALSE(mtx::all_of<int>([](int val) { return val < 42; }, 54, 48, 42));
|
||||
}
|
||||
|
||||
TEST(ListUtils, none_of) {
|
||||
EXPECT_TRUE(mtx::none_of<int>([](int val) { return val > 64; }, 42));
|
||||
EXPECT_TRUE(mtx::none_of<int>([](int val) { return val > 64; }, 54, 42, 48));
|
||||
EXPECT_TRUE(mtx::none_of<int>([](int val) { return val > 64; }, 42, 54, 48));
|
||||
EXPECT_TRUE(mtx::none_of<int>([](int val) { return val > 64; }, 54, 48, 42));
|
||||
|
||||
EXPECT_FALSE(mtx::none_of<int>([](int val) { return val < 56; }, 42));
|
||||
EXPECT_FALSE(mtx::none_of<int>([](int val) { return val < 56; }, 54, 42, 48));
|
||||
EXPECT_FALSE(mtx::none_of<int>([](int val) { return val < 56; }, 42, 54, 48));
|
||||
EXPECT_FALSE(mtx::none_of<int>([](int val) { return val < 56; }, 54, 48, 42));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user