Skip to content

Commit

Permalink
$mol_slicer: temp window
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Sep 17, 2023
1 parent 99fce47 commit 40f5f77
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
27 changes: 19 additions & 8 deletions slicer/slicer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
})
}
Expand Down
30 changes: 16 additions & 14 deletions slicer/slicer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -45,38 +47,38 @@ 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)
}

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
Expand Down

0 comments on commit 40f5f77

Please sign in to comment.