Skip to content

Conversation

@marc-mabe
Copy link
Owner

@marc-mabe marc-mabe commented Sep 20, 2016

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

  • EnumSet::getEnumerators() : Enum[]
  • EnumSet::getValues() : scalar[]
  • EnumSet::getNames() : string[]

@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 marc-mabe added this to the 2.3.0 milestone Sep 20, 2016
Copy link
Collaborator

@prolic prolic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good

@marc-mabe
Copy link
Owner Author

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:

  • PHP 7.0.8-0ubuntu0.16.04.2 (cli) ( NTS )
  • Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz

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

 - added getOrdinals()
 - optimized getEnumerators(), getNames() and getValues()
 - added tests for all get*() methods
@marc-mabe marc-mabe changed the title [WIP] EnumSet: added getEnumerators(), getNames() and getValues() EnumSet: added getEnumerators(), getNames() and getValues() Sep 30, 2016
Copy link
Collaborator

@prolic prolic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even better! :)

@marc-mabe marc-mabe merged commit 3090b04 into master Sep 30, 2016
@marc-mabe
Copy link
Owner Author

@prolic thanks for review :)

@marc-mabe marc-mabe deleted the enumset2array branch October 3, 2016 19:07
@marc-mabe marc-mabe mentioned this pull request Oct 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants