Skip to content

Instantly share code, notes, and snippets.

@allada
Created June 27, 2014 04:12
Show Gist options
  • Save allada/faf7cf7ebb2cb07eca8a to your computer and use it in GitHub Desktop.
Save allada/faf7cf7ebb2cb07eca8a to your computer and use it in GitHub Desktop.
<?php
function findset($s, $l){
$len = count($s);
$outAry = array();
$ptrs = array();
for($i=0;$i<$l;$i++){
$ptrs[] = $i;
}
$i=0;
$curPtr = $l-1;
while($ptrs[0] < $len - $l + 1){
foreach($ptrs as $j => $pt){
$outAry[$i][$j] = $s[$pt];
}
if($ptrs[$curPtr]+1 < $len-$curPtr){
$ptrs[$curPtr]++;
}else{
// go back to find where new pointer can start again
for($j=$curPtr-1;$j>=0;$j--){
if($ptrs[$j] < $len - 1 - $j){
$curPtr = $j;
break;
}
}
$ptrs[$curPtr]++;
$curPtr++;
for($j=$curPtr;$j<$l;$j++){
$ptrs[$j]=$ptrs[$j-1]+1;
}
}
$i++;
};
return $outAry;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment