Skip to content

Commit e909e3c

Browse files
author
Oskar Widmark
committed
feat: sub 1ms draw times
1 parent 112f856 commit e909e3c

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Features:
22

3-
- [ ] Sub 1ms draw times
3+
- [x] Sub 1ms draw times
44
- [ ] Parallelization for algorithms
55
- [ ] Merge sort
66
- [x] 2D color matrix visualization

src/App.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class App extends React.Component<Props> {
4545
private nbrOfSwaps: number = 0;
4646
private nbrOfComparisons: number = 0;
4747
private nbrOfAuxWrites: number = 0;
48+
private swapCounter = 0;
49+
private comparisonCounter = 0;
50+
private auxWriteCounter = 0;
4851
state: AppState;
4952
canvasController: CanvasController;
5053

@@ -203,7 +206,7 @@ class App extends React.Component<Props> {
203206
nbrOfSwaps: prevState.nbrOfSwaps + 1,
204207
}));
205208
this.canvasController.highlight(arr, [i1, i2]);
206-
await sleep(this.state.settings.swapTime);
209+
await sleep(this.state.settings.swapTime, this.swapCounter++);
207210
}
208211
};
209212

@@ -255,7 +258,7 @@ class App extends React.Component<Props> {
255258
}));
256259
const indexes = 'value' in params ? [i1] : [i1, params.i2];
257260
this.canvasController.highlight(arr, indexes);
258-
await sleep(this.state.settings.compareTime);
261+
await sleep(this.state.settings.compareTime, this.comparisonCounter++);
259262
}
260263

261264
const value = 'value' in params ? params.value : arr[params.i2].value;
@@ -296,7 +299,7 @@ class App extends React.Component<Props> {
296299
nbrOfAuxWrites: prevState.nbrOfAuxWrites + 1,
297300
}));
298301
this.canvasController.highlight(arr, [i]);
299-
await sleep(this.state.settings.auxWriteTime);
302+
await sleep(this.state.settings.auxWriteTime, this.auxWriteCounter++);
300303
}
301304
};
302305

src/components/TimeSlider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function TimeSlider(props: {
1515
valueLabelDisplay="auto"
1616
min={0}
1717
step={0.1}
18-
max={10}
18+
max={14}
1919
scale={(x) => timeScale(x)}
2020
onChangeCommitted={(_, value) => changeTime(timeScale(value as number))}
2121
valueLabelFormat={(value: number) => `${value} ms`}

src/utils.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@ export const createArr = (columnNbr: number): SortValue[] =>
3737
return { value: a, id: idx };
3838
});
3939

40-
export const timeScale = (x: number) => Math.round(2 ** x) - 1;
41-
export const inverseTimeScale = (x: number) => Math.log2(x + 1);
40+
export const timeScale = (x: number) => (Math.round(2 ** x) - 1) / 10;
41+
export const inverseTimeScale = (x: number) => Math.log2((x + 1) * 10);
4242

43-
export const sleep = (ms: number) => {
43+
export const sleep = (ms: number, counter: number) => {
44+
if (ms < 1) {
45+
const hz = Math.round(1 / ms);
46+
if (counter % hz !== 0) {
47+
return Promise.resolve();
48+
}
49+
}
4450
return new Promise((resolve) => setTimeout(resolve, ms));
4551
};
4652

0 commit comments

Comments
 (0)