Skip to content

Commit 7a0ac03

Browse files
author
yurial
committed
Add TUniqueTypeList and NTL::TUnique
Позволяет получить TTypeList с уникальными типами. commit_hash:589914da95d44bdc457d7e4341e4a377df2da2b8
1 parent 61ab9a7 commit 7a0ac03

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

util/generic/typelist.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,29 @@ namespace NTL {
7575
using type = TTypeList<R1..., R2...>;
7676
};
7777

78+
template <class TResult, class T, class... Ts>
79+
struct TUniqueImpl;
80+
81+
template <class TResult, class T>
82+
struct TUniqueImpl<TResult, T> {
83+
using type = std::conditional_t<TResult::template THave<T>::value, TResult, typename NTL::TConcat<TResult, TTypeList<T>>::type>;
84+
};
85+
86+
template <class TResult, class T, class... Ts>
87+
struct TUniqueImpl {
88+
using type = std::conditional_t<TResult::template THave<T>::value,
89+
typename TUniqueImpl<TResult, Ts...>::type,
90+
typename TUniqueImpl<typename NTL::TConcat<TResult, TTypeList<T>>::type, Ts...>::type>;
91+
};
92+
93+
template <class... Ts>
94+
struct TUnique;
95+
96+
template <class... Ts>
97+
struct TUnique<TTypeList<Ts...>> {
98+
using type = typename TUniqueImpl<TTypeList<>, Ts...>::type;
99+
};
100+
78101
template <bool isSigned, class T, class TS, class TU>
79102
struct TTypeSelectorBase {
80103
using TSignedInts = typename TConcat<TTypeList<T>, TS>::type;
@@ -95,6 +118,9 @@ namespace NTL {
95118
using T2 = TTypeSelector<wchar_t, T1::TSignedInts, T1::TUnsignedInts>;
96119
} // namespace NTL
97120

121+
template <class... Ts>
122+
using TUniqueTypeList = typename NTL::TUniqueImpl<TTypeList<>, Ts...>::type;
123+
98124
using TSignedInts = NTL::T2::TSignedInts;
99125
using TUnsignedInts = NTL::T2::TUnsignedInts;
100126

util/generic/typelist_ut.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class TTypeListTest: public TTestBase {
1111
UNIT_TEST(TestGet);
1212
UNIT_TEST(TestFloatList);
1313
UNIT_TEST(TestSelectBy);
14+
UNIT_TEST(TestUnique);
15+
UNIT_TEST(TestUniqueTypeList);
1416
UNIT_TEST_SUITE_END();
1517

1618
public:
@@ -79,6 +81,19 @@ class TTypeListTest: public TTestBase {
7981
UNIT_ASSERT_TYPES_EQUAL(TFixedWidthFloat<ui64>, double);
8082
UNIT_ASSERT_TYPES_EQUAL(TFixedWidthFloat<i64>, double);
8183
}
84+
85+
void TestUnique() {
86+
static_assert(std::is_same_v<NTL::TUnique<TSignedInts>::type, TSignedInts>);
87+
static_assert(std::is_same_v<typename NTL::TUnique<typename NTL::TConcat<TSignedInts, TSignedInts>::type>::type, TSignedInts>);
88+
}
89+
90+
void TestUniqueTypeList() {
91+
static_assert(std::is_same_v<TUniqueTypeList<int>, TTypeList<int>>);
92+
static_assert(std::is_same_v<TUniqueTypeList<int, int, int, int, int, int, int, int, int>, TTypeList<int>>);
93+
static_assert(std::is_same_v<TUniqueTypeList<TSignedInts>, TTypeList<TSignedInts>>);
94+
static_assert(std::is_same_v<TUniqueTypeList<TSignedInts, TSignedInts, TSignedInts, TSignedInts, TSignedInts, TSignedInts, TSignedInts, TSignedInts, TSignedInts>, TTypeList<TSignedInts>>);
95+
static_assert(std::is_same_v<TUniqueTypeList<TSignedInts, TSignedInts, TSignedInts, TSignedInts, TSignedInts, TSignedInts, TSignedInts, TSignedInts, TSignedInts>, TUniqueTypeList<TSignedInts, TSignedInts, TSignedInts>>);
96+
}
8297
};
8398

8499
UNIT_TEST_SUITE_REGISTRATION(TTypeListTest);

0 commit comments

Comments
 (0)