《STL源码剖析》---stl_deque.h阅读笔记(2)

看完,《STL源码剖析》---stl_deque.h阅读笔记(1)后,再看代码:

G++ 2.91.57,cygnus\cygwin-b20\include\g++\stl_deque.h 完整列表
/*
 *
 * 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) 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_DEQUE_H
#define __SGI_STL_INTERNAL_DEQUE_H

/* Class 的恆長特性(invariants):
 *  對於任何 nonsingular iterator I:
 *    i.node 是 map array 中的某個元素的位址。
 *      i.node 所指內容則是一個指標,指向某個節點(緩衝區)的頭。
 *    i.first == *(i.node)
 *    i.last  == i.first + node_size(也就是 buffer_size())
 *    i.cur 是一個指標,指向範圍 [i.first, i.last) 之間。注意:
 *      這意味 i.cur 永遠是一個 dereferenceable pointer,
 *      縱使 i 是一個 past-the-end iterator.
 *  Start 和 Finish 總是 nonsingular iterators。注意:這意味
 *    empty deque 一定會有一個node,而一個具有N個元素的deque,
 *    (N 表示緩衝區大小),一定會有兩個nodes。
 *  對於start.node 和finish.node 以外的每一個node,其中的每一個元素
 *    都是一個經過初始化的物件。如果 start.node == finish.node,
 *    那麼 [start.cur, finish.cur) 都是經過初始化的物件,而該範圍以外
 *    元素則是未經初始化的空間。否則,[start.cur, start.last) 和
 *    [finish.first, finish.cur) 是經過初始化的物件,而
 *    [start.first, start.cur) 和 [finish.cur, finish.last)
 *    則是未經初始化的空間
 *  [map, map + map_size) 是一個有效的,non-empty 的範圍。
 *  [start.node, finish.node] 是一個有效的範圍,內含於
 *    [map, map + map_size) 之內。
 *  範圍 [map, map + map_size) 內的任何一個指標會指向一個經過配置的
 *    node — 若且唯若該指標在範圍 [start.node, finish.node] 之內。
 */

/*
 * 在前一版的deque中,node_size 由編譯器定死。這個版本允許使用者選擇節點
 * (node)的大小。Deque 有三個 template 參數,其中第三個是一個型別為 size_t
 * 的數值,代表每個節點(node)內含的元素個數。如果第三個 template 參數為0
 * (那是預設值),deque 就使用預設的節點大小。
 *
 * 使用不同的節點大小的唯一理由是,或許你的程式需要不同的效率並願意付出其他方
 * 面的代價。例如,假設你的程式內含許多deques,每一個都只內含一些元素,那麼
 * 你可以使用較小的 nodes 來節省記憶體(或許會因此犧牲速度)。
 *
 * 不幸的是,某些編譯器面對 non-type template 參數會有問題。stl_config.h 之
 * 中為此定義了一個 __STL_NON_TYPE_TMPL_PARAM_BUG。如果你的編譯器正是如
 * 此,你就無法使用不同的節點大小,你必須使用預設大小。
*/

__STL_BEGIN_NAMESPACE 

#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma set woff 1174
#endif

/*
此函数用来计算缓冲区的大小
 如果n不等于0,那么返回n,开发者自己决定
    否则:如果sz小于512,返回512/sz
		  如果sz大于512,返回1
*/
inline size_t __deque_buf_size(size_t n, size_t sz)
{
  return n != 0 ? n : (sz < 512 ? size_t(512 / sz) : size_t(1));
}

//deque的迭代器,它没有继承std::iterator
#ifndef __STL_NON_TYPE_TMPL_PARAM_BUG
template <class T, class Ref, class Ptr, size_t BufSiz>
struct __deque_iterator {
  typedef __deque_iterator<T, T&, T*, BufSiz>      iterator;
  typedef __deque_iterator<T, const T&, const T*, BufSiz> const_iterator;
  static size_t buffer_size() {return __deque_buf_size(BufSiz, sizeof(T)); }
#else /* __STL_NON_TYPE_TMPL_PARAM_BUG */
template <class T, class Ref, class Ptr>
struct __deque_iterator {
  typedef __deque_iterator<T, T&, T*>             iterator;
  typedef __deque_iterator<T, const T&, const T*> const_iterator;
  static size_t buffer_size() {return __deque_buf_size(0, sizeof(T)); }
#endif

  //没有继承std::iterator,自己定义5个迭代器相应的类型。

  //其占用内存连续(部分连续)迭代器类型是random_access_iterator_tag
  typedef random_access_iterator_tag iterator_category; // (1)
  typedef T value_type; 				// (2)
  typedef Ptr pointer; 				// (3)
  typedef Ref reference; 				// (4)
  typedef size_t size_type;
  typedef ptrdiff_t difference_type; 	// (5)
  typedef T** map_pointer;	//注意,是指针的指针
  //map_pointer指向中控器,中控器的存储的是指针,指向node-buf结点缓冲区

  typedef __deque_iterator self;
/*
关于下面4个元素的意义以及和map中控器、缓冲区buffer的关系,见图(2)
*/

  T* cur;	// 迭代器所指元素
  T* first;	// 迭代器所指元素所在缓冲区的开头
  T* last;	// 迭代器所指元素所在缓冲区的结尾(结尾包含在缓冲区内)
  map_pointer node;//指向中控器的结点,这个结点指向迭代器所指元素所在的缓冲区

//迭代器的构造函数

	//x是迭代器所指结点,y为中控器中的结点的值,指向x所指缓冲区
  __deque_iterator(T* x, map_pointer y)
    : cur(x), first(*y), last(*y + buffer_size()), node(y) {}
	//默认构造函数
  __deque_iterator() : cur(0), first(0), last(0), node(0) {}
    //用一个迭代器x初始化本迭代器
  __deque_iterator(const iterator& x)
    : cur(x.cur), first(x.first), last(x.last), node(x.node) {}

  //迭代器需要重载的运算符
  reference operator*() const { return *cur; }
#ifndef __SGI_STL_NO_ARROW_OPERATOR
	//重载箭头是返回地址
  pointer operator->() const { return &(operator*()); }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */
/*
两个迭代器之间的距离。这两个迭代器可能不在同一个buffer上。
*/
  difference_type operator-(const self& x) const {
    return difference_type(buffer_size()) * (node - x.node - 1) +
      (cur - first) + (x.last - x.cur);
  }

