Skip to content

Commit 0d87550

Browse files
committed
Fix capacity tests, implement ArrayAccess
1 parent d0ebb78 commit 0d87550

File tree

14 files changed

+134
-52
lines changed

14 files changed

+134
-52
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
"ext-json": "*"
1414
},
1515
"require-dev": {
16-
"php-ds/tests": "^1.3"
16+
"php-ds/tests": "dev-master"
1717
},
1818
"provide": {
19-
"ext-ds": "1.0.0"
19+
"ext-ds": "1.3.0"
2020
},
2121
"suggest": {
2222
"ext-ds": "to improve performance and reduce memory usage"

src/Deque.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@
1111
*
1212
* @package Ds
1313
*/
14-
final class Deque implements \IteratorAggregate, \ArrayAccess, Sequence
14+
final class Deque implements Sequence
1515
{
1616
use Traits\GenericCollection;
1717
use Traits\GenericSequence;
1818
use Traits\SquaredCapacity;
1919

2020
const MIN_CAPACITY = 8;
21+
22+
protected function shouldIncreaseCapacity(): bool
23+
{
24+
return count($this) >= $this->capacity;
25+
}
2126
}

src/Map.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* @package Ds
1414
*/
15-
final class Map implements \IteratorAggregate, \ArrayAccess, Collection
15+
final class Map implements Collection, \ArrayAccess
1616
{
1717
use Traits\GenericCollection;
1818
use Traits\SquaredCapacity;
@@ -694,4 +694,17 @@ public function offsetExists($offset)
694694
{
695695
return $this->get($offset, null) !== null;
696696
}
697+
698+
/**
699+
* Returns a representation that can be natively converted to JSON, which is
700+
* called when invoking json_encode.
701+
*
702+
* @return mixed
703+
*
704+
* @see \JsonSerializable
705+
*/
706+
public function jsonSerialize()
707+
{
708+
return (object) $this->toArray();
709+
}
697710
}

src/Pair.php

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66
/**
77
* A pair which represents a key and an associated value.
88
*
9+
* @property mixed $key
10+
* @property mixed $value
11+
*
912
* @package Ds
1013
*/
1114
final class Pair implements \JsonSerializable
1215
{
1316
/**
14-
* @param mixed $key The pair's key
17+
* @var mixed The pair's key
1518
*/
1619
public $key;
1720

1821
/**
19-
* @param mixed $value The pair's value
22+
* @var mixed The pair's value
2023
*/
2124
public $value;
2225

@@ -32,6 +35,20 @@ public function __construct($key = null, $value = null)
3235
$this->value = $value;
3336
}
3437

38+
/**
39+
*
40+
* @param mixed $name
41+
*
42+
* @return mixed|null
43+
*/
44+
public function __isset($name)
45+
{
46+
if ($name === 'key' || $name === 'value') {
47+
return $this->$name !== null;
48+
}
49+
return false;
50+
}
51+
3552
/**
3653
* This allows unset($pair->key) to not completely remove the property,
3754
* but be set to null instead.
@@ -46,7 +63,34 @@ public function __unset($name)
4663
$this->$name = null;
4764
return;
4865
}
66+
throw new OutOfBoundsException();
67+
}
4968

69+
/**
70+
* @param mixed $name
71+
*
72+
* @return mixed|null
73+
*/
74+
public function &__get($name)
75+
{
76+
if ($name === 'key' || $name === 'value') {
77+
return $this->$name;
78+
}
79+
throw new OutOfBoundsException();
80+
}
81+
82+
/**
83+
* @param mixed $name
84+
* @param mixed $value
85+
*
86+
* @return mixed|null
87+
*/
88+
public function __set($name, $value)
89+
{
90+
if ($name === 'key' || $name === 'value') {
91+
$this->$name = $value;
92+
return;
93+
}
5094
throw new OutOfBoundsException();
5195
}
5296

@@ -69,11 +113,14 @@ public function __debugInfo()
69113
}
70114

