Exemplo n.º 1
0
 public function process($index, \SplDoublyLinkedList $list)
 {
     $first = $list->offsetGet($index - 1);
     $second = $list->offsetGet($index + 1);
     $result = $first * $second;
     $list->offsetSet($index - 1, $result);
     unset($list[$index]);
     unset($list[$index]);
 }
Exemplo n.º 2
0
 public function process($index, \SplDoublyLinkedList $list)
 {
     $first = $list->offsetGet($index - 1);
     $second = $list->offsetGet($index + 1);
     if ($second == 0) {
         throw new \Exception('zero division error');
     }
     $result = $first / $second;
     $list->offsetSet($index - 1, $result);
     unset($list[$index]);
     unset($list[$index]);
 }
Exemplo n.º 3
0
 /**
  * Returns whether or not the iterator has more elements
  *
  * @return bool
  */
 public function valid()
 {
     if ($this->_buffer->offsetExists($this->_index)) {
         $current = $this->_buffer->offsetGet($this->_index);
         $this->_current = $current['value'];
         $this->_key = $current['key'];
         return true;
     }
     $valid = parent::valid();
     if ($valid) {
         $this->_current = parent::current();
         $this->_key = parent::key();
         $this->_buffer->push(['key' => $this->_key, 'value' => $this->_current]);
     }
     $this->_finished = !$valid;
     return $valid;
 }
Exemplo n.º 4
0
function test(SplDoublyLinkedList $l)
{
    $l->push("a");
    $l->push("b");
    $l->push("c");
    echo "Foreach:", PHP_EOL;
    foreach ($l as $key => $val) {
        echo $key, '=>', $val, PHP_EOL;
    }
    echo PHP_EOL;
    echo "ArrayAccess:", PHP_EOL;
    var_dump($l[0]);
    var_dump($l[1]);
    var_dump($l[2]);
    echo PHP_EOL;
    echo "ArrayAccess Set:", PHP_EOL;
    $l[2] = "two";
    var_dump($l[2]);
    try {
        $l[3] = "five";
        // 3 would be the next element
    } catch (OutOfRangeException $e) {
        echo "OutOfRangeException caught", PHP_EOL;
    }
    echo PHP_EOL;
    echo "ArrayAccess Exists:", PHP_EOL;
    var_dump(isset($l[0]), isset($l[2]), isset($l[3]));
    // true, true, false
    echo PHP_EOL;
    echo "ArrayAccess Unset:", PHP_EOL;
    unset($l[0]);
    var_dump($l->offsetGet(0));
    echo PHP_EOL;
    echo "Foreach:", PHP_EOL;
    foreach ($l as $key => $val) {
        echo $key, '=>', $val, PHP_EOL;
    }
    echo PHP_EOL;
}
Exemplo n.º 5
0
$list->next();
// prev()
// 指针移到上一个节点, 如果原本指针在第一个,那么前一个节点为-1,并且将无法获得当前值
$list->prev();
// valid()
// 判断该链表是否有更多的值,返回bool
$list->valid();
// isEmpty()
// 判断该链表是否为空链表,返回bool
$list->isEmpty();
// offsetExists($index)
// 判断参索引是否存在,返回bool
$list->offsetExists(2);
// offsetGet($index)
// 返回参数索引的节点值
$list->offsetGet(2);
// offsetSet($index, $newValue)
// 设置参数索引的节点值, $index必须在链表的键范围中。
// 当一个节点用offsetUnset删掉时时,并不能用offsetSet重新给已删掉的节点设置值,只能对当前可见的节点的值进行修改
$list->offsetSet(3, 'value6');
// offsetUnset($index)
// 删除参数索引的节点
$list->offsetUnset(3);
// add($index, $value);
// 对指定的索引新增一个新值。 当一个节点用offsetUnset时,并没有直接删除,该节点还仍然会保存在内存中。用add可以重新给该节点设置值
$list->add(3, 'first');
// unshift($value)
// 在链表的开始节点插入value作为新的开始节点
$list->unshift('second');
// shift()
// 将链表的第一个移除
Exemplo n.º 6
0
 echo "Count: " . count($array) . "\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";
 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) {
 }
Exemplo n.º 7
0
 /**
  * @see \ArrayAccess::offsetGet()
  * @param int $offset
  * @return \BitWasp\Buffertools\BufferInterface
  */
 public function offsetGet($offset)
 {
     $offset = count($this) + $offset;
     return parent::offsetGet($offset);
 }
Exemplo n.º 8
0
 public function offsetGet($index)
 {
     return $this->_frames->offsetGet($index);
 }
<?php

$dll = new SplDoublyLinkedList();
$dll->push(1);
$dll->push(2);
var_dump($dll->offsetGet());
<?php

$array = new SplDoublyLinkedList();
$get = $array->offsetGet('fail');
<?php

$array = new SplDoublyLinkedList();
$get = $array->offsetGet();
Exemplo n.º 12
0
 private function process($index, \SplDoublyLinkedList $list)
 {
     $operator = $list->offsetGet($index);
     if (!isset(static::$operandsClasses[$operator])) {
         throw new \Exception('Unknown operator:' . $operator);
     }
     return static::$operandsClasses[$operator]->process($index, $list);
 }
<?php

$array = new SplDoublyLinkedList();
$get = $array->offsetGet(array('fail'));