Skip to content

Commit

Permalink
feat: remove slidingWindowIdx and use currentSlidingWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
ReidyT committed Oct 25, 2024
1 parent 26e3f33 commit 4eb4535
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
3 changes: 1 addition & 2 deletions src/models/TemperatureIterator.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { describe, expect, it } from 'vitest';

import { SIMULATION_SLIDING_WINDOW } from '@/config/simulation';
// Import your class
import { TemperatureRow } from '@/types/temperatures';
import { TimeUnit, TimeUnitType } from '@/types/time';
import { timeConversionFactors } from '@/utils/time';
Expand Down Expand Up @@ -153,7 +152,7 @@ describe('TemperatureIterator', () => {
});

it('should use the default sliding window if none is provided', () => {
const temperatures = createDummyTemperatures(10, TimeUnit.days);
const temperatures = createDummyTemperatures(10, TimeUnit.hours);
const iterator = new TemperatureIterator({
temperatures,
measurementFrequency: TimeUnit.days,
Expand Down
23 changes: 9 additions & 14 deletions src/models/TemperatureIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ export class TemperatureIterator {

private currentSlidingWindow: SlidingWindow;

private slidingWindowIdx: number;

/**
*
* @param temperatures - The complete array of loaded temperatures.
Expand Down Expand Up @@ -75,21 +73,25 @@ export class TemperatureIterator {
timeConversionFactors[measurementFrequency], // Convert window according to measurement frequency
);

this.slidingWindowIdx = initSlidingWindowIdx(this.slidingWindowSize);
this.currentSlidingWindow = initSlidingWindow(this.slidingWindowIdx);
this.currentSlidingWindow = initSlidingWindow(
initSlidingWindowIdx(this.slidingWindowSize),
);
}

hasMore(): boolean {
if (this.numberOfRows === 0) {
return false;
}

return this.slidingWindowIdx + 1 < this.numberOfRows;
return (
this.currentSlidingWindow.idx + this.slidingWindowSize < this.numberOfRows
);
}

reset(): void {
this.slidingWindowIdx = initSlidingWindowIdx(this.slidingWindowSize);
this.currentSlidingWindow = initSlidingWindow(this.slidingWindowIdx);
this.currentSlidingWindow = initSlidingWindow(
initSlidingWindowIdx(this.slidingWindowSize),
);
}

getNext(): SlidingWindow {
Expand All @@ -111,13 +113,6 @@ export class TemperatureIterator {
const total = temperatures.reduce((sum, currVal) => sum + currVal, 0);
const currentSize = temperatures.length;

// TODO: this is needed for now to work correctly!
// Without it, there is some weird case like:
// temperatures: [10, 11]
// getNext() -> [10, 11], expected [10, 11]
// getNext() -> [11], No more temperatures!
this.slidingWindowIdx = currIdx + 1;

this.currentSlidingWindow = {
idx: currIdx,
mean: Math.round(total / currentSize),
Expand Down

0 comments on commit 4eb4535

Please sign in to comment.