STL学习笔记之容器--slist

slist是SGI STL里面的一个单向链表,不过这个不属于标准定义,所以,G++下面不能用,另外看了下VS2008里面也没有。不过看到书上这一部分讲解,还是配合SGI的代码做一个了解。一般情况下使用标准的list就可以满足要求了,而且会更加方便,list的分析可以参考STL笔记之list一文。因为slist是单向链表,所以它的迭代器类型为forward_iterator。

1. slist节点设计
链表最基本的结构就是指针域和数据域,对单向链表的节点而言,其拥有一个next指针,指向下一个节点,同时拥有一个数据域用于存储节点的数据。slist在这里做了一个分层处理,基类_Slist_node_base只有next指针,用于链表的构造,子类_Slist_node继承自_Slist_node_base,同时拥有一个存储数据的成员。

// node 基类
struct _Slist_node_base
{
  _Slist_node_base* _M_next;
};
 
// slist_node
template <class _Tp>
struct _Slist_node : public _Slist_node_base
{
  _Tp _M_data;
};

一些基本的函数操作,基于_Slist_node_base指针类型。可以看到因为单链表只能往后迭代,所以很多操作都没有提供,即使提供了,也是非常低效的操作,需要从头结点开始遍历。

// 在prev后面插入新节点,返回指向新的节点的指针
inline _Slist_node_base*
__slist_make_link(_Slist_node_base* __prev_node,
                  _Slist_node_base* __new_node)
{
  __new_node->_M_next = __prev_node->_M_next;
  __prev_node->_M_next = __new_node;
  return __new_node;
}
 
// 返回指定节点的上一个节点,需要从head开始遍历
inline _Slist_node_base* 
__slist_previous(_Slist_node_base* __head,
                 const _Slist_node_base* __node)
{
  while (__head && __head->_M_next != __node)
    __head = __head->_M_next;
  return __head;
}
 
// 拼接操作:将[before_first->next, before_last]拼接到pos后
inline void __slist_splice_after(_Slist_node_base* __pos,
                                 _Slist_node_base* __before_first,
                                 _Slist_node_base* __before_last)
{
  if (__pos != __before_first && __pos != __before_last) {
    _Slist_node_base* __first = __before_first->_M_next;
    _Slist_node_base* __after = __pos->_M_next;
    __before_first->_M_next = __before_last->_M_next;
    __pos->_M_next = __first;
    __before_last->_M_next = __after;
  }
}
 
// 将head指向的链表拼接到pos之后
inline void
__slist_splice_after(_Slist_node_base* __pos, _Slist_node_base* __head)
{
  _Slist_node_base* __before_last = __slist_previous(__head, 0);
  if (__before_last != __head) {
    _Slist_node_base* __after = __pos->_M_next;
    __pos->_M_next = __head->_M_next;
    __head->_M_next = 0;
    __before_last->_M_next = __after;
  }
}
 
// 翻转链表
inline _Slist_node_base* __slist_reverse(_Slist_node_base* __node)
{
  _Slist_node_base* __result = __node;
  __node = __node->_M_next;
  __result->_M_next = 0;
  while(__node) {
    _Slist_node_base* __next = __node->_M_next;
    __node->_M_next = __result;
    __result = __node;
    __node = __next;
  }
  return __result;
}
 
// 求链表的节点个数
inline size_t __slist_size(_Slist_node_base* __node)
{
  size_t __result = 0;
  for ( ; __node != 0; __node = __node->_M_next)
    ++__result;
  return __result;
}

2. slist迭代器设计
slist的迭代器属于forward iterator,只能往前迭代,因此除了迭代器的基本操作之外,只实现了operator++操作。

// iterator基类
struct _Slist_iterator_base
{
  typedef size_t               size_type;
  typedef ptrdiff_t            difference_type;
  // 迭代器类型为forward_iterator
  typedef forward_iterator_tag iterator_category;
 
