DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Labels.ih
1 
30 
31 #include <cstdlib>
32 #include <algorithm>
33 #include "DGtal/base/Bits.h"
35 
37 // IMPLEMENTATION of inline methods.
39 
41 // class DGtal::Labels<L, TWord>::ConstEnumerator
42 
44 // ----------------------- Standard services ------------------------------
45 //-----------------------------------------------------------------------------
46 template <unsigned int L, typename TWord>
47 inline
50 {}
51 //-----------------------------------------------------------------------------
52 template <unsigned int L, typename TWord>
53 inline
56  : myWordAddress( 0 ), myLabel( 0 )
57 {}
58 //-----------------------------------------------------------------------------
59 template <unsigned int L, typename TWord>
60 inline
62 ConstEnumerator( const Word* address, SizeType firstWord )
63  : myWordAddress( address + firstWord ),
64  myWordLabel( firstWord * __DGTAL_WORD_NBDIGITS )
65 {
66  ASSERT( firstWord <= __DGTAL_LABELS_NBWORDS );
67  if ( firstWord != __DGTAL_LABELS_NBWORDS )
68  {
69  myWord = *myWordAddress++;
70  this->operator++(); // find first bit
71  }
72  else
73  {
74  myWord = 0;
75  myLabel = 0;
76  }
77 }
78 //-----------------------------------------------------------------------------
79 template <unsigned int L, typename TWord>
80 inline
83  : myWordAddress( other.myWordAddress ), myWordLabel( other.myWordLabel ),
84  myLabel( other.myLabel ), myWord( other.myWord )
85 {}
86 //-----------------------------------------------------------------------------
87 template <unsigned int L, typename TWord>
88 inline
91 operator= ( const Self & other )
92 {
93  if ( this != &other )
94  {
95  myWordAddress = other.myWordAddress;
96  myWordLabel = other.myWordLabel;
97  myLabel = other.myLabel;
98  myWord = other.myWord;
99  }
100  return *this;
101 }
102 //-----------------------------------------------------------------------------
103 template <unsigned int L, typename TWord>
104 inline
107 operator*() const
108 {
109  return myLabel;
110 }
111 //-----------------------------------------------------------------------------
112 template <unsigned int L, typename TWord>
113 inline
116 operator->() const
117 {
118  return &myLabel;
119 }
120 //-----------------------------------------------------------------------------
121 template <unsigned int L, typename TWord>
122 inline
126 {
127  ASSERT( ( myWordLabel < ( __DGTAL_LABELS_NBWORDS
128  * __DGTAL_WORD_NBDIGITS ) ) );
129  while ( myWord == 0 )
130  {
131  myWordLabel += __DGTAL_WORD_NBDIGITS;
132  if ( myWordLabel == ( __DGTAL_LABELS_NBWORDS
133  * __DGTAL_WORD_NBDIGITS ) )
134  {
135  myLabel = 0;
136  return *this;
137  }
138  myWord = *myWordAddress++;
139  }
140  ASSERT( myWord != 0 );
141  Word fsb = Bits::firstSetBit( myWord );
142  myLabel = myWordLabel + Bits::leastSignificantBit( fsb );
143  myWord -= fsb;
144  return *this;
145 }
146 //-----------------------------------------------------------------------------
147 template <unsigned int L, typename TWord>
148 inline
152 {
153  ConstEnumerator tmp( *this );
154  this->operator++();
155  return tmp;
156 }
157 //-----------------------------------------------------------------------------
158 template <unsigned int L, typename TWord>
159 inline
160 bool
162 operator==( const Self & other ) const
163 {
164  return ( myWordAddress == other.myWordAddress )
165  && ( myLabel == other.myLabel );
166 }
167 //-----------------------------------------------------------------------------
168 template <unsigned int L, typename TWord>
169 inline
170 bool
172 operator!=( const Self & other ) const
173 {
174  return ( myWordAddress != other.myWordAddress )
175  || ( myLabel != other.myLabel );
176 }
177 
178 
179 
180 //-----------------------------------------------------------------------------
181 template <unsigned int L, typename TWord>
182 inline
185 {
186  return __DGTAL_LABEL_WORD_INDEX( l );
187 }
188 //-----------------------------------------------------------------------------
189 template <unsigned int L, typename TWord>
190 inline
193 {
194  return __DGTAL_LABEL_DIGIT_INDEX( l );
195 }
196 //-----------------------------------------------------------------------------
197 template <unsigned int L, typename TWord>
198 inline
201 {
202  return ( (Word) 1 ) << _digit( l );
203 }
204 //-----------------------------------------------------------------------------
205 template <unsigned int L, typename TWord>
206 inline
208 {
209 }
210 //-----------------------------------------------------------------------------
211 template <unsigned int L, typename TWord>
212 inline
214 {
215  reset();
216 }
217 //-----------------------------------------------------------------------------
218 template <unsigned int L, typename TWord>
219 inline
221 {
222  std::copy( other.myLabels, other.myLabels + __DGTAL_LABELS_NBWORDS,
223  myLabels );
224 }
225 //-----------------------------------------------------------------------------
226 template <unsigned int L, typename TWord>
227 inline
230 {
231  if ( this != &other )
232  std::copy( other.myLabels, other.myLabels + __DGTAL_LABELS_NBWORDS,
233  myLabels );
234  return *this;
235 }
236 //-----------------------------------------------------------------------------
237 template <unsigned int L, typename TWord>
238 inline
241 {
242  Word* p1 = myLabels;
243  Word* p2 = myLabels + __DGTAL_LABELS_NBWORDS;
244  while ( p1 != p2 )
245  *p1++ = (Word) 0;
246  return *this;
247 }
248 //-----------------------------------------------------------------------------
249 template <unsigned int L, typename TWord>
250 inline
251 bool
253 {
254  return _mask( l ) & myLabels[ _word( l ) ];
255 }
256 //-----------------------------------------------------------------------------
257 template <unsigned int L, typename TWord>
258 inline
261 {
262  if ( val )
263  myLabels[ _word( l ) ] |= _mask( l );
264  else
265  myLabels[ _word( l ) ] &= ~_mask( l );
266  return *this;
267 }
268 //-----------------------------------------------------------------------------
269 template <unsigned int L, typename TWord>
270 inline
273 {
274  myLabels[ _word( l ) ] &= ~_mask( l );
275  return *this;
276 }
277 //-----------------------------------------------------------------------------
278 template <unsigned int L, typename TWord>
279 inline
282 {
283  myLabels[ _word( l ) ] ^= _mask( l );
284  return *this;
285 }
286 //-----------------------------------------------------------------------------
287 template <unsigned int L, typename TWord>
288 inline
291 count() const
292 {
293  SizeType n = 0;
294  const Word* p1 = myLabels;
295  const Word* p2 = myLabels + __DGTAL_LABELS_NBWORDS;
296  while ( p1 != p2 )
297  n += Bits::nbSetBits( *p1++ );
298  return n;
299 }
300 //-----------------------------------------------------------------------------
301 template <unsigned int L, typename TWord>
302 inline
306 {
307  return L;
308 }
309 //-----------------------------------------------------------------------------
310 template <unsigned int L, typename TWord>
311 inline
312 void
314 getLabels( std::vector<Label> & labels ) const
315 {
316  labels.clear();
317  register Word w;
318  register Word fsb;
319  const Word* p1 = myLabels;
320  const Word* p2 = myLabels + __DGTAL_LABELS_NBWORDS;
321  Label l = 0;
322  while ( p1 != p2 )
323  {
324  w = *p1++;
325  while ( w ) // extract labels within word
326  {
327  fsb = Bits::firstSetBit( w );
328  labels.push_back( l + Bits::leastSignificantBit( fsb ) );
329  w -= fsb;
330  ASSERT( index( labels.back() ) == ( labels.size() - 1 ) );
331  }
332  l += __DGTAL_WORD_NBDIGITS;
333  }
334 }
335 //-----------------------------------------------------------------------------
336 template <unsigned int L, typename TWord>
337 inline
340 index( Label l ) const
341 {
342  ASSERT( l < L );
343  const Word* p1 = myLabels;
344  const Word* p2 = myLabels + _word( l );
345  register SizeType i = 0;
346  while ( p1 != p2 )
347  i += Bits::nbSetBits( *p1++ );
348  return ( _mask( l ) & *p1 )
349  ? i + Bits::indexInSetBits( *p1, _digit( l ) ) - 1
350  : L;
351 }
352 //-----------------------------------------------------------------------------
353 template <unsigned int L, typename TWord>
354 inline
357 begin() const
358 {
359  return ConstIterator( myLabels, 0 );
360 }
361 //-----------------------------------------------------------------------------
362 template <unsigned int L, typename TWord>
363 inline
366 end() const
367 {
368  return ConstIterator( myLabels, __DGTAL_LABELS_NBWORDS );
369 }
370 
371 
373 // Interface - public :
374 
379 template <unsigned int L, typename TWord>
380 inline
381 void
382 DGtal::Labels<L, TWord>::selfDisplay ( std::ostream & out ) const
383 {
384  std::vector<Label> v;
385  this->getLabels( v );
386  if ( v.empty() )
387  out << "()";
388  else
389  {
390  out << "(" << v[ 0 ];
391  for ( SizeType i = 1; i < v.size(); ++i )
392  out << "," << v[ i ];
393  out << ")";
394  }
395 }
396 
401 template <unsigned int L, typename TWord>
402 inline
403 bool
405 {
406  return true;
407 }
408 
409 
410 
412 // Implementation of inline functions //
413 
414 template <unsigned int L, typename TWord>
415 inline
416 std::ostream&
417 DGtal::operator<< ( std::ostream & out,
418  const Labels<L, TWord> & object )
419 {
420  object.selfDisplay( out );
421  return out;
422 }
423 
424 // //
426 
427