-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.hpp
82 lines (70 loc) · 1.31 KB
/
util.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
// This file is covered by the LICENSE file in the root of this project.
#pragma once
#include <utility>
template<typename T>
struct Identity
{
using Type = T;
};
// Replaces (value) with (new_value) and returns the old value
template<typename T, typename U>
T exchange(T& value, U&& new_value)
{
auto old_value = std::move(value);
value = std::forward<U>(new_value);
return old_value;
}
template<typename T>
T power(T base, unsigned int exp)
{
T r = 1;
while (exp)
{
if (exp & 1u)
r *= base;
base *= base;
exp >>= 1;
}
return r;
}
template<typename T>
bool between(T value, typename Identity<T>::Type x, typename Identity<T>::Type y)
{
if (x < y)
return x <= value && value <= y;
else
return y <= value && value <= x;
}
template<typename T>
void sort2(T& x, T& y)
{
if (y < x)
std::swap(x, y);
}
// Returns the absolute value of the difference (x - y)
template<typename T>
T abs_diff(T x, T y)
{
return y > x ? y - x : x - y;
}
template<typename T>
std::pair<T, T> minmax(T x, T y)
{
using P = std::pair<T, T>;
return (y < x) ? P{y, x} : P{x, y};
}
template<typename T>
char to_lcase_letter(T n)
{
return static_cast<char>('a' + n);
}
template<typename T>
char to_ucase_letter(T n)
{
return static_cast<char>('A' + n);
}
template<typename T>
char to_digit(T n)
{
return static_cast<char>('0' + n);
}