  _Slist_node_base* _M_node;
 
  _Slist_iterator_base(_Slist_node_base* __x) : _M_node(__x) {}
 
  // 只提供往前访问的方法
  void _M_incr() { _M_node = _M_node->_M_next; }
 
  bool operator==(const _Slist_iterator_base& __x) const {
    return _M_node == __x._M_node;
  }
 
  bool operator!=(const _Slist_iterator_base& __x) const {
    return _M_node != __x._M_node;
  }
};
 
// 迭代器定义
template <class _Tp, class _Ref, class _Ptr>
struct _Slist_iterator : public _Slist_iterator_base
{
  typedef _Slist_iterator<_Tp, _Tp&, _Tp*>             iterator;
  typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
  typedef _Slist_iterator<_Tp, _Ref, _Ptr>             _Self;
 
  typedef _Tp              value_type;
  typedef _Ptr             pointer;
  typedef _Ref             reference;
  typedef _Slist_node<_Tp> _Node;
 
  _Slist_iterator(_Node* __x) : _Slist_iterator_base(__x) {}
  _Slist_iterator() : _Slist_iterator_base(0) {}
  _Slist_iterator(const iterator& __x) : _Slist_iterator_base(__x._M_node) {}
 
  reference operator*() const { return ((_Node*) _M_node)->_M_data; }
  pointer operator->() const { return &(operator*()); }
 
  _Self& operator++()
  {
    _M_incr();
    return *this;
  }
 
  _Self operator++(int)
  {
    _Self __tmp = *this;
    _M_incr();
    return __tmp;
  }
};

3. slist设计
slist也是分层的设计,_Slist_base中定义了一个类型为_Slist_node_base的_M_head成员,head.next即指向链表的第一个节点。

template <class _Tp, class _Alloc> 
struct _Slist_base {
  typedef _Alloc allocator_type;
  allocator_type get_allocator() const { return allocator_type(); }
 
  // 构造函数,将head.next置为空
  _Slist_base(const allocator_type&) { _M_head._M_next = 0; }
  // 析构函数, 通过调用_M_erase_after删除所有节点
  ~_Slist_base() { _M_erase_after(&_M_head, 0); }
 
protected:
  typedef simple_alloc<_Slist_node<_Tp>, _Alloc> _Alloc_type;
  _Slist_node<_Tp>* _M_get_node() { return _Alloc_type::allocate(1); }
  void _M_put_node(_Slist_node<_Tp>* __p) { _Alloc_type::deallocate(__p, 1); }
 
  // 删除指定节点后面的节点
  _Slist_node_base* _M_erase_after(_Slist_node_base* __pos)
  {
    _Slist_node<_Tp>* __next = (_Slist_node<_Tp>*) (__pos->_M_next);
    _Slist_node_base* __next_next = __next->_M_next;
    __pos->_M_next = __next_next;
    destroy(&__next->_M_data);
    _M_put_node(__next);
    return __next_next;
  }
 
  _Slist_node_base* _M_erase_after(_Slist_node_base*, _Slist_node_base*);
protected:
  _Slist_node_base _M_head;
};  
 
// 删除指定节点之后的节点:区间[before_first->next, last)
template <class _Tp, class _Alloc> 
_Slist_node_base*
_Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
                                        _Slist_node_base* __last_node) {
  _Slist_node<_Tp>* __cur = (_Slist_node<_Tp>*) (__before_first->_M_next);
  while (__cur != __last_node) {
    _Slist_node<_Tp>* __tmp = __cur;
    __cur = (_Slist_node<_Tp>*) __cur->_M_next;
    destroy(&__tmp->_M_data);
    _M_put_node(__tmp);
  }
  __before_first->_M_next = __last_node;
  return __last_node;
}

slist基本结构定义:

