Skip to content

Commit d1ef51f

Browse files
authored
Merge pull request #75 from marc-mabe/optimized_enumset
Optimized EnumSet by: - one less iteration on getValues() and getNames() - use EnumSet properties directly instead of using getter methods
2 parents 9001835 + fb013ac commit d1ef51f

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/EnumSet.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ public function count()
211211
*/
212212
public function isEqual(EnumSet $other)
213213
{
214-
return $this->getEnumeration() === $other->getEnumeration()
215-
&& $this->getBinaryBitsetLe() === $other->getBinaryBitsetLe();
214+
return $this->enumeration === $other->enumeration
215+
&& $this->bitset === $other->bitset;
216216
}
217217

218218
/**
@@ -222,12 +222,11 @@ public function isEqual(EnumSet $other)
222222
*/
223223
public function isSubset(EnumSet $other)
224224
{
225-
if ($this->getEnumeration() !== $other->getEnumeration()) {
225+
if ($this->enumeration !== $other->enumeration) {
226226
return false;
227227
}
228228

229-
$thisBitset = $this->getBinaryBitsetLe();
230-
return ($thisBitset & $other->getBinaryBitsetLe()) === $thisBitset;
229+
return ($this->bitset & $other->bitset) === $this->bitset;
231230
}
232231

233232
/**
@@ -237,12 +236,11 @@ public function isSubset(EnumSet $other)
237236
*/
238237
public function isSuperset(EnumSet $other)
239238
{
240-
if ($this->getEnumeration() !== $other->getEnumeration()) {
239+
if ($this->enumeration !== $other->enumeration) {
241240
return false;
242241
}
243242

244-
$thisBitset = $this->getBinaryBitsetLe();
245-
return ($thisBitset | $other->getBinaryBitsetLe()) === $thisBitset;
243+
return ($this->bitset | $other->bitset) === $this->bitset;
246244
}
247245

248246
/**
@@ -375,9 +373,10 @@ public function getOrdinals()
375373
*/
376374
public function getValues()
377375
{
378-
$values = array();
379-
foreach ($this->getEnumerators() as $enumerator) {
380-
$values[] = $enumerator->getValue();
376+
$enumeration = $this->enumeration;
377+
$values = array();
378+
foreach ($this->getOrdinals() as $ord) {
379+
$values[] = $enumeration::getByOrdinal($ord)->getValue();
381380
}
382381
return $values;
383382
}
@@ -388,9 +387,10 @@ public function getValues()
388387
*/
389388
public function getNames()
390389
{
391-
$names = array();
392-
foreach ($this->getEnumerators() as $enumerator) {
393-
$names[] = $enumerator->getName();
390+
$enumeration = $this->enumeration;
391+
$names = array();
392+
foreach ($this->getOrdinals() as $ord) {
393+
$names[] = $enumeration::getByOrdinal($ord)->getName();
394394
}
395395
return $names;
396396
}

0 commit comments

Comments
 (0)