diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim index b08ba53590b04..f6efc44395a18 100644 --- a/lib/pure/collections/sequtils.nim +++ b/lib/pure/collections/sequtils.nim @@ -964,7 +964,7 @@ template mapIt*(s: typed, op: untyped): untyped = op map(s, f) -import std/chunks +import std/views template evalonceVar(lhs, typ, expr) = ## makes sure `expr` is evaluated once, and no copy is done when using @@ -985,7 +985,7 @@ template evalonceVar(lhs, typ, expr) = # macro that transforms `expr` aka `(body; last)` into: # `body; let tmp = unsafeAddr(last)` # template lhs: untyped = expr - let lhs = toMChunk(expr) + let lhs = mview(expr) else: when typ is type(nil): let tmp = addr(expr) diff --git a/lib/std/chunks.nim b/lib/std/chunks.nim deleted file mode 100644 index 10aed79b74a21..0000000000000 --- a/lib/std/chunks.nim +++ /dev/null @@ -1,32 +0,0 @@ -#[ -`Slice` would be a better name but unfortunately, `system.Slice` exists. -]# - -import std/pointers - -type Chunk*[T] = object - data*: ptr T - len*: int # TODO: or lenImpl + template accessor? - -type MChunk*[T] = object - data*: ptr T - len*: int - -proc toChunk*[T](a: openArray[T]): Chunk[T] = - result = Chunk[T](data: a[0].addr, len: a.len) - -# proc toChunk*[T](a: var openArray[T]): var Chunk[T] = -# let x = a[0].unsafeAddr -# # result = Chunk[T](data: a[0].unsafeAddr, len: a.len) -# result = Chunk[T](data: x, len: a.len) - -proc toMChunk*[T](a: var openArray[T]): MChunk[T] = - result = MChunk[T](data: a[0].addr, len: a.len) - -iterator mitems*[T](a: MChunk[T]): var T = - for i in 0..= 0 + assert r.b < a.len + result = View[T](data: a.data + r.a, len: r.len) + +proc `[]`*[T](a: View[T], index: int): lent T {.inline.} = + a.data[index] + +proc `[]`*[T](a: MView[T], index: int): var T {.inline.} = + # TODO: `a: var MView[T]`? + a.data[index] + +# proc `[]`*(a: View[T], index: int): lent T = +# a.data[index] + +proc `[]=`*[T](a: var MView[T], index: int, b: T) {.inline.} = + a.data[index] = b diff --git a/testament/important_packages.nim b/testament/important_packages.nim index 972bb3faae39e..0c0371a93d401 100644 --- a/testament/important_packages.nim +++ b/testament/important_packages.nim @@ -42,7 +42,7 @@ pkg1 "elvis" pkg1 "fidget", true pkg1 "fragments", false, "nim c -r fragments/dsl.nim" pkg1 "gara" -pkg1 "ggplotnim", true, "nim c -d:noCairo -r tests/tests.nim" +# pkg1 "ggplotnim", true, "nim c -d:noCairo -r tests/tests.nim" # pending https://github.com/Vindaar/ginger/pull/22 # pkg1 "gittyup", true, "nimble test", "https://github.com/disruptek/gittyup" pkg1 "glob" pkg1 "gnuplot" diff --git a/tests/stdlib/tviews.nim b/tests/stdlib/tviews.nim new file mode 100644 index 0000000000000..a63a2668cf736 --- /dev/null +++ b/tests/stdlib/tviews.nim @@ -0,0 +1,16 @@ +import std/views +from std/sequtils import toSeq + +block: + var a = @[1,2,3] + let v = a.view() + doAssert toSeq(v) == a + doAssert v[1] == 2 + +block: + var a = @[1,2,3,4] + # let v = a.view()[1..^1] # TODO + let v = a.view()[1..a.len-2] + doAssert toSeq(v) == @[2,3] + doAssert v[0] == 2 + # doAssert v[^1] == 3 # TODO