?? stl_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 __SGI_STL_INTERNAL_DEQUE_H
#define __SGI_STL_INTERNAL_DEQUE_H
# ifndef __SGI_STL_INTERNAL_ALGOBASE_H
# include <stl_algobase.h>
# endif
# ifndef __SGI_STL_INTERNAL_ALLOC_H
# include <stl_alloc.h>
# endif
# ifndef __SGI_STL_INTERNAL_ITERATOR_H
# include <stl_iterator.h>
# endif
# ifndef __SGI_STL_INTERNAL_UNINITIALIZED_H
# include <stl_uninitialized.h>
# endif
# ifndef __STL_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].
*/
/*
* In previous versions of deque, node_size was fixed by the
* implementation. In this version, however, users can select
* the node size. Deque has three template parameters; the third,
* a number of type size_t, is the number of elements per node.
* If the third template parameter is 0 (which is the default),
* then deque will use a default node size.
*
* The only reason for using an alternate node size is if your application
* requires a different performance tradeoff than the default. If,
* for example, your program contains many deques each of which contains
* only a few elements, then you might want to save memory (possibly
* by sacrificing some speed) by using smaller nodes.
*
* Unfortunately, some compilers have trouble with non-type template
* parameters; stl_config.h defines __STL_NON_TYPE_TMPL_PARAM_BUG if
* that is the case. If your compiler is one of them, then you will
* not be able to use alternate node sizes; you will have to use the
* default value.
*/
# undef __deque__
# undef deque
# if defined ( __STL_NO_DEFAULT_NON_TYPE_PARAM )
# define deque __deque
# define __deque__ __deque
# else
# define __deque__ __FULL_NAME(deque)
# define deque __WORKAROUND_RENAME(deque)
# endif
__STL_BEGIN_NAMESPACE
#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma set woff 1174
#pragma set woff 1375
#endif
# if defined ( __STL_USE_ABBREVS )
# define __deque_iterator _dQ__It
# define _Buf_traits _dQ__BTr
# define _Deque_iterator _Dq__It
# endif
// Note: this function is simply a kludge to work around several compilers'
// bugs in handling constant expressions.
inline size_t
__deque_buf_size(size_t __n, size_t size)
{
return __n != 0 ? __n : (size < 512 ? size_t(512 / size) : size_t(1));
}
// this helper class is needed to pass deque not-type parameter
// to its iterators.
template <class _Tp, size_t _BufSize>
struct _Buf_size_traits {
public:
enum _Constants {
_blocksize = 512,
_buf_size = (_BufSize != 0 ? _BufSize : (sizeof(_Tp) < (size_t)_blocksize ?
( (size_t)_blocksize / sizeof(_Tp)) : size_t(1))),
_byte_buf_size = sizeof(_Tp)*_buf_size
};
static size_t buffer_size() { return (size_t)_buf_size; }
};
# ifdef __STL_DEBUG
template <class _Tp>
bool __Deq_dereferenceable(const void* __ptr, _Tp*);
template <class _Tp>
bool __Deq_nonsingular(const void* __ptr, _Tp*);
# endif
template <class _Tp, class __bufsiz>
# if defined ( __STL_DEBUG )
struct _Deque_iterator_base : public __owned_link {
# else
struct _Deque_iterator_base {
# endif
enum { __buffer_size = __bufsiz::_buf_size } ;
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, __bufsiz> _Self;
value_type* _M_cur;
value_type* _M_first;
value_type* _M_last;
_Map_pointer _M_node;
# if defined ( __STL_DEBUG )
bool _M_unsafe;
_Deque_iterator_base(const __owned_list* __root, value_type* __x, _Map_pointer __y)
: __owned_link(__root),_M_cur(__x), _M_first(*__y), _M_last(*__y + __buffer_size),
_M_node(__y), _M_unsafe(false) {}
_Deque_iterator_base() : __owned_link(0), _M_cur(0), _M_first(0), _M_last(0),
_M_node(0), _M_unsafe(false) {}
# else
_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) {}
# endif
difference_type _M_subtract(const _Self& __x) const {
__stl_debug_check(__check_same_owner(*this,__x));
return difference_type(__buffer_size) * (_M_node - __x._M_node - 1) +
(_M_cur - _M_first) + (__x._M_last - __x._M_cur);
}
void _M_increment() {
// ++_M_cur;
if (++_M_cur == _M_last) {
_M_set_node(_M_node + 1);
_M_cur = _M_first;
}
__stl_debug_check(__Deq_nonsingular(this,(value_type*)0));
}
void _M_decrement() {
if (_M_cur == _M_first) {
_M_set_node(_M_node - 1);
_M_cur = _M_last;
}
--_M_cur;
__stl_debug_check(__Deq_nonsingular(this,(value_type*)0));
}
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));
}
__stl_debug_check(__Deq_nonsingular(this,(_Tp*)0));
}
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, class __bufsiz>
struct _Deque_iterator : public _Deque_iterator_base< _Tp, __bufsiz> {
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, __bufsiz> _Base;
typedef _Deque_iterator<_Tp, _Traits, __bufsiz> _Self;
typedef _Deque_iterator<_Tp, _Nonconst_traits<_Tp>,__bufsiz> _Nonconst_self;
typedef _Deque_iterator<_Tp, _Const_traits<_Tp>,__bufsiz> _Const_self;
# ifdef __STL_HAS_NAMESPACES
__STL_USING_BASE_MEMBER _Base::_M_cur;
__STL_USING_BASE_MEMBER _Base::_M_first;
__STL_USING_BASE_MEMBER _Base::_M_last;
__STL_USING_BASE_MEMBER _Base::_M_node;
# endif
# if defined ( __STL_DEBUG )
# ifdef __STL_HAS_NAMESPACES
__STL_USING_BASE_MEMBER _Base::_M_unsafe;
# endif
_Deque_iterator(const __owned_list* __root, value_type* __x, _Map_pointer __y):
_Deque_iterator_base<value_type, __bufsiz>(__root,__x,__y) {}
# else /* __STL_DEBUG */
_Deque_iterator(value_type* __x, _Map_pointer __y) :
_Deque_iterator_base<value_type, __bufsiz>(__x,__y) {}
# endif
_Deque_iterator() {}
_Deque_iterator(const _Nonconst_self& __x) :
_Deque_iterator_base<value_type, __bufsiz>(__x) {}
reference operator*() const {
__stl_debug_check(__Deq_dereferenceable((const void*)this, (value_type*)0));
return *_M_cur;
}
__STL_DEFINE_ARROW_OPERATOR
difference_type operator-(const _Self& __x) const { return _M_subtract(__x); }
_Self& operator++() { _M_increment(); return *this; }
_Self operator++(int) {
_Self __tmp = *this;
++*this;
return __tmp;
}
_Self& operator--() { _M_decrement(); return *this; }
_Self operator--(int) {
_Self __tmp = *this;
--*this;
return __tmp;
}
_Self& operator+=(difference_type __n) { _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); }
};
#ifdef __STL_USE_SEPARATE_RELOPS_NAMESPACE
template <class _Tp, class __bufsiz>
inline bool
operator==(const _Deque_iterator_base<_Tp,__bufsiz>& __x,
const _Deque_iterator_base<_Tp,__bufsiz>& __y) {
__stl_debug_check(__check_same_owner_or_null(__x, __y));
return __x._M_cur == __y._M_cur;
}
template <class _Tp, class __bufsiz>
inline bool
operator < (const _Deque_iterator_base<_Tp,__bufsiz>& __x,
const _Deque_iterator_base<_Tp,__bufsiz>& __y) {
__stl_debug_check(__check_same_owner(__x, __y));
return (__x._M_node == __y._M_node) ?
(__x._M_cur < __y._M_cur) : (__x._M_node < __y._M_node);
}
template <class _Tp, class __bufsiz>
inline bool
operator!=(const _Deque_iterator_base<_Tp,__bufsiz>& __x,
const _Deque_iterator_base<_Tp,__bufsiz>& __y) {
__stl_debug_check(__check_same_owner_or_null(__x, __y));
return __x._M_cur != __y._M_cur;
}
template <class _Tp, class __bufsiz>
inline bool
operator>(const _Deque_iterator_base<_Tp,__bufsiz>& __x,
const _Deque_iterator_base<_Tp,__bufsiz>& __y) {
return __y < __x;
}
template <class _Tp, class __bufsiz>
inline bool operator>=(const _Deque_iterator_base<_Tp,__bufsiz>& __x,
const _Deque_iterator_base<_Tp,__bufsiz>& __y) {
return !(__x < __y);
}
template <class _Tp, class __bufsiz>
inline bool operator<=(const _Deque_iterator_base<_Tp,__bufsiz>& __x,
const _Deque_iterator_base<_Tp,__bufsiz>& __y) {
return !(__y < __x);
}
# else
template <class _Tp, class _Traits1, class _Traits2, class __bufsiz>
inline bool
operator==(const _Deque_iterator<_Tp, _Traits1,__bufsiz>& __x,
const _Deque_iterator<_Tp, _Traits2,__bufsiz>& __y) {
__stl_debug_check(__check_same_owner_or_null(__x, __y));
return __x._M_cur == __y._M_cur;
}
template <class _Tp, class _Traits1, class _Traits2, class __bufsiz>
inline bool
operator < (const _Deque_iterator<_Tp, _Traits1,__bufsiz>& __x,
const _Deque_iterator<_Tp, _Traits2,__bufsiz>& __y) {
__stl_debug_check(__check_same_owner(__x, __y));
return (__x._M_node == __y._M_node) ?
(__x._M_cur < __y._M_cur) : (__x._M_node < __y._M_node);
}
template <class _Tp, class __bufsiz>
inline bool
operator!=(const _Deque_iterator<_Tp, _Nonconst_traits<_Tp>,__bufsiz>& __x,
const _Deque_iterator<_Tp, _Const_traits<_Tp>,__bufsiz>& __y) {
__stl_debug_check(__check_same_owner_or_null(__x, __y));
return __x._M_cur != __y._M_cur;
}
template <class _Tp, class __bufsiz>
inline bool
operator>(const _Deque_iterator<_Tp, _Nonconst_traits<_Tp>,__bufsiz>& __x,
const _Deque_iterator<_Tp, _Const_traits<_Tp>, __bufsiz>& __y) {
return __y < __x;
}
template <class _Tp, class __bufsiz>
inline bool operator>=(const _Deque_iterator<_Tp, _Nonconst_traits<_Tp>,__bufsiz>& __x,
const _Deque_iterator<_Tp, _Const_traits<_Tp>, __bufsiz>& __y) {
return !(__x < __y);
}
template <class _Tp, class __bufsiz>
inline bool operator<=(const _Deque_iterator<_Tp, _Nonconst_traits<_Tp>,__bufsiz>& __x,
const _Deque_iterator<_Tp, _Const_traits<_Tp>, __bufsiz>& __y) {
return !(__y < __x);
}
# endif
#ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
template <class _Tp, class _Traits, class __bufsiz>
inline _Tp*
value_type(const _Deque_iterator<_Tp, _Traits,__bufsiz>&) {
return (_Tp*)0;
}
template <class _Tp, class _Traits, class __bufsiz>
inline random_access_iterator_tag
iterator_category(const _Deque_iterator<_Tp, _Traits,__bufsiz>&) {
return random_access_iterator_tag();
}
template <class _Tp, class _Traits, class __bufsiz>
inline ptrdiff_t*
distance_type(const _Deque_iterator<_Tp, _Traits,__bufsiz>&) {
return 0;
}
#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
// 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, size_t __bufsiz>
class _Deque_base {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -