DS 扩大包门路:[DS扩大包门路]: https://pecl.php.net/package/ds
CentOS7 中 DS 扩大装置(应用 phpize)
> phpize> ./configure --with-php-config=/php/bin/php-config> make && make install
DS 扩大函数介绍
简介:DS 扩大是 PHP7 高效的数据结构,能够作为 array 的代替.
堆栈:堆栈是后进先出(last in, first out)或后进先出(LIFO)汇合,它只容许拜访构造顶部的值,并按该程序进行迭代。
<?php //PHP 中的堆栈实现,PHP 的 DS 扩大中集成了堆栈的操作函数,DS 扩大是须要独自装置的 //DS 中堆栈的所有办法 Ds\Stack implements Ds\Collection , ArrayAccess { /* 办法 */ public allocate ( int $capacity ) : void public capacity ( ) : int public clear ( ) : void public copy ( ) : Ds\Stack public isEmpty ( ) : bool public peek ( ) : mixed public pop ( ) : mixed public push ( mixed ...$values ) : void public toArray ( ) : array } $list = [11,12,13]; //初始化一个堆栈并将输出入栈,入栈程序是 $stack = new \Ds\Stack($list); //设置栈的内存大小,如果该值小于或等于以后容量,容量将放弃不变。 $stack->allocate(16); //查看堆栈 print_r($stack); /* 输入内容 Ds\Stack Object ( [0] => 13 [1] => 12 [2] => 11 ) */ //获取以后容量大小,如果入栈数据大小理论容量大于 allocate() 设置容量将返回理论容量;如果理论容量小于 allocate() 设置的容量将返回设置的容量;下面设置16将返回16;如果不设置则返回8 print_r($stack->capacity()); //清空堆栈 print_r($stack->clear()); /* 输入内容 Ds\Stack Object () */ //堆栈转数组 print_r($stack->toArray()); /* 输入数组: Array ( [0] => 13 [1] => 12 [2] => 11 ) */ //push 办法参数格局 $stack->push(14); $stack->push(14,15); $stack->push([14,15]); $list = [11,12,13]; $stack = new \Ds\Stack($list); print_r($stack->count());//获取堆栈总数 输入:3
堆栈复制并批改堆栈变动比照
<?php $list = [11,12,13]; $stack = new \Ds\Stack($list); print_r($stack); /* 初始化堆栈输入内容: Ds\Stack Object ( [0] => 13 [1] => 12 [2] => 11 ) */ $stack_copy = $stack->copy(); print_r($stack_copy); /* 复制正本输入内容: Ds\Stack Object ( [0] => 13 [1] => 12 [2] => 11 ) */ //原堆栈入栈数据 $stack->push(14); /* 原堆栈输入数据: Ds\Stack Object ( [0] => 14 [1] => 13 [2] => 12 [3] => 11 ) 复制正本堆栈输入数据: Ds\Stack Object ( [0] => 13 [1] => 12 [2] => 11 ) */ //复制正本堆栈入栈数据 $stack->push(15); /* 原堆栈输入数据: Ds\Stack Object ( [0] => 14 [1] => 13 [2] => 12 [3] => 11 ) 复制正本堆栈输入数据: Ds\Stack Object ( [0] => 15 [1] => 13 [2] => 12 [3] => 11 ) */
堆栈是否为空比照
<?php $list = [11,12,13]; $stack = new \Ds\Stack($list); $stack1 = new \Ds\Stack(); var_dump($stack->isEmpty());//输入:bool(false) var_dump($stack1->isEmpty());//输入:bool(true) //清空 stack $stack->clear(); //入栈 stack1 $stack1->push(14); var_dump($stack->isEmpty());//输入:bool(true) var_dump($stack1->isEmpty());//输入:bool(false)
数据出栈比照
<?php $list = [11,12,13]; $stack = new \Ds\Stack($list); print_r($stack); /* 初始化数输入数据: Ds\Stack Object ( [0] => 13 [1] => 12 [2] => 11 ) */ //办法 peek 只返回栈顶数据,不出栈 print_r($stack->peek());//输入:13 print_r($stack); /* 调用 peek 后输入数据: Ds\Stack Object ( [0] => 13 [1] => 12 [2] => 11 ) */ //办法 pop 返回栈顶数据并出栈 print_r($stack->pop());//输入:13 print_r($stack); /* 调用 pop 后输入数据: Ds\Stack Object ( [0] => 12 [1] => 11 ) */
队列:队列是一个先入先出或FIFO汇合,它只容许拜访队列后面的值,并按该程序进行迭代。
<?php Ds\Queue implements Ds\Collection , ArrayAccess { /* Constants */ const int MIN_CAPACITY = 8 ; /* 办法 */ public allocate ( int $capacity ) : void public capacity ( ) : int public clear ( ) : void public copy ( ) : Ds\Queue public isEmpty ( ) : bool public peek ( ) : mixed public pop ( ) : mixed public push ( mixed ...$values ) : void public toArray ( ) : array } $list = [11,12,13]; //初始化队列 $queue = new \Ds\Queue($list); $queue->capacity();//返回以后容量:8 //设置队列内存大小 $queue->allocate(31); $queue->capacity();//返回设置后的以后容量:32 //设置队列内存大小 $queue->allocate(33); $queue->capacity();//返回设置后的以后容量:64 //capacity 容量总是四舍五入到最靠近的2次方。 print_r($queue->count());//获取队列长度 输入:3
队列清空、判断空比照
<?php $list = [11,12,13]; //初始化队列 $queue = new \Ds\Queue($list); $queue1 = new \Ds\Queue(); var_dump($queue->isEmpty());//输入:bool(false) var_dump($queue1->isEmpty());//输入:bool(true) $queue->clear(); $queue1->push(14); var_dump($queue->isEmpty());//输入:bool(true) var_dump($queue1->isEmpty());//输入:bool(false)
队列复制并批改堆栈变动比照
<?php $list = [11,12,13]; //初始化队列 $queue = new \Ds\Queue($list); $queue_copy = $queue->copy(); print_r($queue); /* 初始化输入队列: Ds\Queue Object ( [0] => 11 [1] => 12 [2] => 13 ) */ print_r($queue_copy); /* 复制正本输入队列: Ds\Queue Object ( [0] => 11 [1] => 12 [2] => 13 ) */ //原队列入队数据 $queue->push(14); /* 原队列输入队列: Ds\Queue Object ( [0] => 11 [1] => 12 [2] => 13 [3] => 14 ) 复制正本输入队列: Ds\Queue Object ( [0] => 11 [1] => 12 [2] => 13 ) */ //复制副本队列入队数据 $queue_copy->push(15); /* 原队列输入队列: Ds\Queue Object ( [0] => 11 [1] => 12 [2] => 13 ) 复制正本输入队列: Ds\Queue Object ( [0] => 11 [1] => 12 [2] => 13 [3] => 15 ) */
队列出队数据比照
<?php $list = [11,12,13]; //初始化队列 $queue = new \Ds\Queue($list); //办法 peek 只返回队头数据,不出队列 print_r($queue->peek());//输入:11 print_r($queue); /* 原队列输入队列: Ds\Queue Object ( [0] => 11 [1] => 12 [2] => 13 ) */ //办法 pop 只返回队头数据,并出队列 print_r($queue->pop());//输入:11 print_r($queue); /* 原队列输入队列: Ds\Queue Object ( [0] => 12 [1] => 13 ) */
队列和堆栈入队数据比照
<?php $queue = new \Ds\Queue(); $queue->push(11); $queue->push(12,13); $queue->push([14,15]); print_r($queue); $stack = new \Ds\Stack(); $stack->push(11); $stack->push(12,13); $stack->push([14,15]); print_r($stack); //输入数据 //队列数据: Ds\Queue Object ( [0] => 11 [1] => 12 [2] => 13 [3] => Array ( [a] => 14 [b] => 15 ) [4] => Array ( [0] => 16 [1] => 17 ) ) //堆栈数据: Ds\Stack Object ( [0] => Array ( [0] => 16 [1] => 17 ) [1] => Array ( [a] => 14 [b] => 15 ) [2] => 13 [3] => 12 [4] => 11 ) /* 后果: 1.队列先入队的在队头,出队先入先出 2.堆栈先入栈的在栈尾,出队先入后出 */