type SeqView[T] = object
data: ref seq[T]
bounds: Slice[int]
proc box[T](x: T): ref T =
new(result); shallowCopy(result[], x) # beware :)
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[int](data: box(seq1), bounds: 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