Library of Assembled Shared Sources
modulo.inl
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2024 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_MODULO_INL
46#define LASS_GUARDIAN_OF_INCLUSION_NUM_MODULO_INL
47
48#include "num_common.h"
49#include "modulo.h"
50#include "basic_ops.h"
51
52
53
54namespace lass
55{
56namespace num
57{
58
59// --- public --------------------------------------------------------------------------------------
60
61template <unsigned N, typename T>
62constexpr Modulo<N, T>::Modulo():
63 value_(0)
64{
65}
66
67
68
69template <unsigned N, typename T>
70constexpr Modulo<N, T>::Modulo(TParam value):
71 value_(static_cast<TValue>(mod(value, N)))
72{
73}
74
75
76
77template <unsigned N, typename T> inline
78constexpr Modulo<N, T> Modulo<N, T>::operator+() const
79{
80 return *this;
81}
82
83
84
85template <unsigned N, typename T>
86constexpr Modulo<N, T> Modulo<N, T>::operator-() const
87{
88 return Modulo<N, T>(
89 value_ == 0 ? 0 : static_cast<T>(N) - value_,
90 false);
91}
92
93
94
95template <unsigned N, typename T>
96constexpr Modulo<N, T>& Modulo<N, T>::operator++()
97{
98 value_ = value_ + 1 == static_cast<T>(N) ? 0 : value_ + 1;
99 LASS_ASSERT(isInRange(value_));
100 return *this;
101}
102
103
104
105template <unsigned N, typename T>
106constexpr Modulo<N, T>& Modulo<N, T>::operator--()
107{
108 value_ = value_ == 0 ? static_cast<T>(N) - 1 : value_ - 1;
109 LASS_ASSERT(isInRange(value_));
110 return *this;
111}
112
113
114
115template <unsigned N, typename T>
116constexpr Modulo<N, T> Modulo<N, T>::operator++(int)
117{
118 Modulo<N, T> result(*this);
119 ++*this;
120 return result;
121}
122
123
124
125template <unsigned N, typename T>
126constexpr Modulo<N, T> Modulo<N, T>::operator--(int)
127{
128 Modulo<N, T> result(*this);
129 --*this;
130 return result;
131}
132
133
134
135template <unsigned N, typename T>
136constexpr Modulo<N, T>& Modulo<N, T>::operator+=(const Modulo<N, T>& other)
137{
138 value_ += other.value_;
139 if (value_ >= static_cast<T>(N))
140 {
141 value_ -= N;
142 }
143 LASS_ASSERT(isInRange(value_));
144 return *this;
145}
146
147
148
149template <unsigned N, typename T>
150constexpr Modulo<N, T>& Modulo<N, T>::operator-=(const Modulo<N, T>& other)
151{
152 value_ -= other.value_;
153 if (value_ < 0)
154 {
155 value_ += N;
156 }
157 LASS_ASSERT(isInRange(value_));
158 return *this;
159}
160
161
162
163template <unsigned N, typename T>
164constexpr Modulo<N, T>& Modulo<N, T>::operator*=(const Modulo<N, T>& other)
165{
166 *this = TSelf(value_ * other.value_);
167 return *this;
168}
169
170
171
172/*
173template <unsigned N, typename T>
174constexpr Modulo<N, T>& Modulo<N, T>::operator/=(const Modulo<N, T>& other)
175{
176 value_ /= other.value_;
177 value_ = mod(value_, N);
178 LASS_ASSERT(isInRange(value_));
179 return *this;
180}
181*/
182
183
184// --- protected -----------------------------------------------------------------------------------
185
186
187
188// --- private -------------------------------------------------------------------------------------
189
190/** private constructor bypassing mod
191 */
192template <unsigned N, typename T>
193constexpr Modulo<N, T>::Modulo(TParam value, bool):
194 value_(value)
195{
196 LASS_ASSERT(isInRange(value));
197}
198
199
200
201template <unsigned N, typename T>
202constexpr bool Modulo<N, T>::isInRange(TParam value) const
203{
204 return value >= 0 && value < static_cast<T>(N);
205}
206
207
208
209// --- free ----------------------------------------------------------------------------------------
210
211template <unsigned N, typename T>
212constexpr bool operator==(const Modulo<N, T>& a, const Modulo<N, T>& b)
213{
214 return a.value() == b.value();
215}
216
217
218
219template <unsigned N, typename T>
220constexpr bool operator!=(const Modulo<N, T>& a, const Modulo<N, T>& b)
221{
222 return !(a == b);
223}
224
225
226
227template <unsigned N, typename T>
228constexpr bool operator<(const Modulo<N, T>& a, const Modulo<N, T>& b)
229{
230 return a.value() < b.value();
231}
232
233
234
235template <unsigned N, typename T>
236constexpr bool operator>(const Modulo<N, T>& a, const Modulo<N, T>& b)
237{
238 return b < a;
239}
240
241
242
243template <unsigned N, typename T>
244constexpr bool operator<=(const Modulo<N, T>& a, const Modulo<N, T>& b)
245{
246 return !(b < a);
247}
248
249
250
251template <unsigned N, typename T>
252constexpr bool operator>=(const Modulo<N, T>& a, const Modulo<N, T>& b)
253{
254 return !(a < b);
255}
256
257
258
259template <unsigned N, typename T>
260std::ostream& operator<<(std::ostream& stream, const Modulo<N, T>& a)
261{
262 stream << a.value();
263 return stream;
264}
265
266
267
268template <unsigned N, typename T>
269std::istream& operator>>(std::istream& stream, Modulo<N, T>& a)
270{
271 T result;
272 LASS_ENFORCE(stream) >> result;
273 a = Modulo<N, T>(result);
274 return stream;
275}
276
277
278
279}
280
281}
282
283#endif
284
285// EOF
numeric types and traits.
Definition basic_ops.h:70
Library for Assembled Shared Sources.
Definition config.h:53