MCPcopy Create free account
hub / github.com/Effect-TS/effect / insertSorted

Function insertSorted

packages/effect/src/TxPriorityQueue.ts:87–104  ·  view source on GitHub ↗
(chunk: Chunk<A>, value: A, ord: Order<A>)

Source from the content-addressed store, hash-verified

85}
86
87const insertSorted = <A>(chunk: Chunk<A>, value: A, ord: Order<A>): Chunk<A> => {
88 const arr = C.toArray(chunk) as Array<A>
89 let lo = 0
90 let hi = arr.length
91 while (lo < hi) {
92 const mid = (lo + hi) >>> 1
93 if (ord(arr[mid], value) <= 0) {
94 lo = mid + 1
95 } else {
96 hi = mid
97 }
98 }
99 const out = Array(arr.length + 1) as Array<A>
100 for (let i = 0; i < lo; i++) out[i] = arr[i]
101 out[lo] = value
102 for (let i = lo; i < arr.length; i++) out[i + 1] = arr[i]
103 return C.fromIterable(out)
104}
105
106/**
107 * Creates an empty `TxPriorityQueue` with the given ordering.

Callers 1

TxPriorityQueue.tsFile · 0.85

Calls 1

toArrayMethod · 0.80

Tested by

no test coverage detected