2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Steve Raasch
31 template<class T, T MV>
36 // Compiler should optimize this
37 void setValue(T n) { value = n % MV; }
41 ModNum(T n) { setValue(n); }
42 ModNum(const ModNum<T, MV> &n) : value(n.value) {}
44 ModNum operator=(T n) {
49 const ModNum operator=(ModNum n) {
54 // Return the value if object used as RHS
55 operator T() const { return value; }
60 const ModNum<T, MV> operator+=(ModNum<T, MV> r) {
61 setValue(value + r.value);
65 const ModNum<T, MV> operator+=(T r) {
73 const ModNum<T, MV> operator-=(ModNum<T, MV> r) {
74 setValue(value - r.value);
78 const ModNum<T, MV> operator-=(T r) {
87 const ModNum<T, MV> operator++() {
93 const ModNum<T, MV> operator++(int) {
94 ModNum<T, MV> rv = *this;
105 const ModNum<T, MV> operator--() {
110 // POSTFIX (like a--)
111 const ModNum<T, MV> operator--(int) {
112 ModNum<T, MV> rv = *this;
120 // Define operator "+" like this to avoid creating a temporary
122 template<class T, T MV>
124 operator+(ModNum<T, MV> l, ModNum<T, MV> r) {
129 template<class T, T MV>
131 operator+(ModNum<T, MV> l, T r) {
136 template<class T, T MV>
138 operator+(T l, ModNum<T, MV> r) {
145 // Define operator "-" like this to avoid creating a temporary
147 template<class T, T MV>
149 operator-(ModNum<T, MV> l, ModNum<T, MV> r) {
154 template<class T, T MV>
156 operator-(ModNum<T, MV> l, T r) {
161 template<class T, T MV>
163 operator-(T l, ModNum<T, MV> r) {
170 // Comparison operators
171 // (all other cases are handled with conversons)
173 template<class T, T MV>
175 operator<(ModNum<T, MV> l, ModNum<T, MV> r) {
176 return l.value < r.value;
179 template<class T, T MV>
181 operator>(ModNum<T, MV> l, ModNum<T, MV> r) {
182 return l.value > r.value;
185 template<class T, T MV>
187 operator==(ModNum<T, MV> l, ModNum<T, MV> r) {
188 return l.value == r.value;
191 template<class T, T MV>
193 operator<=(ModNum<T, MV> l, ModNum<T, MV> r) {
194 return l.value <= r.value;
197 template<class T, T MV>
199 operator>=(ModNum<T, MV> l, ModNum<T, MV> r) {
200 return l.value >= r.value;