arraybuf: new constructor (#236)

* arraybuf: new constructor

also add a block to avoid symbol leaks

* speed up for same length
This commit is contained in:
Jacek Sieka 2024-10-07 22:13:34 +02:00 committed by GitHub
parent b7b5969557
commit 763147cf82
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 10 deletions

View File

@ -48,19 +48,22 @@ template len*(b: ArrayBuf): int =
int(b.n) int(b.n)
template setLen*(b: var ArrayBuf, newLenParam: int) = template setLen*(b: var ArrayBuf, newLenParam: int) =
newLenParam.evalOnceAs(newLen) block:
let nl = typeof(b.n)(newLen) newLenParam.evalOnceAs(newLen)
for i in newLen ..< b.len(): let nl = typeof(b.n)(newLen)
reset(b.buf[i]) # reset cleared items when shrinking for i in newLen ..< b.len():
b.n = nl reset(b.buf[i]) # reset cleared items when shrinking
b.n = nl
template data*(bParam: ArrayBuf): openArray = template data*(bParam: ArrayBuf): openArray =
bParam.evalOnceAs(b) block:
b.buf.toOpenArray(0, b.len() - 1) bParam.evalOnceAs(b)
b.buf.toOpenArray(0, b.len() - 1)
template data*(bParam: var ArrayBuf): var openArray = template data*(bParam: var ArrayBuf): var openArray =
bParam.evalOnceAs(b) block:
b.buf.toOpenArray(0, b.len() - 1) bParam.evalOnceAs(b)
b.buf.toOpenArray(0, b.len() - 1)
iterator items*[N, T](b: ArrayBuf[N, T]): lent T = iterator items*[N, T](b: ArrayBuf[N, T]): lent T =
for i in 0 ..< b.len: for i in 0 ..< b.len:
@ -92,6 +95,22 @@ template `==`*(a, b: ArrayBuf): bool =
template `<`*(a, b: ArrayBuf): bool = template `<`*(a, b: ArrayBuf): bool =
a.data() < b.data() a.data() < b.data()
template initCopyFrom*[N, T](
_: type ArrayBuf[N, T], data: openArray[T]
): ArrayBuf[N, T] =
var v: ArrayBuf[N, T]
v.n = typeof(v.n)(v.buf.copyFrom(data))
v
template initCopyFrom*[N, T](
_: type ArrayBuf[N, T], data: array[N, T]
): ArrayBuf[N, T] =
# Shortcut version that avoids zeroMem on matching lengths
ArrayBuf[N, T](
buf: data,
n: N
)
template add*[N, T](b: var ArrayBuf[N, T], v: T) = template add*[N, T](b: var ArrayBuf[N, T], v: T) =
## Adds items up to capacity then drops the rest ## Adds items up to capacity then drops the rest
# TODO `b` is evaluated multiple times but since it's a `var` this should # TODO `b` is evaluated multiple times but since it's a `var` this should
@ -112,4 +131,4 @@ template pop*[N, T](b: var ArrayBuf[N, T]): T =
# _hopefully_ be fine.. # _hopefully_ be fine..
assert b.n > 0, "pop from empty ArrayBuf" assert b.n > 0, "pop from empty ArrayBuf"
b.n -= 1 b.n -= 1
move(b.buf[b.n]) move(b.buf[b.n])

View File

@ -56,3 +56,16 @@ suite "ArrayBuf":
v.len == 1 v.len == 1
v.setLen(2) v.setLen(2)
doAssert v.data() == [byte 1, 0] doAssert v.data() == [byte 1, 0]
test "construction":
let
a0 = ArrayBuf[4, byte].initCopyFrom([])
a2 = ArrayBuf[2, byte].initCopyFrom([byte 2, 3, 4, 5])
a3 = ArrayBuf[5, byte].initCopyFrom([byte 2, 3, 4])
a5 = ArrayBuf[5, byte].initCopyFrom([byte 2, 3])
check:
a0.len == 0
a2.data() == [byte 2, 3]
a3.data() == [byte 2, 3, 4]
a5.data() == [byte 2, 3]