echo "Mem real: " . __convert(memory_get_usage(true)) . "\n"; unset($array); echo "\n-- SplDoublyLinkedList \n"; echo "Mem usage: " . __convert(memory_get_usage()) . "\n"; echo "Mem real: " . __convert(memory_get_usage(true)) . "\n"; $s = microtime(true); $spl = new SplDoublyLinkedList(); for ($i = 0; $i < $v; $i++) { $spl->push($i); } try { $spl->offsetUnset(102); var_dump($spl->offsetGet(100)); var_dump($spl->offsetGet(102)); } catch (OutOfRangeException $e) { echo $e; } $e = microtime(true); echo "Count: " . $spl->count() . "\n"; echo "Elapsed time: " . ($e - $s) . " sec.\n"; echo "Mem usage: " . __convert(memory_get_usage()) . "\n"; echo "Mem real: " . __convert(memory_get_usage(true)) . "\n"; try { for ($i = 0; $i < $v; $i++) { $spl->pop(); } } catch (Exception $e) { } unset($spl); echo "\n"; }
* 栈SplStack * 队列SplQueue * * 栈和队列都是继承于双向链表,所以,所有双向链表的方法都可以被栈和队列使用。调用方法也一致。 */ $list = new SplDoublyLinkedList(); // push($value) // 在结尾插入一个新值 $list->push('value1'); $list->push('value2'); $list->push('value3'); $list->push('value4'); $list->push('value5'); // pop() // 抛出结尾的一个元素,会使得链表结构减少一个 $list->pop(); // key() // 获得当前节点的索引值 $list->key(); // count() // 获得链表的数量 $list->count(); // rewind() // 将指针返回至初始节点 $list->rewind(); // current() // 获得当前节点 $list->current(); // top() // 返回最后一个节点的值 $list->top();
/** * Make an iterable that will yield this iterable's values in reverse order. Keys in the * original iterable will be preserved. * * This iterable will be consumed in its entirety when `reverse()` is called. * * Examples * * Crankshaft::Iter(['a', 'b', 'c', 'd', 'e'])->reverse()->to_array() * // => [4 => 'e', 3 => 'd, 2 => 'c', 1 => 'b', 0 => 'a'] * * Returns an Iterable. */ public function reverse() { $keys = new \SplDoublyLinkedList(); $values = new \SplDoublyLinkedList(); foreach ($this as $key => $value) { $keys->push($key); $values->push($value); } $reversed = []; while (!$values->isEmpty()) { $reversed[$keys->pop()] = $values->pop(); } return Crankshaft::Iter($reversed); }
<?php $dll = new SplDoublyLinkedList(); // errors try { $dll->pop(); } catch (RuntimeException $e) { echo "Exception: " . $e->getMessage() . "\n"; } try { $dll->shift(); } catch (RuntimeException $e) { echo "Exception: " . $e->getMessage() . "\n"; } // data consistency $a = 2; $dll->push($a); echo $dll->pop() . "\n"; $a = 2; $dll->unshift($a); echo $dll->shift() . "\n"; // peakable $dll->push(1); $dll->push(2); echo $dll->top() . "\n"; echo $dll->bottom() . "\n"; $dll->pop(); $dll->pop(); // countable $dll->push(NULL); $dll->push(NULL);
<?php $array = new SplDoublyLinkedList(); $get = $array->pop('param');
<?php $ll = new SplDoublyLinkedList(); $ll->push(1); $ll->push(2); var_dump($ll->pop(1));
<?php $dll = new SplDoublyLinkedList(); $dll->push('foo'); $dll->push('bar'); echo $dll->pop(), PHP_EOL; $dll->rewind(); echo $dll->current(), PHP_EOL; $dll->next(); echo $dll->current(), PHP_EOL;
global $a; $a = range(1, $n); }, function ($i) { global $a; array_pop($a); }, function () { global $a; $a = null; }], SPL_DLL => [function ($n) { global $a; $a = new SplDoublyLinkedList(); for (; $n--; $a[] = rand()) { } }, function ($i) { global $a; $a->pop(); }, function () { global $a; $a = null; }], VECTOR => [function ($n) { global $a; $a = new Vector(range(1, $n)); }, function ($i) { global $a; $a->pop(); }, function () { global $a; $a = null; }], DEQUE => [function ($n) { global $a; $a = new Deque(range(1, $n));
/** * Pops a `Token` from the end of the list. * * @return Token The popped token. * @link http://www.php.net/manual/en/spldoublylinkedlist.pop.php */ public function pop() { return $this->tokens->pop(); }