Open History to see the last commit
1import { TopKArray } from './topKArray.js'
2import type {
3 IndexedValue,
4 TopK,
5 TopKChanges,
6 TopKMoveChanges,
7} from './topKArray.js'
8
9/**
10 * Helper class that manages the state for a single topK window.
11 * Encapsulates the multiplicity tracking and topK data structure,
12 * providing a clean interface for processing elements and moving the window.
13 *
14 * This class is used by both TopKWithFractionalIndexOperator (single instance)
15 * and GroupedTopKWithFractionalIndexOperator (one instance per group).
16 */
17export class TopKState<K extends string | number, T> {
18 #multiplicities: Map<K, number> = new Map()
19 #topK: TopK<[K, T]>
20
21 constructor(topK: TopK<[K, T]>) {
22 this.#topK = topK
23 }
24
25 get size(): number {
26 return this.#topK.size
27 }
28
29 get isEmpty(): boolean {
30 return this.#multiplicities.size === 0 && this.#topK.size === 0
31 }
32
33 /**
34 * Process an element update (insert or delete based on multiplicity change).
35 * Returns the changes to the topK window.
36 */
37 processElement(key: K, value: T, multiplicity: number): TopKChanges<[K, T]> {
38 const { oldMultiplicity, newMultiplicity } = this.#updateMultiplicity(
39 key,
40 multiplicity,
41 )
42
43 if (oldMultiplicity <= 0 && newMultiplicity > 0) {
44 // The value was invisible but should now be visible
45 return this.#topK.insert([key, value])
46 } else if (oldMultiplicity > 0 && newMultiplicity <= 0) {
47 // The value was visible but should now be invisible
48 return this.#topK.delete([key, value])
49 }
50 // The value was invisible and remains invisible,
51 // or was visible and remains visible - no topK change
52 return { moveIn: null, moveOut: null }
53 }
54
55 /**
56 * Move the topK window. Only works with TopKArray implementation.
57 */
58 move(options: { offset?: number; limit?: number }): TopKMoveChanges<[K, T]> {
59 if (!(this.#topK instanceof TopKArray)) {
60 throw new Error(
61 `Cannot move B+-tree implementation of TopK with fractional index`,
62 )
63 }
64 return this.#topK.move(options)
65 }
66
67 #updateMultiplicity(
68 key: K,
69 multiplicity: number,
70 ): { oldMultiplicity: number; newMultiplicity: number } {
71 if (multiplicity === 0) {
72 const current = this.#multiplicities.get(key) ?? 0
73 return { oldMultiplicity: current, newMultiplicity: current }
74 }
75
76 const oldMultiplicity = this.#multiplicities.get(key) ?? 0
77 const newMultiplicity = oldMultiplicity + multiplicity
78 if (newMultiplicity === 0) {
79 this.#multiplicities.delete(key)
80 } else {
81 this.#multiplicities.set(key, newMultiplicity)
82 }
83 return { oldMultiplicity, newMultiplicity }
84 }
85}
86
87/**
88 * Handles a moveIn change by adding it to the result array.
89 */
90export function handleMoveIn<K extends string | number, T>(
91 moveIn: IndexedValue<[K, T]> | null,
92 result: Array<[[K, IndexedValue<T>], number]>,
93): void {
94 if (moveIn) {
95 const [[key, value], index] = moveIn
96 result.push([[key, [value, index]], 1])
97 }
98}
99
100/**
101 * Handles a moveOut change by adding it to the result array.
102 */
103export function handleMoveOut<K extends string | number, T>(
104 moveOut: IndexedValue<[K, T]> | null,
105 result: Array<[[K, IndexedValue<T>], number]>,
106): void {
107 if (moveOut) {
108 const [[key, value], index] = moveOut
109 result.push([[key, [value, index]], -1])
110 }
111}