-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsum.hpp
92 lines (67 loc) · 2.08 KB
/
sum.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) 2015-2020 Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/sequences/
#ifndef TAO_SEQ_SUM_HPP
#define TAO_SEQ_SUM_HPP
#include <utility>
#include "config.hpp"
#ifdef TAO_SEQ_FOLD_EXPRESSIONS
#include "integer_sequence.hpp"
#else
#include <cstddef>
#include <type_traits>
#include "make_integer_sequence.hpp"
#endif
namespace tao
{
namespace sequence
{
#ifdef TAO_SEQ_FOLD_EXPRESSIONS
template< typename T, T... Ns >
struct sum
: std::integral_constant< T, ( T( 0 ) + ... + Ns ) >
{
};
#else
namespace impl
{
template< std::size_t, std::size_t N >
struct chars
{
char dummy[ N + 1 ]; // NOLINT(modernize-avoid-c-arrays)
};
template< typename, std::size_t... >
struct collector;
template< std::size_t... Is, std::size_t... Ns >
struct collector< index_sequence< Is... >, Ns... >
: chars< Is, Ns >...
{
};
template< bool, std::size_t N, typename T, T... Ns >
struct sum;
template< std::size_t N, typename T, T... Ns >
struct sum< true, N, T, Ns... >
{
using type = std::integral_constant< T, T( sizeof( collector< make_index_sequence< N >, Ns... > ) - N ) >;
};
template< bool, std::size_t N, typename T, T... Ns >
struct sum
{
using positive = typename sum< true, N, T, ( ( Ns > 0 ) ? Ns : 0 )... >::type;
using negative = typename sum< true, N, T, ( ( Ns < 0 ) ? -Ns : 0 )... >::type;
using type = std::integral_constant< T, positive::value - negative::value >;
};
} // namespace impl
template< typename T, T... Ns >
struct sum
: impl::sum< std::is_unsigned< T >::value, sizeof...( Ns ) + 1, T, T( 0 ), Ns... >::type
{
};
#endif
template< typename T, T... Ns >
struct sum< integer_sequence< T, Ns... > >
: sum< T, Ns... >
{
};
} // namespace sequence
} // namespace tao
#endif