📄 stl_bvector.h
字号:
// bit_vector and vector<bool> specialization -*- C++ -*-// Copyright (C) 2001, 2002 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 __GLIBCPP_INTERNAL_BVECTOR_H#define __GLIBCPP_INTERNAL_BVECTOR_Hnamespace std{ typedef unsigned long _Bit_type; enum { _M_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) {}public: _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++ == _M_word_bit - 1) { _M_offset = 0; ++_M_p; } } void _M_bump_down() { if (_M_offset-- == 0) { _M_offset = _M_word_bit - 1; --_M_p; } } void _M_incr(ptrdiff_t __i) { difference_type __n = __i + _M_offset; _M_p += __n / _M_word_bit; __n = __n % _M_word_bit; if (__n < 0) { _M_offset = (unsigned int) __n + _M_word_bit; --_M_p; } else _M_offset = (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_toperator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y) { return _M_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; } const_iterator& operator--() { _M_bump_down(); return *this; } const_iterator operator--(int) { const_iterator __tmp = *this; _M_bump_down(); return __tmp; } const_iterator& operator+=(difference_type __i) { _M_incr(__i); return *this; } const_iterator& operator-=(difference_type __i) { *this += -__i; return *this; } const_iterator operator+(difference_type __i) const { const_iterator __tmp = *this; return __tmp += __i; } const_iterator operator-(difference_type __i) const { const_iterator __tmp = *this; return __tmp -= __i; } const_reference operator[](difference_type __i) { return *(*this + __i); }};inline _Bit_const_iterator operator+(ptrdiff_t __n, const _Bit_const_iterator& __x) { return __x + __n; }// Bit-vector base class, which encapsulates the difference between// old SGI-style allocators and standard-conforming allocators.// Base class for ordinary allocators.template <class _Allocator, bool __is_static>class _Bvector_alloc_base {public: typedef typename _Alloc_traits<bool, _Allocator>::allocator_type allocator_type; allocator_type get_allocator() const { return _M_data_allocator; } _Bvector_alloc_base(const allocator_type& __a) : _M_data_allocator(__a), _M_start(), _M_finish(), _M_end_of_storage(0) {}protected: _Bit_type * _M_bit_alloc(size_t __n) { return _M_data_allocator.allocate((__n + _M_word_bit - 1)/_M_word_bit); } void _M_deallocate() { if (_M_start._M_p) _M_data_allocator.deallocate(_M_start._M_p, _M_end_of_storage - _M_start._M_p); } typename _Alloc_traits<_Bit_type, _Allocator>::allocator_type _M_data_allocator; _Bit_iterator _M_start; _Bit_iterator _M_finish; _Bit_type * _M_end_of_storage;};// Specialization for instanceless allocators.template <class _Allocator>class _Bvector_alloc_base<_Allocator, true> {public: typedef typename _Alloc_traits<bool, _Allocator>::allocator_type allocator_type; allocator_type get_allocator() const { return allocator_type(); } _Bvector_alloc_base(const allocator_type&) : _M_start(), _M_finish(), _M_end_of_storage(0) {}protected: typedef typename _Alloc_traits<_Bit_type, _Allocator>::_Alloc_type _Alloc_type; _Bit_type * _M_bit_alloc(size_t __n) { return _Alloc_type::allocate((__n + _M_word_bit - 1)/_M_word_bit); } void _M_deallocate() { if (_M_start._M_p) _Alloc_type::deallocate(_M_start._M_p, _M_end_of_storage - _M_start._M_p); } _Bit_iterator _M_start; _Bit_iterator _M_finish; _Bit_type * _M_end_of_storage;}; template <class _Alloc>class _Bvector_base : public _Bvector_alloc_base<_Alloc, _Alloc_traits<bool, _Alloc>::_S_instanceless>{ typedef _Bvector_alloc_base<_Alloc, _Alloc_traits<bool, _Alloc>::_S_instanceless> _Base;public: typedef typename _Base::allocator_type allocator_type; _Bvector_base(const allocator_type& __a) : _Base(__a) {} ~_Bvector_base() { _Base::_M_deallocate(); }};} // namespace std// Declare a partial specialization of vector<T, Alloc>.#include <bits/stl_vector.h>namespace std{template <typename _Alloc> class vector<bool, _Alloc> : public _Bvector_base<_Alloc> { public: typedef bool value_type; typedef size_t size_type; typedef ptrdiff_t difference_type; typedef _Bit_reference reference; typedef bool const_reference; typedef _Bit_reference* pointer; typedef const bool* const_pointer; typedef _Bit_iterator iterator; typedef _Bit_const_iterator const_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator; typedef std::reverse_iterator<iterator> reverse_iterator; typedef typename _Bvector_base<_Alloc>::allocator_type allocator_type; allocator_type get_allocator() const { return _Bvector_base<_Alloc>::get_allocator(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -