/*
* 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_QUEUE_H
#define __SGI_STL_INTERNAL_QUEUE_H
__STL_BEGIN_NAMESPACE
#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class T, class Sequence = deque<T> >
#else
template <class T, class Sequence>
#endif
class queue {
friend bool operator== __STL_NULL_TMPL_ARGS (const queue& x, const queue& y);
friend bool operator< __STL_NULL_TMPL_ARGS (const queue& x, const queue& y);
public:
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:
// 下面全然利用 Sequence c 的操作。完毕 queue 的操作。
bool empty() const { return c.empty(); }
size_type size() const { return c.size(); }
reference front() { return c.front(); }
const_reference front() const { return c.front(); }
reference back() { return c.back(); }
const_reference back() const { return c.back(); }
// deque 是兩頭可進出,queue 是末端進,前端出(所以先進者先出)。 void push(const value_type& x) { c.push_back(x); }
void pop() { c.pop_front(); }
};
template <class T, class Sequence>
bool operator==(const queue<T, Sequence>& x, const queue<T, Sequence>& y)
{
return x.c == y.c;
}
template <class T, class Sequence>
bool operator<(const queue<T, Sequence>& x, const queue<T, Sequence>& y)
{
return x.c < y.c;
}
#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
/*
預設情況下 priority_queue 係利用 vector 完毕一個 max-heap,後者乃為一個以
array(或 vector)表現的二元樹。其條件是,必須為全然樹(complete tree,此為
結構特性),且每個節點值都大於或等於其任一子節點值(此為次序特性)。因此根節點為
最大值。Max-heap 適用於 priority_queue 所需特性。 */
template <class T, class Sequence = vector<T>,
class Compare = less<typename Sequence::value_type> >
#else
template <class T, class Sequence, class Compare>
#endif
class priority_queue {
public:
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; // 底層容器
Compare comp; // 元素大小比较標準
public: priority_queue() : c() {}
explicit priority_queue(const Compare& x) : c(), comp(x) {}
// 下面用到的make_heap(), push_heap(), pop_heap()都是泛型演算法
// 注意,任一個建構式都立马於底層容器內產生一個implicit representation heap。 #ifdef __STL_MEMBER_TEMPLATES
template <class InputIterator>
priority_queue(InputIterator first, InputIterator last, const Compare& x)
: c(first, last), comp(x) { make_heap(c.begin(), c.end(), comp); }
template <class InputIterator>
priority_queue(InputIterator first, InputIterator last)
: c(first, last) { make_heap(c.begin(), c.end(), comp); }
#else /* __STL_MEMBER_TEMPLATES */
priority_queue(const value_type* first, const value_type* last,
const Compare& x) : c(first, last), comp(x) {
make_heap(c.begin(), c.end(), comp);
}
priority_queue(const value_type* first, const value_type* last)
: c(first, last) { make_heap(c.begin(), c.end(), comp); }
#endif /* __STL_MEMBER_TEMPLATES */
bool empty() const { return c.empty(); }
size_type size() const { return c.size(); }
const_reference top() const { return c.front(); }
void push(const value_type& x) {
__STL_TRY {
// push_heap 是泛型演算法。先利用底層容器的 push_back() 將新元素
// 推入末端。再重排 heap。見C++ Primer p.1195。 c.push_back(x);
push_heap(c.begin(), c.end(), comp); // push_heap 是泛型演算法
}
__STL_UNWIND(c.clear());
}
void pop() {
__STL_TRY {
// pop_heap 是泛型演算法,從 heap 內取出一個元素。它並不是真正將元素
// 彈出。而是重排 heap,然後再以底層容器的 pop_back() 取得被彈出
// 的元素。 見C++ Primer p.1195。 pop_heap(c.begin(), c.end(), comp);
c.pop_back();
}
__STL_UNWIND(c.clear());
}
};
// no equality is provided
__STL_END_NAMESPACE
#endif /* __SGI_STL_INTERNAL_QUEUE_H */
// Local Variables:
// mode:C++
// End:

