SPL學習之SplDoublyLinkedList
2015-07-08 14:22
495 查看
Standard PHP Library(SPL)是官方提供的标准库,从php5.0.0开始已经默认实现在php中,我们可以类比它为ruby中的gem安装的包。spl里面实现了许多迭代器和数据结构对象接口,非常实用和高效。下面是我的学习记录:
从php5.0.0之后才默认可用,而在php5.3.0开始这个扩展将一直开启,且不在php.ini内配置。
根据php.net/spl 相关文档描述,spl分为以下7种大类:
1.Datastructures数据结构对象。
2.Iterators迭代器。
3.interfaces接口。
4.Exceptions异常。
5.SPL Functions标准函数。
6.File Handling文件资源句柄。
7.Miscellaneous Classes and Interfaces多元类和接口。
下面我依次总结:
1.Datastructures
数据结构一直都是编程的主力。以前我们所说的堆、栈、队列,链表,树等。程序=数据结构+算法,已经成了大部分的人共识,在处理一些特定问题,选择适当的数据结构能够事半功倍。
1.The SplDoublyLinkedList class (双向链表)
双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。一般我们都构造双向循环链表。(by wiki description)
funlist can see:
http://php.net/manual/zh/class.spldoublylinkedlist.php
// init an object of double Link list
$dlist = new SplDoublyLinkedList();
// insert item to the end of the list
$dlist->push("Linker");
$dlist->push("Joker");
$dlist->push("forker");
$dlist->push("worker");
// use unshift can insert an object at top of the list
$dlist->unshift('FreePHP');
// pop an object from the bottom of the list
$dlist->pop();
// delete an object from the top of the list
$dlist->shift();
關於遍歷的方式需要用到
public void SplDoublyLinkedList::setIteratorMode ( int
實現迭代器功能的選項有兩種參數,如下:
eg:
$dlist->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO); // use Stack style
for($dlist->rewind();$dlist->valid();$dlist->next()){
echo $dlist->current()."<br/>";;
}
spl中有很多實現迭代器模式的經典例子,此爲其一。在數據庫操作中和數據迭代裏面非常常用。
同样双向链表可以对数据进行序列化和反序列化。
序列化:
$ser = $dlist->serialize();
var_dump($ser);
// out like this : string(45) "i:2;:s:6:"Linker";:s:5:"Joker";:s:6:"forker";"
反序列为unserilalize()方法。
从php5.0.0之后才默认可用,而在php5.3.0开始这个扩展将一直开启,且不在php.ini内配置。
根据php.net/spl 相关文档描述,spl分为以下7种大类:
1.Datastructures数据结构对象。
2.Iterators迭代器。
3.interfaces接口。
4.Exceptions异常。
5.SPL Functions标准函数。
6.File Handling文件资源句柄。
7.Miscellaneous Classes and Interfaces多元类和接口。
下面我依次总结:
1.Datastructures
数据结构一直都是编程的主力。以前我们所说的堆、栈、队列,链表,树等。程序=数据结构+算法,已经成了大部分的人共识,在处理一些特定问题,选择适当的数据结构能够事半功倍。
1.The SplDoublyLinkedList class (双向链表)
双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。一般我们都构造双向循环链表。(by wiki description)
funlist can see:
http://php.net/manual/zh/class.spldoublylinkedlist.php
// init an object of double Link list
$dlist = new SplDoublyLinkedList();
// insert item to the end of the list
$dlist->push("Linker");
$dlist->push("Joker");
$dlist->push("forker");
$dlist->push("worker");
// use unshift can insert an object at top of the list
$dlist->unshift('FreePHP');
// pop an object from the bottom of the list
$dlist->pop();
// delete an object from the top of the list
$dlist->shift();
關於遍歷的方式需要用到
public void SplDoublyLinkedList::setIteratorMode ( int
$mode) 方法
實現迭代器功能的選項有兩種參數,如下:
SplDoublyLinkedList::IT_MODE_LIFO(Stack style)
SplDoublyLinkedList::IT_MODE_FIFO(Queue style)
eg:
$dlist->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO); // use Stack style
for($dlist->rewind();$dlist->valid();$dlist->next()){
echo $dlist->current()."<br/>";;
}
spl中有很多實現迭代器模式的經典例子,此爲其一。在數據庫操作中和數據迭代裏面非常常用。
同样双向链表可以对数据进行序列化和反序列化。
序列化:
$ser = $dlist->serialize();
var_dump($ser);
// out like this : string(45) "i:2;:s:6:"Linker";:s:5:"Joker";:s:6:"forker";"
反序列为unserilalize()方法。
相关文章推荐
- 简单的倒计时效果
- 16进制 ,Color,Colour转换
- muduo网络编程分包和解包(一)
- HttpServletRequest.getParameter("key") 和 HttpServletRequest.getParameterMap().get("key")
- MySql的连接管理与安全性
- TOJ 4116 GCD's magic
- nginx实现简单模块开发
- indy
- Set--TreeSet实现分析
- 【KMP】 HDU 3374 String Problem 最小表示法
- 写给将要实习的你-实习前你必须知道的几件事-SQL命令(二)
- Golang png Decode解析不了png图片
- 记录一个mysql按日期分组统计的查询
- 动态加载字符根据字符串的数量和长度动态加载
- Charles抓包
- winform中利用反射实现泛型数据访问对象基类(2)
- [LeetCode] Binary Search Tree Iterator
- SQLSTATE[HY000] [2002] No such file or directory
- 51nod 1400 序列分解(DFS + 剪枝)
- 如何在quartz的job中实现spring的依赖注入