File: range.hpp

package info (click to toggle)
giada 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,532 kB
  • sloc: cpp: 30,620; sh: 144; xml: 66; makefile: 55; ansic: 1
file content (150 lines) | stat: -rw-r--r-- 3,727 bytes parent folder | download
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
/* -----------------------------------------------------------------------------
 *
 * geompp - Basic geometrical utilities for C++.
 *
 * -----------------------------------------------------------------------------
 *
 * Copyright (C) 2021 Giovanni A. Zuliani | Monocasual Laboratories
 *
 * This file is part of geompp - Basic geometrical utilities for C++.
 *
 * geompp - Basic geometrical utilities for C++ 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.
 *
 * geompp - Basic geometrical utilities for C++ 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 geompp - Basic geometrical utilities for C++. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * -------------------------------------------------------------------------- */

#ifndef GEOMPP_RANGE_HH
#define GEOMPP_RANGE_HH

#include <cassert>
#include <utility>

namespace geompp
{
template <typename T>
class Range
{
public:
	constexpr Range() // Invalid default range
	: a(0)
	, b(0)
	{
	}

	constexpr Range(T a, T b)
	: a(a)
	, b(b)
	{
		assert(a < b);
	}

	bool operator==(const Range<T>& o) const { return a == o.a && b == o.b; }
	bool operator!=(const Range<T>& o) const { return !operator==(o); }

	/* operator * (and the mutating version below)
	Allows for multiplying by another type other than T, e.g. double when T
	is int witout an implicit cast of double to int. */

	template <typename U>
	Range<T> operator*(U m) const { return {static_cast<T>(a * m), static_cast<T>(b * m)}; }
	template <typename U>
	Range<T> operator*=(U m)
	{
		a = static_cast<T>(a * m);
		b = static_cast<T>(b * m);
		return *this;
	}

	/* operator / (and the mutating version below)
	See note above for the * operator. */

	template <typename U>
	Range<T> operator/(U m) const { return {static_cast<T>(a / m), static_cast<T>(b / m)}; }
	template <typename U>
	Range<T> operator/=(U m)
	{
		a = static_cast<T>(a / m);
		b = static_cast<T>(b / m);
		return *this;
	}

	Range<T> operator+(const T m) const { return {a + m, b + m}; }
	Range<T> operator+=(const T m)
	{
		a += m;
		b += m;
		return *this;
	}

	Range<T> operator-(const T m) const { return {a - m, b - m}; }
	Range<T> operator-=(const T m)
	{
		a -= m;
		b -= m;
		return *this;
	}

	T getLength() const { return b - a; }

	bool isValid() const
	{
		return a < b;
	}

	bool contains(T t) const
	{
		return t >= a && t < b;
	}

	/* intersects
	True if Range o intersects this one. */

	bool intersects(Range<T> o) const
	{
		return o.a < b && a < o.b;
	}

	/* contains
	True if Range o lies entirely inside this one. */

	bool contains(Range<T> o) const
	{
		return a <= o.a && b >= o.b;
	}

	/* getDifference
	Returns the difference of this Range with another one as two ranges. They
	might be invalid in some cases.
	See: https://en.wikipedia.org/wiki/Symmetric_difference */

	std::pair<Range<T>, Range<T>> getDifference(Range<T> o) const
	{
		if (!intersects(o))
			return {};
		// if (contains(o))
		//	return {{a, o.a}, {o.b, b}};

		Range<T> r1 = a == o.a ? Range<T>() : a < o.a ? Range(a, o.a)
		                                              : Range(o.a, a);
		Range<T> r2 = b == o.b ? Range<T>() : o.b < b ? Range(o.b, b)
		                                              : Range(b, o.b);

		return {r1, r2};
	}

	T a, b;
};
} // namespace geompp

#endif