  // 參考 More Effective C++, item6: Distinguish between prefix and
  // postfix forms of increment and decrement operators.
  /*
	迭代器前进一步。
	先++cur,再判断cur==last。说明cur不会指向last的。last所指空间不存内容
  */
  self& operator++() {
    ++cur;				// 前进一步
    if (cur == last) {		// 到了所在缓冲区的尾端了
      set_node(node + 1);	// 切换到下一个缓冲区
      cur = first;			//   的第一个元素
    }
    return *this;
  }
  self operator++(int)  {
    self tmp = *this;
    ++*this;
    return tmp;
  }
  //迭代器往回走一步。
  self& operator--() {
    if (cur == first) {	// 如果在所在缓冲区的头部
      set_node(node - 1);	// 切换到前一个缓冲区
      cur = last;			//   的最后一个元素
    }
    --cur;				// 直接往回走一步
    return *this;
  }
  self operator--(int) {
    self tmp = *this;
    --*this;
    return tmp;
  }

  /*
	迭代器向前进或后退n步(取决于n的正负)。这是支持random access iterator 所必须的操作。
	如果这个操作不会是迭代器走出当前所在缓冲区,直接更改cur即可。
	如果这个操作使迭代器走出当前所在缓冲区,要计算出操作后在哪个缓冲区的哪个位置。
  */
  self& operator+=(difference_type n) {
    difference_type offset = n + (cur - first);
    if (offset >= 0 && offset < difference_type(buffer_size()))
      // 不会走出当前所在缓冲区
      cur += n;
    else {
      // 走出了当前所在缓冲区
      difference_type node_offset =
        offset > 0 ? offset / difference_type(buffer_size())
                   : -difference_type((-offset - 1) / buffer_size()) - 1;
      // 切换缓冲区
      set_node(node + node_offset);
      // 找到切换缓冲区后,迭代器所指向的元素
      cur = first + (offset - node_offset * difference_type(buffer_size()));
    }
    return *this;
  }

  self operator+(difference_type n) const {
    self tmp = *this;
    return tmp += n; // 调用operator+=
  }
	//调用operator+=
  self& operator-=(difference_type n) { return *this += -n; }

  self operator-(difference_type n) const {
    self tmp = *this;
    return tmp -= n; // 调用operator-=
  }

  reference operator[](difference_type n) const { return *(*this + n); }
  // 以上调用了operator*, operator+

  /*迭代器关于比较的运算符的重载*/
  bool operator==(const self& x) const { return cur == x.cur; }
  bool operator!=(const self& x) const { return !(*this == x); }
  bool operator<(const self& x) const {
    return (node == x.node) ? (cur < x.cur) : (node < x.node);
  }

  //切换缓冲区,更改了first和last,但是未更改cur
  void set_node(map_pointer new_node) {
    node = new_node;
    first = *new_node;
    last = first + difference_type(buffer_size());
  }
};

//编译器不支持片特性话partial specialization
#ifndef __STL_CLASS_PARTIAL_SPECIALIZATION

#ifndef __STL_NON_TYPE_TMPL_PARAM_BUG

template <class T, class Ref, class Ptr, size_t BufSiz>
inline random_access_iterator_tag
iterator_category(const __deque_iterator<T, Ref, Ptr, BufSiz>&) {
  return random_access_iterator_tag();
}

template <class T, class Ref, class Ptr, size_t BufSiz>
inline T* value_type(const __deque_iterator<T, Ref, Ptr, BufSiz>&) {
  return 0;
}

template <class T, class Ref, class Ptr, size_t BufSiz>
inline ptrdiff_t* distance_type(const __deque_iterator<T, Ref, Ptr, BufSiz>&) {
  return 0;
}

#else /* __STL_NON_TYPE_TMPL_PARAM_BUG */

template <class T, class Ref, class Ptr>
inline random_access_iterator_tag
iterator_category(const __deque_iterator<T, Ref, Ptr>&) {
  return random_access_iterator_tag();
}

template <class T, class Ref, class Ptr>
inline T* value_type(const __deque_iterator<T, Ref, Ptr>&) { return 0; }

template <class T, class Ref, class Ptr>
inline ptrdiff_t* distance_type(const __deque_iterator<T, Ref, Ptr>&) {
  return 0;
}

#endif /* __STL_NON_TYPE_TMPL_PARAM_BUG */

#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */

/*
deque的定义,默认使用alloc配置器
*/
template <class T, class Alloc = alloc, size_t BufSiz = 0>
class deque {
public:                         // Basic types
  typedef T 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;

public:                         // 迭代器
#ifndef __STL_NON_TYPE_TMPL_PARAM_BUG
  typedef __deque_iterator<T, T&, T*, BufSiz>              iterator;
  typedef __deque_iterator<T, const T&, const T&, BufSiz>  const_iterator;
#else /* __STL_NON_TYPE_TMPL_PARAM_BUG */
  typedef __deque_iterator<T, T&, T*>                      iterator;
  typedef __deque_iterator<T, const T&, const T*>          const_iterator;
#endif /* __STL_NON_TYPE_TMPL_PARAM_BUG */

#ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
  typedef reverse_iterator<const_iterator> const_reverse_iterator;
  typedef reverse_iterator<iterator> reverse_iterator;
#else /* __STL_CLASS_PARTIAL_SPECIALIZATION */
  typedef reverse_iterator<const_iterator, value_type, const_reference,
                           difference_type>
          const_reverse_iterator;
  typedef reverse_iterator<iterator, value_type, reference, difference_type>
          reverse_iterator;
#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */

protected:                      // Internal typedefs
  // 指向中控器,是指针的指针(pointer of pointer of T)
  typedef pointer* map_pointer;
  // 空间配置器,用来配置缓冲区
  typedef simple_alloc<value_type, Alloc> data_allocator;
  // 空间配置器,用来配置中控器
  typedef simple_alloc<pointer, Alloc> map_allocator;

  static size_type buffer_size() {
    return __deque_buf_size(BufSiz, sizeof(value_type));
  }
  //默认中控器大小为8
  static size_type initial_map_size() { return 8; }

protected:                      // Data members
  iterator start;		// start.cur指向deque的第一个结点
  iterator finish;	// finish.cur指向迭代器deque的最后一个结点的后一个元素

  map_pointer map;	// 指向中控器。其实是指向中控器的第一个结点。
					//	中控器是连续的,map_size定义了中控器的大小。

  size_type map_size;	// 中控器的大小。

public:                         // 对外的接口
  iterator begin() { return start; }
  iterator end() { return finish; }
  const_iterator begin() const { return start; }
  const_iterator end() const { return finish; }

  reverse_iterator rbegin() { return reverse_iterator(finish); }
  reverse_iterator rend() { return reverse_iterator(start); }
  const_reverse_iterator rbegin() const {
    return const_reverse_iterator(finish);
  }
  const_reverse_iterator rend() const {
    return const_reverse_iterator(start);
  }

  reference operator[](size_type n) {
    return start[difference_type(n)]; // 调用 __deque_iterator<>::operator[]
  }
  const_reference operator[](size_type n) const {
    return start[difference_type(n)];
  }

