Skip to content

Commit

Permalink
fix(Math): Extend API for extracting min/max of an array
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Aug 22, 2019
1 parent 5eef245 commit 3472ce7
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions Sources/Common/Core/Math/index.js
Expand Up @@ -49,9 +49,9 @@ export function degreesFromRadians(rad) {

export const { round, floor, ceil, min, max } = Math;

export function arrayMin(arr) {
export function arrayMin(arr, offset = 0, stride = 1) {
let minValue = Infinity;
for (let i = 0, len = arr.length; i < len; ++i) {
for (let i = offset, len = arr.length; i < len; i += stride) {
if (arr[i] < minValue) {
minValue = arr[i];
}
Expand All @@ -60,9 +60,9 @@ export function arrayMin(arr) {
return minValue;
}

export function arrayMax(arr) {
export function arrayMax(arr, offset = 0, stride = 1) {
let maxValue = -Infinity;
for (let i = 0, len = arr.length; i < len; ++i) {
for (let i = offset, len = arr.length; i < len; i+= stride) {
if (maxValue < arr[i]) {
maxValue = arr[i];
}
Expand All @@ -71,6 +71,21 @@ export function arrayMax(arr) {
return maxValue;
}

export function arrayRange(arr, offset = 0, stride = 1) {
let minValue = Infinity;
let maxValue = -Infinity;
for (let i = offset, len = arr.length; i < len; i += stride) {
if (arr[i] < minValue) {
minValue = arr[i];
}
if (maxValue < arr[i]) {
maxValue = arr[i];
}
}

return [minValue, maxValue];
}

export const ceilLog2 = notImplemented('ceilLog2');
export const factorial = notImplemented('factorial');

Expand Down Expand Up @@ -2070,6 +2085,7 @@ export default {
max,
arrayMin,
arrayMax,
arrayRange,
isPowerOfTwo,
nearestPowerOfTwo,
factorial,
Expand Down

0 comments on commit 3472ce7

Please sign in to comment.