Esempio n. 1
1
/**
 * Get all permutation of an array
 *
 * http://docstore.mik.ua/orelly/webprog/pcook/ch04_26.htm
 * @param array $items
 * @param array $perms
 */
function pc_permute($items, $perms = array())
{
    global $allRoutes;
    if (empty($items)) {
        $allRoutes[] = $perms;
    } else {
        for ($i = count($items) - 1; $i >= 0; --$i) {
            $newitems = $items;
            $newperms = $perms;
            list($foo) = array_splice($newitems, $i, 1);
            array_unshift($newperms, $foo);
            pc_permute($newitems, $newperms);
        }
    }
}
Esempio n. 2
1
function pc_permute($items, $perms = array())
{
    if (empty($items)) {
        (yield $perms);
    } else {
        for ($i = count($items) - 1; $i >= 0; --$i) {
            $newitems = $items;
            $newperms = $perms;
            list($foo) = array_splice($newitems, $i, 1);
            array_unshift($newperms, $foo);
            foreach (pc_permute($newitems, $newperms) as $subPerms) {
                (yield $subPerms);
            }
        }
    }
}
Esempio n. 3
0
function pc_permute($items, $perms = array())
{
    if (empty($items)) {
        print join(' ', $perms) . "<br>";
    } else {
        for ($i = count($items) - 1; $i >= 0; --$i) {
            $newitems = $items;
            $newperms = $perms;
            list($foo) = array_splice($newitems, $i, 1);
            array_unshift($newperms, $foo);
            pc_permute($newitems, $newperms);
        }
    }
}
Esempio n. 4
0
function pc_permute($items, $perms = array(), &$results)
{
    if (empty($items)) {
        $results[] = $perms;
    } else {
        for ($i = count($items) - 1; $i >= 0; --$i) {
            $newitems = $items;
            $newperms = $perms;
            list($foo) = array_splice($newitems, $i, 1);
            array_unshift($newperms, $foo);
            pc_permute($newitems, $newperms, $results);
        }
    }
}
Esempio n. 5
0
function pc_permute($items, $perms = array())
{
    if (empty($items)) {
        $return = array($perms);
    } else {
        $return = array();
        for ($i = count($items) - 1; $i >= 0; --$i) {
            $newitems = $items;
            $newperms = $perms;
            list($foo) = array_splice($newitems, $i, 1);
            array_unshift($newperms, $foo);
            $return = array_merge($return, pc_permute($newitems, $newperms));
        }
    }
    return $return;
}
Esempio n. 6
0
 /**
  * Macro to define a simple singular sql "select" statement
  * @param string $params
  * @return string
  */
 private function defineinsert($params)
 {
     parse_str(trim($params));
     if (!isset($name)) {
         $ret = [];
         foreach (pc_permute(explode(",", $from)) as $perm) {
             $from = implode(",", $perm);
             $name = "{$from}" . "->new in {$table}";
             $vals = explode(",", $from);
             $vals = array_map(function ($v) {
                 return "?";
             }, $vals);
             $vals = implode(", ", $vals);
             $ret[] = $this->define("{$name} | INSERT INTO {$table} ({$from}) VALUES ({$vals})");
         }
         return implode(";\n", $ret);
     } else {
         $vals = explode(",", $from);
         $vals = array_map(function ($v) {
             return "?";
         }, $vals);
         $vals = implode(", ", $vals);
         return $this->define("{$name} | INSERT INTO {$table} ({$from}) VALUES ({$vals})");
     }
 }
Esempio n. 7
0
function pc_permute_optional($items, $optional = NULL)
{
    if (!$optional) {
        return pc_permute($items);
    }
    $optional = make_vec($optional);
    $opt2 = array_slice($optional, 1);
    return array_merge(pc_permute_optional(array_merge($items, [$optional[0]]), $opt2), pc_permute_optional($items, $opt2));
}
Esempio n. 8
0
function pc_permute($items, $perms = array())
{
    if (empty($items)) {
        $_SESSION['valores_search'][$_SESSION['c-valores_search']] = join('%', $perms);
        $_SESSION['c-valores_search']++;
    } else {
        for ($i = count($items) - 1; $i >= 0; --$i) {
            $newitems = $items;
            $newperms = $perms;
            list($foo) = array_splice($newitems, $i, 1);
            array_unshift($newperms, $foo);
            pc_permute($newitems, $newperms);
        }
    }
}