You are here

function _authcache_enum_comb_k in Authenticated User Page Caching (Authcache) 7.2

Given a set of elements, return all possible subsets with a size of k.

If the input looks like this: $set = array('a', 'b', 'c', 'd') $k = 2

The result will be: $result = array( array('a', 'b'), array('a', 'c'), array('a', 'd'), array('b', 'c'), array('b', 'd'), array('c', 'd'), );

3 calls to _authcache_enum_comb_k()
AuthcacheEnumCombTestCase::testCombineK in modules/authcache_enum/lib/Drupal/authcache_enum/Tests/AuthcacheEnumCombTestCase.php
Test calculation of k-combinations.
AuthcacheEnumCombTestCase::testCombineKOffLimit in modules/authcache_enum/lib/Drupal/authcache_enum/Tests/AuthcacheEnumCombTestCase.php
Test calculation of k-combinations where k is out of range.
_authcache_enum_comb in modules/authcache_enum/authcache_enum.comb.inc
Given a set of elements, return all possible subsets.

File

modules/authcache_enum/authcache_enum.comb.inc, line 24
Private helper functions for the Authcache Enum module.

Code

function _authcache_enum_comb_k(array $set, $k) {
  if ($k >= count($set)) {
    return array(
      $set,
    );
  }
  elseif ($k <= 1) {
    return array_chunk($set, 1);
  }
  $result = array();
  while ($k <= count($set)) {
    $head = array_shift($set);
    $tails = _authcache_enum_comb_k($set, $k - 1);
    foreach ($tails as $tail) {
      array_unshift($tail, $head);
      array_push($result, $tail);
    }
  }
  return $result;
}