d3-array : Added group, rollup, and quickselect methods

This commit is contained in:
Hugues Stefanski
2019-01-27 12:51:22 +01:00
parent 7fac46a116
commit 7cbe3ae33a
2 changed files with 81 additions and 2 deletions
+25 -1
View File
@@ -485,6 +485,12 @@ num = mixedObjectDateBisectorObject.right(readonlyMixedObjectArray, new Date(201
num = mixedObjectDateBisectorObject.right(readonlyMixedObjectArray, new Date(2015, 3, 14), 1);
num = mixedObjectDateBisectorObject.right(readonlyMixedObjectArray, new Date(2015, 3, 14), 3, 4);
// quickselect
numbersArray = d3Array.quickselect(numbersArray, 3);
numbersArray = d3Array.quickselect(numbersArray, 3, 0);
numbersArray = d3Array.quickselect(numbersArray, 3, 0, 5);
numbersArray = d3Array.quickselect(numbersArray, 3, 0, 5, d3Array.descending);
// ascending() -----------------------------------------------------------------
num = d3Array.ascending(undefined, 20);
@@ -536,6 +542,24 @@ mergedArray = d3Array.merge([testArray1, [15, 30]]); // fails, type mismatch
mergedArray = d3Array.merge(readonlyTestArrays); // inferred type
mergedArray = d3Array.merge<MixedObject>(readonlyTestArrays); // explicit type
interface ObjDefinition {
name: string;
amount: string;
date: string;
}
const objArray: Array<ObjDefinition> = [
{ name: "jim", amount: "34.0", date: "11/12/2015" },
{ name: "carl", amount: "120.11", date: "11/12/2015" },
{ name: "stacy", amount: "12.01", date: "01/04/2016" },
{ name: "stacy", amount: "34.05", date: "01/04/2016" }
];
let myMap: Map<string, ObjDefinition>;
myMap = d3Array.group(objArray, d => d.name);
myMap = d3Array.rollup(objArray, d => d.length, d => d.name);
// cross() ---------------------------------------------------------------------
let crossed: Array<[string, number]>;
@@ -779,7 +803,7 @@ histoMixedObject_Date = histoMixedObject_Date.domain(timeScale.domain()); // fai
domainFnDate = histoMixedObject_Date.domain();
histoMixedObject_DateOrUndefined = histoMixedObject_DateOrUndefined.domain([new Date(2014, 3, 15), new Date(2017, 4, 15)]);
histoMixedObject_DateOrUndefined = histoMixedObject_DateOrUndefined.domain([domain[0], domain[domain.length]]);
histoMixedObject_DateOrUndefined = histoMixedObject_DateOrUndefined.domain((values) => [values[0]!, values[values.length]!]);
histoMixedObject_DateOrUndefined = histoMixedObject_DateOrUndefined.domain((values) => [values[0]!, values[values.length]!]);
// thresholds(...) -------------------------------------------------------------
+56 -1
View File
@@ -3,7 +3,8 @@
// Definitions by: Alex Ford <https://github.com/gustavderdrache>
// Boris Yankov <https://github.com/borisyankov>
// Tom Wanzek <https://github.com/tomwanzek>
// denisname <https://github.com/denisname>
// denisname <https://github.com/denisname>,
// Hugues Stefanski <https://github.com/ledragon>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
@@ -173,6 +174,44 @@ export interface Bisector<T, U> {
export function bisector<T, U>(comparator: (a: T, b: U) => number): Bisector<T, U>;
export function bisector<T, U>(accessor: (x: T) => U): Bisector<T, U>;
/**
* Rearranges items so that all items in the [left, k] are the smallest. The k-th element will have the (k - left + 1)-th smallest value in [left, right].
*
* @param array The array to partially sort (in place).
* @param k The middle index for partial sorting.
*/
export function quickselect<T>(array: Array<T>, k: number): Array<T>;
/**
* Rearranges items so that all items in the [left, k] are the smallest. The k-th element will have the (k - left + 1)-th smallest value in [left, right].
*
* @param array The array to partially sort (in place).
* @param k The middle index for partial sorting.
* @param left The left index of the range to sort.
*/
export function quickselect<T>(array: Array<T>, k: number, left: number): Array<T>;
/**
* Rearranges items so that all items in the [left, k] are the smallest. The k-th element will have the (k - left + 1)-th smallest value in [left, right].
*
* @param array The array to partially sort (in place).
* @param k The middle index for partial sorting.
* @param left The left index of the range to sort.
* @param right The right index.
*/
export function quickselect<T>(array: Array<T>, k: number, left: number, right: number): Array<T>;
/**
* Rearranges items so that all items in the [left, k] are the smallest. The k-th element will have the (k - left + 1)-th smallest value in [left, right].
*
* @param array The array to partially sort (in place).
* @param k The middle index for partial sorting.
* @param left The left index of the range to sort.
* @param right The right index.
* @param compare The compare function.
*/
export function quickselect<T>(array: Array<T>, k: number, left: number, right: number, compare: (a: Primitive | undefined, b: Primitive | undefined) => number): Array<T>;
// NB. this is limited to primitive values due to D3's use of the <, >, and >= operators. Results get weird for object instances.
/**
* Compares two primitive values for sorting (in ascending order).
@@ -189,6 +228,22 @@ export function descending(a: Primitive | undefined, b: Primitive | undefined):
// Transforming Arrays
// --------------------------------------------------------------------------------------
/**
* Groups the specified array of values into a Map from key to array of value.
* @param a The array to group.
* @param key The key function.
*/
export function group<TObject, TKey>(a: ArrayLike<TObject>, key: (value: TObject) => TKey): Map<TKey, TObject>;
/**
* Groups and reduces the specified array of values into a Map from key to value.
* @param a The array to group.
* @param reduce The reduce function.
* @param key The key function.
*/
export function rollup<TObject, TKey>(a: ArrayLike<TObject>, reduce: (value: Array<TObject>) => number, key: (value: TObject) => TKey): Map<TKey, TObject>;
/**
* Returns the Cartesian product of the two arrays a and b.
* For each element i in the specified array a and each element j in the specified array b, in order,