-
Notifications
You must be signed in to change notification settings - Fork 0
/
checked_numbers.h
160 lines (129 loc) · 4.54 KB
/
checked_numbers.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// Created by appolinariya on 17.10.16.
//
#ifndef CHECKED_NUMBERS_CHECKED_NUMBERS_H
#define CHECKED_NUMBERS_CHECKED_NUMBERS_H
#include <assert.h>
#include <limits>
struct tag_s {
};
struct tag_u {
};
template <bool T>
struct traits {
typedef tag_s tag;
};
template <>
struct traits <false> {
typedef tag_u tag;
};
template <typename T>
T check_add(T left, T right, tag_s) {
assert((left >= 0 && (right <= 0 || left <= std::numeric_limits<T>::max() - right)) ||
(left < 0 && (right >= 0 || left >= std::numeric_limits<T>::min() - right)));
return left + right;
}
template <typename T>
T check_add(T left, T right, tag_u) {
assert(right >= 0 && left <= std::numeric_limits<T>::max() - right);
return left + right;
}
template <typename T>
T check_sub(T left, T right, tag_s) {
assert((left >= 0 && (right >= 0 || left <= std::numeric_limits<T>::max() + right)) ||
(right < 0 || left >= std::numeric_limits<T>::min() + right));
return left - right;
}
template <typename T>
T check_sub(T left, T right, tag_u) {
assert(right >= 0 && left >= right);
return left - right;
}
template <typename T>
T check_mul(T left, T right, tag_s) {
assert((left == 0 || right == 0) ||
(left < 0 && ((right > 0 && left >= std::numeric_limits<T>::min() / right) ||
(left >= std::numeric_limits<T>::max() / right))) ||
(left > 0 && ((right > 0 && left <= std::numeric_limits<T>::max() / right) ||
(right >= std::numeric_limits<T>::min() / left))));
return left * right;
}
template <typename T>
T check_mul(T left, T right, tag_u) {
assert(right == 0 || left <= std::numeric_limits<T>::max() / right);
return left * right;
}
template <typename T>
T check_div(T left, T right, tag_s) {
assert(right != 0 && ((left <= 0 && ((right > 0 && (right > 1 || left >= std::numeric_limits<T>::min() * right))
|| (right < -1 || left >= std::numeric_limits<T>::max() * right)))
|| ((right > 0 && (right > 1 || left <= std::numeric_limits<T>::max() * right))
|| (right < -1 || left <= std::numeric_limits<T>::min() * right))));
return left / right;
}
template <typename T>
T check_div(T left, T right, tag_u) {
assert(right >= 0 && (right > 1 || left <= std::numeric_limits<T>::max() * right));
return left / right;
}
template <typename T>
T check_unary(T right, tag_s) {
assert(right != std::numeric_limits<T>::min());
return -right;
}
template <typename T>
T check_unary(T right, tag_u) {
assert(right == 0);
return -right;
}
template <typename T>
struct checked {
checked() : value() {
}
checked(T v) : value(v) {
}
checked(checked<T>& other) : value(other.value) {
}
checked operator+=(checked& other) {
value = check_add(value, other.value, typename traits<std::numeric_limits<T>::is_signed>::tag());
return *this;
}
checked operator-=(checked& other) {
value = check_sub(value, other.value, typename traits<std::numeric_limits<T>::is_signed>::tag());
return *this;
}
checked operator*=(checked& other) {
value = check_mul(value, other.value, typename traits<std::numeric_limits<T>::is_signed>::tag());
return *this;
}
checked operator/=(checked& other) {
value = check_div(value, other.value, typename traits<std::numeric_limits<T>::is_signed>::tag());
return *this;
}
checked operator-() {
value = check_unary(value, typename traits<std::numeric_limits<T>::is_signed>::tag());
return *this;
}
checked operator+(checked& other) {
checked *tmp = new checked(check_add(value,other.value, typename traits<std::numeric_limits<T>::is_signed>::tag()));
return *tmp;
}
checked operator-(checked& other) {
checked *tmp = new checked(check_sub(value,other.value, typename traits<std::numeric_limits<T>::is_signed>::tag()));
return *tmp;
}
checked operator*(checked& other) {
checked *tmp = new checked(check_mul(value,other.value, typename traits<std::numeric_limits<T>::is_signed>::tag()));
return *tmp;
}
checked operator/(checked& other) {
checked *tmp = new checked(check_div(value,other.value, typename traits<std::numeric_limits<T>::is_signed>::tag()));
return *tmp;
}
void write() {
std::cout << value;
}
private:
T value;
};
#endif //CHECKED_NUMBERS_CHECKED_NUMBERS_H