stl源码剖析 详细学习笔记deque(2)
//---------------------------15/3/13----------------------------
self&operator++()
{
++cur;
if(cur==last)
{
set_node(node+);
cur=first;
}
return *this;
}
self operator++(int) //这里不能返回引用,因为
tmp是临时变量,作用域结束tmp就消失了
{
self tmp=*this;
++*this;
return tmp;
}
self&operator--()
{
if(cur=first)
{
set_node(node-);
cur=last;
}
--cur;
return *this;
}
selfoperator--(int)
{
self tmp=*this;
--*this;
return temp;
}
/*
1.看能否在不改变node的情况下进行指针位置的移动
2.调整node
offset==(n+x) -->>
最后的位置==first + (n + x)
x代表cur
距离first的距离,也就是cur当前的下标
(offset>0): offset/diffenrence_type(buffer_size())
-->> y==(n + x)/(sz) -->>移动的node节点个数为y
(offset<0) -difference_type((-offset -1) / buffer_size()) - 1
-->> y==-(-n - x -1)/(sz) -1 -->> y==(n + x +1)/(sz) -1
不同于(offset<0)的情况,因为当前处于first位置,移动的距离在
-1到-(sz)是都应该移动一个node节点所以才取上式子;
3.调整cur
(offset>0) :offset==(n + x)
node_offset*difference_type(buffer_size())==(n + x -r)
(r代表余数)
-->>z(位移量)==r
(offset<0) :offset==(n + x)
node_offset*difference_type(buffer_size())==(n + x +r-sz)
-->>z(位移量)==-sz + r
cur =z
*/
self&operator+=(difference_type n)
{
difference_type offset =n+(cur - first);
if(offset >=
&& offset < difference_type(buffer_size()))
cur+=n;
else
{
difference_type node_offset=
offset > ? offset/difference_type(buffer_size())
: -difference_type((-offset -) / buffer_size()) - ;
set_node(node + node_offset);
cur = first +(offset - node_offset * difference_type(buffer_size()));
}
return *this;
}
selfoperator+(difference_type n)
const
{
self tmp=*this;
return tmp+=n;
}
self &operator -=(difference_type n){return *this +=-n;}
selfoperator-(difference_type n)
const
{
self tmp =*this;
return tmp-=n;
}
referenceoperator[](difference_type)const{return *(*this
+n);}
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);
}
>
class deque
{
public:
typedef T value_type;
typedef value_type* pointer;
typedef size_t size_type;
typedef __deque_iterator<T,T&,T*,BufSiz> iteratoer;
protected:
typedef pointer* map_pointer;
iteratoer start;
iteratoer finish;
map_pointer map;
size_type map_size;
public:
iteratoer begin(){return start;}
iteratoer end() {return finish;}
referenceoperator[](size_type n)
{
return start[difference_type(n)];
}
reference front(){return *start;}
reference back()
{
iteratoer tmp=finish;
--tmp;
return *tmp;
//上面三行不改为 return *(finish-1)是因为operator -(difference_type n)
//
的操作比--复杂很多
}
size_type size()const {return finish - start;;}//两个;是手误??
}
stl源码剖析 详细学习笔记deque(2)的更多相关文章
- stl源码剖析 详细学习笔记deque(3)
protected: typedef simple_alloc<value_type,Alloc> data_allocator; //用来配置元素的alloc typedef simpl ...
- stl源码剖析 详细学习笔记deque(1)
//--------------------------15/3/12---------------------------- deque { deque没有容量(capacity)观念,是动态分段的 ...
- stl源码剖析 详细学习笔记 hashtable
//---------------------------15/03/24---------------------------- //hashtable { /* 概述: sgi采用的是开链法完成h ...
- stl源码剖析 详细学习笔记 set map
// // set map.cpp // 笔记 // // Created by fam on 15/3/23. // // //---------------------------15/03 ...
- stl源码剖析 详细学习笔记 RB_tree (1)
// // RB_tree_STL.cpp // 笔记 // // Created by fam on 15/3/21. // // #include "RB_tree_STL.h&q ...
- stl源码剖析 详细学习笔记priority_queue slist
// // priority_queue.cpp // 笔记 // // Created by fam on 15/3/16. // // //------------------------- ...
- stl源码剖析 详细学习笔记heap
// // heap.cpp // 笔记 // // Created by fam on 15/3/15. // // //---------------------------15/03/15 ...
- stl源码剖析 详细学习笔记stack queue
// // stack.cpp // 笔记 // // Created by fam on 15/3/15. // // //---------------------------15/03/1 ...
- stl源码剖析 详细学习笔记 空间配置器
//---------------------------15/04/05---------------------------- /* 空间配置器概述: 1:new操作包含两个阶段操作 1>调 ...
随机推荐
- SQL Server 2012 列存储索引分析(转载)
一.概述 列存储索引是SQL Server 2012中为提高数据查询的性能而引入的一个新特性,顾名思义,数据以列的方式存储在页中,不同于聚集索引.非聚集索引及堆表等以行为单位的方式存储.因为它并不要求 ...
- CSS| text文本属性
注意:一般来说,可以为所有块级元素应用 text-indent,但无法将该属性应用于行内元素,图像之类的替换元素上也无法应用 text-indent 1) text-indent 取值: 5px/2 ...
- Centos7(Firewall)防火墙开启常见端口命令
Centos7默认安装了firewalld,如果没有安装的话,则需要YUM命令安装:firewalld真的用不习惯,与之前的iptable防火墙区别太大,但毕竟是未来主流讲究慢慢磨合它的设置规则: 安 ...
- 通过C/C++基于http下载文件
简介 Windows系统如何通过C/C++下载互联网上的文件呢?这里笔者给大家演示一个最简单的方法,利用Windows提供的urlmon库,可以快速实现文件下载的简单实例. 注:本文内容部分参考了&l ...
- Django商城项目笔记No.8用户部分-注册接口实现
Django商城项目笔记No.8用户部分-注册接口实现 users的view.py中增加如下代码 class RegisterUserView(CreateAPIView): "" ...
- (十九)ArcGIS JS 加载WMS服务(超图示例)
前言 在工作中,需要在ArcGIS API for JavaScript中加载超图服务,因为超图的rest服务只可以用于Leaflet .openlayers3 (with MVT) .MapboxG ...
- CF558E A Simple Task
题目大意: 给定一个长度不超过10^5的字符串(小写英文字母),和不超过5000个操作. 每个操作 L R K 表示给区间[L,R]的字符串排序,K=1为升序,K=0为降序. 最后输出最终的字符串 首 ...
- 两个docker容器互连时,提示no route to host错误的问题
大家都知道,两个docker container互连的时候可以用link,但是,我们也知道,container可以将自己的端口映射到宿主机上,比如一个container A上的tomcat将端口暴露给 ...
- php大流量高并发解决方案
一.硬件 提升硬件,影响因素有: 带宽-硬盘读写速度-内存大小-cpu处理速度 二.软件 反向代理负载均衡 mysql : 1.优化你的sql和索引 2.加缓存,memcached,redis 3 ...
- apache不能启动LoadModule php5_module modules/ph
apache不能启动LoadModule php5_module modules/php5apache2.dll的问题 主要是版本问题!!有点不爽!! apache不能启动 加入下面两行,apache ...