Debian Codegraph @

src/util.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2016, Mike Walters <[email protected]>
3 * Copyright (C) 2016, Jared Boone, ShareBrained Technology, Inc.
4 *
5 * This file is part of inspectrum.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #pragma once
22 #include <algorithm>
23 #include <complex>
24 #include <map>
25 #include <math.h>
26 #include <sstream>
27
28 static const double Tau = M_PI * 2.0;
29
30 template <class T> const T& clamp (const T& value, const T& min, const T& max)
31 {
32 return std::min(max, std::max(min, value));
33 }
34
35 template<class Iter>
36 struct iter_pair_range : std::pair<Iter,Iter> {
37 iter_pair_range(std::pair<Iter,Iter> const& x) : std::pair<Iter,Iter>(x) { }
38 Iter begin() const {
39 return this->first;
40 }
41 Iter end() const {
42 return this->second;
43 }
44 };
45
46 template<class Iter>
47 inline iter_pair_range<Iter> as_range(std::pair<Iter,Iter> const& x)
48 {
49 return iter_pair_range<Iter>(x);
50 }
51
52 template<class T>
53 struct range_t {
54 T minimum;
55 T maximum;
56
57 range_t<T>& operator=(const range_t<T> &other) {
58 minimum = other.minimum;
59 maximum = other.maximum;
60 return *this;
61 }
62
63 range_t<T>& operator=(const std::initializer_list<T> &other) {
64 if (other.size() == 2) {
65 minimum = *other.begin();
66 maximum = *(other.begin() + 1);
67 }
68 return *this;
69 }
70
71 const T length() {
72 return maximum - minimum;
73 }
74
75 const T& clip(const T& value) const {
76 return clamp(value, minimum, maximum);
77 }
78
79 void reset_if_outside(T& value, const T& reset_value) const {
80 if( (value < minimum ) ||
81 (value > maximum ) ) {
82 value = reset_value;
83 }
84 }
85
86 bool below_range(const T& value) const {
87 return value < minimum;
88 }
89
90 bool contains(const T& value) const {
91 // TODO: Subtle gotcha here! Range test doesn't include maximum!
92 return (value >= minimum) && (value < maximum);
93 }
94
95 bool out_of_range(const T& value) const {
96 // TODO: Subtle gotcha here! Range test in contains() doesn't include maximum!
97 return !contains(value);
98 }
99 };
100
101 std::string formatSIValue(float value);
102
103 template<typename T> const char* getFileNameFilter();