<?php
final class RangesUtil {
final private function __construct() {}
/**
* @param array $element
*
* @return int[]
* Format: $[$value] = $count
*/
static function elementExtractUnsortedStats(array $element) {
$rawStats = array();
foreach ($element as $value => $item) {
if (isset($item['#count']) && is_int($item['#count'])) {
$rawStats[$value] = $item['#count'];
}
}
return $rawStats;
}
/**
* @param float[]|int[] $steps
* Format: $[] = $stepValue
* @param int[] $ustats
* (unsorted) stats.
* Format: $[$value] = $count
* @param bool $below
* TRUE to accumulate below, FALSE to accumulate above.
* @param bool $dedupe
*
* @return int[]
* Format: $[$value] = $countAccum
*/
static function stepsUstatsAccumulate(array $steps, array $ustats, $below, $dedupe = FALSE) {
$statsCombined = $ustats + array_fill_keys($steps, 0);
ksort($statsCombined, SORT_NUMERIC);
$statsCombinedAccum = self::statsAccumulate($statsCombined, $below);
$stepsAccum = array();
foreach ($steps as $step) {
$stepsAccum[$step] = $statsCombinedAccum[$step];
}
if ($dedupe) {
$stepsAccum = self::statsAccumDedupe($stepsAccum, $below);
}
return $stepsAccum;
}
/**
* @param int[] $statsAccum
* Format: $[$value] = $countAccum
* @param bool $below
*
* @return int[]
* Format: $[$value] = $countAccum
*/
static function statsAccumDedupe(array $statsAccum, $below) {
$countPrev = NULL;
foreach ($below ? $statsAccum : array_reverse($statsAccum, TRUE) as $value => $countAccum) {
if ($countPrev === $countAccum) {
unset($statsAccum[$value]);
}
}
return $statsAccum;
}
/**
* @param array $rawStats
*
* @return int[]
* Format: $[$value] = $count
*/
static function unsortedStatsGetStats(array $rawStats) {
ksort($rawStats, SORT_NUMERIC);
return $rawStats;
}
/**
* @param int[] $stats
* (sorted) stats.
* Format: $[$value] = $count
* @param bool $below
* TRUE to accumulate below, FALSE to accumulate above.
*
* @return int[]
* Format: $[$value] = $countAccum
*/
static function statsAccumulate(array $stats, $below) {
$total = 0;
foreach ($below ? $stats : array_reverse($stats, TRUE) as $value => $count) {
$total += $count;
$stats[$value] = $total;
}
return $stats;
}
}
$ustats = array(
0 => 2,
3 => 10,
7 => 100,
8 => 1000,
19 => 10000,
);
$steps = array(0, 2, 5, 10, 20);
var_export(RangesUtil::stepsUstatsAccumulate($steps, $ustats));
preferences:
49.08 ms | 402 KiB | 5 Q