Skip to content

Commit

Permalink
Extract method in ReplaceConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
Riimu committed Jan 26, 2015
1 parent 97031ae commit b8627b2
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/ReplaceConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,33 @@ private function getRoot(NumberBase $source, NumberBase $target)
*/
private function buildConversionTable()
{
$reduce = $this->source->getRadix() > $this->target->getRadix();
list($max, $min) = $reduce ? [$this->source, $this->target] : [$this->target, $this->source];

$minDigits = $min->getDigitList();
$maxDigits = $max->getDigitList();
$last = $min->getRadix() - 1;
$size = (int) log($max->getRadix(), $min->getRadix());
$number = array_fill(0, $size, $minDigits[0]);
if ($this->source->getRadix() > $this->target->getRadix()) {
return $this->createTable($this->source->getDigitList(), $this->target->getDigitList());
}

return array_flip($this->createTable($this->target->getDigitList(), $this->source->getDigitList()));
}

private function createTable($source, $target)
{
$last = count($target) - 1;
$size = (int) log(count($source), count($target));
$number = array_fill(0, $size, $target[0]);
$next = array_fill(0, $size, 0);
$limit = $max->getRadix();
$table = [$maxDigits[0] => implode('', $number)];
$limit = count($source);
$table = [$source[0] => implode('', $number)];

for ($i = 1; $i < $limit; $i++) {
for ($j = $size - 1; $next[$j] == $last; $j--) {
$number[$j] = $minDigits[0];
$number[$j] = $target[0];
$next[$j] = 0;
}

$number[$j] = $minDigits[++$next[$j]];
$table[$maxDigits[$i]] = implode('', $number);
$number[$j] = $target[++$next[$j]];
$table[$source[$i]] = implode('', $number);
}

return $reduce ? $table : array_flip($table);
return $table;
}

public function setPrecision($precision)
Expand Down

0 comments on commit b8627b2

Please sign in to comment.