Skip to content

Commit 4cb8f85

Browse files
committed
Drop "accept duplicates" feature from HashArray
1 parent c4a39f8 commit 4cb8f85

4 files changed

Lines changed: 5 additions & 166 deletions

File tree

src/HashArray.php

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
* made to them via their mutator methods will not cause an update of
2323
* their associated hash in this array.
2424
*
25-
* When acceptDuplicates is set to true, multiple elements with the same
26-
* hash can reside in the HashArray. Lookup by such a non-unique hash will
27-
* return only the first element and deletion will also delete only
28-
* the first such element.
29-
*
3025
* @since 0.1
3126
*
3227
* @license GPL-2.0+
@@ -39,19 +34,10 @@ abstract class HashArray extends ArrayObject implements Hashable, Comparable {
3934
*
4035
* @since 0.1
4136
*
42-
* @var array [ element hash (string) => array [ element offset (string|int) ] | element offset (string|int) ]
37+
* @var array [ element hash (string) => element offset (string|int) ]
4338
*/
4439
protected $offsetHashes = [];
4540

46-
/**
47-
* If duplicate values (based on hash) should be accepted or not.
48-
*
49-
* @since 0.3
50-
*
51-
* @var bool
52-
*/
53-
protected $acceptDuplicates = false;
54-
5541
/**
5642
* @var integer
5743
*/
@@ -125,20 +111,11 @@ protected function preSetElement( $index, $hashable ) {
125111

126112
$hasHash = $this->hasElementHash( $hash );
127113

128-
if ( !$this->acceptDuplicates && $hasHash ) {
114+
if ( $hasHash ) {
129115
return false;
130116
}
131117
else {
132-
if ( $hasHash ) {
133-
if ( !is_array( $this->offsetHashes[$hash] ) ) {
134-
$this->offsetHashes[$hash] = [ $this->offsetHashes[$hash] ];
135-
}
136-
137-
$this->offsetHashes[$hash][] = $index;
138-
}
139-
else {
140-
$this->offsetHashes[$hash] = $index;
141-
}
118+
$this->offsetHashes[$hash] = $index;
142119

143120
return true;
144121
}
@@ -191,11 +168,6 @@ public function removeElement( Hashable $element ) {
191168
public function removeByElementHash( $elementHash ) {
192169
if ( $this->hasElementHash( $elementHash ) ) {
193170
$offset = $this->offsetHashes[$elementHash];
194-
195-
if ( is_array( $offset ) ) {
196-
$offset = reset( $offset );
197-
}
198-
199171
$this->offsetUnset( $offset );
200172
}
201173
}
@@ -210,7 +182,7 @@ public function removeByElementHash( $elementHash ) {
210182
* @return bool Indicates if the element was added or not.
211183
*/
212184
public function addElement( Hashable $element ) {
213-
$append = $this->acceptDuplicates || !$this->hasElementHash( $element->getHash() );
185+
$append = !$this->hasElementHash( $element->getHash() );
214186

215187
if ( $append ) {
216188
$this->append( $element );
@@ -231,11 +203,6 @@ public function addElement( Hashable $element ) {
231203
public function getByElementHash( $elementHash ) {
232204
if ( $this->hasElementHash( $elementHash ) ) {
233205
$offset = $this->offsetHashes[$elementHash];
234-
235-
if ( is_array( $offset ) ) {
236-
$offset = reset( $offset );
237-
}
238-
239206
return $this->offsetGet( $offset );
240207
}
241208
else {
@@ -259,19 +226,7 @@ public function offsetUnset( $index ) {
259226

260227
$hash = $element->getHash();
261228

262-
if ( array_key_exists( $hash, $this->offsetHashes )
263-
&& is_array( $this->offsetHashes[$hash] )
264-
&& count( $this->offsetHashes[$hash] ) > 1 ) {
265-
$this->offsetHashes[$hash] = array_filter(
266-
$this->offsetHashes[$hash],
267-
function( $value ) use ( $index ) {
268-
return $value !== $index;
269-
}
270-
);
271-
}
272-
else {
273-
unset( $this->offsetHashes[$hash] );
274-
}
229+
unset( $this->offsetHashes[$hash] );
275230

276231
parent::offsetUnset( $index );
277232
}

tests/fixtures/HashArrayWithDuplicates.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/fixtures/HashArrayWithoutDuplicates.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,4 @@ public function getObjectType() {
1414
return '\Hashable';
1515
}
1616

17-
public function __construct( $input = null ) {
18-
$this->acceptDuplicates = false;
19-
parent::__construct( $input );
20-
}
21-
2217
}

tests/unit/HashArray/HashArrayWithDuplicatesTest.php

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)