DGtal  1.5.beta
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)
35 #define Labels_RECURSES
36 
37 #if !defined Labels_h
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;
117  typedef Pointer pointer;
119  typedef std::forward_iterator_tag iterator_category;
120 
121  private:
126 
127  public:
132 
137 
143  ConstEnumerator( const Word* address, SizeType firstWord );
144 
150 
156  Self & operator= ( const Self & other );
157 
163 
169 
179  Self operator++( int );
180 
186  bool operator==( const Self & other ) const;
187 
193  bool operator!=( const Self & other ) const;
194 
195  };
196 
199 
200  // ----------------------- Standard services ------------------------------
201  public:
202 
207 
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 
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 
292 
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)
std::ptrdiff_t DifferenceType
only positive offsets allowed.
Definition: Labels.h:111
bool operator!=(const Self &other) const
ConstEnumerator Self
Definition: Labels.h:107
const Word * myWordAddress
current address of word
Definition: Labels.h:122
Self & operator=(const Self &other)
DifferenceType difference_type
Definition: Labels.h:116
ConstEnumerator(const Word *address, SizeType firstWord)
Word myWord
current word (modified until 0).
Definition: Labels.h:125
Label myLabel
current label
Definition: Labels.h:124
bool operator==(const Self &other) const
Label myWordLabel
index of first label of the current word.
Definition: Labels.h:123
std::forward_iterator_tag iterator_category
Definition: Labels.h:119
ConstEnumerator(const ConstEnumerator &other)
Aim: Stores a set of labels in {O..L-1} as a sequence of bits.
Definition: Labels.h:72
ConstIterator end() const
static SizeType _digit(Label l)
Returns the bit at which is the label l.
BOOST_CONCEPT_ASSERT((concepts::CUnsignedNumber< TWord >))
ConstIterator const_iterator
Definition: Labels.h:198
SizeType count() const
TWord Word
Definition: Labels.h:78
Self & reset()
Labels< L, Word > Self
Definition: Labels.h:79
bool isValid() const
Self & reset(Label l)
Label SizeType
Definition: Labels.h:81
void selfDisplay(std::ostream &out) const
bool test(Label l) const
SizeType index(Label l) const
unsigned int Label
Definition: Labels.h:80
ConstIterator begin() const
ConstEnumerator ConstIterator
Definition: Labels.h:197
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
static SizeType size()
void getLabels(std::vector< Label > &labels) const
BOOST_CONCEPT_ASSERT((concepts::CIntegralNumber< TWord >))
static SizeType _word(Label l)
Returns the word in which is the label l.
Self & set(Label l, bool val=true)
Self & operator=(const Self &other)
static Word _mask(Label l)
Returns the mask for the label l (1 << _digit( l )).
Labels(const Self &other)
BOOST_STATIC_ASSERT((L >=1))
Self & flip(Label l)
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & operator<<(std::ostream &out, const ATu0v1< TKSpace, TLinearAlgebra > &object)
Aim: Concept checking for Integral Numbers. Models of this concept should be listed in NumberTraits c...
Aim: Concept checking for Unsigned numbers. Models of this concept should be listed in NumberTraits c...