本文整理汇总了PHP中SplFixedArray类的典型用法代码示例。如果您正苦于以下问题:PHP SplFixedArray类的具体用法?PHP SplFixedArray怎么用?PHP SplFixedArray使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SplFixedArray类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: bytestring
public function bytestring($data)
{
$a = new \SplFixedArray(strlen($data));
for ($i = 0; $i < strlen($data); $i++) {
$a[$i] = str_pad(ord($data[$i]), 3, ' ', STR_PAD_LEFT);
}
return implode(' ', $a->toArray());
}
示例2: values
/**
* 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);
}
示例3: sort
/**
* {@inheritdoc}
*/
public function sort(\SplFixedArray $array)
{
$length = $array->count();
for ($i = 1; $i < $length; $i++) {
$key = $array[$i];
$j = $i - 1;
while ($j >= 0 && $array[$j] > $key) {
$array[$j + 1] = $array[$j];
$j--;
}
$array[$j + 1] = $key;
}
return $array;
}
示例4: testFromArrayDisordered
public function testFromArrayDisordered()
{
$array = array(1 => 'foo', 3 => 'bar', 2 => 'baz', 0 => 'qux');
$the_DS = DynamicArray::fromArray($array);
$SPL_DS = \SplFixedArray::fromArray($array);
$this->assertMethodsEqual($SPL_DS, $the_DS);
}
示例5: __construct
/**
* Create a new instance.
*
* @param array $matchRegexps The regexes to match files.
*
* @param AbstractFilter[] $filters The filters to apply.
*/
public function __construct(array $matchRegexps, $filters)
{
foreach ($matchRegexps as $pattern) {
$this->matchRegexps[] = $this->toRegex($pattern);
}
$this->filters = \SplFixedArray::fromArray($filters);
}
示例6: sort
/**
* {@inheritdoc}
*/
public function sort(\SplFixedArray $array)
{
$length = $array->count();
for ($i = 0; $i < $length - 1; $i++) {
$smallest = $i;
for ($j = $i + 1; $j < $length; $j++) {
if ($array[$j] < $array[$smallest]) {
$smallest = $j;
}
}
$tmp = $array[$i];
$array[$i] = $array[$smallest];
$array[$smallest] = $tmp;
}
return $array;
}
示例7: fromArray
/**
* @param array $array
* @param boolean $save_indexes = true
* @return SplFixedArray, DataStructures\SerializableFixedArray
*/
public static function fromArray(array $array, $save_indexes = true)
{
if (self::$_useSpl === null) {
self::_checkEnvironment();
}
return self::$_useSpl ? \SplFixedArray::fromArray($array, $save_indexes) : new self(count($array), $save_indexes ? $array : array_values($array));
}
示例8: testMapTraversable
public function testMapTraversable()
{
$expected = [1, 2, 3, 4];
$arr = \SplFixedArray::fromArray($expected);
$actual = t::into([], t::map(t::value()), $arr);
$this->assertEquals($expected, $actual);
}
示例9: copyOf
/**
* Create a new ImmutableVector from the given traversable.
* @param array|Traversable $traversable
* @param bool $preserveKeys
* @return ImmutableVector
*/
public static function copyOf($traversable, $preserveKeys = true)
{
if (is_array($traversable)) {
return new self(\SplFixedArray::fromArray($traversable, $preserveKeys));
} else {
return new self(\SplFixedArray::fromArray(iterator_to_array($traversable), $preserveKeys));
}
}
示例10: slice
/**
* @param int|string $start
* @param int|string $length
* @return $this
*/
public function slice($start, $length)
{
$end = $this->set->getSize();
if ($start > $end || $length > $end) {
throw new \RuntimeException('Invalid start or length');
}
$this->set = \SplFixedArray::fromArray(array_slice($this->set->toArray(), $start, $length));
return $this;
}
示例11: valuesDataProvider
public function valuesDataProvider()
{
$splFixedArrayIn = \SplFixedArray::fromArray([2, 154, 2342, 1001, 7651, 4523, 1343, 756, 6324, 1]);
$expected = [1, 2, 154, 756, 1001, 1343, 2342, 4523, 6324, 7651];
$splFixedArrayIn2 = clone $splFixedArrayIn;
$splFixedArrayIn2[9] = 8000;
$expected2 = [2, 154, 756, 1001, 1343, 2342, 4523, 6324, 7651, 8000];
return [[$splFixedArrayIn, $expected], [$splFixedArrayIn2, $expected2]];
}
示例12: fromArray
/**
* Imports a PHP array in a FixedArray instance.
*
* This method needs to be reimplemented as SplFixedArray does not return `new static`.
* @see https://bugs.php.net/bug.php?id=55128
*
* Subclasses of FixedArray do not need to reimplement this method.
*
* @param array $array
* @param boolean $saveIndexes
*
* @return FixedArray
*
* @throws \InvalidArgumentException If the array contains non-numeric or negative indexes.
*/
public static function fromArray($array, $saveIndexes = true)
{
$splFixedArray = \SplFixedArray::fromArray($array, $saveIndexes);
$result = new static($splFixedArray->count());
$source = $saveIndexes ? $array : $splFixedArray;
foreach ($source as $key => $value) {
$result[$key] = $value;
}
return $result;
}
示例13: __construct
/**
*
* @param PointInterface[] $points
* @param LineFactoryInterface $lineFactory
*/
public function __construct(array $points, LineFactoryInterface $lineFactory)
{
$items = [];
for ($i = 0; $i < count($points) - 1;) {
$items[] = $lineFactory->createLineSegment($points[$i], $points[++$i]);
}
// and add last segment
$items[] = $lineFactory->createLineSegment($points[$i], $points[0]);
$this->items = \SplFixedArray::fromArray($items);
}
示例14: getItem
/**
* {@inheritdoc}
*/
public function getItem($index)
{
if (!$this->items->offsetExists($index)) {
throw new \OutOfBoundsException('The index ' . $index . ' is not set');
}
return $this->items[$index];
}
示例15: keep
/**
* @param int $numberOfCardsToKeep
*
* @return HandInterface
*/
public function keep(int $numberOfCardsToKeep) : HandInterface
{
if ($this->cards->count() <= $numberOfCardsToKeep) {
return $this;
}
return new self(...array_slice($this->getCards(), 0, $numberOfCardsToKeep));
}