Skip to content

Commit 4c6da6b

Browse files
committed
Added number to string conversion.
1 parent 0b03617 commit 4c6da6b

File tree

9 files changed

+213
-3
lines changed

9 files changed

+213
-3
lines changed

math/even.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include "../fundamental/integral.hpp"
4+
5+
namespace mplex
6+
{
7+
struct is_even
8+
{
9+
template <typename Value>
10+
struct apply
11+
{
12+
using type = bool_<Value::value % 2 == 0>;
13+
constexpr static auto value = type::value;
14+
};
15+
};
16+
17+
template <typename Value>
18+
using is_even_t = typename is_even::template apply <Value>::type;
19+
}

math/log10.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#include <mplex/control/if.hpp>
4+
#include <mplex/fundamental/integral.hpp>
5+
#include <mplex/fundamental/identity.hpp>
6+
7+
namespace mplex
8+
{
9+
/**
10+
* Does not have decimal places
11+
*/
12+
struct log10
13+
{
14+
template <
15+
typename Constant,
16+
typename Counter = long_long_<1>
17+
>
18+
struct apply
19+
{
20+
using integral_type = typename Constant::value_type;
21+
22+
static constexpr bool stop = Constant::value < 10;
23+
static constexpr integral_type value = lazy_if_vt <
24+
!stop,
25+
then_ <log10, std::integral_constant<integral_type, Constant::value / 10>, std::integral_constant<integral_type, Counter::value + 1>>,
26+
else_ <identity_functor, Counter>
27+
>::value;
28+
29+
using type = std::integral_constant <integral_type, value>;
30+
//using type = bool_<stop>;
31+
};
32+
};
33+
}

math/pow.hpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#pragma once
2+
3+
#include "even.hpp"
4+
#include "../control/if.hpp"
5+
#include "../fundamental/integral.hpp"
6+
#include "../fundamental/identity.hpp"
7+
8+
namespace mplex
9+
{
10+
struct pow
11+
{
12+
template <
13+
typename Base,
14+
typename Exp
15+
>
16+
struct apply
17+
{
18+
using type = long_long_ <
19+
pow::template apply <Base, long_long_<Exp::value / 2>>::type::value *
20+
pow::template apply <Base, long_long_<Exp::value / 2>>::type::value *
21+
std::conditional_t <is_even_t <Exp>::value, long_long_<1>, Base>::value
22+
>;
23+
constexpr static auto value = type::value;
24+
};
25+
26+
template <typename Base>
27+
struct apply <Base, long_long_<3>>
28+
{
29+
constexpr static auto value = Base::value * Base::value * Base::value;
30+
using type = long_long_<value>;
31+
};
32+
template <typename Base>
33+
struct apply <Base, long_long_<2>>
34+
{
35+
constexpr static auto value = Base::value * Base::value;
36+
using type = long_long_<value>;
37+
};
38+
template <typename Base>
39+
struct apply <Base, long_long_<1>>
40+
{
41+
using type = Base;
42+
constexpr static auto value = type::value;
43+
};
44+
template <typename Base>
45+
struct apply <Base, long_long_<0>>
46+
{
47+
using type = long_long_<1>;
48+
constexpr static auto value = 1;
49+
};
50+
};
51+
52+
template <typename Base, typename Exp>
53+
using pow_t = typename pow::template apply <Base, Exp>::type;
54+
55+
template <long long Base, long long Exp>
56+
using pow_vt = typename pow::template apply <long_long_ <Base>, long_long_ <Exp>>::type;
57+
}

string/combine.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
#include "string.hpp"
4+
#include "strcat.hpp"
5+
6+
namespace mplex { namespace string_algorithm
7+
{
8+
/**
9+
* Basically:
10+
*
11+
* combine_strings <a, b, c, ..., z> =
12+
* stream << a << b << c << ... << z;
13+
*/
14+
template <typename T, typename U, typename... Strings>
15+
struct combine_strings {
16+
using type = typename combine_strings <typename strcat <T, U>::type, Strings...>::type;
17+
};
18+
19+
template <typename T, typename U>
20+
struct combine_strings <T, U> {
21+
using type = typename strcat <T, U>::type;
22+
};
23+
}
24+
}