  reference front() { return *start; } // 调用 __deque_iterator<>::operator*

  //取出最后一个元素
  reference back() {
    iterator tmp = finish;
    --tmp;	// 调用 __deque_iterator<>::operator--
    return *tmp; 	// 调用 __deque_iterator<>::operator*
  }

  //返回第一个元素,并不删除
  const_reference front() const { return *start; }
  const_reference back() const {
    const_iterator tmp = finish;
    --tmp;
    return *tmp;
  }

  //deque中元素个数。后面有两个分号。迭代器调用了iterator::operator-
  size_type size() const { return finish - start;; }
	//deque最大容量。
  size_type max_size() const { return size_type(-1); }
  //下面调用了operator::iterator==
  bool empty() const { return finish == start; }

public:
   // Constructor, destructor.

   //默认构造函数
  deque()
    : start(), finish(), map(0), map_size(0)
    // 以上 start() 和 finish() 调用 iterator(亦即 __deque_iterator)
    // 的 default constructor,令 cur, first, last, node 都为0。
  {
    create_map_and_nodes(0);
  }
//用一个deque构建新的deque
  deque(const deque& x)
    : start(), finish(), map(0), map_size(0)
  {
    create_map_and_nodes(x.size());
    __STL_TRY {
      uninitialized_copy(x.begin(), x.end(), start);
    }
	//commit or rollback
    __STL_UNWIND(destroy_map_and_nodes());
  }
//构建大小为n,元素值为value的deque
  deque(size_type n, const value_type& value)
    : start(), finish(), map(0), map_size(0)
  {
    fill_initialize(n, value);
  }

  deque(int n, const value_type& value)
    : start(), finish(), map(0), map_size(0)
  {
    fill_initialize(n, value);
  }

  deque(long n, const value_type& value)
    : start(), finish(), map(0), map_size(0)
  {
    fill_initialize(n, value);
  }
//构建大小为n的deque,默认值为T(),说明deque容器的元素要有默认构造函数
  explicit deque(size_type n)
    : start(), finish(), map(0), map_size(0)
  {
    fill_initialize(n, value_type());
  }

 /*用一段元素构建的确 */
#ifdef __STL_MEMBER_TEMPLATES

  template <class InputIterator>
  deque(InputIterator first, InputIterator last)
    : start(), finish(), map(0), map_size(0)
  {
    range_initialize(first, last, iterator_category(first));
  }

#else /* __STL_MEMBER_TEMPLATES */

  deque(const value_type* first, const value_type* last)
    : start(), finish(), map(0), map_size(0)
  {
    create_map_and_nodes(last - first);
    __STL_TRY {
      uninitialized_copy(first, last, start);
    }
    __STL_UNWIND(destroy_map_and_nodes());
  }

  deque(const_iterator first, const_iterator last)
    : start(), finish(), map(0), map_size(0)
  {
    create_map_and_nodes(last - first);
    __STL_TRY {
      uninitialized_copy(first, last, start);
    }
    __STL_UNWIND(destroy_map_and_nodes());
  }

#endif /* __STL_MEMBER_TEMPLATES */

  ~deque() {
    destroy(start, finish);
    destroy_map_and_nodes();
  }

  deque& operator= (const deque& x) {
    const size_type len = size();
    if (&x != this) {
      if (len >= x.size())
        erase(copy(x.begin(), x.end(), start), finish);
      else {
        const_iterator mid = x.begin() + difference_type(len);
        copy(x.begin(), mid, start);
        insert(finish, mid, x.end());
      }
    }
    return *this;
  }        

  void swap(deque& x) {
    __STD::swap(start, x.start);
    __STD::swap(finish, x.finish);
    __STD::swap(map, x.map);
    __STD::swap(map_size, x.map_size);
  }

public:                         // push_* and pop_*
  //在deque末尾添加元素
  void push_back(const value_type& t) {
    if (finish.cur != finish.last - 1) {
      // 当前缓冲区还有空间
      construct(finish.cur, t);	// 直接在可用空间构建
      ++finish.cur;	// 调整finish迭代器
    }
    else  // 当前缓冲区无可用空间(last不能存储元素用)
      push_back_aux(t);
  }

  //在deque头添加元素
  void push_front(const value_type& t) {
    if (start.cur != start.first) { 	// 当前缓冲区还有空间
      construct(start.cur - 1, t);
      --start.cur;
    }
    else // 当前缓冲区无空间可用了
      push_front_aux(t);
  }

	//删掉末尾元素
  void pop_back() {
    if (finish.cur != finish.first) {//最后一个缓冲区(finish指的缓冲区)有多于一个元素(含一个)
      --finish.cur;
      destroy(finish.cur);
    }
    else
      // 最后一个缓冲区无元素
      pop_back_aux();		// 这里会进行缓冲区的释放工作
  }

  //在deque头删除元素
  void pop_front() {
    if (start.cur != start.last - 1) {
      // start.node所指缓冲区有多余一个元素(不含一个)
      destroy(start.cur);
      ++start.cur;
    }
    else
      // start.node所指缓冲区只有一个元素
      pop_front_aux();		// 这里会进行缓冲区释放工作
  }

public:                         // Insert

  /*在position处插入一个元素
	如果position是deque的最前端,则调用push_front()。
	如果position是deque的最末端,则调用push_back()。
	在两个元素之间插入的话,就调用insert_aux。
  */
  // 在position 处安插一個元素,其值为 x
  iterator insert(iterator position, const value_type& x) {
    if (position.cur == start.cur) {
      push_front(x);
      return start;
    }
    else if (position.cur == finish.cur) {
      push_back(x);
      iterator tmp = finish;
      --tmp;
      return tmp;
    }
    else {
      return insert_aux(position, x);	 	// 交给insert_aux 去做
    }
  }
	// 在position 处安插一個元素,其值为T()
  iterator insert(iterator position) { return insert(position, value_type()); }

  void insert(iterator pos, size_type n, const value_type& x); 

  void insert(iterator pos, int n, const value_type& x) {
    insert(pos, (size_type) n, x);
  }
  void insert(iterator pos, long n, const value_type& x) {
    insert(pos, (size_type) n, x);
  }

#ifdef __STL_MEMBER_TEMPLATES  

  template <class InputIterator>
  void insert(iterator pos, InputIterator first, InputIterator last) {
    insert(pos, first, last, iterator_category(first));
  }

#else /* __STL_MEMBER_TEMPLATES */

  void insert(iterator pos, const value_type* first, const value_type* last);
  void insert(iterator pos, const_iterator first, const_iterator last);

#endif /* __STL_MEMBER_TEMPLATES */
/*
调整deque的大小。
如果deque变小,直接擦除掉多余的元素。
如果deque变大,则在deque后面插入元素补充,元素值为x/T()
*/
  void resize(size_type new_size, const value_type& x) {
    const size_type len = size();
    if (new_size < len)
      erase(start + new_size, finish);
    else
      insert(finish, new_size - len, x);
  }

