You are here

protected function crumbs_Admin_ElementObject_WeightsTextual::findIdealLength in Crumbs, the Breadcrumbs suite 7.2

This algorithm is copied 1:1 from blockadminlight

Parameters

array $key_lengths:

int $factor:

Return value

int

1 call to crumbs_Admin_ElementObject_WeightsTextual::findIdealLength()
crumbs_Admin_ElementObject_WeightsTextual::getDefaultText in lib/Admin/ElementObject/WeightsTextual.php
Get the text for the textarea

File

lib/Admin/ElementObject/WeightsTextual.php, line 168

Class

crumbs_Admin_ElementObject_WeightsTextual

Code

protected function findIdealLength(array $key_lengths, $factor = 30) {
  sort($key_lengths, SORT_NUMERIC);
  $n = count($key_lengths);
  $length = 0;
  $best_length = 0;
  $cost = $n * $factor;
  $best_cost = $cost;
  for ($i = 0; $i < $n; ++$i) {
    $increment = $key_lengths[$i] - $length;
    $length = $key_lengths[$i];
    $cost += $i * $increment;
    $cost -= $factor;
    if ($cost < $best_cost) {
      $best_cost = $cost;
      $best_length = $length;
    }
  }
  return $best_length;
}