Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

topK.ts

92 lines3.5 KB

1import { MultiSet } from "../multiset.js"
2import { reduce } from "./reduce.js"
3import type { IStreamBuilder, PipedOperator } from "../types"
4import type { KeyValue } from "../types.js"
5​
6interface TopKOptions {
7 limit?: number
8 offset?: number
9}
10​
11/**
12 * Limits the number of results based on a comparator, with optional offset.
13 * This works on a keyed stream, where the key is the first element of the tuple
14 * The ordering is withing a key group, i.e. elements are sorted within a key group
15 * and the limit + offset is applied to that sorted group.
16 * To order the entire stream, key by the same value for all elements such as null.
17 *
18 * @param comparator - A function that compares two elements
19 * @param options - An optional object containing limit and offset properties
20 * @returns A piped operator that limits the number of results
21 */
22export function topK<
23 KType extends T extends KeyValue<infer K, infer _V> ? K : never,
24 V1Type extends T extends KeyValue<KType, infer V> ? V : never,
25 T,
26>(
27 comparator: (a: V1Type, b: V1Type) => number,
28 options?: TopKOptions
29): PipedOperator<T, T> {
30 const limit = options?.limit ?? Infinity
31 const offset = options?.offset ?? 0
32​
33 return (stream: IStreamBuilder<T>): IStreamBuilder<T> => {
34 const reduced = stream.pipe(
35 reduce((values) => {
36 // `values` is a list of tuples, first element is the value, second is the multiplicity
37 const consolidated = new MultiSet(values).consolidate()
38 const sortedValues = consolidated
39 .getInner()
40 .sort((a, b) => comparator(a[0] as V1Type, b[0] as V1Type))
41 return sortedValues.slice(offset, offset + limit)
42 })
43 )
44 return reduced as IStreamBuilder<T>
45 }
47​
48/**
49 * Limits the number of results based on a comparator, with optional offset.
50 * This works on a keyed stream, where the key is the first element of the tuple
51 * The ordering is withing a key group, i.e. elements are sorted within a key group
52 * and the limit + offset is applied to that sorted group.
53 * To order the entire stream, key by the same value for all elements such as null.
54 * Adds the index of the element to the result as [key, [value, index]]
55 *
56 * @param comparator - A function that compares two elements
57 * @param options - An optional object containing limit and offset properties
58 * @returns A piped operator that orders the elements and limits the number of results
59 */
60export function topKWithIndex<
61 KType extends T extends KeyValue<infer K, infer _V> ? K : never,
62 V1Type extends T extends KeyValue<KType, infer V> ? V : never,
63 T,
64>(
65 comparator: (a: V1Type, b: V1Type) => number,
66 options?: TopKOptions
67): PipedOperator<T, KeyValue<KType, [V1Type, number]>> {
68 const limit = options?.limit ?? Infinity
69 const offset = options?.offset ?? 0
70​
71 return (
72 stream: IStreamBuilder<T>
73 ): IStreamBuilder<KeyValue<KType, [V1Type, number]>> => {
74 const reduced = stream.pipe(
75 reduce<KType, V1Type, [V1Type, number], T>((values) => {
76 // `values` is a list of tuples, first element is the value, second is the multiplicity
77 const consolidated = new MultiSet(values).consolidate()
78 let i = offset
79 const sortedValues = consolidated
80 .getInner()
81 .sort((a, b) => comparator(a[0], b[0]))
82 .slice(offset, offset + limit)
83 .map(([value, multiplicity]): [[V1Type, number], number] => [
84 [value, i++],
85 multiplicity,
86 ])
87 return sortedValues
88 })
89 )
90 return reduced
91 }