  void resize(size_type new_size) { resize(new_size, value_type()); }

public:
  // 清除 pos 所指的元素。
  /*判断pos距离头近还是距离尾近,距离那个位置近就移动那个位置的元素,保证移动元素个数最少*/
  iterator erase(iterator pos) {
    iterator next = pos;
    ++next;
    difference_type index = pos - start;	// pos和deque开头元素的个数
    if (index < (size() >> 1)) {			// size() >> 1为size()/2。
	//如果pos距离deque头比较近的话,deque的开头到pos元素向后移
      copy_backward(start, pos, next);
      pop_front();				// 移动后,删除第一个元素
    }
    else {					// 否则pos+1到结尾元素向前移,
      copy(next, finish, pos);
      pop_back();
    }
    return start + index;
  }

  iterator erase(iterator first, iterator last);
  void clear(); 

protected:                        // Internal construction/destruction

  void create_map_and_nodes(size_type num_elements);
  void destroy_map_and_nodes();
  void fill_initialize(size_type n, const value_type& value);

#ifdef __STL_MEMBER_TEMPLATES  

  template <class InputIterator>
  void range_initialize(InputIterator first, InputIterator last,
                        input_iterator_tag);

  template <class ForwardIterator>
  void range_initialize(ForwardIterator first, ForwardIterator last,
                        forward_iterator_tag);

#endif /* __STL_MEMBER_TEMPLATES */

protected:                        // Internal push_* and pop_*

  void push_back_aux(const value_type& t);
  void push_front_aux(const value_type& t);
  void pop_back_aux();
  void pop_front_aux();

protected:                        // Internal insert functions

#ifdef __STL_MEMBER_TEMPLATES  

  template <class InputIterator>
  void insert(iterator pos, InputIterator first, InputIterator last,
              input_iterator_tag);

  template <class ForwardIterator>
  void insert(iterator pos, ForwardIterator first, ForwardIterator last,
              forward_iterator_tag);

#endif /* __STL_MEMBER_TEMPLATES */

  iterator insert_aux(iterator pos, const value_type& x);
  void insert_aux(iterator pos, size_type n, const value_type& x);

#ifdef __STL_MEMBER_TEMPLATES  

  template <class ForwardIterator>
  void insert_aux(iterator pos, ForwardIterator first, ForwardIterator last,
                  size_type n);

#else /* __STL_MEMBER_TEMPLATES */

  void insert_aux(iterator pos,
                  const value_type* first, const value_type* last,
                  size_type n);

  void insert_aux(iterator pos, const_iterator first, const_iterator last,
                  size_type n);

#endif /* __STL_MEMBER_TEMPLATES */
	//在队列头或者尾预留n个位置,如果缓冲区不够则开辟新缓冲区。
  iterator reserve_elements_at_front(size_type n) {
    size_type vacancies = start.cur - start.first;
    if (n > vacancies)
      new_elements_at_front(n - vacancies);
    return start - difference_type(n);
  }

  iterator reserve_elements_at_back(size_type n) {
    size_type vacancies = (finish.last - finish.cur) - 1;
    if (n > vacancies)
      new_elements_at_back(n - vacancies);
    return finish + difference_type(n);
  }

  void new_elements_at_front(size_type new_elements);
  void new_elements_at_back(size_type new_elements);

  void destroy_nodes_at_front(iterator before_start);
  void destroy_nodes_at_back(iterator after_finish);

protected:                      // Allocation of map and nodes

  // Makes sure the map has space for new nodes.  Does not actually
  //  add the nodes.  Can invalidate map pointers.  (And consequently,
  //  deque iterators.)
//在map尾添加缓冲区
  void reserve_map_at_back (size_type nodes_to_add = 1) {
    if (nodes_to_add + 1 > map_size - (finish.node - map))
	//map空间不够用,则开辟新的map空间,把原来map内容拷贝过来。释放原来的
      reallocate_map(nodes_to_add, false);
  }

  //在map头添加缓冲区
  void reserve_map_at_front (size_type nodes_to_add = 1) {
    if (nodes_to_add > start.node - map)
      reallocate_map(nodes_to_add, true);
  }

