2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU Lesser General Public License as
4 * published by the Free Software Foundation, either version 3 of the
5 * License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * @file ModuloComputer.ih
19 * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20 * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
24 * Implementation of inline methods defined in ModuloComputer.h
26 * This file is part of the DGtal library.
30 //////////////////////////////////////////////////////////////////////////////
32 //////////////////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
43 * Initializes the modulo computer with the value [m].
44 * @param m any non-zero integer.
48 DGtal::ModuloComputer<T>::ModuloComputer( UnsignedIntegerParamType m )
53 ///////////////////////////////////////////////////////////////////////////////
54 // Interface - public :
59 * Increment the value [i] modulo.
60 * @param i any value between 0 and [k] (excluded).
65 DGtal::ModuloComputer<T>::increment( UnsignedInteger & i ) const
67 if ( ++i == k ) i = 0;
72 * Decrement the value [i] modulo.
73 * @param i any value between 0 and [k] (excluded).
78 DGtal::ModuloComputer<T>::decrement( UnsignedInteger & i ) const
86 * @param i any value between 0 and [k] (excluded).
87 * @return the incremented value of [i] modulo [k].
91 typename DGtal::ModuloComputer<T>::UnsignedInteger
92 DGtal::ModuloComputer<T>::next( UnsignedIntegerParamType i ) const
95 return ( (i+1) == k ) ? 0 : (i+1);
100 * @param i any value between 0 and [k] (excluded).
101 * @return the decremented value of [i] modulo [k].
103 template <typename T>
105 typename DGtal::ModuloComputer<T>::UnsignedInteger
106 DGtal::ModuloComputer<T>::previous( UnsignedIntegerParamType i ) const
108 return ( i == 0 ) ? k - 1 : i - 1;
113 * @param i any integer value.
114 * @return the value of [i] modulo [k].
116 template <typename T>
118 typename DGtal::ModuloComputer<T>::UnsignedInteger
119 DGtal::ModuloComputer<T>::cast( IntegerParamType i ) const
122 while ( tmp < 0 ) tmp += k;
123 UnsignedInteger ip = (Integer) tmp;
124 while ( ip >= k ) ip -= k;
130 * Less comparator modulo. Be careful, modulo comparisons have no
131 * sense when the absolute difference of the values are around k / 2.
133 * @param i any value between 0 and [k] (excluded). @param j any
134 * value between 0 and [k] (excluded). @return 'true' if [i] strictly
135 * precedes [j] in a window 'floor([k]/2)'. @see k
137 template <typename T>
140 DGtal::ModuloComputer<T>::less( UnsignedIntegerParamType i, UnsignedIntegerParamType j ) const
142 Integer d = ( (T) j ) - ( (T) i );
144 return d < (T) ( k / 2 );
146 return (-d) >= (T) ( k / 2 );
151 * Performs j - i modulo, assuming less(i,j) is true.
153 * @param j any value between 0 and [k] (excluded).
154 * @param i any value between 0 and [k] (excluded).
155 * @return the value j - i, always positive.
157 template <typename T>
159 typename DGtal::ModuloComputer<T>::UnsignedInteger
160 DGtal::ModuloComputer<T>::posDiff( UnsignedIntegerParamType j, UnsignedIntegerParamType i ) const
162 return ( i <= j ) ? j - i : j + k - i;
168 * Writes/Displays the object on an output stream.
169 * @param out the output stream where the object is written.
171 template <typename T>
174 DGtal::ModuloComputer<T>::selfDisplay ( std::ostream & out ) const
176 out << "[ModuloComputer]";
180 * Checks the validity/consistency of the object.
181 * @return 'true' if the object is valid, 'false' otherwise.
183 template <typename T>
186 DGtal::ModuloComputer<T>::isValid() const