MCPcopy Create free account
hub / github.com/cyan-2048/solid-telekram / insert

Method insert

@mtcute/core/utils/sorted-array.ts:21–54  ·  view source on GitHub ↗
(item: T | T[])

Source from the content-addressed store, hash-verified

19 }
20
21 insert(item: T | T[]): number {
22 if (Array.isArray(item)) {
23 let ind = -1
24 item.forEach((it) => {
25 ind = this.insert(it)
26 })
27
28 return ind
29 }
30
31 if (this.raw.length === 0) {
32 this.raw.push(item)
33
34 return 0
35 }
36
37 // find insert position
38 let lo = 0
39 let hi = this.raw.length
40
41 while (lo < hi) {
42 const mid = Math.floor((lo + hi) / 2)
43
44 if (this.comparator(this.raw[mid], item) > 0) {
45 hi = mid
46 } else {
47 lo = mid + 1
48 }
49 }
50
51 this.raw.splice(lo, 0, item)
52
53 return lo
54 }
55
56 // closest: return the closest value (right-hand side)
57 // meaning that raw[idx - 1] <= item <= raw[idx]

Callers 5

_onRpcResultMethod · 0.80
_doFlushMethod · 0.80
handleUpdateMethod · 0.80
_fetchDifferenceMethod · 0.80
_loopMethod · 0.80

Calls 1

pushMethod · 0.80

Tested by

no test coverage detected