📄 _deque.h
字号:
/* * * Copyright (c) 1994 * Hewlett-Packard Company * * Copyright (c) 1996,1997 * Silicon Graphics Computer Systems, Inc. * * Copyright (c) 1997 * Moscow Center for SPARC Technology * * Copyright (c) 1999 * Boris Fomitchev * * This material is provided "as is", with absolutely no warranty expressed * or implied. Any use is at your own risk. * * Permission to use or copy this software for any purpose is hereby granted * without fee, provided the above notices are retained on all copies. * Permission to modify the code and to distribute modified code is granted, * provided the above notices are retained, and a notice that the code was * modified is included with the above copyright notice. * *//* NOTE: This is an internal header file, included by other STL headers. * You should not attempt to use it directly. */#ifndef _STLP_INTERNAL_DEQUE_H#define _STLP_INTERNAL_DEQUE_H#ifndef _STLP_INTERNAL_ALGOBASE_H# include <stl/_algobase.h>#endif#ifndef _STLP_INTERNAL_ALLOC_H# include <stl/_alloc.h>#endif#ifndef _STLP_INTERNAL_ITERATOR_H# include <stl/_iterator.h>#endif#ifndef _STLP_INTERNAL_UNINITIALIZED_H# include <stl/_uninitialized.h>#endif#ifndef _STLP_RANGE_ERRORS_H# include <stl/_range_errors.h>#endif/* Class invariants: * For any nonsingular iterator i: * i.node is the address of an element in the map array. The * contents of i.node is a pointer to the beginning of a node. * i.first == *(i.node) * i.last == i.first + node_size * i.cur is a pointer in the range [i.first, i.last). NOTE: * the implication of this is that i.cur is always a dereferenceable * pointer, even if i is a past-the-end iterator. * Start and Finish are always nonsingular iterators. NOTE: this means * that an empty deque must have one node, and that a deque * with N elements, where N is the buffer size, must have two nodes. * For every node other than start.node and finish.node, every element * in the node is an initialized object. If start.node == finish.node, * then [start.cur, finish.cur) are initialized objects, and * the elements outside that range are uninitialized storage. Otherwise, * [start.cur, start.last) and [finish.first, finish.cur) are initialized * objects, and [start.first, start.cur) and [finish.cur, finish.last) * are uninitialized storage. * [map, map + map_size) is a valid, non-empty range. * [start.node, finish.node] is a valid range contained within * [map, map + map_size). * A pointer in the range [map, map + map_size) points to an allocated node * if and only if the pointer is in the range [start.node, finish.node]. */_STLP_BEGIN_NAMESPACE_STLP_MOVE_TO_PRIV_NAMESPACEtemplate <class _Tp>struct _Deque_iterator_base { enum _Constants { _blocksize = _MAX_BYTES, __buffer_size = (sizeof(_Tp) < (size_t)_blocksize ? ( (size_t)_blocksize / sizeof(_Tp)) : size_t(1)) }; typedef random_access_iterator_tag iterator_category; typedef _Tp value_type; typedef size_t size_type; typedef ptrdiff_t difference_type; typedef value_type** _Map_pointer; typedef _Deque_iterator_base< _Tp > _Self; value_type* _M_cur; value_type* _M_first; value_type* _M_last; _Map_pointer _M_node; _Deque_iterator_base(value_type* __x, _Map_pointer __y) : _M_cur(__x), _M_first(*__y), _M_last(*__y + __buffer_size), _M_node(__y) {} _Deque_iterator_base() : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) {}// see comment in doc/README.evc4 and doc/README.evc8#if defined (_STLP_MSVC) && (_STLP_MSVC <= 1401) && defined (MIPS) && defined (NDEBUG) _Deque_iterator_base(_Deque_iterator_base const& __other) : _M_cur(__other._M_cur), _M_first(__other._M_first), _M_last(__other._M_last), _M_node(__other._M_node) {}#endif difference_type _M_subtract(const _Self& __x) const { return difference_type(__buffer_size) * (_M_node - __x._M_node - 1) + (_M_cur - _M_first) + (__x._M_last - __x._M_cur); } void _M_increment() { if (++_M_cur == _M_last) { _M_set_node(_M_node + 1); _M_cur = _M_first; } } void _M_decrement() { if (_M_cur == _M_first) { _M_set_node(_M_node - 1); _M_cur = _M_last; } --_M_cur; } void _M_advance(difference_type __n) { difference_type __offset = __n + (_M_cur - _M_first); if (__offset >= 0 && __offset < difference_type(__buffer_size)) _M_cur += __n; else { difference_type __node_offset = __offset > 0 ? __offset / __buffer_size : -difference_type((-__offset - 1) / __buffer_size) - 1; _M_set_node(_M_node + __node_offset); _M_cur = _M_first + (__offset - __node_offset * difference_type(__buffer_size)); } } void _M_set_node(_Map_pointer __new_node) { _M_last = (_M_first = *(_M_node = __new_node)) + difference_type(__buffer_size); }};template <class _Tp, class _Traits>struct _Deque_iterator : public _Deque_iterator_base< _Tp> { typedef random_access_iterator_tag iterator_category; typedef _Tp value_type; typedef typename _Traits::reference reference; typedef typename _Traits::pointer pointer; typedef size_t size_type; typedef ptrdiff_t difference_type; typedef value_type** _Map_pointer; typedef _Deque_iterator_base< _Tp > _Base; typedef _Deque_iterator<_Tp, _Traits> _Self; typedef typename _Traits::_NonConstTraits _NonConstTraits; typedef _Deque_iterator<_Tp, _NonConstTraits> iterator; typedef typename _Traits::_ConstTraits _ConstTraits; typedef _Deque_iterator<_Tp, _ConstTraits> const_iterator; _Deque_iterator(value_type* __x, _Map_pointer __y) : _Deque_iterator_base<value_type>(__x,__y) {} _Deque_iterator() {} //copy constructor for iterator and constructor from iterator for const_iterator _Deque_iterator(const iterator& __x) : _Deque_iterator_base<value_type>(__x) {} reference operator*() const { return *this->_M_cur; } _STLP_DEFINE_ARROW_OPERATOR difference_type operator-(const const_iterator& __x) const { return this->_M_subtract(__x); } _Self& operator++() { this->_M_increment(); return *this; } _Self operator++(int) { _Self __tmp = *this; ++*this; return __tmp; } _Self& operator--() { this->_M_decrement(); return *this; } _Self operator--(int) { _Self __tmp = *this; --*this; return __tmp; } _Self& operator+=(difference_type __n) { this->_M_advance(__n); return *this; } _Self operator+(difference_type __n) const { _Self __tmp = *this; return __tmp += __n; } _Self& operator-=(difference_type __n) { return *this += -__n; } _Self operator-(difference_type __n) const { _Self __tmp = *this; return __tmp -= __n; } reference operator[](difference_type __n) const { return *(*this + __n); }};template <class _Tp, class _Traits>inline _Deque_iterator<_Tp, _Traits> _STLP_CALLoperator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Traits>& __x){ return __x + __n; }#if defined (_STLP_USE_SEPARATE_RELOPS_NAMESPACE)template <class _Tp>inline bool _STLP_CALLoperator==(const _Deque_iterator_base<_Tp >& __x, const _Deque_iterator_base<_Tp >& __y){ return __x._M_cur == __y._M_cur; }template <class _Tp>inline bool _STLP_CALLoperator < (const _Deque_iterator_base<_Tp >& __x, const _Deque_iterator_base<_Tp >& __y) { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur) : (__x._M_node < __y._M_node);}template <class _Tp>inline bool _STLP_CALLoperator!=(const _Deque_iterator_base<_Tp >& __x, const _Deque_iterator_base<_Tp >& __y){ return __x._M_cur != __y._M_cur; }template <class _Tp>inline bool _STLP_CALLoperator>(const _Deque_iterator_base<_Tp >& __x, const _Deque_iterator_base<_Tp >& __y){ return __y < __x; }template <class _Tp>inline bool _STLP_CALL operator>=(const _Deque_iterator_base<_Tp >& __x, const _Deque_iterator_base<_Tp >& __y){ return !(__x < __y); }template <class _Tp>inline bool _STLP_CALL operator<=(const _Deque_iterator_base<_Tp >& __x, const _Deque_iterator_base<_Tp >& __y){ return !(__y < __x); }#else /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */template <class _Tp, class _Traits1, class _Traits2>inline bool _STLP_CALLoperator==(const _Deque_iterator<_Tp, _Traits1 >& __x, const _Deque_iterator<_Tp, _Traits2 >& __y){ return __x._M_cur == __y._M_cur; }template <class _Tp, class _Traits1, class _Traits2>inline bool _STLP_CALLoperator < (const _Deque_iterator<_Tp, _Traits1 >& __x, const _Deque_iterator<_Tp, _Traits2 >& __y) { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur) : (__x._M_node < __y._M_node);}template <class _Tp>inline bool _STLP_CALLoperator!=(const _Deque_iterator<_Tp, _Nonconst_traits<_Tp> >& __x, const _Deque_iterator<_Tp, _Const_traits<_Tp> >& __y){ return __x._M_cur != __y._M_cur; }template <class _Tp>inline bool _STLP_CALLoperator>(const _Deque_iterator<_Tp, _Nonconst_traits<_Tp> >& __x, const _Deque_iterator<_Tp, _Const_traits<_Tp> >& __y){ return __y < __x; }template <class _Tp>inline bool _STLP_CALLoperator>=(const _Deque_iterator<_Tp, _Nonconst_traits<_Tp> >& __x, const _Deque_iterator<_Tp, _Const_traits<_Tp> >& __y){ return !(__x < __y); }template <class _Tp>inline bool _STLP_CALLoperator<=(const _Deque_iterator<_Tp, _Nonconst_traits<_Tp> >& __x, const _Deque_iterator<_Tp, _Const_traits<_Tp> >& __y){ return !(__y < __x); }#endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */#if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)_STLP_MOVE_TO_STD_NAMESPACEtemplate <class _Tp, class _Traits>struct __type_traits<_STLP_PRIV _Deque_iterator<_Tp, _Traits> > { typedef __false_type has_trivial_default_constructor; typedef __true_type has_trivial_copy_constructor; typedef __true_type has_trivial_assignment_operator; typedef __true_type has_trivial_destructor; typedef __false_type is_POD_type;};_STLP_MOVE_TO_PRIV_NAMESPACE#endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */#if defined (_STLP_USE_OLD_HP_ITERATOR_QUERIES)_STLP_MOVE_TO_STD_NAMESPACEtemplate <class _Tp, class _Traits> inline _Tp* _STLP_CALLvalue_type(const _STLP_PRIV _Deque_iterator<_Tp, _Traits >&) { return (_Tp*)0; }template <class _Tp, class _Traits> inline random_access_iterator_tag _STLP_CALLiterator_category(const _STLP_PRIV _Deque_iterator<_Tp, _Traits >&) { return random_access_iterator_tag(); }template <class _Tp, class _Traits> inline ptrdiff_t* _STLP_CALLdistance_type(const _STLP_PRIV _Deque_iterator<_Tp, _Traits >&) { return 0; }_STLP_MOVE_TO_PRIV_NAMESPACE#endif/* Deque base class. It has two purposes. First, its constructor * and destructor allocate (but don't initialize) storage. This makes * exception safety easier. Second, the base class encapsulates all of * the differences between SGI-style allocators and standard-conforming * allocators. */template <class _Tp, class _Alloc>class _Deque_base { typedef _Deque_base<_Tp, _Alloc> _Self;public: typedef _Tp value_type; _STLP_FORCE_ALLOCATORS(_Tp, _Alloc) typedef typename _Alloc_traits<_Tp,_Alloc>::allocator_type allocator_type; typedef _STLP_alloc_proxy<size_t, value_type, allocator_type> _Alloc_proxy; typedef typename _Alloc_traits<_Tp*, _Alloc>::allocator_type _Map_alloc_type; typedef _STLP_alloc_proxy<value_type**, value_type*, _Map_alloc_type> _Map_alloc_proxy; typedef _Deque_iterator<_Tp, _Nonconst_traits<_Tp> > iterator; typedef _Deque_iterator<_Tp, _Const_traits<_Tp> > const_iterator; static size_t _STLP_CALL buffer_size() { return (size_t)_Deque_iterator_base<_Tp>::__buffer_size; } _Deque_base(const allocator_type& __a, size_t __num_elements) : _M_start(), _M_finish(), _M_map(_STLP_CONVERT_ALLOCATOR(__a, _Tp*), 0), _M_map_size(__a, (size_t)0) { _M_initialize_map(__num_elements); } _Deque_base(const allocator_type& __a) : _M_start(), _M_finish(), _M_map(_STLP_CONVERT_ALLOCATOR(__a, _Tp*), 0), _M_map_size(__a, (size_t)0) {} _Deque_base(__move_source<_Self> src) : _M_start(src.get()._M_start), _M_finish(src.get()._M_finish),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -