common: add variadic template list utility functions

This commit is contained in:
Moritz Bunkus 2015-06-01 21:13:11 +02:00
parent 1498fa98a4
commit c4aacc4569
2 changed files with 135 additions and 0 deletions

78
src/common/list_utils.h Normal file
View 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

View 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));
}
}