-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcint.h
101 lines (83 loc) · 1.58 KB
/
cint.h
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
93
94
95
96
97
98
99
100
101
#pragma once
#include "cstring.h"
#include <type_traits>
namespace cstp
{
using uint = std::uint64_t;
using sint = std::int64_t;
template<class T>
inline constexpr int get_digit(T n)
{
return n < 10 ? 1 : 1 + get_digit(n / 10);
}
inline constexpr int is_neg(sint n)
{
return n < 0;
}
template<uint N, int D>
struct uint_to_cstr
{
const cstring<D> str = uint_to_cstr<N / 10, D - 1>().str + to_cstr(N % 10 + '0');
};
template<uint N>
struct uint_to_cstr<N, 1>
{
const cstring<1> str = N % 10 + '0';
};
template<uint N>
class cuint
{
public:
constexpr cuint() :
val_(N)
{
}
constexpr cstring<get_digit(N)> cstr() const
{
return uint_to_cstr<N, get_digit(N)>().str;
}
constexpr uint value() const
{
return val_;
}
private:
uint val_;
};
template<sint N, bool Neg>
class _csint_basic
{
public:
constexpr _csint_basic() :
val_(N)
{
}
constexpr sint value() const
{
return val_;
}
private:
sint val_;
};
template<sint N, bool Neg>
class _csint : public _csint_basic<N, Neg>
{
public:
constexpr cstring<get_digit(-N) + 1> cstr() const
{
return '-' + uint_to_cstr<-N, get_digit(-N)>().str;
}
};
template<sint N>
class _csint<N, false> : public _csint_basic<N, false>
{
public:
constexpr cstring<get_digit(N)> cstr() const
{
return cuint<N>().cstr();
}
};
template<sint N>
class csint : public _csint<N, is_neg(N)>
{
};
} // namespace cstp