stl_bvector.h

来自「symbian上STL模板库的实现」· C头文件 代码 · 共 877 行 · 第 1/3 页

H
877
字号
// vector<bool> specialization -*- C++ -*-// Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.//// This file is part of the GNU ISO C++ Library.  This library is free// software; you can redistribute it and/or modify it under the// terms of the GNU General Public License as published by the// Free Software Foundation; either version 2, or (at your option)// any later version.// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.// You should have received a copy of the GNU General Public License along// with this library; see the file COPYING.  If not, write to the Free// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,// USA.// As a special exception, you may use this file as part of a free software// library without restriction.  Specifically, if other files instantiate// templates or use macros or inline functions from this file, or you compile// this file and link it with other files to produce an executable, this// file does not by itself cause the resulting executable to be covered by// the GNU General Public License.  This exception does not however// invalidate any other reasons why the executable file might be covered by// the GNU General Public License./* * * Copyright (c) 1994 * Hewlett-Packard Company * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation.  Hewlett-Packard Company makes no * representations about the suitability of this software for any * purpose.  It is provided "as is" without express or implied warranty. * * * Copyright (c) 1996-1999 * Silicon Graphics Computer Systems, Inc. * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation.  Silicon Graphics makes no * representations about the suitability of this software for any * purpose.  It is provided "as is" without express or implied warranty. *//** @file stl_bvector.h *  This is an internal header file, included by other library headers. *  You should not attempt to use it directly. */#ifndef _BVECTOR_H#define _BVECTOR_H 1namespace _GLIBCXX_STD{    typedef unsigned long _Bit_type;    enum { _S_word_bit = int(CHAR_BIT * sizeof(_Bit_type)) };    struct _Bit_reference    {        _Bit_type * _M_p;        _Bit_type _M_mask;        _Bit_reference(_Bit_type * __x, _Bit_type __y)            : _M_p(__x), _M_mask(__y) { }        _Bit_reference() : _M_p(0), _M_mask(0) { }        operator bool() const { return !!(*_M_p & _M_mask); }        _Bit_reference&            operator=(bool __x)            {                if (__x)                    *_M_p |= _M_mask;                else                    *_M_p &= ~_M_mask;                return *this;            }        _Bit_reference&            operator=(const _Bit_reference& __x)            { return *this = bool(__x); }        bool            operator==(const _Bit_reference& __x) const            { return bool(*this) == bool(__x); }        bool            operator<(const _Bit_reference& __x) const            { return !bool(*this) && bool(__x); }        void            flip() { *_M_p ^= _M_mask; }    };    struct _Bit_iterator_base : public iterator<random_access_iterator_tag, bool>    {        _Bit_type * _M_p;        unsigned int _M_offset;        _Bit_iterator_base(_Bit_type * __x, unsigned int __y)            : _M_p(__x), _M_offset(__y) { }        void            _M_bump_up()            {                if (_M_offset++ == _S_word_bit - 1)                {                    _M_offset = 0;                    ++_M_p;                }            }        void            _M_bump_down()            {                if (_M_offset-- == 0)                {                    _M_offset = _S_word_bit - 1;                    --_M_p;                }            }        void            _M_incr(ptrdiff_t __i)            {                difference_type __n = __i + _M_offset;                _M_p += __n / _S_word_bit;                __n = __n % _S_word_bit;                if (__n < 0)                {                    _M_offset = static_cast<unsigned int>(__n + _S_word_bit);                    --_M_p;                }                else                    _M_offset = static_cast<unsigned int>(__n);            }        bool            operator==(const _Bit_iterator_base& __i) const            { return _M_p == __i._M_p && _M_offset == __i._M_offset; }        bool            operator<(const _Bit_iterator_base& __i) const            {                return _M_p < __i._M_p                    || (_M_p == __i._M_p && _M_offset < __i._M_offset);            }        bool            operator!=(const _Bit_iterator_base& __i) const            { return !(*this == __i); }        bool            operator>(const _Bit_iterator_base& __i) const            { return __i < *this; }        bool            operator<=(const _Bit_iterator_base& __i) const            { return !(__i < *this); }        bool            operator>=(const _Bit_iterator_base& __i) const            { return !(*this < __i); }    };    inline ptrdiff_t        operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)        {            return _S_word_bit * (__x._M_p - __y._M_p) + __x._M_offset - __y._M_offset;        }    struct _Bit_iterator : public _Bit_iterator_base    {        typedef _Bit_reference  reference;        typedef _Bit_reference* pointer;        typedef _Bit_iterator   iterator;        _Bit_iterator() : _Bit_iterator_base(0, 0) { }        _Bit_iterator(_Bit_type * __x, unsigned int __y)            : _Bit_iterator_base(__x, __y) { }        reference            operator*() const { return reference(_M_p, 1UL << _M_offset); }        iterator&            operator++()            {                _M_bump_up();                return *this;            }        iterator            operator++(int)            {                iterator __tmp = *this;                _M_bump_up();                return __tmp;            }        iterator&            operator--()            {                _M_bump_down();                return *this;            }        iterator            operator--(int)            {                iterator __tmp = *this;                _M_bump_down();                return __tmp;            }        iterator&            operator+=(difference_type __i)            {                _M_incr(__i);                return *this;            }        iterator&            operator-=(difference_type __i)            {                *this += -__i;                return *this;            }        iterator            operator+(difference_type __i) const            {                iterator __tmp = *this;                return __tmp += __i;            }        iterator            operator-(difference_type __i) const            {                iterator __tmp = *this;                return __tmp -= __i;            }        reference            operator[](difference_type __i)            { return *(*this + __i); }    };    inline _Bit_iterator        operator+(ptrdiff_t __n, const _Bit_iterator& __x) { return __x + __n; }    struct _Bit_const_iterator : public _Bit_iterator_base    {        typedef bool                 reference;        typedef bool                 const_reference;        typedef const bool*          pointer;        typedef _Bit_const_iterator  const_iterator;        _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }        _Bit_const_iterator(_Bit_type * __x, unsigned int __y)            : _Bit_iterator_base(__x, __y) { }        _Bit_const_iterator(const _Bit_iterator& __x)            : _Bit_iterator_base(__x._M_p, __x._M_offset) { }        const_reference            operator*() const            { return _Bit_reference(_M_p, 1UL << _M_offset); }        const_iterator&            operator++()            {                _M_bump_up();                return *this;            }        const_iterator            operator++(int)            {                const_iterator __tmp = *this;                _M_bump_up();                return __tmp;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?