-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathalg.h
More file actions
163 lines (142 loc) · 4.37 KB
/
alg.h
File metadata and controls
163 lines (142 loc) · 4.37 KB
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
161
162
163
/*
Copyright (C) 2017-2026 Topological Manifold
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "error.h"
#include "type/concept.h"
#include "type/limit.h"
#include <algorithm>
#include <cstddef>
namespace ns
{
template <typename T>
void sort_and_unique(T* const v)
{
std::sort(v->begin(), v->end());
v->erase(std::unique(v->begin(), v->end()), v->end());
}
template <typename T>
requires (!std::is_pointer_v<T>)
[[nodiscard]] T sort_and_unique(T v)
{
sort_and_unique(&v);
return v;
}
template <typename T>
[[nodiscard]] constexpr bool all_non_negative(const T& data)
{
return std::all_of(
data.cbegin(), data.cend(),
[](const auto& v)
{
return v >= 0;
});
}
template <typename T>
[[nodiscard]] constexpr bool all_positive(const T& data)
{
return std::all_of(
data.cbegin(), data.cend(),
[](const auto& v)
{
return v > 0;
});
}
template <typename T>
[[nodiscard]] constexpr bool all_negative(const T& data)
{
return std::all_of(
data.cbegin(), data.cend(),
[](const auto& v)
{
return v < 0;
});
}
template <typename Result, typename Data>
[[nodiscard]] constexpr Result multiply_all(const Data& data)
{
static_assert(
(Integral<typename Data::value_type> && Integral<Result>)
|| (FloatingPoint<typename Data::value_type> && FloatingPoint<Result>));
static_assert(Signed<typename Data::value_type> == Signed<Result>);
static_assert(Limits<typename Data::value_type>::digits() <= Limits<Result>::digits());
if (data.empty())
{
error("Empty container for multiply all");
}
Result res = data[0];
for (std::size_t i = 1; i < data.size(); ++i)
{
res *= data[i];
}
return res;
}
template <typename Result, typename Data>
[[nodiscard]] constexpr Result add_all(const Data& data)
{
static_assert(
(Integral<typename Data::value_type> && Integral<Result>)
|| (FloatingPoint<typename Data::value_type> && FloatingPoint<Result>));
static_assert(Signed<typename Data::value_type> == Signed<Result>);
static_assert(Limits<typename Data::value_type>::digits() <= Limits<Result>::digits());
if (data.empty())
{
error("Empty container for add all");
}
Result res = data[0];
for (std::size_t i = 1; i < data.size(); ++i)
{
res += data[i];
}
return res;
}
// template <typename T1, typename T2>
// [[nodiscard]] bool intersect(T1 t1, T2 t2)
// {
// std::sort(t1.begin(), t1.end());
// std::sort(t2.begin(), t2.end());
//
// auto i1 = std::cbegin(t1);
// auto i2 = std::cbegin(t2);
//
// while (true)
// {
// if (i1 == std::cend(t1))
// {
// return false;
// }
//
// if (i2 == std::cend(t2))
// {
// return false;
// }
//
// if (*i1 < *i2)
// {
// ++i1;
// }
// else if (*i1 > *i2)
// {
// ++i2;
// }
// else if (!(*i1 == *i2))
// {
// error("Failed to find intersection. Not (a < b) and not (a > b) and not (a == b)");
// }
// else
// {
// return true;
// }
// }
// }
}