Vaucanson  1.4.1
bitset.hh
Go to the documentation of this file.
1 // bitset.hh: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 #ifndef VCSN_MISC_BITSET_HH
18 # define VCSN_MISC_BITSET_HH
19 
28 # include <iterator>
29 # include <iostream>
30 # include <functional>
31 
32 namespace vcsn
33 {
34  namespace misc
35  {
36 
45  struct Bitset
46  {
47  typedef int key_type;
48  typedef int value_type;
49  typedef std::less<key_type> key_compare;
50  typedef key_compare value_compare;
51  // FIXME: Missing to be std::set compliant: allocator_type.
52  // FIXME: reference and const_reference are not real references, because
53  // it would prevents a code using a reverse_iterator to compile.
54  typedef value_type reference;
55  typedef const value_type const_reference;
56  struct iterator;
57  struct const_iterator;
58  typedef unsigned int size_type;
59  typedef int difference_type;
60  typedef value_type* pointer;
61  typedef const value_type* const_pointer;
62  typedef std::reverse_iterator<iterator> reverse_iterator;
63  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
64 
65  // data_type is the type used for the bit field.
66  typedef unsigned int data_type;
67 
68  // FIXME: Constructors have almost nothing common to std::set.
69 
75  Bitset (size_type max);
76 
78  Bitset (size_type max, const data_type* data);
79 
81  Bitset (const Bitset& bs);
82 
84  template <class InputIterator>
85  Bitset (InputIterator first, InputIterator last);
86 
88  ~Bitset ();
89 
91  Bitset& operator= (const Bitset& rhs);
92 
94 
95  iterator begin ();
96  const_iterator begin () const;
97  iterator end ();
98  const_iterator end () const;
101 
102 
103  reverse_iterator rbegin ();
104  const_reverse_iterator rbegin () const;
105  reverse_iterator rend ();
106  const_reverse_iterator rend () const;
109  bool empty () const;
110  size_type size () const;
111  size_type max_size () const;
112 
114 
115  std::pair<iterator, bool> insert (const value_type& x);
116  iterator insert (iterator position, const value_type& x);
117  template <class InputIterator>
118  void insert (InputIterator first,
119  InputIterator last);
122 
123 
124  void erase (iterator position);
125  size_type erase (const key_type& x);
126  void erase (iterator first, iterator last);
129 
130  void swap (Bitset& other);
131 
133  void clear ();
134 
135  key_compare key_comp () const;
136  value_compare value_comp () const;
137 
139  iterator find (const key_type& x) const;
140 
142  size_type count (const key_type& x) const;
143 
144  iterator lower_bound (const key_type& x) const;
145  iterator upper_bound (const key_type& x) const;
146  std::pair<iterator, iterator> equal_range (const key_type& x) const;
147 
148  bool operator== (const Bitset& rhs) const;
149  bool operator< (const Bitset& rhs) const;
150  bool operator!= (const Bitset& rhs) const;
151  bool operator> (const Bitset& rhs) const;
152  bool operator>= (const Bitset& rhs) const;
153  bool operator<= (const Bitset& rhs) const;
154 
155  Bitset operator& (const Bitset& rhs) const;
156  Bitset operator| (const Bitset& rhs) const;
157 
159  bool operator[] (const key_type& x) const;
160 
162  Bitset project (const Bitset& b) const;
163 
171  Bitset unproject (const Bitset& b) const;
172 
174  unsigned to_unsigned () const;
175 
177  template <typename Type>
178  const Type cast () const;
179 
181  std::ostream& print (std::ostream& ostr) const;
182 
183  protected:
185  static
186  size_type get_data_size (size_type max);
187 
189  static
190  size_type get_index (const key_type& x);
191 
193  static
194  size_type get_bitnum (const key_type& x);
195 
197  bool get_bit (size_type index, size_type bit) const;
198 
200  size_type compute_size () const;
201 
202  /*-------------------.
203  | Iterator on bits. |
204  `-------------------*/
205  struct bit_iterator
206  {
207  bit_iterator (size_type index = 0, size_type bitnum = 0);
208  const bit_iterator& operator-- ();
209  const bit_iterator& operator++ ();
210  value_type operator* () const;
211  bool operator== (const bit_iterator& rhs) const;
212  bool operator!= (const bit_iterator& rhs) const;
213  size_type get_index () const;
214  size_type get_bitnum () const;
215  protected:
216  size_type index_;
217  size_type bitnum_;
218  value_type value_;
219  };
220 
221  bit_iterator bit_begin () const;
222  const bit_iterator& bit_end () const;
223 
225  bool get_bit (const bit_iterator& it) const;
226 
227  /*------------------.
228  | Actions on bits. |
229  `------------------*/
230 
231  template <typename CountType, CountType Start, CountType Step>
232  struct BitActionCount
233  {
234  BitActionCount ();
235  CountType value;
236  bool operator() (const Bitset& bitset,
237  size_type index,
238  size_type bit,
239  bool value);
240  };
241 
242  /*--------------------------------.
243  | do_on_bit (BitAction, element_t) |
244  `--------------------------------*/
245 
247 
248  template <class BitAction>
249  bool do_on_bit (BitAction& action, const key_type& x);
250  template <class BitAction>
251  bool do_on_bit (BitAction& action, const bit_iterator& it);
254 
255 
256  template <class ConstBitAction>
257  bool do_on_bit (ConstBitAction& action, const key_type& x) const;
258  template <class ConstBitAction>
259  bool do_on_bit (ConstBitAction& action,
260  const bit_iterator& it) const;
263  /*-------------------.
264  | Attributes, misc. |
265  `-------------------*/
266 
267  enum { invalid_size = static_cast <unsigned int> (-1) };
268 
269  size_type data_size_;
270  data_type* data_;
271 
272  size_type max_size_;
273  mutable size_type size_;
274  bit_iterator end_;
275 
276  public:
277 
278  /*-----------------.
279  | const_iterator. |
280  `-----------------*/
281 
282  struct const_iterator
283  {
284  typedef std::bidirectional_iterator_tag iterator_category;
285  typedef Bitset::value_type value_type;
286  typedef Bitset::difference_type difference_type;
287  typedef Bitset::reference reference;
288  typedef Bitset::pointer pointer;
289 
290  const_iterator ();
291  const_iterator (const Bitset* bs);
292  const_iterator (const Bitset* bs, const bit_iterator& cbit);
293  const_iterator (const const_iterator& it);
294  const_iterator (const iterator& it);
295 
296  const const_iterator& operator++ ();
297  const_iterator operator++ (int);
298  const const_iterator& operator-- ();
299  const_iterator operator-- (int);
300  bool operator== (const const_iterator& rhs) const;
301  bool operator== (const iterator& rhs) const;
302  bool operator!= (const const_iterator& rhs) const;
303  bool operator!= (const iterator& rhs) const;
304  value_type operator* () const;
305 
306  protected:
307  void skip_zeros_forward ();
308  void skip_zeros_backward ();
309 
310  const Bitset* bs_;
311  bit_iterator cbit_;
312  };
313 
314  /*-----------.
315  | iterator. |
316  `-----------*/
317 
318  struct iterator
319  {
320  typedef std::bidirectional_iterator_tag iterator_category;
321  typedef Bitset::value_type value_type;
322  typedef Bitset::difference_type difference_type;
323  typedef Bitset::reference reference;
324  typedef Bitset::pointer pointer;
325 
326  iterator ();
327  iterator (const Bitset* bs);
328  iterator (const Bitset* bs, const bit_iterator& cbit);
329  iterator (const iterator& it);
330 
331  // Friend functions for const_iterator
332  friend const_iterator::const_iterator (const iterator& it);
333 
334  const iterator& operator++ ();
335  iterator operator++ (int);
336  const iterator& operator-- ();
337  iterator operator-- (int);
338  bool operator== (const iterator& rhs) const;
339  bool operator== (const const_iterator& rhs) const;
340  bool operator!= (const iterator& rhs) const;
341  bool operator!= (const const_iterator& rhs) const;
342  value_type operator* () const;
343 
344  protected:
345  void skip_zeros_forward ();
346  void skip_zeros_backward ();
347 
348  const Bitset* bs_;
349  bit_iterator cbit_;
350  };
351  };
352 
353 
355  std::ostream&
356  operator<< (std::ostream& ostr, const Bitset& set);
357 
358  } // end of namespace misc.
359 } // end of namespace vcsn.
360 
361 namespace std
362 {
364  template <>
365  void swap (vcsn::misc::Bitset& lhs, vcsn::misc::Bitset& rhs);
366 
368  template <>
369  class insert_iterator<vcsn::misc::Bitset> :
370  public iterator<output_iterator_tag, void, void, void, void>
371  {
372  public:
374 
375  insert_iterator (vcsn::misc::Bitset& x, vcsn::misc::Bitset::iterator);
377  operator= (vcsn::misc::Bitset::const_reference value);
380  insert_iterator<vcsn::misc::Bitset>& operator++ (int);
381  protected:
382  vcsn::misc::Bitset* container;
383  };
384 }
385 
386 
387 # if !defined VCSN_USE_INTERFACE_ONLY || defined VCSN_USE_LIB
388 # include <vaucanson/misc/bitset.hxx>
389 # endif // VCSN_USE_INTERFACE_ONLY
390 
391 
392 #endif // ! VCSN_MISC_BITSET_HH