/** * 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; }
$list->bottom(); // next() // 指针移到下一个节点 $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作为新的开始节点
/** * @see \ArrayAccess::offsetExists() * @param int $offset * @return bool */ public function offsetExists($offset) { $offset = count($this) + $offset; return parent::offsetExists($offset); }
public function offsetExists($index) { return $this->_frames->offsetExists($index); }
<?php $list = new SplDoublyLinkedList(); // Push two values onto the list $list->push('abc'); $list->push('def'); // Validate that we can see the first value if ($list->offsetExists(0) === true) { echo "PASS\n"; } // Validate that we can see the second value if ($list->offsetExists(1) === true) { echo "PASS\n"; } // Check that there is no third value if ($list->offsetExists(2) === false) { echo "PASS\n"; }
<?php $list = new SplDoublyLinkedList(); $a = $list->offsetExists(); if (is_null($a)) { echo 'PASS'; }