bitset.hh

Go to the documentation of this file.
00001 // bitset.hh: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2001, 2002, 2003, 2004, 2005 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 #ifndef VCSN_MISC_BITSET_HH
00018 # define VCSN_MISC_BITSET_HH
00019 
00028 # include <iterator>
00029 # include <iostream>
00030 
00031 namespace utility
00032 {
00033 
00042   struct Bitset
00043   {
00044     typedef int                                         key_type;
00045     typedef int                                         value_type;
00046     typedef std::less<key_type>                         key_compare;
00047     typedef key_compare                                 value_compare;
00048     // FIXME: Missing to be std::set compliant: allocator_type.
00049     // FIXME: reference and const_reference are not real references, because
00050     // it would prevents a code using a reverse_iterator to compile.
00051     typedef value_type                                  reference;
00052     typedef const value_type                            const_reference;
00053     struct                                              iterator;
00054     struct                                              const_iterator;
00055     typedef unsigned int                                size_type;
00056     typedef int                                         difference_type;
00057     typedef value_type*                                 pointer;
00058     typedef const value_type*                           const_pointer;
00059     typedef std::reverse_iterator<iterator>             reverse_iterator;
00060     typedef std::reverse_iterator<const_iterator>       const_reverse_iterator;
00061 
00062     // data_type is the type used for the bit field.
00063     typedef unsigned int                                data_type;
00064 
00065     // FIXME: Constructors have almost nothing common to std::set.
00066 
00072     Bitset(size_type max);
00073 
00075     Bitset(size_type max, const data_type* data);
00076 
00078     Bitset(const Bitset& bs);
00079 
00081     template <class InputIterator>
00082     Bitset(InputIterator first, InputIterator last);
00083 
00085     ~Bitset();
00086 
00088     Bitset&     operator = (const Bitset& rhs);
00089 
00091 
00092     iterator            begin();
00093     const_iterator      begin() const;
00094     iterator            end();
00095     const_iterator      end() const;
00098 
00099 
00100     reverse_iterator            rbegin();
00101     const_reverse_iterator      rbegin() const;
00102     reverse_iterator            rend();
00103     const_reverse_iterator      rend() const;
00106     bool        empty() const;
00107     size_type   size() const;
00108     size_type   max_size() const;
00109 
00111 
00112     std::pair<iterator, bool>   insert(const value_type& x);
00113     iterator                    insert(iterator position, const value_type& x);
00114     template <class InputIterator>
00115     void                        insert(InputIterator first,
00116                                        InputIterator last);
00119 
00120 
00121     void        erase(iterator position);
00122     size_type   erase(const key_type& x);
00123     void        erase(iterator first, iterator last);
00126 
00127     void        swap(Bitset& other);
00128 
00130     void        clear();
00131 
00132     key_compare         key_comp() const;
00133     value_compare       value_comp() const;
00134 
00136     iterator    find(const key_type& x) const;
00137 
00139     size_type   count(const key_type& x) const;
00140 
00141     iterator                            lower_bound(const key_type& x) const;
00142     iterator                            upper_bound(const key_type& x) const;
00143     std::pair<iterator, iterator>       equal_range(const key_type& x) const;
00144 
00145     bool        operator == (const Bitset& rhs) const;
00146     bool        operator < (const Bitset& rhs) const;
00147     bool        operator != (const Bitset& rhs) const;
00148     bool        operator > (const Bitset& rhs) const;
00149     bool        operator >= (const Bitset& rhs) const;
00150     bool        operator <= (const Bitset& rhs) const;
00151 
00152     Bitset      operator & (const Bitset& rhs) const;
00153     Bitset      operator | (const Bitset& rhs) const;
00154 
00156     bool        operator [] (const key_type& x) const;
00157 
00159     Bitset      project(const Bitset& b) const;
00160 
00168     Bitset      unproject(const Bitset& b) const;
00169 
00171     unsigned    to_unsigned() const;
00172 
00174     template <typename Type>
00175     const Type  cast() const;
00176 
00178     std::ostream&       print(std::ostream& ostr) const;
00179 
00180   protected:
00182     static
00183     size_type   get_data_size(size_type max);
00184 
00186     static
00187     size_type   get_index(const key_type& x);
00188 
00190     static
00191     size_type   get_bitnum(const key_type& x);
00192 
00194     bool        get_bit(size_type index, size_type bit) const;
00195 
00197     size_type   compute_size() const;
00198 
00199     /*-------------------.
00200     | Iterator on bits.  |
00201     `-------------------*/
00202     struct bit_iterator
00203     {
00204       bit_iterator(size_type index = 0, size_type bitnum = 0);
00205       const bit_iterator&       operator -- ();
00206       const bit_iterator&       operator ++ ();
00207       value_type                operator * () const;
00208       bool                      operator == (const bit_iterator& rhs) const;
00209       bool                      operator != (const bit_iterator& rhs) const;
00210       size_type                 get_index() const;
00211       size_type                 get_bitnum() const;
00212     protected:
00213       size_type         index_;
00214       size_type         bitnum_;
00215       value_type        value_;
00216     };
00217 
00218     bit_iterator        bit_begin() const;
00219     const bit_iterator& bit_end() const;
00220 
00222     bool        get_bit(const bit_iterator& it) const;
00223 
00224     /*------------------.
00225     | Actions on bits.  |
00226     `------------------*/
00227 
00228     template <typename CountType, CountType Start, CountType Step>
00229     struct BitActionCount
00230     {
00231       BitActionCount();
00232 
00233       CountType value;
00234 
00235       bool      operator () (const Bitset& bitset,
00236                              size_type index,
00237                              size_type bit,
00238                              bool value);
00239     };
00240 
00241     /*--------------------------------.
00242     | do_on_bit(BitAction, element_t) |
00243     `--------------------------------*/
00244 
00246 
00247     template <class BitAction>
00248     bool        do_on_bit(BitAction& action, const key_type& x);
00249     template <class BitAction>
00250     bool        do_on_bit(BitAction& action, const bit_iterator& it);
00253 
00254 
00255     template <class ConstBitAction>
00256     bool        do_on_bit(ConstBitAction& action, const key_type& x) const;
00257     template <class ConstBitAction>
00258     bool        do_on_bit(ConstBitAction& action,
00259                           const bit_iterator& it) const;
00262     /*-------------------.
00263     | Attributes, misc.  |
00264     `-------------------*/
00265 
00266     enum { invalid_size = static_cast <unsigned int> (-1) };
00267 
00268     size_type           data_size_;
00269     data_type*          data_;
00270 
00271     size_type           max_size_;
00272     mutable size_type   size_;
00273     bit_iterator        end_;
00274 
00275   public:
00276 
00277     /*-----------------.
00278     | const_iterator.  |
00279     `-----------------*/
00280 
00281     struct const_iterator
00282     {
00283       typedef std::bidirectional_iterator_tag   iterator_category;
00284       typedef Bitset::value_type                value_type;
00285       typedef Bitset::difference_type           difference_type;
00286       typedef Bitset::reference                 reference;
00287       typedef Bitset::pointer                   pointer;
00288 
00289       const_iterator();
00290       const_iterator(const Bitset* bs);
00291       const_iterator(const Bitset* bs, const bit_iterator& cbit);
00292       const_iterator(const const_iterator& it);
00293       const_iterator(const iterator& it);
00294 
00295       const const_iterator&     operator ++ ();
00296       const_iterator            operator ++ (int);
00297       const const_iterator&     operator -- ();
00298       const_iterator            operator -- (int);
00299       bool                      operator == (const const_iterator& rhs) const;
00300       bool                      operator == (const iterator& rhs) const;
00301       bool                      operator != (const const_iterator& rhs) const;
00302       bool                      operator != (const iterator& rhs) const;
00303       value_type                operator * () const;
00304 
00305     protected:
00306       void                      skip_zeros_forward();
00307       void                      skip_zeros_backward();
00308 
00309       const Bitset* bs_;
00310       bit_iterator cbit_;
00311     };
00312 
00313     /*-----------.
00314     | iterator.  |
00315     `-----------*/
00316 
00317     struct iterator
00318     {
00319       typedef std::bidirectional_iterator_tag   iterator_category;
00320       typedef Bitset::value_type                value_type;
00321       typedef Bitset::difference_type           difference_type;
00322       typedef Bitset::reference                 reference;
00323       typedef Bitset::pointer                   pointer;
00324 
00325       iterator();
00326       iterator(const Bitset* bs);
00327       iterator(const Bitset* bs, const bit_iterator& cbit);
00328       iterator(const iterator& it);
00329 
00330       // Friend functions for const_iterator
00331       friend const_iterator::const_iterator(const iterator& it);
00332 
00333       const iterator&   operator ++ ();
00334       iterator          operator ++ (int);
00335       const iterator&   operator -- ();
00336       iterator          operator -- (int);
00337       bool              operator == (const iterator& rhs) const;
00338       bool              operator == (const const_iterator& rhs) const;
00339       bool              operator != (const iterator& rhs) const;
00340       bool              operator != (const const_iterator& rhs) const;
00341       value_type        operator * () const;
00342 
00343     protected:
00344       void              skip_zeros_forward();
00345       void              skip_zeros_backward();
00346 
00347       const Bitset* bs_;
00348       bit_iterator cbit_;
00349     };
00350   };
00351 
00352 
00354   std::ostream&
00355   operator << (std::ostream& ostr, const Bitset& set);
00356 
00357 } // end of namespace utility
00358 
00359 namespace std
00360 {
00362   template <>
00363   void swap(utility::Bitset& lhs, utility::Bitset& rhs);
00364 
00366   template <>
00367   class insert_iterator<utility::Bitset> :
00368     public iterator<output_iterator_tag, void, void, void, void>
00369   {
00370   public:
00371     typedef utility::Bitset             container_type;
00372 
00373     insert_iterator(utility::Bitset& x, utility::Bitset::iterator);
00374 
00375     insert_iterator<utility::Bitset>&
00376     operator = (utility::Bitset::const_reference value);
00377 
00378     insert_iterator<utility::Bitset>&
00379     operator * ();
00380 
00381     insert_iterator<utility::Bitset>&
00382     operator ++ ();
00383 
00384     insert_iterator<utility::Bitset>&
00385     operator ++ (int);
00386   protected:
00387     utility::Bitset*            container;
00388   };
00389 }
00390 
00391 
00392 #ifndef VCSN_USE_INTERFACE_ONLY
00393     # include <vaucanson/misc/bitset.hxx>
00394 #endif // VCSN_USE_INTERFACE_ONLY
00395 
00396 
00397 #endif // ! VCSN_MISC_BITSET_HH

Generated on Fri Jul 28 12:18:30 2006 for Vaucanson by  doxygen 1.4.6