public function setup() { $array = new \SplFixedArray(3); $array->offsetSet(0, new StringLiteral('one')); $array->offsetSet(1, new StringLiteral('two')); $array->offsetSet(2, new Integer(3)); $this->collection = new Collection($array); }
/** * {@inheritdoc} */ public function offsetSet($index, $newval) { if (!empty($newval) && false === $newval instanceof Hypothesis) { throw new \InvalidArgumentException(sprintf('HypothesesList could only contain Hypothesis, %s given.', get_class($newval))); } parent::offsetSet($index, $newval); }
public function offsetSet($offset, $value) { if (!$this->isValid($offset, $value)) { throw new \RuntimeException(); } return parent::offsetSet($offset, $value); }
/** * Returns a Collection of the values. * * @return Collection */ public function values() { $count = $this->count()->toNative(); $valuesArray = new \SplFixedArray($count); foreach ($this->items as $key => $item) { $valuesArray->offsetSet($key, $item->getValue()); } return new Collection($valuesArray); }
/** * Set collection item. * * Warning! `$newval` must not be typehinted to be compatible with `ArrayAccess::offsetSet` method. * * @param int $index * @param Token $newval */ public function offsetSet($index, $newval) { $this->changed = true; $this->registerFoundToken($newval); parent::offsetSet($index, $newval); }
<?php /* spl 提供一种固定数组数据结构,可以用以替代php的(数字索引)数组 它的性能更高。 */ $a = new SplFixedArray(6); $a[5] = 'iam5'; $a[1] = 'iam7'; //$a['a'] ="aaa"; //对固定数组对象的操作最好使用它提供的方法,而非直接以数组的方式操作,如下是设置数组元素值的方法 $a->offsetSet(0, 'iam0'); // current() 等方法都是操作的数组内部的指针,而非索引 // 默认情况下指针指向数组第一个元素。 $va = $a->current(); var_dump($va); //将一个普通数组转换为splfixarray // 默认保留数字索引,且以最大数字索引为依据生成固定数组大小,如这里是9+1 = 10 $ar = ['aaa', 'bbb', 6 => 'ccc', 9 => 'ddd']; $wo = SplFixedArray::fromArray($ar); $h4 = $wo->offsetExists(6); var_dump($h4, $wo);
/** * @param int $offset * @param mixed $value */ public function offsetSet($offset, $value) { $this->set->offsetSet($offset, $value); }
/** * Constructor * @param \Segment\Model\SearchValues|\Segment\Model\Expression $value1 * @param \Segment\Model\SearchValues|\Segment\Model\Expression $values Variable-length variable. */ public function __construct($value1, ...$values) { $count = count($values); $v_arr = new \SplFixedArray($count + 1); $v_arr->offsetSet(0, $value1); for ($i = 0; $i < $count; $i++) { $v_arr->offsetSet($i + 1, $values[$i]); } $this->values = $v_arr; $this->count = $v_arr->count(); }
<?php $array = new SplFixedArray(5); $a = $array->offsetSet(2); if (is_null($a)) { echo 'PASS'; }
public function offsetSet($n, $v) { echo "A::offsetSet\n"; return parent::offsetSet($n, $v); }
// 获得当前节点 $array->current(); // next() // 指针移动到下一个节点 $array->next(); // setSize(int $size) // 重新设置阵列数组的大小 $array->setSize(10); // getSize() // 获得阵列数组的大小 $array->getSize(); // offsetExists(int $index) // 判断该索引是否存在值,返回boolean $array->offsetExists(3); // offsetGet(int $index) // 获得该索引对应的值 $array->offsetGet(3); // offsetSet(int $index, mixed $value) // 设置该索引对应的值 $array->offsetSet(6, 'value3'); // offsetUnset(int $index) // 删除该索引对应的值 $array->offsetUnset(6); // toArray() // 将阵列转化成php数组 // output: Array ( [0] => [1] => 2 [2] => [3] => value2 [4] => [5] => [6] => [7] => [8] => [9] => ) $php_array = $array->toArray(); // fromArray($php_array) // 将php数组转化成阵列 // output: SplFixedArray Object ( [0] => [1] => 2 [2] => [3] => value2 [4] => [5] => [6] => [7] => [8] => [9] => ) $spl_array = SplFixedArray::fromArray($php_array);
/** * Reads the redis pending response * @return string|integer|boolean|array * @throws ClientIOError * @throws ClientRedisError */ protected function _read() { $reply = fgets($this->_socket, 512); if ($reply === false) { $this->onClientIOError('Network error - unable to read header response'); } $reply = trim($reply); if (empty($reply)) { return null; } switch ($reply[0]) { case '+': // inline reply $reply = substr($reply, 1); return strcasecmp($reply, 'OK') === 0 ? true : $reply; break; case '-': // error $this->onClientRedisError(trim(substr($reply, 4))); return false; break; case ':': // inline numeric return intval(substr($reply, 1)); break; case '$': // bulk reply $size = intval(substr($reply, 1)); if ($size === -1) { return null; } $reply = ''; if ($size > 0) { $read = 0; do { $block_size = $size - $read > 1024 ? 1024 : $size - $read; $r = fread($this->getSocket(), $block_size); if ($r === FALSE) { $this->onClientIOError('Failed to read bulk response from stream'); } else { $read += strlen($r); $reply .= $r; } } while ($read < $size); } fread($this->getSocket(), 2); /* discard crlf */ return $reply; break; case '*': // multi-bulk reply $size = intval(substr($reply, 1)); if ($size === -1) { return null; } if ($size === 0) { return array(); } $reply = new \SplFixedArray($size); for ($i = 0; $i < $size; $i++) { $reply->offsetSet($i, $this->_read()); } return $reply; break; } $this->onClientRedisError('Undefined protocol response type : ' . $reply); }
protected function resize($size, $count = null) { $data = new \SplFixedArray($size); $count = null === $count ? $this->count : $count; $start = (int) (($size - $count) / 2); $n = 0; $N = $count < $this->count ? $count : $this->count; while ($n < $N) { $data->offsetSet($n + $start, $this->data->offsetGet($this->map($n))); ++$n; } $this->data = $data; $this->start = $start; $this->count = $count; }
<?php $array = new SplFixedArray(5); $a = $array->offsetSet(); if (is_null($a)) { echo 'PASS'; }