-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathaggregate-collection.ts
More file actions
86 lines (74 loc) · 1.54 KB
/
Copy pathaggregate-collection.ts
File metadata and controls
86 lines (74 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
export class AggregateCollection {
#min: number
#max: number
#sum: number
#count: number
#frequencies: Map<number, number>
#items: number[] | null
constructor(samples = false) {
this.#min = Infinity
this.#max = -Infinity
this.#sum = 0
this.#count = 0
this.#frequencies = new Map()
this.#items = samples ? [] : null
}
/**
* Add a new Integer at the end of this AggregateCollection
* @param item - The item to add
*/
push(item: number) {
this.#sum += item
this.#count++
if (item < this.#min) this.#min = item
if (item > this.#max) this.#max = item
let freq = (this.#frequencies.get(item) || 0) + 1
this.#frequencies.set(item, freq)
if (this.#items !== null) {
this.#items.push(item)
}
}
size() {
return this.#count
}
aggregate() {
let len = this.#count
if (len === 0) {
return {
min: 0,
max: 0,
mean: 0,
mode: 0,
range: 0,
sum: 0,
}
}
// Find max frequency for mode calculation — O(k) where k = unique values
let maxFreq = 0
for (let freq of this.#frequencies.values()) {
if (freq > maxFreq) maxFreq = freq
}
// Average of all values with max frequency (matches prior behavior)
let modeSum = 0
let modeCount = 0
for (let [val, freq] of this.#frequencies) {
if (freq === maxFreq) {
modeSum += val
modeCount++
}
}
let min = this.#min
let max = this.#max
return {
min,
max,
mean: this.#sum / len,
mode: modeSum / modeCount,
range: max - min,
sum: this.#sum,
}
}
toArray(): number[] {
return this.#items ?? []
}
}