Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
aranega committed Aug 13, 2024
1 parent 9e72bb5 commit 0c44d46
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
NINJA_PAGINATION_PER_PAGE = 100

# Fragments for various artifacts
# In this format: {var} is changed by the backend, while {{var}} is something that need to be changed by the frontend
# In this format: {var} is changed by the backend, while the first {{var}} is something that will be changed by the frontend
# NEURON_REPRESENTATION_3D_URL_FORMAT = "resources/{{dataset}}/3d-model/{name}"
# DATASET_NEURON_REPRESENTATION_3D_URL_FORMAT = "resources/{dataset}/3d-model/{{name}}"
# DATASET_EMDATA_URL_FORMAT = "resources/{dataset}/em-data/tiles/{{index}}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ interface ScaleControlsHandlers {
onPrint: () => void;
}

function SceneControls({
onZoomIn,
onResetView,
onZoomOut,
onPrint,
}: ScaleControlsHandlers) {
function SceneControls({ onZoomIn, onResetView, onZoomOut, onPrint }: ScaleControlsHandlers) {
return (
<Box
sx={{
Expand Down
307 changes: 154 additions & 153 deletions applications/visualizer/frontend/src/helpers/slidingRing.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export interface SlidingRingCb<T> {
onPush: (n: number) => T; // called any time a new object is pushed into the ring
onSelected: (n: number, o: T) => void; // called when the selected object changes
onUnselected: (n: number, o: T) => void; // called when an object is unselected
onEvict: (n: number, o: T) => void; // called when an object is evicted from the ring
onPush: (n: number) => T; // called any time a new object is pushed into the ring
onSelected: (n: number, o: T) => void; // called when the selected object changes
onUnselected: (n: number, o: T) => void; // called when an object is unselected
onEvict: (n: number, o: T) => void; // called when an object is evicted from the ring
}

export interface SlidingRingOptions<T> extends SlidingRingCb<T> {
cacheSize: number;
startAt: number;
extent: [number, number];
cacheSize: number;
startAt: number;
extent: [number, number];
}

/**
Expand All @@ -29,169 +29,170 @@ export interface SlidingRingOptions<T> extends SlidingRingCb<T> {
* Going back on the previous element follows the same logic.
*/
export class SlidingRing<T> {
private extent: [number, number];

private ring: Array<{
n: number; // position within extent
o: T;
}>;

private pos: number; // current buffer position
private tail: number; // buffer tail
private head: number; // buffer head

private cb: SlidingRingCb<T>

constructor(options: SlidingRingOptions<T>) {
if (options.cacheSize < 3) {
throw Error("cache should be greater that 3")
}

const [min, max] = options.extent

if (min >= max) {
throw Error("extent should be [min,max], where min < max")
}

if (options.startAt > max || options.startAt < min) {
throw Error("startAt must be within extent")
}

const extentSize = max - min
if (extentSize < 3) {
throw Error("extent size is too small, should be greater than 3")
}

if (extentSize < options.cacheSize) {
options.cacheSize = max - min
}

this.ring = new Array(options.cacheSize)
this.extent = options.extent

this.cb = {
onPush: options.onPush,
onSelected: options.onSelected,
onUnselected: options.onUnselected,
onEvict: options.onEvict,
}

// initialize ring
const halfSize = Math.floor(this.ring.length / 2)
let tailN = options.startAt - halfSize
let headN = options.startAt + halfSize

// the ring may start near the extent
// this account for that adjustment
if (tailN < min) tailN = min
if (headN > max) {
headN = max
tailN = headN - this.ring.length - 1
}

// populate the ring
for (let i = 0; i < this.ring.length; i++) {
const n = tailN + i
const o = this.cb.onPush(n)

this.ring[i] = {n, o}
}

this.pos = options.startAt - tailN
this.tail = 0
this.head = this.ring.length - 1

this.cb.onSelected(options.startAt, this.ring[this.pos].o)
private extent: [number, number];

private ring: Array<{
n: number; // position within extent
o: T;
}>;

private pos: number; // current buffer position
private tail: number; // buffer tail
private head: number; // buffer head

private cb: SlidingRingCb<T>;

constructor(options: SlidingRingOptions<T>) {
if (options.cacheSize < 3) {
throw Error("cache should be greater that 3");
}

const [min, max] = options.extent;

if (min >= max) {
throw Error("extent should be [min,max], where min < max");
}

if (options.startAt > max || options.startAt < min) {
throw Error("startAt must be within extent");
}

const extentSize = max - min;
if (extentSize < 3) {
throw Error("extent size is too small, should be greater than 3");
}

if (extentSize < options.cacheSize) {
options.cacheSize = max - min;
}

this.ring = new Array(options.cacheSize);
this.extent = options.extent;

this.cb = {
onPush: options.onPush,
onSelected: options.onSelected,
onUnselected: options.onUnselected,
onEvict: options.onEvict,
};

// initialize ring
const halfSize = Math.floor(this.ring.length / 2);
let tailN = options.startAt - halfSize;
let headN = options.startAt + halfSize;

// the ring may start near the extent
// this account for that adjustment
if (tailN < min) tailN = min;
if (headN > max) {
headN = max;
tailN = headN - this.ring.length - 1;
}

next() {
const [_, max] = this.extent
const nextN = this.ring[this.pos].n + 1
if (nextN > max) return
// populate the ring
for (let i = 0; i < this.ring.length; i++) {
const n = tailN + i;
const o = this.cb.onPush(n);

// update selected
const nextPos = (this.pos + this.ring.length + 1) % this.ring.length
this.cb.onSelected(this.ring[nextPos].n, this.ring[nextPos].o)
this.cb.onUnselected(this.ring[this.pos].n, this.ring[this.pos].o)
this.ring[i] = { n, o };
}

this.pos = options.startAt - tailN;
this.tail = 0;
this.head = this.ring.length - 1;

this.cb.onSelected(options.startAt, this.ring[this.pos].o);
}

// update sliding window
const nextHeadN = this.ring[this.head].n + 1
const halfSize = Math.floor(this.ring.length / 2) // TODO: to avoid calc should we save as class property?
const canSlideFurther = nextN - this.ring[this.tail].n > halfSize
next() {
const [_, max] = this.extent;
const nextN = this.ring[this.pos].n + 1;
if (nextN > max) return;

if (nextHeadN <= max && canSlideFurther) {
const tail = this.ring[this.tail]
this.cb.onEvict(tail.n, tail.o)
// update selected
const nextPos = (this.pos + this.ring.length + 1) % this.ring.length;
this.cb.onSelected(this.ring[nextPos].n, this.ring[nextPos].o);
this.cb.onUnselected(this.ring[this.pos].n, this.ring[this.pos].o);

this.ring[this.tail] = {
n: nextHeadN,
o: this.cb.onPush(nextHeadN)
}
// update sliding window
const nextHeadN = this.ring[this.head].n + 1;
const halfSize = Math.floor(this.ring.length / 2); // TODO: to avoid calc should we save as class property?
const canSlideFurther = nextN - this.ring[this.tail].n > halfSize;

this.head = this.tail
this.tail = (this.tail + this.ring.length + 1) % this.ring.length
}
if (nextHeadN <= max && canSlideFurther) {
const tail = this.ring[this.tail];
this.cb.onEvict(tail.n, tail.o);

this.pos = nextPos
this.ring[this.tail] = {
n: nextHeadN,
o: this.cb.onPush(nextHeadN),
};

this.head = this.tail;
this.tail = (this.tail + this.ring.length + 1) % this.ring.length;
}

prev() {
const [min, _] = this.extent
const prevN = this.ring[this.pos].n - 1
if (prevN < min) return
this.pos = nextPos;
}

// update selected
const prevPos = (this.pos + this.ring.length - 1) % this.ring.length
this.cb.onSelected(this.ring[prevPos].n, this.ring[prevPos].o)
this.cb.onUnselected(this.ring[this.pos].n, this.ring[this.pos].o)
prev() {
const [min, _] = this.extent;
const prevN = this.ring[this.pos].n - 1;
if (prevN < min) return;

// update sliding window
const nextTailN = this.ring[this.tail].n - 1
const halfSize = Math.floor(this.ring.length / 2) // TODO: to avoid calc should we save as class property?
const canSlideBackwards = this.ring[this.head].n - prevN > halfSize
// update selected
const prevPos = (this.pos + this.ring.length - 1) % this.ring.length;
this.cb.onSelected(this.ring[prevPos].n, this.ring[prevPos].o);
this.cb.onUnselected(this.ring[this.pos].n, this.ring[this.pos].o);

if (nextTailN >= min && canSlideBackwards) {
const head = this.ring[this.head]
this.cb.onEvict(head.n, head.o)
// update sliding window
const nextTailN = this.ring[this.tail].n - 1;
const halfSize = Math.floor(this.ring.length / 2); // TODO: to avoid calc should we save as class property?
const canSlideBackwards = this.ring[this.head].n - prevN > halfSize;

this.ring[this.head] = {
n: nextTailN,
o: this.cb.onPush(nextTailN)
}
if (nextTailN >= min && canSlideBackwards) {
const head = this.ring[this.head];
this.cb.onEvict(head.n, head.o);

this.tail = this.head
this.head = (this.tail + this.ring.length - 1) % this.ring.length
}
this.ring[this.head] = {
n: nextTailN,
o: this.cb.onPush(nextTailN),
};

this.pos = prevPos
this.tail = this.head;
this.head = (this.tail + this.ring.length - 1) % this.ring.length;
}

debug() {
let text = '['

for (let i = 0; i < this.ring.length; i++) {
if (this.ring[i] === undefined) { // should not happen
text = text. concat('?')
continue
}

switch (true) {
case i === this.pos:
text = text.concat('*')
break
case i === this.tail:
text = text.concat("-")
break
case i === this.head:
text = text.concat("+")
break
}

text = text.concat(`${this.ring[i].n}`)
if (i !== this.ring.length - 1) text = text.concat(', ')
}
text = text.concat(']')

console.debug(text)
this.pos = prevPos;
}

debug() {
let text = "[";

for (let i = 0; i < this.ring.length; i++) {
if (this.ring[i] === undefined) {
// should not happen
text = text.concat("?");
continue;
}

switch (true) {
case i === this.pos:
text = text.concat("*");
break;
case i === this.tail:
text = text.concat("-");
break;
case i === this.head:
text = text.concat("+");
break;
}

text = text.concat(`${this.ring[i].n}`);
if (i !== this.ring.length - 1) text = text.concat(", ");
}
text = text.concat("]");

console.debug(text);
}
}

0 comments on commit 0c44d46

Please sign in to comment.