  void reallocate_map(size_type nodes_to_add, bool add_at_front);
//配置新的缓冲区
  pointer allocate_node() { return data_allocator::allocate(buffer_size()); }
  //释放缓冲区
  void deallocate_node(pointer n) {
    data_allocator::deallocate(n, buffer_size());
  }
//重载比较运算符
#ifdef __STL_NON_TYPE_TMPL_PARAM_BUG
public:
  bool operator==(const deque<T, Alloc, 0>& x) const {
    return size() == x.size() && equal(begin(), end(), x.begin());
  }
  bool operator!=(const deque<T, Alloc, 0>& x) const {
    return size() != x.size() || !equal(begin(), end(), x.begin());
  }
  bool operator<(const deque<T, Alloc, 0>& x) const {
    return lexicographical_compare(begin(), end(), x.begin(), x.end());
  }
#endif /* __STL_NON_TYPE_TMPL_PARAM_BUG */
};

// Non-inline member functions
//不是内联函数

//在pos处插入n个元素,元素值为n
template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::insert(iterator pos,
                                      size_type n, const value_type& x) {
  if (pos.cur == start.cur) {//pos是deque的头
    iterator new_start = reserve_elements_at_front(n);
    uninitialized_fill(new_start, start, x);
    start = new_start;
  }
  else if (pos.cur == finish.cur) {//pos是deque的尾
    iterator new_finish = reserve_elements_at_back(n);
    uninitialized_fill(finish, new_finish, x);
    finish = new_finish;
  }
  else //中间位置
    insert_aux(pos, n, x);
}

#ifndef __STL_MEMBER_TEMPLATES
//两个迭代器之间的元素插入到pos处
template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::insert(iterator pos,
                                      const value_type* first,
                                      const value_type* last) {
  size_type n = last - first;
  if (pos.cur == start.cur) {//deque的头
  //先预留好位置
    iterator new_start = reserve_elements_at_front(n);
    __STL_TRY {
	//在未初始化内存上直接构造
      uninitialized_copy(first, last, new_start);
      start = new_start;
    }
    __STL_UNWIND(destroy_nodes_at_front(new_start));
  }
  else if (pos.cur == finish.cur) {
    iterator new_finish = reserve_elements_at_back(n);
    __STL_TRY {
      uninitialized_copy(first, last, finish);
      finish = new_finish;
    }
	//commi or rollback
    __STL_UNWIND(destroy_nodes_at_back(new_finish));
  }
  else
    insert_aux(pos, first, last, n);
}

template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::insert(iterator pos,
                                      const_iterator first,
                                      const_iterator last)
{
  size_type n = last - first;
  if (pos.cur == start.cur) {
    iterator new_start = reserve_elements_at_front(n);
    __STL_TRY {
      uninitialized_copy(first, last, new_start);
      start = new_start;
    }
    __STL_UNWIND(destroy_nodes_at_front(new_start));
  }
  else if (pos.cur == finish.cur) {
    iterator new_finish = reserve_elements_at_back(n);
    __STL_TRY {
      uninitialized_copy(first, last, finish);
      finish = new_finish;
    }
    __STL_UNWIND(destroy_nodes_at_back(new_finish));
  }
  else
    insert_aux(pos, first, last, n);
}

#endif /* __STL_MEMBER_TEMPLATES */

//擦除两个迭代器之间的元素
template <class T, class Alloc, size_t BufSize>
deque<T, Alloc, BufSize>::iterator
deque<T, Alloc, BufSize>::erase(iterator first, iterator last) {
  if (first == start && last == finish) { // 如果是清除整个 deque
    clear();							// 直接调用 clear() 即可
    return finish;
  }
  else {
    difference_type n = last - first;			// 擦除区间长度
    difference_type elems_before = first - start;	// 擦除区间前方元素的个数
    if (elems_before < (size() - n) / 2) {		// 如果前方的元素比更少,
      copy_backward(start, first, last);		// 前方元素向后移(覆盖擦除区间)
      iterator new_start = start + n;			// deque 的新起点
      destroy(start, new_start);				// 多于元素析构
      // 释放多于元素所占内存
      for (map_pointer cur = start.node; cur < new_start.node; ++cur)
        data_allocator::deallocate(*cur, buffer_size());
      start = new_start;
    }
    else {	// 后方元素更少
      copy(last, finish, first);
      iterator new_finish = finish - n;
      destroy(new_finish, finish);		

      for (map_pointer cur = new_finish.node + 1; cur <= finish.node; ++cur)
        data_allocator::deallocate(*cur, buffer_size());
      finish = new_finish;
    }
    return start + elems_before;
  }
}

//清空deque。最后保留了一个缓冲区,这是deque的策略,也是其初始状态
template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::clear() {
  //下面针对头尾以外的缓冲区,它们肯定是满的。
  for (map_pointer node = start.node + 1; node < finish.node; ++node) {
    // 先析构
    destroy(*node, *node + buffer_size());
    // 再释放
    data_allocator::deallocate(*node, buffer_size());
  }

  if (start.node != finish.node) {	// 至少有2个以上(含)缓冲区
    destroy(start.cur, start.last);	// 头缓冲区元素析构
    destroy(finish.first, finish.cur); // 尾缓冲区元素析构
    // 释放尾缓冲区,保留了头缓冲区
    data_allocator::deallocate(finish.first, buffer_size());
  }
  else	// 只有一个缓冲区
    destroy(start.cur, finish.cur);	// 析构,但是不释放

  finish = start;	// 调整迭代器,deque为空
}

//创建map,num_elements为元素个数
template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::create_map_and_nodes(size_type num_elements) {

  //需要多少个缓冲区,即对应多少个map_node,如果刚好整除,会多出一个来
  size_type num_nodes = num_elements / buffer_size() + 1;

  //创建map结构,(如果少于8个)要多出2个来,前后各预留一个备用
  map_size = max(initial_map_size(), num_nodes + 2);
  map = map_allocator::allocate(map_size);

  /*下面是nstart和nfinish指向map结构的最中间,这样可以使前后添加能力一样大。*/
  map_pointer nstart = map + (map_size - num_nodes) / 2;
  map_pointer nfinish = nstart + num_nodes - 1;

  map_pointer cur;
  __STL_TRY {

   //为map内已用结点配置缓冲区。
    for (cur = nstart; cur <= nfinish; ++cur)
      *cur = allocate_node();
  }
#     ifdef  __STL_USE_EXCEPTIONS
  catch(...) {
    // "commit or rollback"
    for (map_pointer n = nstart; n < cur; ++n)
      deallocate_node(*n);
    map_allocator::deallocate(map, map_size);
    throw;
  }
#     endif /* __STL_USE_EXCEPTIONS */

  //为deque的迭代器start和end设定正确内容
  start.set_node(nstart);
  finish.set_node(nfinish);
  start.cur = start.first;		// first, cur都是public
  finish.cur = finish.first + num_elements % buffer_size();
  //正如前面所说,如果刚好整除会多出一个map_node。此时cur指向多出的这个缓冲区的起始位置。
}

// This is only used as a cleanup function in catch clauses.
template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::destroy_map_and_nodes() {
  for (map_pointer cur = start.node; cur <= finish.node; ++cur)
    deallocate_node(*cur);
  map_allocator::deallocate(map, map_size);
}

//分配n个结点,用value初始化
template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::fill_initialize(size_type n,
                                               const value_type& value) {
  create_map_and_nodes(n);	 // 把deque的结构都安排好
  map_pointer cur;
  __STL_TRY {
    // 每个结点缓冲区设定初始值。
    for (cur = start.node; cur < finish.node; ++cur)
      uninitialized_fill(*cur, *cur + buffer_size(), value);
	//最后一个结点有点不一样,因为尾端可能有未用空间,不必设置初始值
    uninitialized_fill(finish.first, finish.cur, value);
  }
#       ifdef __STL_USE_EXCEPTIONS
  catch(...) {
    // "commit or rollback"
    for (map_pointer n = start.node; n < cur; ++n)
      destroy(*n, *n + buffer_size());
    destroy_map_and_nodes();
    throw;
  }
#       endif /* __STL_USE_EXCEPTIONS */
}
//用两个迭代器之间的元素初始化。迭代器类型不同,实现方法不同。
//input_iterator_tag类型迭代器一个一个初始化
//forward_iterator_tag(含其派生类型)内存处理工具初始化。
#ifdef __STL_MEMBER_TEMPLATES  

template <class T, class Alloc, size_t BufSize>
template <class InputIterator>
void deque<T, Alloc, BufSize>::range_initialize(InputIterator first,
                                                InputIterator last,
                                                input_iterator_tag) {
  create_map_and_nodes(0);//不分配结点
  for ( ; first != last; ++first)//一个一个初始化
    push_back(*first);
}

template <class T, class Alloc, size_t BufSize>
template <class ForwardIterator>
void deque<T, Alloc, BufSize>::range_initialize(ForwardIterator first,
                                                ForwardIterator last,
                                                forward_iterator_tag) {
  size_type n = 0;
  distance(first, last, n);
  create_map_and_nodes(n);//分配好结点
  __STL_TRY {
    uninitialized_copy(first, last, start);
  }
  //commit or rollback
  __STL_UNWIND(destroy_map_and_nodes());
}

#endif /* __STL_MEMBER_TEMPLATES */

//只有当finish.cur == finish.last – 1才有调用。
//只有当最后一个缓冲区只剩一个未用空间时才调用
template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::push_back_aux(const value_type& t) {
  value_type t_copy = t;
  reserve_map_at_back();		//  若符合某重条件则必须重换一个map
  *(finish.node + 1) = allocate_node();	// 配置一个新结点(缓冲区)
  __STL_TRY {
    construct(finish.cur, t_copy);		// 设置值
    finish.set_node(finish.node + 1);	// 改变finish,令其指向新结点
    finish.cur = finish.first;			// 设置 finish 的状态
  }
  __STL_UNWIND(deallocate_node(*(finish.node + 1)));
}

// 只有当start.cur == start.first才会调用。
// 第一个缓冲区没有未用空间时才会调用。和上面实现类似
template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::push_front_aux(const value_type& t) {
  value_type t_copy = t;
  reserve_map_at_front();
  *(start.node - 1) = allocate_node();
  __STL_TRY {
    start.set_node(start.node - 1);
    start.cur = start.last - 1;
    construct(start.cur, t_copy);
  }
#     ifdef __STL_USE_EXCEPTIONS
  catch(...) {
    // "commit or rollback"
    start.set_node(start.node + 1);
    start.cur = start.first;
    deallocate_node(*(start.node - 1));
    throw;
  }
#     endif /* __STL_USE_EXCEPTIONS */
} 

// 只有当finish.cur == finish.first才会调用
template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::pop_back_aux() {
  deallocate_node(finish.first);
  finish.set_node(finish.node - 1);
  finish.cur = finish.last - 1;
  destroy(finish.cur);
}

// 只有当start.cur == start.last - 1时才会调用
template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::pop_front_aux() {
  destroy(start.cur);
  deallocate_node(start.first);
  start.set_node(start.node + 1);
  start.cur = start.first;
}      

#ifdef __STL_MEMBER_TEMPLATES  

template <class T, class Alloc, size_t BufSize>
template <class InputIterator>
void deque<T, Alloc, BufSize>::insert(iterator pos,
                                      InputIterator first, InputIterator last,
                                      input_iterator_tag) {
  copy(first, last, inserter(*this, pos));
}
//在pos处插入[first last)元素,对应迭代器类型为forward_iterator_tag(含派生)
template <class T, class Alloc, size_t BufSize>
template <class ForwardIterator>
void deque<T, Alloc, BufSize>::insert(iterator pos,
                                      ForwardIterator first,
                                      ForwardIterator last,
                                      forward_iterator_tag) {
  size_type n = 0;
  distance(first, last, n);
  if (pos.cur == start.cur) {
    iterator new_start = reserve_elements_at_front(n);
    __STL_TRY {
      uninitialized_copy(first, last, new_start);
      start = new_start;
    }
    __STL_UNWIND(destroy_nodes_at_front(new_start));
  }
  else if (pos.cur == finish.cur) {
    iterator new_finish = reserve_elements_at_back(n);
    __STL_TRY {
      uninitialized_copy(first, last, finish);
      finish = new_finish;
    }
    __STL_UNWIND(destroy_nodes_at_back(new_finish));
  }
  else
    insert_aux(pos, first, last, n);
}

#endif /* __STL_MEMBER_TEMPLATES */
//在pos处插入一个元素,值为x。要判断插入点距头更近还是尾更近……
template <class T, class Alloc, size_t BufSize>
typename deque<T, Alloc, BufSize>::iterator
deque<T, Alloc, BufSize>::insert_aux(iterator pos, const value_type& x) {
  difference_type index = pos - start;
  value_type x_copy = x;
  if (index < size() / 2) {
    push_front(front());
    iterator front1 = start;
    ++front1;
    iterator front2 = front1;
    ++front2;
    pos = start + index;
    iterator pos1 = pos;
    ++pos1;
    copy(front2, pos1, front1);
  }
  else {
    push_back(back());
    iterator back1 = finish;
    --back1;
    iterator back2 = back1;
    --back2;
    pos = start + index;
    copy_backward(pos, back2, back1);
  }
  *pos = x_copy;
  return pos;
}
//在pos处插入n个元素,值为x
template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::insert_aux(iterator pos,
                                          size_type n, const value_type& x) {
  const difference_type elems_before = pos - start;
  size_type length = size();
  value_type x_copy = x;
  if (elems_before < length / 2) {
    iterator new_start = reserve_elements_at_front(n);
    iterator old_start = start;
    pos = start + elems_before;
    __STL_TRY {
      if (elems_before >= difference_type(n)) {
        iterator start_n = start + difference_type(n);
        uninitialized_copy(start, start_n, new_start);
        start = new_start;
        copy(start_n, pos, old_start);
        fill(pos - difference_type(n), pos, x_copy);
      }
      else {
        __uninitialized_copy_fill(start, pos, new_start, start, x_copy);
        start = new_start;
        fill(old_start, pos, x_copy);
      }
    }
    __STL_UNWIND(destroy_nodes_at_front(new_start));
  }
  else {
    iterator new_finish = reserve_elements_at_back(n);
    iterator old_finish = finish;
    const difference_type elems_after = difference_type(length) - elems_before;
    pos = finish - elems_after;
    __STL_TRY {
      if (elems_after > difference_type(n)) {
        iterator finish_n = finish - difference_type(n);
        uninitialized_copy(finish_n, finish, finish);
        finish = new_finish;
        copy_backward(pos, finish_n, old_finish);
        fill(pos, pos + difference_type(n), x_copy);
      }
      else {
        __uninitialized_fill_copy(finish, pos + difference_type(n),
                                  x_copy,
                                  pos, finish);
        finish = new_finish;
        fill(pos, old_finish, x_copy);
      }
    }
    __STL_UNWIND(destroy_nodes_at_back(new_finish));
  }
}

#ifdef __STL_MEMBER_TEMPLATES
//在pos处插入n个元素,n个元素值为[first last)
template <class T, class Alloc, size_t BufSize>
template <class ForwardIterator>
void deque<T, Alloc, BufSize>::insert_aux(iterator pos,
                                          ForwardIterator first,
                                          ForwardIterator last,
                                          size_type n)
{
  const difference_type elems_before = pos - start;
  size_type length = size();
  if (elems_before < length / 2) {
    iterator new_start = reserve_elements_at_front(n);
    iterator old_start = start;
    pos = start + elems_before;
    __STL_TRY {
      if (elems_before >= difference_type(n)) {
        iterator start_n = start + difference_type(n);
        uninitialized_copy(start, start_n, new_start);
        start = new_start;
        copy(start_n, pos, old_start);
        copy(first, last, pos - difference_type(n));
      }
      else {
        ForwardIterator mid = first;
        advance(mid, difference_type(n) - elems_before);
        __uninitialized_copy_copy(start, pos, first, mid, new_start);
        start = new_start;
        copy(mid, last, old_start);
      }
    }
    __STL_UNWIND(destroy_nodes_at_front(new_start));
  }
  else {
    iterator new_finish = reserve_elements_at_back(n);
    iterator old_finish = finish;
    const difference_type elems_after = difference_type(length) - elems_before;
    pos = finish - elems_after;
    __STL_TRY {
      if (elems_after > difference_type(n)) {
        iterator finish_n = finish - difference_type(n);
        uninitialized_copy(finish_n, finish, finish);
        finish = new_finish;
        copy_backward(pos, finish_n, old_finish);
        copy(first, last, pos);
      }
      else {
        ForwardIterator mid = first;
        advance(mid, elems_after);
        __uninitialized_copy_copy(mid, last, pos, finish, finish);
        finish = new_finish;
        copy(first, mid, pos);
      }
    }
    __STL_UNWIND(destroy_nodes_at_back(new_finish));
  }
}

#else /* __STL_MEMBER_TEMPLATES */

template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::insert_aux(iterator pos,
                                          const value_type* first,
                                          const value_type* last,
                                          size_type n)
{
  const difference_type elems_before = pos - start;
  size_type length = size();
  if (elems_before < length / 2) {
    iterator new_start = reserve_elements_at_front(n);
    iterator old_start = start;
    pos = start + elems_before;
    __STL_TRY {
      if (elems_before >= difference_type(n)) {
        iterator start_n = start + difference_type(n);
        uninitialized_copy(start, start_n, new_start);
        start = new_start;
        copy(start_n, pos, old_start);
        copy(first, last, pos - difference_type(n));
      }
      else {
        const value_type* mid = first + (difference_type(n) - elems_before);
        __uninitialized_copy_copy(start, pos, first, mid, new_start);
        start = new_start;
        copy(mid, last, old_start);
      }
    }
    __STL_UNWIND(destroy_nodes_at_front(new_start));
  }
  else {
    iterator new_finish = reserve_elements_at_back(n);
    iterator old_finish = finish;
    const difference_type elems_after = difference_type(length) - elems_before;
    pos = finish - elems_after;
    __STL_TRY {
      if (elems_after > difference_type(n)) {
        iterator finish_n = finish - difference_type(n);
        uninitialized_copy(finish_n, finish, finish);
        finish = new_finish;
        copy_backward(pos, finish_n, old_finish);
        copy(first, last, pos);
      }
      else {
        const value_type* mid = first + elems_after;
        __uninitialized_copy_copy(mid, last, pos, finish, finish);
        finish = new_finish;
        copy(first, mid, pos);
      }
    }
    __STL_UNWIND(destroy_nodes_at_back(new_finish));
  }
}

template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::insert_aux(iterator pos,
                                          const_iterator first,
                                          const_iterator last,
                                          size_type n)
{
  const difference_type elems_before = pos - start;
  size_type length = size();
  if (elems_before < length / 2) {
    iterator new_start = reserve_elements_at_front(n);
    iterator old_start = start;
    pos = start + elems_before;
    __STL_TRY {
      if (elems_before >= n) {
        iterator start_n = start + n;
        uninitialized_copy(start, start_n, new_start);
        start = new_start;
        copy(start_n, pos, old_start);
        copy(first, last, pos - difference_type(n));
      }
      else {
        const_iterator mid = first + (n - elems_before);
        __uninitialized_copy_copy(start, pos, first, mid, new_start);
        start = new_start;
        copy(mid, last, old_start);
      }
    }
    __STL_UNWIND(destroy_nodes_at_front(new_start));
  }
  else {
    iterator new_finish = reserve_elements_at_back(n);
    iterator old_finish = finish;
    const difference_type elems_after = length - elems_before;
    pos = finish - elems_after;
    __STL_TRY {
      if (elems_after > n) {
        iterator finish_n = finish - difference_type(n);
        uninitialized_copy(finish_n, finish, finish);
        finish = new_finish;
        copy_backward(pos, finish_n, old_finish);
        copy(first, last, pos);
      }
      else {
        const_iterator mid = first + elems_after;
        __uninitialized_copy_copy(mid, last, pos, finish, finish);
        finish = new_finish;
        copy(first, mid, pos);
      }
    }
    __STL_UNWIND(destroy_nodes_at_back(new_finish));
  }
}

#endif /* __STL_MEMBER_TEMPLATES */
//在deque头分配新的结点
template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::new_elements_at_front(size_type new_elements) {
  size_type new_nodes = (new_elements + buffer_size() - 1) / buffer_size();
  reserve_map_at_front(new_nodes);
  size_type i;
  __STL_TRY {
    for (i = 1; i <= new_nodes; ++i)
      *(start.node - i) = allocate_node();
  }
#       ifdef __STL_USE_EXCEPTIONS
  catch(...) {
    for (size_type j = 1; j < i; ++j)
      deallocate_node(*(start.node - j));
    throw;
  }
#       endif /* __STL_USE_EXCEPTIONS */
}
//在deque尾分配新的结点
template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::new_elements_at_back(size_type new_elements) {
  size_type new_nodes = (new_elements + buffer_size() - 1) / buffer_size();
  reserve_map_at_back(new_nodes);
  size_type i;
  __STL_TRY {
    for (i = 1; i <= new_nodes; ++i)
      *(finish.node + i) = allocate_node();
  }
#       ifdef __STL_USE_EXCEPTIONS
  catch(...) {
    for (size_type j = 1; j < i; ++j)
      deallocate_node(*(finish.node + j));
    throw;
  }
#       endif /* __STL_USE_EXCEPTIONS */
}
//释放[before_start statr)
template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::destroy_nodes_at_front(iterator before_start) {

  for (map_pointer n = before_start.node; n < start.node; ++n)
    deallocate_node(*n);
}
//释放(finish.node after_finish]
template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::destroy_nodes_at_back(iterator after_finish) {

  for (map_pointer n = after_finish.node; n > finish.node; --n)
    deallocate_node(*n);
}
//添加map结点,指向新的缓冲区,add_at_front=true添加在map头,否则添加在尾
template <class T, class Alloc, size_t BufSize>
void deque<T, Alloc, BufSize>::reallocate_map(size_type nodes_to_add,
                                              bool add_at_front) {
  size_type old_num_nodes = finish.node - start.node + 1;
  size_type new_num_nodes = old_num_nodes + nodes_to_add;

  map_pointer new_nstart;
  if (map_size > 2 * new_num_nodes) {
    new_nstart = map + (map_size - new_num_nodes) / 2
                     + (add_at_front ? nodes_to_add : 0);
    if (new_nstart < start.node)
      copy(start.node, finish.node + 1, new_nstart);
    else
      copy_backward(start.node, finish.node + 1, new_nstart + old_num_nodes);
  }
  else {
    size_type new_map_size = map_size + max(map_size, nodes_to_add) + 2;
    // 配置新的结点,准备给map使用
    map_pointer new_map = map_allocator::allocate(new_map_size);
    new_nstart = new_map + (new_map_size - new_num_nodes) / 2
                         + (add_at_front ? nodes_to_add : 0);
    // 把原map 內容拷贝
    copy(start.node, finish.node + 1, new_nstart);
    // 释放放原map
    map_allocator::deallocate(map, map_size);
    // 设置新map起始位置和大小
    map = new_map;
    map_size = new_map_size;
  }

  // 重新设置迭代器 start 和 finish
  start.set_node(new_nstart);
  finish.set_node(new_nstart + old_num_nodes - 1);
}

// Nonmember functions.
//非成员函数,标准STL算法
#ifndef __STL_NON_TYPE_TMPL_PARAM_BUG

template <class T, class Alloc, size_t BufSiz>
bool operator==(const deque<T, Alloc, BufSiz>& x,
                const deque<T, Alloc, BufSiz>& y) {
  return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
}

template <class T, class Alloc, size_t BufSiz>
bool operator<(const deque<T, Alloc, BufSiz>& x,
               const deque<T, Alloc, BufSiz>& y) {
  return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
}

#endif /* __STL_NON_TYPE_TMPL_PARAM_BUG */

#if defined(__STL_FUNCTION_TMPL_PARTIAL_ORDER) &&     !defined(__STL_NON_TYPE_TMPL_PARAM_BUG)

template <class T, class Alloc, size_t BufSiz>
inline void swap(deque<T, Alloc, BufSiz>& x, deque<T, Alloc, BufSiz>& y) {
  x.swap(y);
}

#endif

#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma reset woff 1174
#endif

__STL_END_NAMESPACE 

#endif /* __SGI_STL_INTERNAL_DEQUE_H */

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

《STL源码剖析》---stl_deque.h阅读笔记(2)

时间: 2024-12-12 13:39:39

《STL源码剖析》---stl_deque.h阅读笔记(2)的相关文章

《STL源码剖析》---stl_hash_set.阅读笔记

STL只规定接口和复杂度,对于具体实现不作要求.set大多以红黑树实现,但STL在标准规格之外提供了一个所谓的hash_set,以hash table实现.hash_set的接口,hash_table都提供了,所以几乎所有的hash_set操作都是直接调用hash_table的函数而已. 除了hash_set,还有hash_multiset,它们两个的关系就像set和multiset的关系,一个不允许键值重复,另外一个允许键值重复.其他实现一样. G++ 2.91.57,cygnus\cygwi

《STL源码剖析》---stl_uninitialized阅读笔记

这节讲解在已分配但未初始化的空间上构造对象(可能是一段内存,构造多个对象). 使内存的配置与对象的构造分离开来.在未初始化的内存上构造对象时,会先判断对象类型是否是POD类型.POD全称是Plain old data,也就是标量类型(基本类型和指针类型)或者传统的C struct类型.POD类型有trivial的constructor.deconstructor.copy.assignment(构造.析构.复制构造函数.赋值操作符)操作,所以对POD类型采用最有效的复制手法,而对non-POD类

STL 源码剖析 stl_numeric.h -- copy

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie copy //唯一对外接口 /*-------------------------------------------------------------------------------------- * copy 函数及其重载形式 */ //完全泛化版本. template<class InputIterator, class OutputIterator> // ? 这里的 In

STL 源码剖析 stl_algobase.h

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 1.iter_swap 描述:将两个 ForwardIterator 所指的对象对调 源码: //version 1 template <class ForwardIterator1, class ForwardIterator2, class T> inline void __iter_swap(ForwardIterator1 a, ForwardIterator2 b, T*) {

STL 源码剖析 stl_numeric.h

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 描述.源码.示例 version 1:普通操作版本 version 2: 泛化操作版本 1.accumulate 描述:计算 init 和 [first, last) 内所有元素的总和 源码: //version 1 template <class InputIterator, class T> T accumulate(InputIterator first, InputIterato

C++ 《STL源码剖析》学习-vector

本文章是笔者学习<STL源码剖析>的学习笔记,记录的是笔者的个人理解,因为个人的水平有限,难免会有理解不当的地方,而且该书出版的时间比较久,难免会有些不一样.如有不当,欢迎指出. vector是c++中经常用到的数据结构,而且在面试时也会有提及,因此了解vector很重要. 一说到vector,我们就很容易想到另外一个与它十分相似的数据结构,关于它们之间显著的差别,我觉得是在于空间运用的灵活性上.数组是静态的,在声明的时候就要指明其具体的空间大小,而vector是动态的,随着元素的增加,它内部

《STL源码剖析》---stl_deque.h阅读笔记(1)

双端队列deque是容器的一种.它比vector更强大,vector只可以在尾端插入元素,deque不只是可以再尾端插入,也可以在队列头插入.下面借助<STL源代码剖析>的图片讲解. 1.deque的内存结构 vector是开辟一段连续的内存,deque可以在前端插入元素,如果像vector开辟一段连续的内存,向前面插入元素不易维护.例如如果只是开辟一段连续内存,那么前端到开辟内存的起始位置要空多少个位置?当这段内存满了时,拷贝到更大内存时,前端空留多少位置?显然难以维护.deque在STL中

《STL源码剖析》---stl_pair.h阅读笔记

pair是STL中的模板类型,它可以存储两个元素,它也被称作"对组".在map中已经用到了它,pair其实就是一个struct结构,存有两个public的元素,重载了几个运算符,没有什么成员函数,源代码很简单. G++ 2.91.57,cygnus\cygwin-b20\include\g++\stl_pair.h 完整列表 /* * * Copyright (c) 1994 * Hewlett-Packard Company * * Permission to use, copy,

《STL源码剖析》---stl_tree.h阅读笔记

STL中,关联式容器的内部结构是一颗平衡二叉树,以便获得良好的搜索效率.红黑树是平衡二叉树的一种,它不像AVL树那样要求绝对平衡,降低了对旋转的要求,但是其性能并没有下降很多,它的搜索.插入.删除都能以O(nlogn)时间完成.平衡可以在一次或者两次旋转解决,是"性价比"很高的平衡二叉树. RB-tree(red black tree)红黑树是平衡二叉树.它满足一下规则 (1)每个节点不是红色就是黑色. (2)根节点是黑色. (3)如果节点为红色,则其子节点比为黑色. (4)任何一个节