add concat operation
This commit is contained in:
parent
a9f253b8a4
commit
c23f4ddf1b
|
@ -145,3 +145,19 @@ proc `[]=`*[T, U, V](r: MutRange[T], s: HSlice[U, V], v: Range[T]) {.inline.} =
|
||||||
r[s] = toOpenArray(v)
|
r[s] = toOpenArray(v)
|
||||||
|
|
||||||
proc baseAddr*[T](r: Range[T]): ptr T {.inline.} = r.start
|
proc baseAddr*[T](r: Range[T]): ptr T {.inline.} = r.start
|
||||||
|
|
||||||
|
template toRange*[T](a: Range[T]): Range[T] = a
|
||||||
|
|
||||||
|
proc concat*[T](v: varargs[Range[T], toRange]): seq[T] =
|
||||||
|
var len = 0
|
||||||
|
for c in v: inc(len, c.len)
|
||||||
|
result = newSeq[T](len)
|
||||||
|
len = 0
|
||||||
|
for c in v:
|
||||||
|
copyMem(result[len].addr, c.start, sizeof(T) * c.len)
|
||||||
|
inc(len, c.len)
|
||||||
|
|
||||||
|
proc `&`*[T](a, b: Range[T]): seq[T] =
|
||||||
|
result = newSeq[T](a.len + b.len)
|
||||||
|
copyMem(result[0].addr, a.start, sizeof(T) * a.len)
|
||||||
|
copyMem(result[a.len].addr, b.start, sizeof(T) * b.len)
|
||||||
|
|
|
@ -26,3 +26,20 @@ suite "Typed ranges":
|
||||||
var z = toRange(@[1, 2, 3, 4])
|
var z = toRange(@[1, 2, 3, 4])
|
||||||
check y == z
|
check y == z
|
||||||
check x != z
|
check x != z
|
||||||
|
|
||||||
|
test "concat operation":
|
||||||
|
var a = toRange(@[1,2,3])
|
||||||
|
var b = toRange(@[4,5,6])
|
||||||
|
var c = toRange(@[7,8,9])
|
||||||
|
var d = @[1,2,3,4,5,6,7,8,9]
|
||||||
|
var e = @[1,2,3,4,5,6]
|
||||||
|
var f = @[4,5,6,7,8,9]
|
||||||
|
var x = concat(a, b, c)
|
||||||
|
var y = a & b
|
||||||
|
check x == d
|
||||||
|
check y == e
|
||||||
|
var z = concat(b, @[7,8,9])
|
||||||
|
check z == f
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue