GithubHelp home page GithubHelp logo

bpolaszek / cartesian-product Goto Github PK

View Code? Open in Web Editor NEW
82.0 5.0 12.0 25 KB

PHP - A simple, low-memory footprint function to generate all combinations from a multi-dimensionnal array.

License: MIT License

PHP 100.00%
php cartesian combinations array product function

cartesian-product's People

Contributors

bpolaszek avatar hannesvdvreken avatar ostrolucky avatar progdog-ru avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cartesian-product's Issues

Cannot rewind combinations

There's a bug preventing to unroll the same cartesian product object twice:

use function BenTools\CartesianProduct\cartesian_product;

$data = [
    'hair' => [
        'blond',
        'black'
    ],
    'eyes' => [
        'blue',
        'green',
        function (array $combination) { // You can use closures to dynamically generate possibilities
            if ('black' === $combination['hair']) { // Then you have access to the current combination being built
                return 'brown';
            }
            return 'grey';
        }
    ]
];

$combinations = cartesian_product($data);
foreach ($combinations as $combination) {
    printf('Hair: %s - Eyes: %s' . PHP_EOL, $combination['hair'], $combination['eyes']);
}

foreach ($combinations as $combination) {
    printf('Hair: %s - Eyes: %s' . PHP_EOL, $combination['hair'], $combination['eyes']);
}

Produces:

Hair: blond - Eyes: blue
Hair: blond - Eyes: green
Hair: blond - Eyes: grey
Hair: black - Eyes: blue
Hair: black - Eyes: green
Hair: black - Eyes: brown
PHP Notice:  Undefined index: eyes
Notice: Undefined index: eyes
Hair: blond - Eyes: 
PHP Notice:  Undefined index: eyes
Notice: Undefined index: eyes
Hair: black - Eyes: 

Add benchmark to README

I've created a benchmark in order to compare my own cartesian iterator implementation with others, including yours: https://github.com/PatchRanger/php-cartesian-benchmark . It looks like your library is the best in all cases except the last synthetic one. I believe you may want to add the link to the benchmark to README in order to convince to use your library. Up to you.

performance test

    round(memory_get_usage() / 1024 / 1024),
    round(memory_get_peak_usage() / 1024 / 1024)

memory_get_usage() capture before the cartesian function is called should be subtracted from these results to not pollute it with autoloading and what not which is done before that.

Also these functions are the best for getting a memory profile. Use xdebug profiler for good memory usage information. But don't do benchmarking and profiling at the same time because profiling slows down code.

0 MB / 1 MB could be more accurate, maybe use bigger test input

Iterators as subsets should be supported

Hey, I've run into your useful library and as far as I see it's the only one that does cartesian products while preserving string keys. However, it does not support Iterator as subsets, because of this exception being thrown

throw new \InvalidArgumentException(sprintf('Key "%s" should return a non-empty array', $key));

My use case is to do cartesian product of two mongodb cursors.

Certain amount from each array

Hello,
Can you library do this?
$data = [
'hair' => [
'blond',
'red',
],
'eyes' => [
'blue',
'green',
'brown',
],
'gender' => [
'male',
'female',
]
];

Generate all combinations based on pick any 1 item from hair, pick any 2 items from eyes and any 1 item from gender. And where it can be any number of item arrays and any number of items in the item array, and I can pass as second option how many items to choose from from each item array.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.