From 40f5f77be0fe78127f3d4e76a6678af3a03a4213 Mon Sep 17 00:00:00 2001 From: Stefan Zerkalica Date: Mon, 18 Sep 2023 00:20:31 +0300 Subject: [PATCH] $mol_slicer: temp window --- slicer/slicer.test.ts | 27 +++++++++++++++++++-------- slicer/slicer.ts | 30 ++++++++++++++++-------------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/slicer/slicer.test.ts b/slicer/slicer.test.ts index 4d262c8f7c9..3c781a2997d 100644 --- a/slicer/slicer.test.ts +++ b/slicer/slicer.test.ts @@ -38,22 +38,33 @@ namespace $ { 'multiple push'($) { const { range, stub } = make_range() + const arr = range.range() + let calls = 0 + const fresh_stub = stub_ids(10) + range.chunk = () => { + calls++ + return fresh_stub + } const appended = stub_ids(30) range.temp_push(...appended) $mol_assert_equal(arr.length, appended.length + stub.length) + $mol_assert_equal(arr[29], appended[4]) + $mol_assert_equal(arr[30], appended[5]) + range.temp_cut() + $mol_assert_equal(arr.length, appended.length + stub.length) + $mol_assert_equal(arr[29], appended[4]) + $mol_assert_equal(arr[30], fresh_stub[0]) + $mol_assert_equal(calls, 1) - let calls = 0 - range.chunk = offset => { - calls++ - return [] - } - - arr[50] + const appended2 = stub_ids(30) + range.temp_push(...appended2) + $mol_assert_equal(arr[60], appended2[5]) + range.temp_cut() + $mol_assert_equal(arr[60], fresh_stub[0]) - $mol_assert_equal(calls, 0) }, }) } diff --git a/slicer/slicer.ts b/slicer/slicer.ts index de637f9563c..0357a616fad 100644 --- a/slicer/slicer.ts +++ b/slicer/slicer.ts @@ -28,14 +28,16 @@ namespace $ { return id } + protected temp = [] as Item[] + @ $mol_mem - temp(next?: readonly Item[]) { - return next ?? [] + temp_len(next?: number) { + return next ?? 0 } temp_push(...ids: readonly Item[]) { - this.temp([ ...this.temp(), ...ids ]) - this.temp_cut() + this.temp.push(...ids) + this.temp_len(this.temp.length) return this.length } @@ -45,30 +47,30 @@ namespace $ { } temp_cut() { - const temp = this.temp() - if (! temp) return + if (! this.temp.length) return const limit = this.chunk_size() const addenum = this.count() % limit - const delete_chunks = Math.ceil((temp.length - addenum) / limit) - this.temp_chunk_min() + const delete_chunks = Math.ceil((this.temp.length - addenum) / limit) - this.temp_chunk_min() if (delete_chunks <= 0) return const delete_count = delete_chunks * limit - const next = temp.slice() - next.splice(addenum, delete_count) - this.temp(next) + this.temp.splice(addenum, delete_count) this.removed_count += delete_count + this.temp_len(this.temp.length) } at(index: number) { if (index < 0) index = Math.max(0, this.length + index) const count = this.count() const addenum = count % this.chunk_size() - const temp = this.temp() + const temp_len = this.temp_len() let temp_index = index - count - if (temp_index > addenum) temp_index -= this.removed_count - if (temp_index >= 0 && temp_index < temp.length) return temp[temp_index] + + if (temp_index >= 0 && temp_index < temp_len && temp_index < addenum) return this.temp[temp_index] + temp_index -= this.removed_count + if (temp_index >= 0 && temp_index < temp_len) return this.temp[temp_index] return this.item(index) } @@ -76,7 +78,7 @@ namespace $ { protected removed_count = 0 get length() { - return this.count() + this.removed_count + this.temp().length + return this.count() + this.removed_count + this.temp_len() } @ $mol_mem