1 | /*
|
---|
2 | * This is a TypeScript port of the original Java version, which was written by
|
---|
3 | * Gil Tene as described in
|
---|
4 | * https://github.com/HdrHistogram/HdrHistogram
|
---|
5 | * and released to the public domain, as explained at
|
---|
6 | * http://creativecommons.org/publicdomain/zero/1.0/
|
---|
7 | */
|
---|
8 | import JsHistogram from "./JsHistogram";
|
---|
9 |
|
---|
10 | type TypedArray = ArrayLike<number> & {
|
---|
11 | readonly BYTES_PER_ELEMENT: number;
|
---|
12 | [key: number]: number;
|
---|
13 | fill(v: number): void;
|
---|
14 | set(other: TypedArray): void;
|
---|
15 | };
|
---|
16 |
|
---|
17 | class TypedArrayHistogram extends JsHistogram {
|
---|
18 | _counts: TypedArray;
|
---|
19 | _totalCount: number;
|
---|
20 |
|
---|
21 | constructor(
|
---|
22 | private arrayCtr: new (size: number) => TypedArray,
|
---|
23 | lowestDiscernibleValue: number,
|
---|
24 | highestTrackableValue: number,
|
---|
25 | numberOfSignificantValueDigits: number
|
---|
26 | ) {
|
---|
27 | super(
|
---|
28 | lowestDiscernibleValue,
|
---|
29 | highestTrackableValue,
|
---|
30 | numberOfSignificantValueDigits
|
---|
31 | );
|
---|
32 | this._totalCount = 0;
|
---|
33 | this._counts = new arrayCtr(this.countsArrayLength);
|
---|
34 | }
|
---|
35 |
|
---|
36 | clearCounts() {
|
---|
37 | this._counts.fill(0);
|
---|
38 | }
|
---|
39 |
|
---|
40 | incrementCountAtIndex(index: number) {
|
---|
41 | const currentCount = this._counts[index];
|
---|
42 | const newCount = currentCount + 1;
|
---|
43 | if (newCount < 0) {
|
---|
44 | throw newCount + " would overflow short integer count";
|
---|
45 | }
|
---|
46 | this._counts[index] = newCount;
|
---|
47 | }
|
---|
48 |
|
---|
49 | addToCountAtIndex(index: number, value: number) {
|
---|
50 | const currentCount = this._counts[index];
|
---|
51 | const newCount = currentCount + value;
|
---|
52 | if (
|
---|
53 | newCount < Number.MIN_SAFE_INTEGER ||
|
---|
54 | newCount > Number.MAX_SAFE_INTEGER
|
---|
55 | ) {
|
---|
56 | throw newCount + " would overflow integer count";
|
---|
57 | }
|
---|
58 | this._counts[index] = newCount;
|
---|
59 | }
|
---|
60 |
|
---|
61 | setCountAtIndex(index: number, value: number) {
|
---|
62 | if (value < Number.MIN_SAFE_INTEGER || value > Number.MAX_SAFE_INTEGER) {
|
---|
63 | throw value + " would overflow integer count";
|
---|
64 | }
|
---|
65 | this._counts[index] = value;
|
---|
66 | }
|
---|
67 |
|
---|
68 | resize(newHighestTrackableValue: number) {
|
---|
69 | this.establishSize(newHighestTrackableValue);
|
---|
70 | const newCounts = new this.arrayCtr(this.countsArrayLength);
|
---|
71 | newCounts.set(this._counts);
|
---|
72 | this._counts = newCounts;
|
---|
73 | }
|
---|
74 |
|
---|
75 | getCountAtIndex(index: number) {
|
---|
76 | return this._counts[index];
|
---|
77 | }
|
---|
78 |
|
---|
79 | protected _getEstimatedFootprintInBytes() {
|
---|
80 | return 1024 + this._counts.BYTES_PER_ELEMENT * this._counts.length;
|
---|
81 | }
|
---|
82 |
|
---|
83 | copyCorrectedForCoordinatedOmission(
|
---|
84 | expectedIntervalBetweenValueSamples: number
|
---|
85 | ) {
|
---|
86 | const copy = new TypedArrayHistogram(
|
---|
87 | this.arrayCtr,
|
---|
88 | this.lowestDiscernibleValue,
|
---|
89 | this.highestTrackableValue,
|
---|
90 | this.numberOfSignificantValueDigits
|
---|
91 | );
|
---|
92 | copy.addWhileCorrectingForCoordinatedOmission(
|
---|
93 | this,
|
---|
94 | expectedIntervalBetweenValueSamples
|
---|
95 | );
|
---|
96 | return copy;
|
---|
97 | }
|
---|
98 |
|
---|
99 | toString() {
|
---|
100 | return `Histogram ${this._counts.BYTES_PER_ELEMENT * 8}b ${JSON.stringify(
|
---|
101 | this,
|
---|
102 | null,
|
---|
103 | 2
|
---|
104 | )}`;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | export default TypedArrayHistogram;
|
---|