-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.php
71 lines (61 loc) · 2.02 KB
/
Vector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace Altair\Structure;
use Altair\Structure\Contracts\CapacityInterface;
use Altair\Structure\Contracts\VectorInterface;
use ArrayAccess;
use IteratorAggregate;
/**
* Vector.
*
* A Vector is a Sequence of values in a contiguous buffer that grows and shrinks automatically. It’s the most efficient
* sequential structure because a value’s index is a direct mapping to its index in the buffer, and the growth factor
* isn't bound to a specific multiple or exponent.
*
* @link https://medium.com/@rtheunissen/efficient-data-structures-for-php-7-9dda7af674cd#.gl62k1xqr
*
*/
class Vector implements IteratorAggregate, ArrayAccess, VectorInterface, CapacityInterface
{
use Traits\SequenceTrait;
use Traits\CapacityTrait;
/**
* Creates an instance using the values of an array or Traversable object.
*
* @param array|\Traversable|Contracts\CollectionInterface|null $values
*/
public function __construct($values = null)
{
$this->capacity = VectorInterface::MIN_VECTOR_CAPACITY;
if (func_num_args()) {
$this->pushAll($this->normalizeItems(($values??[])));
}
}
/**
* Adjusts the structure's capacity according to its current size.
*/
protected function adjustCapacity()
{
$size = count($this);
// Automatically truncate the allocated buffer when the size of the
// structure drops low enough.
if ($size < $this->capacity / 4) {
$this->capacity = max(VectorInterface::MIN_VECTOR_CAPACITY, $this->capacity / 2);
} else {
// Also check if we should increase capacity when the size changes.
if ($size >= $this->capacity) {
$this->increaseCapacity();
}
}
}
/**
* Increase capacity.
*/
protected function increaseCapacity()
{
$size = count($this);
if ($size > $this->capacity) {
$this->capacity = max(intval($this->capacity * 1.5), $size);
}
return $this;
}
}