Skip to content

Commit b17875c

Browse files
committed
refactor(value-sequence): reduced duplications
1 parent beeb9b8 commit b17875c

File tree

2 files changed

+63
-131
lines changed

2 files changed

+63
-131
lines changed

include/stdsharp/type_traits/type_sequence.h

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -57,70 +57,33 @@ namespace stdsharp
5757
using append_front_t = regular_type_sequence<Others..., T...>;
5858

5959
private:
60-
template<std::size_t Index>
61-
struct insert
60+
template<typename, typename>
61+
struct seq_traits;
62+
63+
template<auto... I, auto... J>
64+
struct seq_traits<std::index_sequence<I...>, std::index_sequence<J...>>
6265
{
63-
template<typename... Others, auto... I, auto... J>
64-
static consteval auto impl( //
65-
std::index_sequence<I...> /*unused*/,
66-
std::index_sequence<J...> /*unused*/
67-
)
66+
struct type
6867
{
69-
return regular_type_sequence<
68+
template<typename... Others>
69+
using apply = regular_type_sequence<
7070
type_sequence::type<I>...,
7171
Others...,
72-
type_sequence::type<J + Index>...>{};
73-
}
74-
75-
template<typename... Others>
76-
using type = decltype( //
77-
impl<Others...>(
78-
std::make_index_sequence<Index>{},
79-
std::make_index_sequence<size() - Index>{}
80-
)
81-
);
82-
};
83-
84-
template<std::size_t Index>
85-
struct remove_at
86-
{
87-
template<auto... I, auto... J>
88-
static consteval regular_type_sequence<type<I>..., type<J + Index + 1>...>
89-
impl(std::index_sequence<I...>, std::index_sequence<J...>);
90-
91-
using type = decltype( //
92-
impl(
93-
std::make_index_sequence<Index>{},
94-
std::make_index_sequence<size() - Index - 1>{}
95-
)
96-
);
97-
};
98-
99-
template<std::size_t Index>
100-
struct replace_at
101-
{
102-
template<typename Other, auto... I, auto... J>
103-
static consteval regular_type_sequence<type<I>..., Other, type<J + Index + 1>...>
104-
impl(std::index_sequence<I...>, std::index_sequence<J...>);
105-
106-
template<typename Other>
107-
using type = decltype( //
108-
impl<Other>(
109-
std::make_index_sequence<Index>{},
110-
std::make_index_sequence<size() - Index - 1>{}
111-
)
112-
);
72+
type_sequence::type<J>...>;
73+
};
11374
};
11475

11576
public:
11677
template<std::size_t Index, typename... Other>
117-
using insert_t = insert<Index>::template type<Other...>;
78+
using insert_t = details::seq_insert_trait<size(), seq_traits>:: //
79+
template type<Index>::template apply<Other...>;
11880

119-
template<std::size_t... Index>
120-
using remove_at_t = remove_at<Index...>::type;
81+
template<std::size_t Index, typename... Other>
82+
using replace_t = details::seq_rmv_trait<size(), seq_traits>:: //
83+
template type<Index>::template apply<Other...>;
12184

122-
template<std::size_t Index, typename Other>
123-
using replace_t = replace_at<Index>::template type<Other>;
85+
template<std::size_t Index>
86+
using remove_at_t = replace_t<Index>;
12487
};
12588
}
12689

include/stdsharp/type_traits/value_sequence.h

Lines changed: 46 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,42 @@ namespace stdsharp
2323

2424
namespace stdsharp::details
2525
{
26-
struct value_indexer
26+
template<std::size_t Size, template<typename, typename> typename Impl>
27+
struct seq_insert_trait
2728
{
28-
template<auto V>
29-
struct filter
29+
template<std::size_t Index, auto... I, auto... J>
30+
static consteval auto
31+
impl(std::index_sequence<I...> /*unused*/, std::index_sequence<J...> /*unused*/)
3032
{
31-
[[nodiscard]] consteval decltype(V) get() const noexcept { return V; }
32-
};
33+
return typename Impl<std::index_sequence<I...>, std::index_sequence<(J + Index)...>>::
34+
type{};
35+
}
3336

34-
template<std::size_t>
35-
struct invalid
36-
{
37-
};
37+
template<std::size_t Index>
38+
using type = decltype( //
39+
impl<Index>(std::make_index_sequence<Index>{}, std::make_index_sequence<Size - Index>{})
40+
);
41+
};
3842