string/string.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
namespace mplex
2020
{
21-
template <char ... List>
21+
template <char... List>
2222
struct string;
2323

2424
template <typename... CharArray>
@@ -29,7 +29,7 @@ namespace mplex
2929
template <typename... CharArray>
3030
using translate = typename __string <CharArray...>::type;
3131

32-
template <char ... List>
32+
template <char... List>
3333
struct string
3434
{
3535
using size_type = std::make_signed <std::size_t>::type;

string/to_string.hpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
3+
#include "string.hpp"
4+
#include "../math/log10.hpp"
5+
#include "../math/pow.hpp"
6+
#include "../tuple/apply.hpp"
7+
8+
#include <utility>
9+
10+
namespace mplex
11+
{
12+
namespace internal
13+
{
14+
template <typename Sequence, typename SequenceSize, typename Value>
15+
struct to_string_impl{};
16+
17+
template <typename Value, typename SequenceSize, typename T, T... Is>
18+
struct to_string_impl <std::integer_sequence <T, Is...>, SequenceSize, Value>
19+
{
20+
using digits = std::integer_sequence <
21+
char,
22+
('0' + (
23+
Value::value / pow_vt<10, (SequenceSize::value - (Is + 1))>::value -
24+
10 * (Value::value / pow_vt<10, (SequenceSize::value - (Is))>::value)
25+
))...
26+
>;
27+
};
28+
}
29+
30+
template <
31+
typename Value
32+
>
33+
struct to_string
34+
{
35+
using str_size = typename log10::template apply<Value>::type;
36+
37+
using type = typename apply_values <
38+
char,
39+
typename internal::to_string_impl <std::make_index_sequence <str_size::value>, str_size, Value>::digits,
40+
string
41+
>::type;
42+
};
43+
}

tuple/apply.hpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef MPLEX_TUPLE_APPLY_HPP_INCLUDED
22
#define MPLEX_TUPLE_APPLY_HPP_INCLUDED
33

4+
#include <utility>
5+
46
#include "back.hpp"
57
#include "front.hpp"
68
#include "pop_back.hpp"
@@ -16,7 +18,8 @@ namespace mplex
1618
* @return basically Function<TupleElements...>.
1719
*/
1820
template <typename Tuple, template <typename...> class Function>
19-
struct apply { };
21+
struct apply
22+
{ };
2023

2124
template <template <typename...> class Function, typename... List>
2225
struct apply <std::tuple <List...>, Function> {
@@ -36,6 +39,27 @@ namespace mplex
3639
using type = Function <List...>;
3740
};
3841

42+
template <typename ValueT, typename Sequence, template <ValueT...> class Function>
43+
struct apply_values
44+
{
45+
static_assert(std::is_same_v<Sequence,Sequence>, "Could not apply specialization with std::integer_sequence");
46+
};
47+
48+
template <typename ValueT, template <ValueT...> class Function, ValueT... List>
49+
struct apply_values <ValueT, std::integer_sequence <ValueT, List...>, Function> {
50+
using type = Function <List...>;
51+
};
52+
53+
struct apply_values_a {
54+
template <typename ValueT, typename Sequence, template <ValueT...> class Function>
55+
struct apply {};
56+
};
57+
58+
template <typename ValueT, template <ValueT...> class Function, ValueT... List>
59+
struct apply_values_a::apply <ValueT, std::integer_sequence <ValueT, List...>, Function> {
60+
using type = Function <List...>;
61+
};
62+
3963
struct apply_af {
4064
template <typename Tuple, typename Functor>
4165
struct apply {};

tuple/back.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ namespace mplex
1616
using type = typename std::tuple_element <std::tuple_size <Tuple>::value - 1, Tuple>::type;
1717
};
1818

19+
template <>
20+
struct back <std::tuple<>> {
21+
using type = void;
22+
};
23+
1924
struct back_a {
2025
template <typename Tuple>
2126
struct apply {

tuple/front.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ namespace mplex
1616
using type = typename std::tuple_element <0, Tuple>::type;
1717
};
1818

19+
template <>
20+
struct front <std::tuple<>> {
21+
using type = void;
22+
};
23+
1924
struct front_a {
2025
template <typename Tuple>
2126
struct apply {

0 commit comments

Comments
 (0)