comparison src/range.hpp @ 0:add35537fdbb tip

Initial import
author irh <ian.r.hobson@gmail.com>
date Thu, 25 Aug 2011 11:05:55 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:add35537fdbb
1 // Copyright 2011, Ian Hobson.
2 //
3 // This file is part of gpsynth.
4 //
5 // gpsynth is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // gpsynth is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with gpsynth in the file COPYING.
17 // If not, see http://www.gnu.org/licenses/.
18
19 #pragma once
20
21 #include <ostream>
22
23 namespace stdx {
24
25 // like std::pair, designed for minimum/maximum
26 template<typename T>
27 struct Range {
28 T minimum_;
29 T maximum_;
30
31 Range()
32 : minimum_(T()),
33 maximum_(T())
34 {}
35
36 Range(T minimum, T maximum)
37 : minimum_(minimum),
38 maximum_(maximum)
39 {}
40
41 template<typename T2>
42 Range(const Range<T2>& other)
43 : minimum_(other.minimum_),
44 maximum_(other.maximum_)
45 {}
46
47 // getters / setters
48 T Minimum() const { return minimum_; }
49 T Maximum() const { return maximum_; }
50
51 void SetMinimum(const T& value) { minimum_ = value; }
52 void SetMaximum(const T& value) { maximum_ = value; }
53
54 void SetRange(const T& minimum, const T& maximum) {
55 minimum_ = minimum;
56 maximum_ = maximum;
57 }
58
59 T Size() const { return maximum_ - minimum_; }
60
61 // Assignment operators
62 template<typename T2>
63 void operator=(const Range<T2>& other) {
64 minimum_ = other.minumum_;
65 maximum_ = other.maximum_;
66 }
67
68 template<typename T2>
69 Range<T>& operator+=(const Range<T2>& other) {
70 minimum_ += other.minimum_;
71 maximum_ += other.maximum_;
72 return *this;
73 }
74 template<typename T2>
75 Range<T>& operator+=(const T2& value) {
76 minimum_ += value;
77 maximum_ += value;
78 return *this;
79 }
80
81 template<typename T2>
82 Range<T>& operator-=(const Range<T2>& other) {
83 minimum_ -= other.minimum_;
84 maximum_ -= other.maximum_;
85 return *this;
86 }
87 template<typename T2>
88 Range<T>& operator-=(const T2& value) {
89 minimum_ -= value;
90 maximum_ -= value;
91 return *this;
92 }
93
94 template<typename T2>
95 Range<T>& operator*=(const T2& value) {
96 minimum_ *= value;
97 maximum_ *= value;
98 return *this;
99 }
100 template<typename T2>
101 Range<T>& operator/=(const T2& value) {
102 minimum_ /= value;
103 maximum_ /= value;
104 return *this;
105 }
106
107
108 // Arithmetic
109 template<typename T2>
110 Range<T> operator+(const T2& value) {
111 return Range<T>(minimum_ + value, maximum_ + value);
112 }
113 template<typename T2>
114 Range<T> operator+(const Range<T2>& other) {
115 return Range<T>(minimum_ + other.minimum_, maximum_ + other.maximum_);
116 }
117
118 template<typename T2>
119 Range<T> operator-(const T2& value) {
120 return Range<T>(minimum_ - value, maximum_ - value);
121 }
122 template<typename T2>
123 Range<T> operator-(const Range<T2>& other) {
124 return Range<T>(minimum_ - other.minimum_, maximum_ - other.maximum_);
125 }
126
127 template<typename T2>
128 Range<T> operator*(const T2& value) {
129 return Range<T>(minimum_ * value, maximum_ * value);
130 }
131 template<typename T2>
132 Range<T> operator/(const T2& value) {
133 return Range<T>(minimum_ / value, maximum_ / value);
134 }
135
136 // Increments/Decrements
137 Range<T>& operator++() {
138 ++minimum_;
139 ++maximum_;
140 return *this;
141 }
142 Range<T>& operator--() {
143 --minimum_;
144 --maximum_;
145 return *this;
146 }
147
148 Range<T> operator++(int) {
149 Range<T> temp(*this);
150 ++(*this);
151 return temp;
152 }
153 Range<T> operator--(int) {
154 Range<T> temp(*this);
155 --(*this);
156 return temp;
157 }
158
159 };
160
161 // Comparison operators
162 template<typename T>
163 bool operator==(const Range<T>& x, const Range<T>& y) {
164 return (x.minimum_ == y.minimum_) && (x.maximum_ == y.maximum_);
165 }
166
167 template<typename T>
168 bool operator<(const Range<T>& x, const Range<T>& y) {
169 return (x.minimum_ < y.minimum_)
170 || (!(y.minimum_ < x.minimum_) && (x.maximum_ < y.maximum_));
171 }
172
173 template<typename T>
174 bool operator!=(const Range<T>& x, const Range<T>& y) {
175 return !(x == y);
176 }
177
178 template<typename T>
179 bool operator>(const Range<T>& x, const Range<T>& y) {
180 return y < x;
181 }
182
183 template<typename T>
184 bool operator<=(const Range<T>& x, const Range<T>& y) {
185 return !(y < x);
186 }
187
188 template<typename T>
189 bool operator>=(const Range<T>& x, const Range<T>& y) {
190 return !(x < y);
191 }
192
193 // Output
194 template<typename T>
195 std::ostream& operator<<(std::ostream& out, const Range<T>& range) {
196 out << '[' << range.minimum_ << ',' << range.maximum_ << ']';
197 return out;
198 }
199
200 } // stdx namespace
201