原帖由
塑料袋 于 2008-3-7 23:38 发表
libstdc++不了解
不过坦白说,即使搞通这个libstdc++,也不足以觉得自己很牛X.
来,让我们来欣赏一下libstdc++中的list的源代码艺术品,开始YY:模板用的那个cool、运算符重载的那个不多不少、变量名和函数名称命名的那个艺术,其实,看gcc-4.1.2/libstdc++-v3是会让人GC的,又开始BT了
namespace _GLIBCXX_STD
{
// Supporting structures are split into common and templated types; the
// latter publicly inherits from the former in an effort to reduce code
// duplication. This results in some "needless" static_cast'ing later on,
// but it's all safe downcasting.
/// @if maint Common part of a node in the %list. @endif
struct _List_node_base
{
_List_node_base* _M_next; ///< Self-explanatory
_List_node_base* _M_prev; ///< Self-explanatory
static void
swap(_List_node_base& __x, _List_node_base& __y);
void
transfer(_List_node_base * const __first,
_List_node_base * const __last);
void
reverse();
void
hook(_List_node_base * const __position);
void
unhook();
};
/// @if maint An actual node in the %list. @endif
template<typename _Tp>
struct _List_node : public _List_node_base
{
_Tp _M_data; ///< User's data.
};
template<typename _Tp>
struct _List_iterator
{
typedef _List_iterator<_Tp> _Self;
typedef _List_node<_Tp> _Node;
typedef ptrdiff_t difference_type;
typedef std::bidirectional_iterator_tag iterator_category;
typedef _Tp value_type;
typedef _Tp* pointer;
typedef _Tp& reference;
_List_iterator()
: _M_node() { }
explicit
_List_iterator(_List_node_base* __x)
: _M_node(__x) { }
// Must downcast from List_node_base to _List_node to get to _M_data.
reference
operator*() const
{ return static_cast<_Node*>(_M_node)->_M_data; }
pointer
operator->() const
{ return &static_cast<_Node*>(_M_node)->_M_data; }
_Self&
operator++()
{
_M_node = _M_node->_M_next;
return *this;
}
_Self
operator++(int)
{
_Self __tmp = *this;
_M_node = _M_node->_M_next;
return __tmp;
}
_Self&
operator--()
{
_M_node = _M_node->_M_prev;
return *this;
}
_Self
operator--(int)
{
_Self __tmp = *this;
_M_node = _M_node->_M_prev;
return __tmp;
}
bool
operator==(const _Self& __x) const
{ return _M_node == __x._M_node; }
bool
operator!=(const _Self& __x) const
{ return _M_node != __x._M_node; }
// The only member points to the %list element.
_List_node_base* _M_node;
};
template<typename _Tp>
struct _List_const_iterator
{
typedef _List_const_iterator<_Tp> _Self;
typedef const _List_node<_Tp> _Node;
typedef _List_iterator<_Tp> iterator;
typedef ptrdiff_t difference_type;
typedef std::bidirectional_iterator_tag iterator_category;
typedef _Tp value_type;
typedef const _Tp* pointer;
typedef const _Tp& reference;
_List_const_iterator()
: _M_node() { }
explicit
_List_const_iterator(const _List_node_base* __x)
: _M_node(__x) { }
_List_const_iterator(const iterator& __x)
: _M_node(__x._M_node) { }
// Must downcast from List_node_base to _List_node to get to
// _M_data.
reference
operator*() const
{ return static_cast<_Node*>(_M_node)->_M_data; }
pointer
operator->() const
{ return &static_cast<_Node*>(_M_node)->_M_data; }
_Self&
operator++()
{
_M_node = _M_node->_M_next;
return *this;
}
_Self
operator++(int)
{
_Self __tmp = *this;
_M_node = _M_node->_M_next;
return __tmp;
}
_Self&
operator--()
{
_M_node = _M_node->_M_prev;
return *this;
}
_Self
operator--(int)
{
_Self __tmp = *this;
_M_node = _M_node->_M_prev;
return __tmp;
}
bool
operator==(const _Self& __x) const
{ return _M_node == __x._M_node; }
bool
operator!=(const _Self& __x) const
{ return _M_node != __x._M_node; }
// The only member points to the %list element.
const _List_node_base* _M_node;
};
template<typename _Val>
inline bool
operator==(const _List_iterator<_Val>& __x,
const _List_const_iterator<_Val>& __y)
{ return __x._M_node == __y._M_node; }
template<typename _Val>
inline bool
operator!=(const _List_iterator<_Val>& __x,
const _List_const_iterator<_Val>& __y)
{ return __x._M_node != __y._M_node; }
template<typename _Tp, typename _Alloc>
class _List_base
{
protected:
typedef typename _Alloc::template rebind<_List_node<_Tp> >::other
_Node_alloc_type;
typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
struct _List_impl
: public _Node_alloc_type
{
_List_node_base _M_node;
_List_impl(const _Node_alloc_type& __a)
: _Node_alloc_type(__a), _M_node()
{ }
};
_List_impl _M_impl;
_List_node<_Tp>*
_M_get_node()
{ return _M_impl._Node_alloc_type::allocate(1); }
void
_M_put_node(_List_node<_Tp>* __p)
{ _M_impl._Node_alloc_type::deallocate(__p, 1); }
public:
typedef _Alloc allocator_type;
_Tp_alloc_type
_M_get_Tp_allocator() const
{ return *static_cast<const _Node_alloc_type*>(&this->_M_impl); }
allocator_type
get_allocator() const
{ return _M_get_Tp_allocator(); }
_List_base(const allocator_type& __a)
: _M_impl(__a)
{ _M_init(); }
// This is what actually destroys the list.
~_List_base()
{ _M_clear(); }
void
_M_clear();
void
_M_init()
{
this->_M_impl._M_node._M_next = &this->_M_impl._M_node;
this->_M_impl._M_node._M_prev = &this->_M_impl._M_node;
}
};
template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
class list : protected _List_base<_Tp, _Alloc>
{
// concept requirements
typedef typename _Alloc::value_type _Alloc_value_type;
__glibcxx_class_requires(_Tp, _SGIAssignableConcept)
__glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
typedef _List_base<_Tp, _Alloc> _Base;
typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
public:
typedef _Tp value_type;
typedef typename _Tp_alloc_type::pointer pointer;
typedef typename _Tp_alloc_type::const_pointer const_pointer;
typedef typename _Tp_alloc_type::reference reference;
typedef typename _Tp_alloc_type::const_reference const_reference;
typedef _List_iterator<_Tp> iterator;
typedef _List_const_iterator<_Tp> const_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef _Alloc allocator_type;
protected:
// Note that pointers-to-_Node's can be ctor-converted to
// iterator types.
typedef _List_node<_Tp> _Node;
using _Base::_M_impl;
using _Base::_M_put_node;
using _Base::_M_get_node;
using _Base::_M_get_Tp_allocator;
_Node*
_M_create_node(const value_type& __x)
{
_Node* __p = this->_M_get_node();
try
{
_M_get_Tp_allocator().construct(&__p->_M_data, __x);
}
catch(...)
{
_M_put_node(__p);
__throw_exception_again;
}
return __p;
}
public:
// [23.2.2.1] construct/copy/destroy
// (assign() and get_allocator() are also listed in this section)
explicit
list(const allocator_type& __a = allocator_type())
: _Base(__a) { }
explicit
list(size_type __n, const value_type& __value = value_type(),
const allocator_type& __a = allocator_type())
: _Base(__a)
{ this->insert(begin(), __n, __value); }
list(const list& __x)
: _Base(__x.get_allocator())
{ this->insert(begin(), __x.begin(), __x.end()); }
template<typename _InputIterator>
list(_InputIterator __first, _InputIterator __last,
const allocator_type& __a = allocator_type())
: _Base(__a)
{ this->insert(begin(), __first, __last); }
list&
operator=(const list& __x);
void
assign(size_type __n, const value_type& __val)
{ _M_fill_assign(__n, __val); }
template<typename _InputIterator>
void
assign(_InputIterator __first, _InputIterator __last)
{
// Check whether it's an integral type. If so, it's not an iterator.
typedef typename std::__is_integer<_InputIterator>::__type _Integral;
_M_assign_dispatch(__first, __last, _Integral());
}
/// Get a copy of the memory allocation object.
allocator_type
get_allocator() const
{ return _Base::get_allocator(); }
// iterators
iterator
begin()
{ return iterator(this->_M_impl._M_node._M_next); }
const_iterator
begin() const
{ return const_iterator(this->_M_impl._M_node._M_next); }
iterator
end()
{ return iterator(&this->_M_impl._M_node); }
const_iterator
end() const
{ return const_iterator(&this->_M_impl._M_node); }
reverse_iterator
rbegin()
{ return reverse_iterator(end()); }
const_reverse_iterator
rbegin() const
{ return const_reverse_iterator(end()); }
reverse_iterator
rend()
{ return reverse_iterator(begin()); }
const_reverse_iterator
rend() const
{ return const_reverse_iterator(begin()); }
// [23.2.2.2] capacity
bool
empty() const
{ return this->_M_impl._M_node._M_next == &this->_M_impl._M_node; }
size_type
size() const
{ return std::distance(begin(), end()); }
size_type
max_size() const
{ return size_type(-1); }
void
resize(size_type __new_size, value_type __x = value_type());
// element access
reference
front()
{ return *begin(); }
const_reference
front() const
{ return *begin(); }
reference
back()
{
iterator __tmp = end();
--__tmp;
return *__tmp;
}
const_reference
back() const
{
const_iterator __tmp = end();
--__tmp;
return *__tmp;
}
// [23.2.2.3] modifiers
void
push_front(const value_type& __x)
{ this->_M_insert(begin(), __x); }
void
pop_front()
{ this->_M_erase(begin()); }
void
push_back(const value_type& __x)
{ this->_M_insert(end(), __x); }
void
pop_back()
{ this->_M_erase(iterator(this->_M_impl._M_node._M_prev)); }
iterator
insert(iterator __position, const value_type& __x);
void
insert(iterator __position, size_type __n, const value_type& __x)
{ _M_fill_insert(__position, __n, __x); }
template<typename _InputIterator>
void
insert(iterator __position, _InputIterator __first,
_InputIterator __last)
{
// Check whether it's an integral type. If so, it's not an iterator.
typedef typename std::__is_integer<_InputIterator>::__type _Integral;
_M_insert_dispatch(__position, __first, __last, _Integral());
}
iterator
erase(iterator __position);
iterator
erase(iterator __first, iterator __last)
{
while (__first != __last)
__first = erase(__first);
return __last;
}
void
swap(list& __x)
{ _List_node_base::swap(this->_M_impl._M_node, __x._M_impl._M_node); }
void
clear()
{
_Base::_M_clear();
_Base::_M_init();
}
// [23.2.2.4] list operations
void
splice(iterator __position, list& __x)
{
if (!__x.empty())
this->_M_transfer(__position, __x.begin(), __x.end());
}
void
splice(iterator __position, list&, iterator __i)
{
iterator __j = __i;
++__j;
if (__position == __i || __position == __j)
return;
this->_M_transfer(__position, __i, __j);
}
void
splice(iterator __position, list&, iterator __first, iterator __last)
{
if (__first != __last)
this->_M_transfer(__position, __first, __last);
}
void
remove(const _Tp& __value);
template<typename _Predicate>
void
remove_if(_Predicate);
void
unique();
template<typename _BinaryPredicate>
void
unique(_BinaryPredicate);
void
merge(list& __x);
template<typename _StrictWeakOrdering>
void
merge(list&, _StrictWeakOrdering);
void
reverse()
{ this->_M_impl._M_node.reverse(); }
void
sort();
template<typename _StrictWeakOrdering>
void
sort(_StrictWeakOrdering);
protected:
// Internal assign functions follow.
// Called by the range assign to implement [23.1.1]/9
template<typename _Integer>
void
_M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
{
_M_fill_assign(static_cast<size_type>(__n),
static_cast<value_type>(__val));
}
// Called by the range assign to implement [23.1.1]/9
template<typename _InputIterator>
void
_M_assign_dispatch(_InputIterator __first, _InputIterator __last,
__false_type);
// Called by assign(n,t), and the range assign when it turns out
// to be the same thing.
void
_M_fill_assign(size_type __n, const value_type& __val);
// Internal insert functions follow.
// Called by the range insert to implement [23.1.1]/9
template<typename _Integer>
void
_M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
__true_type)
{
_M_fill_insert(__pos, static_cast<size_type>(__n),
static_cast<value_type>(__x));
}
// Called by the range insert to implement [23.1.1]/9
template<typename _InputIterator>
void
_M_insert_dispatch(iterator __pos,
_InputIterator __first, _InputIterator __last,
__false_type)
{
for (; __first != __last; ++__first)
_M_insert(__pos, *__first);
}
// Called by insert(p,n,x), and the range insert when it turns out
// to be the same thing.
void
_M_fill_insert(iterator __pos, size_type __n, const value_type& __x)
{
for (; __n > 0; --__n)
_M_insert(__pos, __x);
}
// Moves the elements from [first,last) before position.
void
_M_transfer(iterator __position, iterator __first, iterator __last)
{ __position._M_node->transfer(__first._M_node, __last._M_node); }
// Inserts new element at position given and with value given.
void
_M_insert(iterator __position, const value_type& __x)
{
_Node* __tmp = _M_create_node(__x);
__tmp->hook(__position._M_node);
}
// Erases element at position given.
void
_M_erase(iterator __position)
{
__position._M_node->unhook();
_Node* __n = static_cast<_Node*>(__position._M_node);
_M_get_Tp_allocator().destroy(&__n->_M_data);
_M_put_node(__n);
}
};
template<typename _Tp, typename _Alloc>
inline bool
operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
{
typedef typename list<_Tp, _Alloc>::const_iterator const_iterator;
const_iterator __end1 = __x.end();
const_iterator __end2 = __y.end();
const_iterator __i1 = __x.begin();
const_iterator __i2 = __y.begin();
while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
{
++__i1;
++__i2;
}
return __i1 == __end1 && __i2 == __end2;
}
template<typename _Tp, typename _Alloc>
inline bool
operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
{ return std::lexicographical_compare(__x.begin(), __x.end(),
__y.begin(), __y.end()); }
/// Based on operator==
template<typename _Tp, typename _Alloc>
inline bool
operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
{ return !(__x == __y); }
/// Based on operator<
template<typename _Tp, typename _Alloc>
inline bool
operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
{ return __y < __x; }
/// Based on operator<
template<typename _Tp, typename _Alloc>
inline bool
operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
{ return !(__y < __x); }
/// Based on operator<
template<typename _Tp, typename _Alloc>
inline bool
operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
{ return !(__x < __y); }
/// See std::list::swap().
template<typename _Tp, typename _Alloc>
inline void
swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
{ __x.swap(__y); }
} // namespace std