template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >
class slist : private _Slist_base<_Tp,_Alloc>
{
private:
  typedef _Slist_base<_Tp,_Alloc> _Base;
 
public:
  typedef _Tp               value_type;
  typedef value_type*       pointer;
  typedef const value_type* const_pointer;
  typedef value_type&       reference;
  typedef const value_type& const_reference;
  typedef size_t            size_type;
  typedef ptrdiff_t         difference_type;
 
  typedef _Slist_iterator<_Tp, _Tp&, _Tp*>             iterator;
  typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
 
  typedef typename _Base::allocator_type allocator_type;
 
  allocator_type get_allocator() const { return _Base::get_allocator(); }
 
private:
  typedef _Slist_node<_Tp>      _Node;
  typedef _Slist_node_base      _Node_base;
  typedef _Slist_iterator_base  _Iterator_base;
 
  _Node* _M_create_node(const value_type& __x) {
    _Node* __node = this->_M_get_node();
    __STL_TRY {
      construct(&__node->_M_data, __x);
      __node->_M_next = 0;
    }
    __STL_UNWIND(this->_M_put_node(__node));
    return __node;
  }
 
  _Node* _M_create_node() {
    _Node* __node = this->_M_get_node();
    __STL_TRY {
      construct(&__node->_M_data);
      __node->_M_next = 0;
    }
    __STL_UNWIND(this->_M_put_node(__node));
    return __node;
  }
 
public:
  iterator begin() { return iterator((_Node*)this->_M_head._M_next); }
 
  iterator end() { return iterator(0); }
 
  size_type size() const { return __slist_size(this->_M_head._M_next); }
 
  size_type max_size() const { return size_type(-1); }
 
  bool empty() const { return this->_M_head._M_next == 0; }
 
  void swap(slist& __x)
    { __STD::swap(this->_M_head._M_next, __x._M_head._M_next); }
 
public:
  reference front() { return ((_Node*) this->_M_head._M_next)->_M_data; }
  void push_front(const value_type& __x)   {
    __slist_make_link(&this->_M_head, _M_create_node(__x));
  }
 
  void pop_front() {
    _Node* __node = (_Node*) this->_M_head._M_next;
    this->_M_head._M_next = __node->_M_next;
    destroy(&__node->_M_data);
    this->_M_put_node(__node);
  }
 
  iterator previous(const_iterator __pos) {
    return iterator((_Node*) __slist_previous(&this->_M_head, __pos._M_node));
  }
 
private:
  _Node* _M_insert_after(_Node_base* __pos, const value_type& __x) {
    return (_Node*) (__slist_make_link(__pos, _M_create_node(__x)));
  }
 
public:
  void reverse() { 
    if (this->_M_head._M_next)
      this->_M_head._M_next = __slist_reverse(this->_M_head._M_next);
  }
};

4. slist排序
slist的sort代码与list的sort代码非常类似,可以参考list sort一节。

template <class _Tp, class _Alloc>
void slist<_Tp,_Alloc>::sort()
{
  if (this->_M_head._M_next && this->_M_head._M_next->_M_next) {
    slist __carry;
    slist __counter[64];
    int __fill = 0;
 
    while (!empty()) {
      __slist_splice_after(&__carry._M_head,
                           &this->_M_head, this->_M_head._M_next);
      int __i = 0;
      while (__i < __fill && !__counter[__i].empty()) {
        __counter[__i].merge(__carry);
        __carry.swap(__counter[__i]);
        ++__i;
      }
      __carry.swap(__counter[__i]);
      if (__i == __fill)
        ++__fill;
    }
 
    for (int __i = 1; __i < __fill; ++__i)
      __counter[__i].merge(__counter[__i-1]);
    this->swap(__counter[__fill-1]);
  }
}

至此,STL常见序列容器基本分析完毕,接下来准备看看关联容器,以及记录一下使用STL过程中遇到的一些“坑”(比如最近使用sort导致了core dump)。


原文链接:http://www.programlife.net/stl-slist.html,作者:代码疯子

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值