39-
public:
40-
template<std::size_t I, std::size_t... J, auto... V>
41-
static consteval decltype(auto) impl(
42-
const std::index_sequence<J...> /*unused*/,
43-
const regular_value_sequence<V...> /*unused*/
44-
)
43+
template<std::size_t Size, template<typename, typename> typename Impl>
44+
struct seq_rmv_trait
45+
{
46+
template<std::size_t Index, auto... I, auto... J>
47+
static consteval auto
48+
impl(std::index_sequence<I...> /*unused*/, std::index_sequence<J...> /*unused*/)
4549
{
46-
constexpr struct STDSHARP_EBO : std::conditional_t<I == J, filter<V>, invalid<J>>...
47-
{
48-
} f{};
49-
50-
return f.get();
50+
return
51+
typename Impl<std::index_sequence<I...>, std::index_sequence<(J + Index + 1)...>>::
52+
type{};
5153
}
5254

53-
template<std::size_t I, auto... V>
54-
static constexpr decltype(auto) value =
55-
impl<I>(std::make_index_sequence<sizeof...(V)>{}, regular_value_sequence<V...>{});
55+
template<std::size_t Index>
56+
using type = decltype( //
57+
impl<Index>(
58+
std::make_index_sequence<Index>{},
59+
std::make_index_sequence<Size - Index - 1>{}
60+
)
61+
);
5662
};
5763
}
5864

@@ -70,7 +76,7 @@ namespace stdsharp
7076
requires requires { requires I < size(); }
7177
[[nodiscard]] static constexpr value_type<I> get() noexcept
7278
{
73-
return details::value_indexer::template value<I, Values...>;
79+
return type_at<I, constant<Values>...>::value;
7480
}
7581

7682
template<std::size_t I>
@@ -102,56 +108,17 @@ namespace stdsharp
102108
static consteval void predicate_test()
103109
noexcept((nothrow_predicate<Func&, const decltype(Values)&> && ...));
104110

105-
template<std::size_t Index>
106-
struct insert
107-
{
108-
template<auto... Others, auto... I, auto... J>
109-
static consteval auto impl( //
110-
std::index_sequence<I...> /*unused*/,
111-
std::index_sequence<J...> /*unused*/
112-
)
113-
{
114-
return regular_value_sequence<get<I>()..., Others..., get<J + Index>()...>{};
115-
}
116-
117-
template<auto... Others>
118-
using type = decltype( //
119-
impl<Others...>(
120-
std::make_index_sequence<Index>{},
121-
std::make_index_sequence<size() - Index>{}
122-
)
123-
);
124-
};
125-
126-
template<std::size_t Index>
127-
struct remove_at
128-
{
129-
template<auto... I, auto... J>
130-
static consteval regular_value_sequence<get<I>()..., get<J + Index + 1>()...>
131-
impl(std::index_sequence<I...>, std::index_sequence<J...>);
132-
133-
using type = decltype( //
134-
impl(
135-
std::make_index_sequence<Index>{},
136-
std::make_index_sequence<size() - Index - 1>{}
137-
)
138-
);
139-
};
111+
template<typename, typename>
112+
struct seq_traits;
140113

141-
template<std::size_t Index>
142-
struct replace_at
114+
template<auto... I, auto... J>
115+
struct seq_traits<std::index_sequence<I...>, std::index_sequence<J...>>
143116
{
144-
template<auto Other, auto... I, auto... J>
145-
static consteval regular_value_sequence<get<I>()..., Other, get<J + Index + 1>()...>
146-
impl(std::index_sequence<I...>, std::index_sequence<J...>);
147-
148-
template<auto Other>
149-
using type = decltype( //
150-
impl<Other>(
151-
std::make_index_sequence<Index>{},
152-
std::make_index_sequence<size() - Index - 1>{}
153-
)
154-
);
117+
struct type
118+
{
119+
template<auto... Others>
120+
using apply = regular_value_sequence<get<I>()..., Others..., get<J>()...>;
121+
};
155122
};
156123

157124
public:
@@ -224,13 +191,15 @@ namespace stdsharp
224191
using append_front_t = regular_value_sequence<Others..., Values...>;
225192

226193
template<std::size_t Index, auto... Other>
227-
using insert_t = insert<Index>::template type<Other...>;
194+
using insert_t = details::seq_insert_trait<size(), seq_traits>:: //
195+
template type<Index>::template apply<Other...>;
228196

229-
template<std::size_t Index>
230-
using remove_at_t = remove_at<Index>::type;
197+
template<std::size_t Index, auto... Other>
198+
using replace_t = details::seq_rmv_trait<size(), seq_traits>:: //
199+
template type<Index>::template apply<Other...>;
231200

232-
template<std::size_t Index, auto Other>
233-
using replace_t = replace_at<Index>::template type<Other>;
201+
template<std::size_t Index>
202+
using remove_at_t = replace_t<Index>;
234203
};
235204
}
236205

0 commit comments

Comments
 (0)