📄 id3lib_bitset
字号:
template<class _WordT>
struct _Base_bitset<1, _WordT> {
_WordT _M_w;
_Base_bitset( void ) { _M_do_reset(); }
_Base_bitset(unsigned long __val);
static size_t _S_whichword( size_t __pos ) {
return __pos / __BITS_PER_WORDT(_WordT);
}
static size_t _S_whichbyte( size_t __pos ) {
return (__pos % __BITS_PER_WORDT(_WordT)) / CHAR_BIT;
}
static size_t _S_whichbit( size_t __pos ) {
return __pos % __BITS_PER_WORDT(_WordT);
}
static _WordT _S_maskbit( size_t __pos ) {
return (static_cast<_WordT>(1)) << _S_whichbit(__pos);
}
_WordT& _M_getword(size_t) { return _M_w; }
_WordT _M_getword(size_t) const { return _M_w; }
_WordT& _M_hiword() { return _M_w; }
_WordT _M_hiword() const { return _M_w; }
void _M_do_and(const _Base_bitset<1,_WordT>& __x) { _M_w &= __x._M_w; }
void _M_do_or(const _Base_bitset<1,_WordT>& __x) { _M_w |= __x._M_w; }
void _M_do_xor(const _Base_bitset<1,_WordT>& __x) { _M_w ^= __x._M_w; }
void _M_do_left_shift(size_t __shift) { _M_w <<= __shift; }
void _M_do_right_shift(size_t __shift) { _M_w >>= __shift; }
void _M_do_flip() { _M_w = ~_M_w; }
void _M_do_set() { _M_w = ~static_cast<_WordT>(0); }
void _M_do_reset() { _M_w = 0; }
bool _M_is_equal(const _Base_bitset<1,_WordT>& __x) const {
return _M_w == __x._M_w;
}
bool _M_is_any() const {
return _M_w != 0;
}
size_t _M_do_count() const {
size_t __result = 0;
const unsigned char* __byte_ptr = (const unsigned char*)&_M_w;
const unsigned char* __end_ptr = ((const unsigned char*)&_M_w)+sizeof(_M_w);
while ( __byte_ptr < __end_ptr ) {
__result += _Bit_count<true>::_S_bit_count[*__byte_ptr];
__byte_ptr++;
}
return __result;
}
unsigned long _M_do_to_ulong() const {
if (sizeof(_WordT) <= sizeof(unsigned long))
return static_cast<unsigned long>(_M_w);
else {
const _WordT __mask = static_cast<_WordT>(static_cast<unsigned long>(-1));
if (_M_w & ~__mask)
__STL_THROW(overflow_error("bitset"));
return static_cast<unsigned long>(_M_w);
}
}
size_t _M_do_find_first(size_t __not_found) const;
// find the next "on" bit that follows "prev"
size_t _M_do_find_next(size_t __prev, size_t __not_found) const;
};
//
// Definitions of non-inline functions from the single-word version of
// _Base_bitset.
//
template <class _WordT>
_Base_bitset<1, _WordT>::_Base_bitset(unsigned long __val)
{
_M_do_reset();
const size_t __n = min(sizeof(unsigned long)*CHAR_BIT,
__BITS_PER_WORDT(_WordT)*_Nw);
for(size_t __i = 0; __i < __n; ++__i, __val >>= 1)
if ( __val & 0x1 )
_M_w |= _S_maskbit(__i);
}
template <class _WordT>
size_t _Base_bitset<1, _WordT>::_M_do_find_first(size_t __not_found) const
{
_WordT __thisword = _M_w;
if ( __thisword != static_cast<_WordT>(0) ) {
// find byte within word
for ( size_t __j = 0; __j < sizeof(_WordT); __j++ ) {
unsigned char __this_byte
= static_cast<unsigned char>(__thisword & (~(unsigned char)0));
if ( __this_byte )
return __j*CHAR_BIT + _First_one<true>::_S_first_one[__this_byte];
__thisword >>= CHAR_BIT;
}
}
// not found, so return a value that indicates failure.
return __not_found;
}
template <class _WordT>
size_t
_Base_bitset<1, _WordT>::_M_do_find_next(size_t __prev,
size_t __not_found ) const
{
// make bound inclusive
++__prev;
// check out of bounds
if ( __prev >= __BITS_PER_WORDT(_WordT) )
return __not_found;
// search first (and only) word
_WordT __thisword = _M_w;
// mask off bits below bound
__thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
if ( __thisword != static_cast<_WordT>(0) ) {
// find byte within word
// get first byte into place
__thisword >>= _S_whichbyte(__prev) * CHAR_BIT;
for ( size_t __j = _S_whichbyte(__prev); __j < sizeof(_WordT); __j++ ) {
unsigned char __this_byte
= static_cast<unsigned char>(__thisword & (~(unsigned char)0));
if ( __this_byte )
return __j*CHAR_BIT + _First_one<true>::_S_first_one[__this_byte];
__thisword >>= CHAR_BIT;
}
}
// not found, so return a value that indicates failure.
return __not_found;
} // end _M_do_find_next
//
// One last specialization: _M_do_to_ulong() and the constructor from
// unsigned long are very simple if the bitset consists of a single
// word of type unsigned long.
//
template<>
inline unsigned long
_Base_bitset<1, unsigned long>::_M_do_to_ulong() const { return _M_w; }
template<>
inline _Base_bitset<1, unsigned long>::_Base_bitset(unsigned long __val) {
_M_w = __val;
}
// ------------------------------------------------------------
// Helper class to zero out the unused high-order bits in the highest word.
template <class _WordT, size_t _Extrabits> struct _Sanitize {
static void _M_do_sanitize(_WordT& __val)
{ __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
};
template <class _WordT> struct _Sanitize<_WordT, 0> {
static void _M_do_sanitize(_WordT) {}
};
// ------------------------------------------------------------
// Class bitset.
// _Nb may be any nonzero number of type size_t.
// Type _WordT may be any unsigned integral type.
template<size_t _Nb, class _WordT = unsigned long>
class bitset : private _Base_bitset<__BITSET_WORDS(_Nb,_WordT), _WordT>
{
private:
typedef _Base_bitset<__BITSET_WORDS(_Nb,_WordT), _WordT> _Base;
// Import base's protected interface. Necessary because of new template
// name resolution rules.
using _Base::_S_whichword;
using _Base::_S_whichbyte;
using _Base::_S_whichbit;
using _Base::_S_maskbit;
using _Base::_M_getword;
using _Base::_M_hiword;
using _Base::_M_do_and;
using _Base::_M_do_or;
using _Base::_M_do_xor;
using _Base::_M_do_left_shift;
using _Base::_M_do_right_shift;
using _Base::_M_do_flip;
using _Base::_M_do_set;
using _Base::_M_do_reset;
using _Base::_M_is_equal;
using _Base::_M_is_any;
using _Base::_M_do_count;
using _Base::_M_do_to_ulong;
using _Base::_M_do_find_first;
using _Base::_M_do_find_next;
private:
void _M_do_sanitize() {
_Sanitize<_WordT,_Nb%__BITS_PER_WORDT(_WordT) >
::_M_do_sanitize(_M_hiword());
}
public:
// bit reference:
class reference;
friend class reference;
class reference {
friend class bitset;
_WordT *_M_wp;
size_t _M_bpos;
// left undefined
reference();
reference( bitset& __b, size_t __pos ) {
_M_wp = &__b._M_getword(__pos);
_M_bpos = _S_whichbit(__pos);
}
public:
~reference() {}
// for b[i] = __x;
reference& operator=(bool __x) {
if ( __x )
*_M_wp |= _S_maskbit(_M_bpos);
else
*_M_wp &= ~_S_maskbit(_M_bpos);
return *this;
}
// for b[i] = b[__j];
reference& operator=(const reference& __j) {
if ( (*(__j._M_wp) & _S_maskbit(__j._M_bpos)) )
*_M_wp |= _S_maskbit(_M_bpos);
else
*_M_wp &= ~_S_maskbit(_M_bpos);
return *this;
}
// flips the bit
bool operator~() const { return (*(_M_wp) & _S_maskbit(_M_bpos)) == 0; }
// for __x = b[i];
operator bool() const { return (*(_M_wp) & _S_maskbit(_M_bpos)) != 0; }
// for b[i].flip();
reference& flip() {
*_M_wp ^= _S_maskbit(_M_bpos);
return *this;
}
};
// 23.3.5.1 constructors:
bitset() {}
bitset(unsigned long __val) :
_Base_bitset<__BITSET_WORDS(_Nb,_WordT), _WordT>(__val) {}
template<class _CharT, class _Traits, class _Alloc>
explicit bitset(const basic_string<_CharT,_Traits,_Alloc>& __s,
size_t __pos = 0,
size_t __n = size_t(basic_string<_CharT,_Traits,_Alloc>::npos))
: _Base()
{
if (__pos > __s.size())
__STL_THROW(out_of_range("bitset"));
_M_copy_from_string(__s, __pos, __n);
}
// 23.3.5.2 bitset operations:
bitset<_Nb,_WordT>& operator&=(const bitset<_Nb,_WordT>& __rhs) {
_M_do_and(__rhs);
return *this;
}
bitset<_Nb,_WordT>& operator|=(const bitset<_Nb,_WordT>& __rhs) {
_M_do_or(__rhs);
return *this;
}
bitset<_Nb,_WordT>& operator^=(const bitset<_Nb,_WordT>& __rhs) {
_M_do_xor(__rhs);
return *this;
}
bitset<_Nb,_WordT>& operator<<=(size_t __pos) {
_M_do_left_shift(__pos);
_M_do_sanitize();
return *this;
}
bitset<_Nb,_WordT>& operator>>=(size_t __pos) {
_M_do_right_shift(__pos);
_M_do_sanitize();
return *this;
}
//
// Extension:
// Versions of single-bit set, reset, flip, test with no range checking.
//
bitset<_Nb,_WordT>& _Unchecked_set(size_t __pos) {
_M_getword(__pos) |= _S_maskbit(__pos);
return *this;
}
bitset<_Nb,_WordT>& _Unchecked_set(size_t __pos, int __val) {
if (__val)
_M_getword(__pos) |= _S_maskbit(__pos);
else
_M_getword(__pos) &= ~_S_maskbit(__pos);
return *this;
}
bitset<_Nb,_WordT>& _Unchecked_reset(size_t __pos) {
_M_getword(__pos) &= ~_S_maskbit(__pos);
return *this;
}
bitset<_Nb,_WordT>& _Unchecked_flip(size_t __pos) {
_M_getword(__pos) ^= _S_maskbit(__pos);
return *this;
}
bool _Unchecked_test(size_t __pos) const {
return (_M_getword(__pos) & _S_maskbit(__pos)) != static_cast<_WordT>(0);
}
// Set, reset, and flip.
bitset<_Nb,_WordT>& set() {
_M_do_set();
_M_do_sanitize();
return *this;
}
bitset<_Nb,_WordT>& set(size_t __pos) {
if (__pos >= _Nb)
__STL_THROW(out_of_range("bitset"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -