DGtal  0.9.2
Labels.h
1 
17 #pragma once
18 
31 #if defined(Labels_RECURSES)
32 #error Recursive header files inclusion detected in Labels.h
33 #else // defined(Labels_RECURSES)
34 
35 #define Labels_RECURSES
36 
37 #if !defined Labels_h
38 
39 #define Labels_h
40 
42 // Inclusions
43 #include <iostream>
44 #include <vector>
45 #include "DGtal/base/Common.h"
46 #include "DGtal/kernel/CUnsignedNumber.h"
47 #include "DGtal/kernel/CIntegralNumber.h"
49 
50 namespace DGtal
51 {
52 
54  // template class Labels
70  template <unsigned int L, typename TWord = DGtal::uint8_t>
71  class Labels
72  {
73  BOOST_STATIC_ASSERT(( L >= 1 ));
76 
77  public:
78  typedef TWord Word;
80  typedef unsigned int Label;
81  typedef Label SizeType;
82 
83 #define __DGTAL_WORD_NBDIGITS ( sizeof( Word ) * 8 )
84 #define __DGTAL_LABELS_NBWORDS ( (L-1) / __DGTAL_WORD_NBDIGITS + 1 )
85 #define __DGTAL_LABEL_WORD_INDEX( label ) ( label / __DGTAL_WORD_NBDIGITS )
86 #define __DGTAL_LABEL_DIGIT_INDEX( label ) ( label % __DGTAL_WORD_NBDIGITS )
87 
88  protected:
89 
91  Word myLabels[ __DGTAL_LABELS_NBWORDS ];
92 
94  static SizeType _word( Label l );
96  static SizeType _digit( Label l );
98  static Word _mask( Label l );
99 
100  public:
106  public:
108  typedef Label Value;
109  typedef const Value* Pointer;
110  typedef const Value& Reference;
111  typedef std::ptrdiff_t DifferenceType;
112 
113  // ----------------------- std types ----------------------------------
114  typedef Value value_type;
115  typedef std::size_t size_type;
116  typedef DifferenceType difference_type;
117  typedef Pointer pointer;
118  typedef Reference reference;
119  typedef std::forward_iterator_tag iterator_category;
120 
121  private:
122  const Word* myWordAddress;
123  Label myWordLabel;
124  Label myLabel;
125  Word myWord;
126 
127  public:
132 
136  ConstEnumerator();
137 
143  ConstEnumerator( const Word* address, SizeType firstWord );
144 
149  ConstEnumerator( const ConstEnumerator & other );
150 
156  Self & operator= ( const Self & other );
157 
162  Reference operator*() const;
163 
168  Pointer operator->() const;
169 
174  Self& operator++();
179  Self operator++( int );
180 
186  bool operator==( const Self & other ) const;
187 
193  bool operator!=( const Self & other ) const;
194 
195  };
196 
197  typedef ConstEnumerator ConstIterator;
198  typedef ConstIterator const_iterator;
199 
200  // ----------------------- Standard services ------------------------------
201  public:
202 
206  ~Labels();
207 
211  Labels();
212 
217  Labels ( const Self & other );
218 
224  Self & operator= ( const Self & other );
225 
230  bool test( Label l ) const;
231 
232  // bitset<N>& set ( );
233  // bitset<N>& reset ( );
234 
241  Self& set ( Label l, bool val = true );
242 
248  Self& reset( Label l );
249 
255  Self& flip( Label l );
256 
261  Self& reset();
262 
266  SizeType count() const;
267 
272  static SizeType size();
273 
280  void getLabels( std::vector<Label> & labels ) const;
281 
286  SizeType index( Label l ) const;
287 
291  ConstIterator begin() const;
292 
296  ConstIterator end() const;
297 
298  // ----------------------- Interface --------------------------------------
299  public:
300 
305  void selfDisplay ( std::ostream & out ) const;
306 
311  bool isValid() const;
312 
313  // ------------------------- Protected Datas ------------------------------
314  private:
315  // ------------------------- Private Datas --------------------------------
316  private:
317 
318  // ------------------------- Hidden services ------------------------------
319  protected:
320 
321 
322  // ------------------------- Internals ------------------------------------
323  private:
324 
325  }; // end of class Labels
326 
327 
334  template <unsigned int L, typename TWord>
335  std::ostream&
336  operator<< ( std::ostream & out, const Labels<L, TWord> & object );
337 
338 } // namespace DGtal
339 
340 
342 // Includes inline functions.
343 #include "DGtal/base/Labels.ih"
344 
345 // //
347 
348 #endif // !defined Labels_h
349 
350 #undef Labels_RECURSES
351 #endif // else defined(Labels_RECURSES)
BOOST_STATIC_ASSERT((L >=1))
Aim: Stores a set of labels in {O..L-1} as a sequence of bits.
Definition: Labels.h:71
Aim: Concept checking for Unsigned numbers. Models of this concept should be listed in NumberTraits c...
void getLabels(std::vector< Label > &labels) const
static SizeType _word(Label l)
Returns the word in which is the label l.
Label myWordLabel
index of first label of the current word.
Definition: Labels.h:123
void selfDisplay(std::ostream &out) const
static SizeType size()
Self & reset()
ConstEnumerator ConstIterator
Definition: Labels.h:197
static SizeType _digit(Label l)
Returns the bit at which is the label l.
ConstIterator end() const
Self & set(Label l, bool val=true)
ConstEnumerator Self
Definition: Labels.h:107
Label SizeType
Definition: Labels.h:81
SizeType count() const
Reference operator*() const
BOOST_CONCEPT_ASSERT((concepts::CUnsignedNumber< TWord >))
Aim: Concept checking for Integral Numbers. Models of this concept should be listed in NumberTraits c...
bool operator!=(const Self &other) const
Word myLabels[__DGTAL_LABELS_NBWORDS]
Stores the labels as bits. The bit of index l is set if l is in the set.
Definition: Labels.h:91
Labels< L, Word > Self
Definition: Labels.h:79
bool isValid() const
DGtal is the top-level namespace which contains all DGtal functions and types.
Self & operator=(const Self &other)
Label myLabel
current label
Definition: Labels.h:124
std::forward_iterator_tag iterator_category
Definition: Labels.h:119
bool test(Label l) const
ConstIterator begin() const
Self & operator=(const Self &other)
bool operator==(const Self &other) const
std::ptrdiff_t DifferenceType
only positive offsets allowed.
Definition: Labels.h:111
ConstIterator const_iterator
Definition: Labels.h:198
TWord Word
Definition: Labels.h:78
const Word * myWordAddress
current address of word
Definition: Labels.h:122
SizeType index(Label l) const
DifferenceType difference_type
Definition: Labels.h:116
unsigned int Label
Definition: Labels.h:80
static Word _mask(Label l)
Returns the mask for the label l (1 << _digit( l )).
Self & flip(Label l)
Word myWord
current word (modified until 0).
Definition: Labels.h:125