《STL原始碼剖析》-- stl_stack.h

凝霜發表於2011-07-31
// Filename:    stl_stack.h

// Comment By:  凝霜
// E-mail:      mdl2009@vip.qq.com
// Blog:        http://blog.csdn.net/mdl13412

////////////////////////////////////////////////////////////////////////////////
// stack是一種先進後出(First In Last Out, FILO)的資料結構, 其只有一個出口
// 支援對棧頂元素的追加, 彈出, 獲取, 但是不提供對其它位置元素的訪問
////////////////////////////////////////////////////////////////////////////////
// 以下為使用deque時的佈局
//
// 棧底                      當前棧頂                         預留的記憶體邊界
// ↓                            ↓                                 ↓
// --------------------------------------------------------------------
// |   |   | ...... |   |   |   |   |   |   |   | ......  |   |   | X |
// --------------------------------------------------------------------
//                              ↑   ↑                             ↑
//                              |   |                             |
//                              |   -------------------------------
//                              |    這裡是尚未使用的預留記憶體, 可能為0
//                              |
//                       僅支援在這裡進行push(), pop(), top()操作
////////////////////////////////////////////////////////////////////////////////

/*
 *
 * 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,1997
 * 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.
 */

/* NOTE: This is an internal header file, included by other STL headers.
 *   You should not attempt to use it directly.
 */

#ifndef __SGI_STL_INTERNAL_STACK_H
#define __SGI_STL_INTERNAL_STACK_H

__STL_BEGIN_NAMESPACE

// 如果編譯器不能根據前面模板引數推匯出後面使用的預設引數型別,
// 那麼就需要手工指定, 本實作stack內部容器預設使用deque
// 選用deque可以在儲存空間不足時可以動態增加, 而且代價很低
#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class T, class Sequence = deque<T> >
#else
template <class T, class Sequence>
#endif
class stack
{
  // 特化的全域性運算子, 提供operator==和<過載則構建出所有運算子
  // 其具體細節見<stl_pair.h>中的說明
  friend bool operator== __STL_NULL_TMPL_ARGS (const stack&, const stack&);
  friend bool operator< __STL_NULL_TMPL_ARGS (const stack&, const stack&);

public:
  // 由於stack僅支援對棧頂元素的操作, 所以不定義STL要求的
  // pointer, iterator, difference_type
  typedef typename Sequence::value_type value_type;
  typedef typename Sequence::size_type size_type;
  typedef typename Sequence::reference reference;
  typedef typename Sequence::const_reference const_reference;

protected:
  Sequence c;   // 這個是我們實際維護的容器

public:
  // 下面的操作完全使用內部容器的成員函式實現
  // 這再次體現了STL高度的可複用性:-)

  // 判斷stack是否為空
  bool empty() const { return c.empty(); }

  // stack中元素個數
  size_type size() const { return c.size(); }

  // 返回棧頂元素, 注意這裡返回的是引用!!!
  reference top() { return c.back(); }
  const_reference top() const { return c.back(); }

  // 在棧頂追加新元素
  void push(const value_type& x) { c.push_back(x); }

  // 移除棧頂元素, 注意不返回元素的引用,
  // 很多初學者隨機用此容器時經常誤認為pop()操作同時會返回棧頂元素的引用
  void pop() { c.pop_back(); }
};

// 判斷兩個stack是否相等, 就要測試其內部維護容器是否相等
// x.c == y.c會呼叫容器過載的operator ==
template <class T, class Sequence>
bool operator==(const stack<T, Sequence>& x, const stack<T, Sequence>& y)
{
  return x.c == y.c;
}

template <class T, class Sequence>
bool operator<(const stack<T, Sequence>& x, const stack<T, Sequence>& y)
{
  return x.c < y.c;
}

__STL_END_NAMESPACE

#endif /* __SGI_STL_INTERNAL_STACK_H */

// Local Variables:
// mode:C++
// End:

相關文章