DGtal  1.4.2
ModuloComputer.ih
1 /**
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.
6  *
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.
11  *
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/>.
14  *
15  **/
16 
17 /**
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
21  *
22  * @date 2010/07/02
23  *
24  * Implementation of inline methods defined in ModuloComputer.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 //////////////////////////////////////////////////////////////////////////////
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
40 
41 
42 /**
43  * Initializes the modulo computer with the value [m].
44  * @param m any non-zero integer.
45  */
46 template <typename T>
47 inline
48 DGtal::ModuloComputer<T>::ModuloComputer( UnsignedIntegerParamType m )
49  : k( m )
50 {
51 }
52 
53 ///////////////////////////////////////////////////////////////////////////////
54 // Interface - public :
55 
56 
57 
58 /**
59  * Increment the value [i] modulo.
60  * @param i any value between 0 and [k] (excluded).
61  */
62 template <typename T>
63 inline
64 void
65 DGtal::ModuloComputer<T>::increment( UnsignedInteger & i ) const
66 {
67  if ( ++i == k ) i = 0;
68 }
69 
70 
71 /**
72  * Decrement the value [i] modulo.
73  * @param i any value between 0 and [k] (excluded).
74  */
75 template <typename T>
76 inline
77 void
78 DGtal::ModuloComputer<T>::decrement( UnsignedInteger & i ) const
79 {
80  if ( i == 0 ) i = k;
81  --i;
82 }
83 
84 
85 /**
86  * @param i any value between 0 and [k] (excluded).
87  * @return the incremented value of [i] modulo [k].
88  */
89 template <typename T>
90 inline
91 typename DGtal::ModuloComputer<T>::UnsignedInteger
92 DGtal::ModuloComputer<T>::next( UnsignedIntegerParamType i ) const
93 {
94 
95  return ( (i+1) == k ) ? 0 : (i+1);
96 }
97 
98 
99 /**
100  * @param i any value between 0 and [k] (excluded).
101  * @return the decremented value of [i] modulo [k].
102  */
103 template <typename T>
104 inline
105 typename DGtal::ModuloComputer<T>::UnsignedInteger
106 DGtal::ModuloComputer<T>::previous( UnsignedIntegerParamType i ) const
107 {
108  return ( i == 0 ) ? k - 1 : i - 1;
109 }
110 
111 
112 /**
113  * @param i any integer value.
114  * @return the value of [i] modulo [k].
115  */
116 template <typename T>
117 inline
118 typename DGtal::ModuloComputer<T>::UnsignedInteger
119 DGtal::ModuloComputer<T>::cast( IntegerParamType i ) const
120 {
121  Integer tmp = i;
122  while ( tmp < 0 ) tmp += k;
123  UnsignedInteger ip = (Integer) tmp;
124  while ( ip >= k ) ip -= k;
125  return ip;
126 }
127 
128 
129 /**
130  * Less comparator modulo. Be careful, modulo comparisons have no
131  * sense when the absolute difference of the values are around k / 2.
132  *
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
136  */
137 template <typename T>
138 inline
139 bool
140 DGtal::ModuloComputer<T>::less( UnsignedIntegerParamType i, UnsignedIntegerParamType j ) const
141 {
142  Integer d = ( (T) j ) - ( (T) i );
143  if ( d > 0 )
144  return d < (T) ( k / 2 );
145  else
146  return (-d) >= (T) ( k / 2 );
147 }
148 
149 
150 /**
151  * Performs j - i modulo, assuming less(i,j) is true.
152  *
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.
156  */
157 template <typename T>
158 inline
159 typename DGtal::ModuloComputer<T>::UnsignedInteger
160 DGtal::ModuloComputer<T>::posDiff( UnsignedIntegerParamType j, UnsignedIntegerParamType i ) const
161 {
162  return ( i <= j ) ? j - i : j + k - i;
163 }
164 
165 
166 
167 /**
168  * Writes/Displays the object on an output stream.
169  * @param out the output stream where the object is written.
170  */
171 template <typename T>
172 inline
173 void
174 DGtal::ModuloComputer<T>::selfDisplay ( std::ostream & out ) const
175 {
176  out << "[ModuloComputer]";
177 }
178 
179 /**
180  * Checks the validity/consistency of the object.
181  * @return 'true' if the object is valid, 'false' otherwise.
182  */
183 template <typename T>
184 inline
185 bool
186 DGtal::ModuloComputer<T>::isValid() const
187 {
188  return true;
189 }
190 
191 
192