SPL是用于解决典型问题(standard problems)的一组接口与类的集合。php
此扩展只能在php 5.0之后使用,从PHP 5.3.0 再也不被关闭,会一直有效.成为php内核组件一部份。node
SPL提供了一组标准数据结构。python
1.构建此扩展不须要其余扩展。数组
更详细的状况可参考 http://php.net/manual/zh/spl.datastructures.php数据结构
双链表是一种重要的线性存储结构,对于双链表中的每一个节点,不单单存储本身的信息,还要保存前驱和后继节点的地址。this
SplDoublyLinkedList implements Iterator , ArrayAccess , Countable { /* 方法 */ public __construct ( void ) public void add ( mixed $index , mixed $newval ) public mixed bottom ( void )//双链表的尾部节点 public int count ( void )//双联表元素的个数 public mixed current ( void )//当前记录 public int getIteratorMode ( void ) //获取迭代模式 public bool isEmpty ( void )//检测双链表是否为空 public mixed key ( void )//当前节点索引 public void next ( void )//移到下条记录 public bool offsetExists ( mixed $index )//指定index处节点是否存在 public mixed offsetGet ( mixed $index )//获取指定index处节点值 public void offsetSet ( mixed $index , mixed $newval )//设置指定index处值 public void offsetUnset ( mixed $index )//删除指定index处节点 public mixed pop ( void )//从双链表的尾部弹出元素 public void prev ( void )//移到上条记录 public void push ( mixed $value )//添加元素到双链表的尾部 public void rewind ( void )//将指针指向迭代开始处 public string serialize ( void )//序列化存储 public void setIteratorMode ( int $mode )//设置迭代模式 public mixed shift ( void )//双链表的头部移除元素 public mixed top ( void )//双链表的头部节点 public void unserialize ( string $serialized )//反序列化 public void unshift ( mixed $value )//双链表的头部添加元素 public bool valid ( void )//检查双链表是否还有节点 }
接下来是使用方法:.net
$list = new SplDoublyLinkedList(); $list->push('a'); $list->push('b'); $list->push('c'); $list->push('d'); $list->unshift('top'); $list->shift(); $list->rewind();//rewind操做用于把节点指针指向Bottom所在的节点 echo 'curren node:'.$list->current()."<br />";//获取当前节点 $list->next();//指针指向下一个节点 echo 'next node:'.$list->current()."<br />"; $list->next(); $list->next(); $list->prev();//指针指向上一个节点 echo 'next node:'.$list->current()."<br />"; if($list->current()) echo 'current node is valid<br />'; else echo 'current node is invalid<br />'; if($list->valid())//若是当前节点是有效节点,valid返回true echo "valid list<br />"; else echo "invalid list <br />"; var_dump(array( 'pop' => $list->pop(), 'count' => $list->count(), 'isEmpty' => $list->isEmpty(), 'bottom' => $list->bottom(), 'top' => $list->top() )); $list->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO); var_dump($list->getIteratorMode()); for($list->rewind(); $list->valid(); $list->next()) { echo $list->current().PHP_EOL; } var_dump($a = $list->serialize()); //print_r($list->unserialize($a)); $list->offsetSet(0,'new one'); $list->offsetUnset(0); var_dump(array( 'offsetExists' => $list->offsetExists(4), 'offsetGet' => $list->offsetGet(0), )); var_dump($list); //堆栈,先进后出 $stack = new SplStack();//继承自SplDoublyLinkedList类 $stack->push("a<br />"); $stack->push("b<br />"); echo $stack->pop(); echo $stack->pop(); echo $stack->offsetSet(0,'B');//堆栈的offset=0是Top所在的位置,offset=1是Top位置节点靠近bottom位置的相邻节点,以此类推 $stack->rewind();//双向链表的rewind和堆栈的rewind相反,堆栈的rewind使得当前指针指向Top所在的位置,而双向链表调用以后指向bottom所在位置 echo 'current:'.$stack->current().'<br />'; $stack->next();//堆栈的next操做使指针指向靠近bottom位置的下一个节点,而双向链表是靠近top的下一个节点 echo 'current:'.$stack->current().'<br />'; echo '<br /><br />'; //队列,先进先出 $queue = new SplQueue();//继承自SplDoublyLinkedList类 $queue->enqueue("a<br />");//插入一个节点到队列里面的Top位置 $queue->enqueue("b<br />"); $queue->offsetSet(0,'A');//堆栈的offset=0是Top所在的位置,offset=1是Top位置节点靠近bottom位置的相邻节点,以此类推 echo $queue->dequeue(); echo $queue->dequeue(); echo "<br /><br />";
堆(Heap)就是为了实现优先队列而设计的一种数据结构,它是经过构造二叉堆(二叉树的一种)实现。根节点最大的堆叫作最大堆或大根堆(SplMaxHeap),根节点最小的堆叫作最小堆或小根堆(SplMinHeap)。二叉堆还经常使用于排序(堆排序)设计
abstract SplHeap implements Iterator , Countable { /* 方法 用法同双向链表一致 */ public __construct ( void ) abstract protected int compare ( mixed $value1 , mixed $value2 ) public int count ( void ) public mixed current ( void ) public mixed extract ( void ) public void insert ( mixed $value ) public bool isEmpty ( void ) public mixed key ( void ) public void next ( void ) public void recoverFromCorruption ( void ) public void rewind ( void ) public mixed top ( void ) public bool valid ( void ) }
使用方法:指针
//堆 class MySplHeap extends SplHeap{ //compare()方法用来比较两个元素的大小,绝对他们在堆中的位置 public function compare( $value1, $value2 ) { return ( $value1 - $value2 ); } } $obj = new MySplHeap(); $obj->insert(0); $obj->insert(1); $obj->insert(2); $obj->insert(3); $obj->insert(4); echo $obj->top();//4 echo $obj->count();//5 foreach ($obj as $item) { echo $item."<br />"; }
优先队列也是很是实用的一种数据结构,能够经过加权对值进行排序,因为排序在php内部实现,业务代码中将精简很多并且更高效。经过SplPriorityQueue::setExtractFlags(int $flag)设置提取方式能够提取数据(等同最大堆)、优先级、和二者都提取的方式。
SplFixedArray implements Iterator , ArrayAccess , Countable { /* 方法 */ public __construct ([ int $size = 0 ] ) public int count ( void ) public mixed current ( void ) public static SplFixedArray fromArray ( array $array [, bool $save_indexes = true ] ) public int getSize ( void ) public int key ( void ) public void next ( void ) public bool offsetExists ( int $index ) public mixed offsetGet ( int $index ) public void offsetSet ( int $index , mixed $newval ) public void offsetUnset ( int $index ) public void rewind ( void ) public int setSize ( int $size ) public array toArray ( void ) public bool valid ( void ) public void __wakeup ( void ) }
使用方法:
$arr = new SplFixedArray(4); $arr[0] = 'php'; $arr[1] = 1; $arr[3] = 'python'; //遍历, $arr[2] 为null foreach($arr as $v) { echo $v . PHP_EOL; } //获取数组长度 echo $arr->getSize(); //4 //增长数组长度 $arr->setSize(5); $arr[4] = 'new one'; //捕获异常 try{ echo $arr[10]; } catch (RuntimeException $e) { echo $e->getMessage(); }
用来存储一组对象的,特别是当你须要惟一标识对象的时候。
SplObjectStorage implements Countable , Iterator , Serializable , ArrayAccess { /* 方法 */ public void addAll ( SplObjectStorage $storage ) public void attach ( object $object [, mixed $data = NULL ] ) public bool contains ( object $object ) public int count ( void ) public object current ( void ) public void detach ( object $object ) public string getHash ( object $object ) public mixed getInfo ( void ) public int key ( void ) public void next ( void ) public bool offsetExists ( object $object ) public mixed offsetGet ( object $object ) public void offsetSet ( object $object [, mixed $data = NULL ] ) public void offsetUnset ( object $object ) public void removeAll ( SplObjectStorage $storage ) public void removeAllExcept ( SplObjectStorage $storage ) public void rewind ( void ) public string serialize ( void ) public void setInfo ( mixed $data ) public void unserialize ( string $serialized ) public bool valid ( void ) }
使用方法:
class A { public $i; public function __construct($i) { $this->i = $i; } } $a1 = new A(1); $a2 = new A(2); $a3 = new A(3); $a4 = new A(4); $container = new SplObjectStorage(); //SplObjectStorage::attach 添加对象到Storage中 $container->attach($a1); $container->attach($a2); $container->attach($a3); //SplObjectStorage::detach 将对象从Storage中移除 $container->detach($a2); //SplObjectStorage::contains用于检查对象是否存在Storage中 var_dump($container->contains($a1)); //true var_dump($container->contains($a4)); //false //遍历 $container->rewind(); while($container->valid()) { var_dump($container->current()); $container->next(); }
顾名思义,固定长度的数组
SplFixedArray implements Iterator , ArrayAccess , Countable { /* 方法 */ public __construct ([ int $size = 0 ] ) public count ( void ) : int public current ( void ) : mixed public static fromArray ( array $array [, bool $save_indexes = TRUE ] ) : SplFixedArray public getSize ( void ) : int public key ( void ) : int public next ( void ) : void public offsetExists ( int $index ) : bool public offsetGet ( int $index ) : mixed public offsetSet ( int $index , mixed $newval ) : void public offsetUnset ( int $index ) : void public rewind ( void ) : void public setSize ( int $size ) : bool public toArray ( void ) : array public valid ( void ) : bool public __wakeup ( void ) : void }
使用方法:
<?php // Initialize the array with a fixed length $array = new SplFixedArray(5); $array[1] = 2; $array[4] = "foo"; var_dump($array[0]); // NULL var_dump($array[1]); // int(2) var_dump($array["4"]); // string(3) "foo" // Increase the size of the array to 10 $array->setSize(10); $array[9] = "asdf"; // Shrink the array to a size of 2 $array->setSize(2); // The following lines throw a RuntimeException: Index invalid or out of range try { var_dump($array["non-numeric"]); } catch(RuntimeException $re) { echo "RuntimeException: ".$re->getMessage()."\n"; } try { var_dump($array[-1]); } catch(RuntimeException $re) { echo "RuntimeException: ".$re->getMessage()."\n"; } try { var_dump($array[5]); } catch(RuntimeException $re) { echo "RuntimeException: ".$re->getMessage()."\n"; } ?>