71115
/**
72-
* @inheritDoc
116+
* @return array
73117
*/
74118
public function toArray(): array
75119
{
76-
return ['key' => $this->key, 'value' => $this->value];
120+
return [
121+
'key' => $this->key,
122+
'value' => $this->value,
123+
];
77124
}
78125

79126
/**

src/PriorityQueue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @package Ds
1212
*/
13-
final class PriorityQueue implements \IteratorAggregate, Collection
13+
final class PriorityQueue implements Collection
1414
{
1515
use Traits\GenericCollection;
1616
use Traits\SquaredCapacity;

src/Queue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @package Ds
1212
*/
13-
final class Queue implements \IteratorAggregate, \ArrayAccess, Collection
13+
final class Queue implements Collection, \ArrayAccess
1414
{
1515
use Traits\GenericCollection;
1616

src/Sequence.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* @package Ds
1313
*/
14-
interface Sequence extends Collection
14+
interface Sequence extends Collection, \ArrayAccess
1515
{
1616
/**
1717
* Ensures that enough memory is allocated for a required capacity.

src/Set.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @package Ds
1212
*/
13-
final class Set implements \IteratorAggregate, \ArrayAccess, Collection
13+
final class Set implements Collection, \ArrayAccess
1414
{
1515
use Traits\GenericCollection;
1616

src/Stack.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @package Ds
1212
*/
13-
final class Stack implements \IteratorAggregate, \ArrayAccess, Collection
13+
final class Stack implements Collection, \ArrayAccess
1414
{
1515
use Traits\GenericCollection;
1616

src/Traits/Capacity.php

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,38 +78,55 @@ protected function checkCapacity()
7878
}
7979

8080
/**
81-
* Called when capacity should be increased to accommodate new values.
81+
* @param int $total
8282
*/
83-
protected function increaseCapacity()
83+
protected function ensureCapacity(int $total)
8484
{
85-
$this->capacity = max($this->count(), $this->capacity * $this->getGrowthFactor());
85+
if ($total > $this->capacity()) {
86+
$this->capacity = max($total, $this->nextCapacity());
87+
}
8688
}
8789

8890
/**
89-
* Called when capacity should be decrease if it drops below a threshold.
91+
* @return bool whether capacity should be increased.
9092
*/
91-
protected function decreaseCapacity()
93+
protected function shouldIncreaseCapacity(): bool
9294
{
93-
$this->capacity = max(self::MIN_CAPACITY, $this->capacity * $this->getDecayFactor());
95+
return $this->count() >= $this->capacity();
96+
}
97+
98+
protected function nextCapacity(): int
99+
{
100+
return $this->capacity() * $this->getGrowthFactor();
94101
}
95102

96103
/**
97-
* @return bool whether capacity should be increased.
104+
* Called when capacity should be increased to accommodate new values.
98105
*/
99-
protected function shouldDecreaseCapacity(): bool
106+
protected function increaseCapacity()
100107
{
101-
return count($this) <= $this->capacity * $this->getTruncateThreshold();
108+
$this->capacity = max(
109+
$this->count(),
110+
$this->nextCapacity()
111+
);
102112
}
103113

104114
/**
105-
* @return bool whether capacity should be increased.
115+
* Called when capacity should be decrease if it drops below a threshold.
106116
*/
107-
protected function shouldIncreaseCapacity(): bool
117+
protected function decreaseCapacity()
108118
{
109-
if ($this instanceof Deque) {
110-
return count($this) > $this->capacity;
111-
}
119+
$this->capacity = max(
120+
self::MIN_CAPACITY,
121+
$this->capacity() * $this->getDecayFactor()
122+
);
123+
}
112124

113-
return count($this) >= $this->capacity;
125+
/**
126+
* @return bool whether capacity should be increased.
127+
*/
128+
protected function shouldDecreaseCapacity(): bool
129+
{
130+
return count($this) <= $this->capacity() * $this->getTruncateThreshold();
114131
}
115132
}

0 commit comments

Comments
 (0)