type SeqView[T] = object
data: ref seq[T]
bounds: Slice[int]
proc seqView*[T](x: seq[T], bounds: Slice): SeqView[T] =
var rf: ref seq[T]
new(rf)
shallowCopy(rf[], x) # beware :)
SeqView[T](data: rf, bounds: bounds)
iterator items*[T](sv: SeqView[T]): T =
for pos in sv.bounds:
yield sv.data[pos]
iterator mitems*[T](sv: SeqView[T]): var T =
for pos in sv.bounds:
yield sv.data[pos]
var
seq1 = @[1, 2, 3, 4, 5, 6]
sv = seqView(seq1, 2..4)
echo "a:"
for el in sv:
echo el
# inc using the mitems on the "slice"
for el in mitems(sv):
inc el
echo "\nb:"
for el in sv:
echo el
# update and print original data
for i in 2..4:
inc seq1[i]
echo "\nc:"
for el in seq1:
echo el
echo "\nd:"
# test if true reference or copy
for el in sv:
echo el