STL 之 queue、priority_queue 源代码剖析的更多相关文章

  1. STL源代码剖析 读书总结

    <<STL源代码剖析>> 侯捷著 非常早就买了这本书, 一直没看, 如今在实验室师兄代码的时候发现里面使用了大量泛型编程的内容, 让我有了先看看这本书的想法. 看之前我对于泛型 ...

  2. STL总结之queue, priority_queue, stack

    之所以把这三个容器放在一起,是因为他们都是容器适配器.   STL中queue就是我们常用的FIFO队列,实现是一个容器适配器,这种数据结构在网络中经常使用.   queue的模板声明: templa ...

  3. STL源代码剖析(一) - 内存分配

    Allocaor allocator 指的是空间配置器,用于分配内存.STL中默认使用SGI STL alloc作为STL的内存分配器,尽管未能符合标准规格,但效率上更好.SGI STL也定义有一个符 ...

  4. STL源代码剖析——STL算法stl_algo.h

    前言 在前面的博文中剖析了STL的数值算法.基本算法和set集合算法.本文剖析STL其它的算法,比如排序算法.合并算法.查找算法等等.在剖析的时候.会针对函数给出一些样例说明函数的使用.源代码出自SG ...

  5. STL源代码剖析——基本算法stl_algobase.h

    前言 在STL中.算法是常常被使用的,算法在整个STL中起到很关键的数据.本节介绍的是一些基本算法,包括equal.fill.fill_n,iter_swap.lexicographical_comp ...

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

    看完,<STL源代码剖析>---stl_deque.h阅读笔记(1)后.再看代码: G++ 2.91.57,cygnus\cygwin-b20\include\g++\stl_deque. ...

  7. STL源代码剖析——STL算法之set集合算法

    前言 本节介绍set集合的相关算法,各自是并集set_union,差集set_difference,交集set_intersection 和对称差集set_symmetric_difference.这 ...

  8. STL源代码剖析(二) - 迭代器与traits技法

    提要 先看一段用迭代器的代码: int a[] = {1, 2, 3, 4, 5}; vector<int> v1( a, a+5); vector<int>::iterato ...

  9. [STL]heap和priority_queue

    一.heap 在STL中,priority_queue(优先权队列)的底层机制是最大堆,因此有必要先来了解一下heap.heap采用完全二叉树的结构,当然不是真正的binary tree,因为对于完全 ...

随机推荐

  1. iconv 文件编码转换

    iconv 文件编码转换 http://www.cnblogs.com/xuxm2007/archive/2010/11/09/1872379.html 查看iconv的支持的编码: $ iconv  ...

  2. Python 第三篇(下):collections系列、集合(set)、单双队列、深浅copy、内置函数

     一.collections系列: collections其实是python的标准库,也就是python的一个内置模块,因此使用之前导入一下collections模块即可,collections在py ...

  3. java--异常处理总结

    [在程序中抛出异常] 在程序中抛出异常,一定要使用关键字throw. throw+异常实例对象. public class Demo2 { public static void main(String ...

  4. VMware 11安装Mac OS X 10.10 及安装Mac Vmware Tools(超详细),以及动态调整虚拟机硬盘大小

    先上一张效果图兴奋一下,博主穷屌丝一个,只能通过虚拟黑苹果体验下高富帅的生活,感觉超爽的,废话不多说的,直接上图了! 目录: 1.安装所需软件下载: 2.Mac OS X10.10 安装基本步骤: 3 ...

  5. Qt 内存管理机制

    这篇文章首先发布于我的主页 http://www.devbean.info,以后也会直接发布在那里.现在有 Flex 4 的一篇和 <从 C++ 到 Objective-C>系列,感谢大家 ...

  6. 用PowerMockito来mock私有方法(转)

    话说我们做的所谓的接口测试真的是不伦不类啊,测的是controller层,那叫接口木??!!可是老大们说写的是接口测试,那就接口吧! 自接手写这个接口测试不久,很多人,包括一个关系比较好的架构师就跟我 ...

  7. IOI2014 day2 task4 Holiday

    题目 题目链接 大意:从左到右有\(n\)个城市,一开始在城市\(start\),每一天有两种选择: 前往相邻的城市. 访问当前城市(每个城市只能访问一次),访问城市\(i\)可以获得\(attrac ...

  8. OGR API Tutorial

    This document is intended to document using the OGR C++ classes to read and write data from a file. ...

  9. Oracle单表的简单查询

    Oracle单表的简单查询 查看表结构 desc emp; 查询所有列 Select * from emp; 查找所以部门编号(查指定的列) select deptnofrom emp; 查找编号不同 ...

  10. C++断言assert

    assert宏是在标准库中提供的.它在库文件<cassert>中声明,它能够在程序中測试逻辑表达式,假设指定的逻辑表达式是false,assert()就会终止程序,并显示诊断消息.关闭断言 ...