library of assembled shared sources

http://lass.cocamware.com

modulo.inl

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 
00044 
00045 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_MODULO_INL
00046 #define LASS_GUARDIAN_OF_INCLUSION_NUM_MODULO_INL
00047 
00048 #include "num_common.h"
00049 #include "modulo.h"
00050 #include "basic_ops.h"
00051 
00052 
00053 
00054 namespace lass
00055 {
00056 namespace num
00057 {
00058 
00059 // --- public --------------------------------------------------------------------------------------
00060 
00061 template <unsigned N, typename T>
00062 Modulo<N, T>::Modulo(TParam iValue):
00063     value_(mod(iValue, N))
00064 {
00065     LASS_ASSERT(static_cast<T>(N) > 0 && static_cast<T>(N) < NumTraits<T>::max);
00066 }
00067 
00068 
00069 
00070 template <unsigned N, typename T> inline
00071 Modulo<N, T> Modulo<N, T>::operator+() const
00072 {
00073     return *this;
00074 }
00075 
00076 
00077 
00078 template <unsigned N, typename T>
00079 Modulo<N, T> Modulo<N, T>::operator-() const
00080 {
00081     return Modulo<N, T>(-value_);
00082 }
00083 
00084 
00085 
00086 template <unsigned N, typename T>
00087 Modulo<N, T>& Modulo<N, T>::operator++()
00088 {
00089     ++value_;
00090     if (value_ == N)
00091     {
00092         value_ = 0;
00093     }
00094     LASS_ASSERT(isInRange(value_));
00095     return *this;
00096 }
00097 
00098 
00099 
00100 template <unsigned N, typename T>
00101 Modulo<N, T>& Modulo<N, T>::operator--()
00102 {
00103     if (value_ == 0)
00104     {
00105         value_ = N;
00106     }
00107     --value_;
00108     LASS_ASSERT(isInRange(value_));
00109     return *this;
00110 }
00111 
00112 
00113 
00114 template <unsigned N, typename T>
00115 Modulo<N, T> Modulo<N, T>::operator++(int)
00116 {
00117     Modulo<N, T> result(*this);
00118     ++*this;
00119     return result;
00120 }
00121 
00122 
00123 
00124 template <unsigned N, typename T>
00125 Modulo<N, T> Modulo<N, T>::operator--(int)
00126 {
00127     Modulo<N, T> result(*this);
00128     --*this;
00129     return result;
00130 }
00131 
00132 
00133 
00134 template <unsigned N, typename T>
00135 Modulo<N, T>& Modulo<N, T>::operator+=(const Modulo<N, T>& iOther)
00136 {
00137     value_ += iOther.value_;
00138     if (value_ >= static_cast<T>(N))
00139     {
00140         value_ -= N;
00141     }
00142     LASS_ASSERT(isInRange(value_));
00143     return *this;
00144 }
00145 
00146 
00147 
00148 template <unsigned N, typename T>
00149 Modulo<N, T>& Modulo<N, T>::operator-=(const Modulo<N, T>& iOther)
00150 {
00151     value_ -= iOther.value_;
00152     if (value_ < 0)
00153     {
00154         value_ += N;
00155     }
00156     LASS_ASSERT(isInRange(value_));
00157     return *this;
00158 }
00159 
00160 
00161 
00162 template <unsigned N, typename T>
00163 Modulo<N, T>& Modulo<N, T>::operator*=(const Modulo<N, T>& iOther)
00164 {
00165     value_ *= iOther.value_;
00166     value_ = mod(value_, N);
00167     LASS_ASSERT(isInRange(value_));
00168     return *this;
00169 }
00170 
00171 
00172 
00173 template <unsigned N, typename T>
00174 Modulo<N, T>& Modulo<N, T>::operator/=(const Modulo<N, T>& iOther)
00175 {
00176     value_ /= iOther.value_;
00177     value_ = mod(value_, N);
00178     LASS_ASSERT(isInRange(value_));
00179     return *this;
00180 }
00181 
00182 
00183 
00184 template <unsigned N, typename T> inline
00185 Modulo<N, T>::operator T() const
00186 {
00187     return value_;
00188 }
00189 
00190 
00191 
00192 template <unsigned N, typename T> inline
00193 typename Modulo<N, T>::TParam Modulo<N, T>::value() const
00194 {
00195     return value_;
00196 }
00197 
00198 
00199 
00200 // --- protected -----------------------------------------------------------------------------------
00201 
00202 
00203 
00204 // --- private -------------------------------------------------------------------------------------
00205 
00206 template <unsigned N, typename T>
00207 bool Modulo<N, T>::isInRange(TParam iValue) const
00208 {
00209     return iValue >= 0 && iValue < static_cast<T>(N);
00210 }
00211 
00212 
00213 
00214 // --- free ----------------------------------------------------------------------------------------
00215 
00216 template <unsigned N, typename T>
00217 bool operator==(const Modulo<N, T>& iA, const Modulo<N, T>& iB)
00218 {
00219     return iA.value() == iB.value();
00220 }
00221 
00222 
00223 
00224 template <unsigned N, typename T>
00225 bool operator!=(const Modulo<N, T>& iA, const Modulo<N, T>& iB)
00226 {
00227     return !(iA == iB);
00228 }
00229 
00230 
00231 
00232 template <unsigned N, typename T>
00233 bool operator<(const Modulo<N, T>& iA, const Modulo<N, T>& iB)
00234 {
00235     return iA.value() < iB.value_();
00236 }
00237 
00238 
00239 
00240 template <unsigned N, typename T>
00241 bool operator>(const Modulo<N, T>& iA, const Modulo<N, T>& iB)
00242 {
00243     return iB < iA;
00244 }
00245 
00246 
00247 
00248 template <unsigned N, typename T>
00249 bool operator<=(const Modulo<N, T>& iA, const Modulo<N, T>& iB)
00250 {
00251     return !(iB < iA);
00252 }
00253 
00254 
00255 
00256 template <unsigned N, typename T>
00257 bool operator>=(const Modulo<N, T>& iA, const Modulo<N, T>& iB)
00258 {
00259     return !(iA < iB);
00260 }
00261 
00262 
00263 
00264 template <unsigned N, typename T>
00265 std::ostream& operator<<(std::ostream& oS, Modulo<N, T> iM)
00266 {
00267     oS << iM.value();
00268     return oS;
00269 }
00270 
00271 
00272 
00273 template <unsigned N, typename T>
00274 std::istream& operator>>(std::istream& iS, Modulo<N, T>& iM)
00275 {
00276     int result;
00277     LASS_ENFORCE(iS) >> result;
00278     iM = Modulo<N, T>(result);
00279     return iS;
00280 }
00281 
00282 
00283 
00284 }
00285 
00286 }
00287 
00288 #endif
00289 
00290 // EOF

Generated on Mon Nov 10 14:20:31 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo