Skip to content

Commit

Permalink
chunks => views; expanded View API
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jul 2, 2020
1 parent 511abe7 commit fd0dabd
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 35 deletions.
4 changes: 2 additions & 2 deletions lib/pure/collections/sequtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
32 changes: 0 additions & 32 deletions lib/std/chunks.nim

This file was deleted.

66 changes: 66 additions & 0 deletions lib/std/views.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
##[
Aka in other languages as: slice (in go), span (in C#).
Note that `system.Slice` already exists with a different meaning.
Note: experimental module, unstable API.
]##

import std/pointers

type View*[T] = object
## provides a view over a region of memory representing elements of type T
data*: ptr T
len*: int # TODO: or lenImpl + template accessor?

type MView*[T] = object
data*: ptr T
len*: int

type SomeView*[T] = View[T]|MView[T]

proc view*[T](a: openArray[T]): View[T] {.inline.} =
## return an immutable view over `a`
# PRTEMP: unsafeAddr?
result = View[T](data: a[0].addr, len: a.len)

proc view*[T](a: var openArray[T]): View[T] {.inline.} =
## return an immutable view over `a`
result = View[T](data: a[0].addr, len: a.len)

# proc toView*[T](a: var openArray[T]): var View[T] =
# let x = a[0].unsafeAddr
# # result = View[T](data: a[0].unsafeAddr, len: a.len)
# result = View[T](data: x, len: a.len)

proc mview*[T](a: var openArray[T]): MView[T] {.inline.} =
## return a mutable view over `a`
result = MView[T](data: a[0].addr, len: a.len)

# iterator items*[T](a: MView[T] | View[T]): lent T =
iterator items*[T](a: SomeView[T]): lent T =
## iterator over `a`
for i in 0..<a.len:
yield a.data[i]

iterator mitems*[T](a: MView[T]): var T =
## mutable iterator over `a`
for i in 0..<a.len:
yield a.data[i]

proc `[]`*[T, I](a: View[T], r: Slice[I]): View[T] {.inline.} =
assert r.a >= 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
2 changes: 1 addition & 1 deletion testament/important_packages.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 16 additions & 0 deletions tests/stdlib/tviews.nim
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fd0dabd

Please sign in to comment.