1 /*-----------------------------------------------------------------------------+ 2 Author: Joachim Faulhaber 3 Copyright (c) 2009-2009: Joachim Faulhaber 4 +------------------------------------------------------------------------------+ 5 Distributed under the Boost Software License, Version 1.0. 6 (See accompanying file LICENCE.txt or copy at 7 http://www.boost.org/LICENSE_1_0.txt) 8 +-----------------------------------------------------------------------------*/ 9 #ifndef BOOST_LIBS_ICL_EXAMPLE_LARGE_BITSET_BITS_HPP_JOFA_091019 10 #define BOOST_LIBS_ICL_EXAMPLE_LARGE_BITSET_BITS_HPP_JOFA_091019 11 //[mini_bits_includes 12 // These includes are needed ... 13 #include <string> // for conversion to output and to 14 #include <boost/icl/type_traits/has_set_semantics.hpp>//declare that bits has the 15 // behavior of a set. 16 //] 17 18 namespace mini 19 { 20 //[mini_bits_class_bits 21 template<class NaturalT> class bits 22 { 23 public: 24 typedef NaturalT word_type; 25 static const int digits = std::numeric_limits<NaturalT>::digits; 26 static const word_type w1 = static_cast<NaturalT>(1) ; 27 bits()28 bits():_bits(){} bits(word_type value)29 explicit bits(word_type value):_bits(value){} 30 word() const31 word_type word()const{ return _bits; } operator |=(const bits & value)32 bits& operator |= (const bits& value){_bits |= value._bits; return *this;} operator &=(const bits & value)33 bits& operator &= (const bits& value){_bits &= value._bits; return *this;} operator ^=(const bits & value)34 bits& operator ^= (const bits& value){_bits ^= value._bits; return *this;} operator ~() const35 bits operator ~ ()const { return bits(~_bits); } operator <(const bits & value) const36 bool operator < (const bits& value)const{return _bits < value._bits;} operator ==(const bits & value) const37 bool operator == (const bits& value)const{return _bits == value._bits;} 38 contains(word_type element) const39 bool contains(word_type element)const{ return ((w1 << element) & _bits) != 0; } 40 std::string as_string(const char off_on[2] = " 1")const; 41 42 private: 43 word_type _bits; 44 }; 45 //] 46 47 template<class NaturalT> as_string(const char off_on[2]) const48std::string bits<NaturalT>::as_string(const char off_on[2])const 49 { 50 std::string sequence; 51 for(int bit=0; bit < digits; bit++) 52 sequence += contains(bit) ? off_on[1] : off_on[0]; 53 return sequence; 54 } 55 56 } // mini 57 58 //[mini_bits_is_set 59 namespace boost { namespace icl 60 { 61 template<class NaturalT> 62 struct is_set<mini::bits<NaturalT> > 63 { 64 typedef is_set<mini::bits<NaturalT> > type; 65 BOOST_STATIC_CONSTANT(bool, value = true); 66 }; 67 68 template<class NaturalT> 69 struct has_set_semantics<mini::bits<NaturalT> > 70 { 71 typedef has_set_semantics<mini::bits<NaturalT> > type; 72 BOOST_STATIC_CONSTANT(bool, value = true); 73 }; 74 }} 75 //] 76 77 #endif 78