marc-mabe / php-enum

Simple and fast implementation of enumerations with native PHP
BSD 3-Clause "New" or "Revised" License
464 stars 36 forks source link

EnumSet: added getEnumerators(), getNames() and getValues() #65

Closed marc-mabe closed 8 years ago

marc-mabe commented 8 years ago

see #62 Add methods to convert a set into an array

@prolic I decided to use this names to be consistent with the already existing method Enum::getEnumerators() and I also return it just as lists so the ordinal number will not be the array key. I analyzed all my use-cases and couldn't find any where the ordinal number as array key is required. On the other hand I have some use-cases where I actually require a simple list. Also it's still simple possible to just use iterator_to_array($set) to convert the set into an associative array with ordinal number to enumerator instance.

PS: The same set of methods should also work on EnumMap and on Enum (as static versions)

marc-mabe commented 8 years ago

I did some changes to optimize the performance of the methods and to not reset the internal iterator position ;)

Also added the method getOrdinals() : int[] as this offers the basic information stored in a set as array and all other get*s() methods are based on that.

Benchmark

Script: https://gist.github.com/marc-mabe/a26227adf5ea1e9c96c2072bf4351ec1 Run on:

Before:

$ php bench.php 
empty set getEnumerators(): 0.0085999965667725ms
empty set getValues()     : 0.0090000629425049ms
empty set getNames()      : 0.0085229873657227ms
full set getEnumerators() : 1.471755027771ms
full set getValues()      : 1.533252954483ms
full set getNames()       : 1.9099199771881ms

After:

$ php bench.php 
empty set getEnumerators(): 0.0077300071716309ms
empty set getValues()     : 0.0088911056518555ms
empty set getNames()      : 0.0087888240814209ms
full set getEnumerators() : 0.68992781639099ms
full set getValues()      : 0.74091100692749ms
full set getNames()       : 1.0174210071564ms
marc-mabe commented 8 years ago

@prolic thanks for review :)