17 #ifndef VCSN_MISC_BITSET_HXX
18 # define VCSN_MISC_BITSET_HXX
44 data_ (new data_type[data_size_]),
47 end_ (bit_iterator (get_index (max_size_),
48 get_bitnum (max_size_)))
50 memset (data_, 0, data_size_ *
sizeof (data_type));
55 : data_size_ (get_data_size (max)),
56 data_ (new data_type[data_size_]),
59 end_ (bit_iterator (get_index (max_size_),
60 get_bitnum (max_size_)))
62 memcpy (data_, data, data_size_ *
sizeof (data_type));
68 data_ (new data_type[data_size_]),
69 max_size_ (bs.max_size_),
73 memcpy (data_, bs.data_, data_size_ * sizeof (data_type));
77 template <
class InputIterator>
82 for (InputIterator i = first; i != last; ++i)
89 data_ =
new data_type[data_size_];
90 bzero (data_, data_size_ *
sizeof (data_type));
111 data_size_ = rhs.data_size_;
113 data_ =
new data_type[data_size_];
114 memcpy (data_, rhs.data_, data_size_ * sizeof (data_type));
115 max_size_ = rhs.max_size_;
119 postcondition (*
this == rhs);
132 return iterator (
this);
136 Bitset::const_iterator
139 return const_iterator (
this);
147 return iterator (
this, end_);
151 Bitset::const_iterator
154 return const_iterator (
this, end_);
163 Bitset::reverse_iterator
166 return reverse_iterator (
end ());
170 Bitset::const_reverse_iterator
173 return const_reverse_iterator (
end ());
178 Bitset::reverse_iterator
181 return reverse_iterator (
begin ());
185 Bitset::const_reverse_iterator
188 return const_reverse_iterator (
begin ());
198 Bitset::empty ()
const
206 Bitset::size ()
const
214 Bitset::max_size ()
const
224 std::pair<Bitset::iterator, bool>
227 precondition (static_cast<size_type> (x) < max_size_);
233 return std::make_pair (
end (),
false);
236 data_[idx] |= (1 << bnm);
237 if (size_ != invalid_size)
239 return std::make_pair (iterator (
this, bit_iterator (idx, bnm)),
true);
250 template <
class InputIterator>
254 while (first != last)
269 precondition (static_cast<size_type> (*position) < max_size_);
278 precondition (static_cast<size_type> (x) < max_size_);
285 data_[idx] &= ~ (1 << bnm);
286 if (size_ != invalid_size)
298 while (first != last)
314 std::swap (data_size_, other.data_size_);
326 bzero (data_, data_size_ *
sizeof (data_type));
333 Bitset::key_comp ()
const
335 return key_compare ();
340 Bitset::value_compare
341 Bitset::value_comp ()
const
343 return value_compare ();
351 precondition (static_cast<size_type> (x) < max_size_);
355 return iterator (
this, it);
357 return iterator (
this, end_);
365 precondition (static_cast<size_type> (x) < max_size_);
373 Bitset::lower_bound (
const key_type& x)
const
375 precondition (static_cast<size_type> (x) < max_size_);
379 while ( (it != bit_end ()) && !
get_bit (it))
381 return iterator (
this, it);
387 Bitset::upper_bound (
const key_type& x)
const
389 precondition (static_cast<size_type> (x) < max_size_);
393 if (it == bit_begin ())
394 return iterator (
this, end_);
401 while ( (it != bit_begin ()) && !
get_bit (it));
402 return iterator (
this,
get_bit (it) ? it : end_);
408 std::pair<Bitset::iterator, Bitset::iterator>
409 Bitset::equal_range (
const key_type& x)
const
412 return std::make_pair (lower_bound (x), upper_bound (x));
421 Bitset::operator== (
const Bitset& rhs)
const
423 if (rhs.data_size_ < data_size_)
424 return rhs.operator== (*this);
427 for (size_type i = 0; i < data_size_; ++i)
428 if (data_[i] != rhs.data_[i])
430 for (size_type i = data_size_; i < rhs.data_size_; ++i)
439 Bitset::operator< (
const Bitset& rhs)
const
442 for (size_type i = rhs.data_size_ - 1; i >= data_size_; ++i)
446 for (size_type i = data_size_ - 1; i >= rhs.data_size_; ++i)
450 for (
int i = std::min (data_size_, rhs.data_size_) - 1; i >= 0; ++i)
451 if (rhs.data_[i] != data_[i])
452 return rhs.data_[i] > data_[i];
459 Bitset::operator!= (
const Bitset& rhs)
const
461 return ! (*
this == rhs);
466 Bitset::operator> (
const Bitset& rhs)
const
473 Bitset::operator<= (
const Bitset& rhs)
const
475 return ! (*
this > rhs);
480 Bitset::operator>= (
const Bitset& rhs)
const
482 return ! (*
this < rhs);
487 Bitset::operator& (
const Bitset& rhs)
const
489 const size_type r_data_size = std::min (data_size_, rhs.data_size_);
490 const size_type r_max_size = std::min (max_size_, rhs.max_size_);
491 Bitset result (r_max_size);
493 for (size_type i = 0; i < r_data_size; ++i)
495 result.data_[i] = data_[i] & rhs.data_[i];
497 result.size_ = invalid_size;
504 Bitset::operator| (
const Bitset& rhs)
const
506 if (rhs.data_size_ < data_size_)
507 return rhs.operator| (*this);
510 Bitset result (rhs.max_size_);
512 for (size_type i = 0; i < data_size_; ++i)
514 result.data_[i] = data_[i] | rhs.data_[i];
516 result.size_ = invalid_size;
518 for (size_type i = data_size_; i < rhs.data_size_; ++i)
520 result.data_[i] = rhs.data_[i];
522 result.size_ = invalid_size;
543 BitActionCount<int, -1, 1> bit_counter;
544 Bitset result (b.size ());
546 for (bit_iterator it = bit_begin (), b_it = b.bit_begin ();
547 it != bit_end () && b_it != b.bit_end ();
551 result.
insert (bit_counter.value);
560 BitActionCount<int, 0, 1> bit_counter;
561 Bitset result (b.max_size ());
562 bit_iterator b_it = b.bit_begin ();
564 while (b_it != b.bit_end ()
567 for (bit_iterator it = bit_begin (); it != bit_end (); ++it)
570 while ( (b_it != b.bit_end ()) && (bit_counter.value < *it))
572 assertion (bit_counter.value == *it);
573 result.insert (*b_it);
586 return cast<unsigned> ();
589 template <
class Type>
593 const Type* ptr =
reinterpret_cast<const Type*
> (data_);
606 for (Bitset::bit_iterator it = bit_begin (); it != bit_end (); ++it)
621 precondition (max > 0);
623 const size_type data_bits =
sizeof (data_type) * 8;
624 return max / data_bits + (max % data_bits ? 1 : 0);
632 return x / (
sizeof (data_type) * 8);
640 return x % (
sizeof (data_type) * 8);
648 return (data_[index] & (1 << bit)) != 0;
655 return get_bit (it.get_index (), it.get_bitnum ());
663 BitActionCount<int, 0, 1> bc;
664 for (bit_iterator it = bit_begin (); it != bit_end (); ++it)
666 return size_ = bc.value;
676 bit_iterator::bit_iterator (size_type index, size_type bitnum) :
679 value_ (index_ * sizeof (data_type) * 8 + bitnum_)
685 const Bitset::bit_iterator&
686 Bitset::bit_iterator::operator-- ()
693 bitnum_ =
sizeof (data_type) * 8 - 1;
700 const Bitset::bit_iterator&
701 Bitset::bit_iterator::operator++ ()
704 if (++bitnum_ >=
sizeof (data_type) * 8)
721 Bitset::bit_iterator::operator== (
const bit_iterator& rhs)
const
723 return rhs.value_ == value_;
730 return ! (*
this == rhs);
752 Bitset::bit_begin ()
const
754 return bit_iterator ();
759 const Bitset::bit_iterator&
760 Bitset::bit_end ()
const
770 template <
typename CountType, CountType Start, CountType Step>
772 BitActionCount<CountType, Start, Step>::BitActionCount () : value (Start)
776 template <
typename CountType, CountType Start, CountType Step>
779 BitActionCount<CountType, Start, Step>::operator() (
const Bitset&,
794 template <
class BitAction>
798 precondition (x < max_size_);
802 const bool value =
get_bit (index, bit);
803 return action (*
this, index, bit, value);
806 template <
class BitAction>
810 return action (*
this, it.get_index (), it.get_bitnum (),
get_bit (it));
814 template <
class ConstBitAction>
818 precondition (x < max_size_);
822 const bool value =
get_bit (index, bit);
823 return action (*
this, index, bit, value);
826 template <
class ConstBitAction>
830 return action (*
this, it.get_index (), it.get_bitnum (),
get_bit (it));
839 Bitset::const_iterator::const_iterator () : bs_ (0), cbit_ ()
841 WARNING (
"The constructor Bitset::const_iterator::const_iterator () "
842 "is dangerous and therefore should not be used.");
846 Bitset::const_iterator::const_iterator (
const Bitset* bs) : bs_ (bs),
849 skip_zeros_forward ();
853 Bitset::const_iterator::const_iterator (
const Bitset* bs,
854 const bit_iterator& cbit)
858 skip_zeros_forward ();
863 Bitset::const_iterator::const_iterator (
const const_iterator& it)
864 : bs_ (it.bs_), cbit_ (it.cbit_)
869 Bitset::const_iterator::const_iterator (
const iterator& it)
870 : bs_ (it.bs_), cbit_ (it.cbit_)
876 const Bitset::const_iterator&
877 Bitset::const_iterator::operator++ ()
880 skip_zeros_forward ();
885 Bitset::const_iterator
886 Bitset::const_iterator::operator++ (
int)
888 const_iterator ret (*
this);
895 const Bitset::const_iterator&
896 Bitset::const_iterator::operator-- ()
899 skip_zeros_backward ();
904 Bitset::const_iterator
905 Bitset::const_iterator::operator-- (
int)
907 const_iterator ret (*
this);
915 Bitset::const_iterator::operator== (
const const_iterator& rhs)
const
917 return * (*this) == *rhs;
922 Bitset::const_iterator::operator== (
const iterator& rhs)
const
924 return * (*this) == *rhs;
931 return ! (*
this == rhs);
938 return ! (*
this == rhs);
950 Bitset::const_iterator::skip_zeros_forward ()
952 precondition (bs_ != 0);
954 while ( (cbit_ != bs_->bit_end ()) && !bs_->get_bit (cbit_))
960 Bitset::const_iterator::skip_zeros_backward ()
962 precondition (bs_ != 0);
964 while ( (cbit_ != bs_->bit_begin ()) && !bs_->get_bit (cbit_))
974 Bitset::iterator::iterator () : bs_ (0), cbit_ ()
976 WARNING (
"The constructor Bitset::iterator::iterator () is dangerous "
977 "and therefore should not be used.");
981 Bitset::iterator::iterator (
const Bitset* bs)
985 skip_zeros_forward ();
989 Bitset::iterator::iterator (
const Bitset* bs,
const bit_iterator& cbit)
993 skip_zeros_forward ();
998 Bitset::iterator::iterator (
const iterator& it) : bs_ (it.bs_), cbit_ (it.cbit_)
1004 const Bitset::iterator&
1005 Bitset::iterator::operator++ ()
1008 skip_zeros_forward ();
1014 Bitset::iterator::operator++ (
int)
1016 iterator res (*
this);
1023 const Bitset::iterator&
1024 Bitset::iterator::operator-- ()
1027 skip_zeros_backward ();
1033 Bitset::iterator::operator-- (
int)
1035 iterator res (*
this);
1043 Bitset::iterator::operator== (
const iterator& rhs)
const
1045 return * (*this) == *rhs;
1050 Bitset::iterator::operator== (
const const_iterator& rhs)
const
1052 return * (*this) == *rhs;
1059 return ! (*
this == rhs);
1066 return ! (*
this == rhs);
1078 Bitset::iterator::skip_zeros_forward ()
1080 precondition (bs_ != 0);
1082 while ( (cbit_ != bs_->bit_end ()) && !bs_->get_bit (cbit_))
1088 Bitset::iterator::skip_zeros_backward ()
1090 precondition (bs_ != 0);
1092 while ( (cbit_ != bs_->bit_begin ()) && !bs_->get_bit (cbit_))
1100 return set.print (ostr);
1116 insert_iterator<vcsn::misc::Bitset>::
1118 vcsn::misc::Bitset::iterator)
1124 insert_iterator<vcsn::misc::Bitset>&
1126 const_reference value)
1128 container->
insert (value);
1133 insert_iterator<vcsn::misc::Bitset>&
1140 insert_iterator<vcsn::misc::Bitset>&
1141 insert_iterator<vcsn::misc::Bitset>::operator++ ()
1147 insert_iterator<vcsn::misc::Bitset>&
1148 insert_iterator<vcsn::misc::Bitset>::operator++ (
int)
1154 #endif // ! VCSN_MISC_BITSET_HXX