work in progress: new implementation for 'a[^1]'

This commit is contained in:
Andreas Rumpf 2017-10-29 19:46:17 +01:00
commit d52a1061b3
13 changed files with 96 additions and 89 deletions

View file

@ -308,13 +308,14 @@ when defined(nimArrIdx):
shallowCopy(x, y)
type
Slice*[T] = object ## builtin slice type
a*, b*: T ## the bounds
Slice*[T, U] = object ## builtin slice type
a*: T ## the lower bound (inclusive)
b*: U ## the upper bound (inclusive)
when defined(nimalias):
{.deprecated: [TSlice: Slice].}
proc `..`*[T](a, b: T): Slice[T] {.noSideEffect, inline, magic: "DotDot".} =
proc `..`*[T, U](a: T, b: U): Slice[T, U] {.noSideEffect, inline, magic: "DotDot".} =
## `slice`:idx: operator that constructs an interval ``[a, b]``, both `a`
## and `b` are inclusive. Slices can also be used in the set constructor
## and in ordinal case statements, but then they are special-cased by the
@ -322,7 +323,7 @@ proc `..`*[T](a, b: T): Slice[T] {.noSideEffect, inline, magic: "DotDot".} =
result.a = a
result.b = b
proc `..`*[T](b: T): Slice[T] {.noSideEffect, inline, magic: "DotDot".} =
proc `..`*[T](b: T): Slice[T, T] {.noSideEffect, inline, magic: "DotDot".} =
## `slice`:idx: operator that constructs an interval ``[default(T), b]``
result.b = b
@ -1168,7 +1169,7 @@ proc contains*[T](x: set[T], y: T): bool {.magic: "InSet", noSideEffect.}
## is achieved by reversing the parameters for ``contains``; ``in`` then
## passes its arguments in reverse order.
proc contains*[T](s: Slice[T], value: T): bool {.noSideEffect, inline.} =
proc contains*[T](s: Slice[T, T], value: T): bool {.noSideEffect, inline.} =
## Checks if `value` is within the range of `s`; returns true iff
## `value >= s.a and value <= s.b`
##
@ -2087,7 +2088,7 @@ proc clamp*[T](x, a, b: T): T =
if x > b: return b
return x
proc len*[T: Ordinal](x: Slice[T]): int {.noSideEffect, inline.} =
proc len*[T: Ordinal](x: Slice[T, T]): int {.noSideEffect, inline.} =
## length of ordinal slice, when x.b < x.a returns zero length
##
## .. code-block:: Nim
@ -2155,7 +2156,7 @@ iterator items*(E: typedesc[enum]): E =
for v in low(E)..high(E):
yield v
iterator items*[T](s: Slice[T]): T =
iterator items*[T](s: Slice[T, T]): T =
## iterates over the slice `s`, yielding each value between `s.a` and `s.b`
## (inclusively).
for x in s.a..s.b:
@ -3414,14 +3415,13 @@ proc `/`*(x, y: int): float {.inline, noSideEffect.} =
## integer division that results in a float.
result = toFloat(x) / toFloat(y)
proc `^`*[T](x: int; y: openArray[T]): int {.noSideEffect, magic: "Roof".}
proc `^`*(x: int): int {.noSideEffect, magic: "Roof".} =
type
BackwardsIndex* = distinct int ## type that is constructed by ``^`` for
## reversed array accesses.
template `^`*(x: int): BackwardsIndex = BackwardsIndex(x)
## builtin `roof`:idx: operator that can be used for convenient array access.
## ``a[^x]`` is rewritten to ``a[a.len-x]``. However currently the ``a``
## expression must not have side effects for this to compile. Note that since
## this is a builtin, it automatically works for all kinds of
## overloaded ``[]`` or ``[]=`` accessors.
discard
## ``a[^x]`` is a shortcut for ``a[a.len-x]``.
template `..^`*(a, b: untyped): untyped =
## a shortcut for '.. ^' to avoid the common gotcha that a space between
@ -3453,17 +3453,20 @@ template spliceImpl(s, a, L, b: untyped): untyped =
# fill the hole:
for i in 0 ..< b.len: s[a+i] = b[i]
template `^^`(s, i: untyped): untyped =
(when i is BackwardsIndex: s.len - int(i) else: int(i))
when hasAlloc or defined(nimscript):
proc `[]`*(s: string, x: Slice[int]): string {.inline.} =
proc `[]`*[T, U](s: string, x: Slice[T, U]): string {.inline.} =
## slice operation for strings.
## returns the inclusive range [s[x.a], s[x.b]]:
##
## .. code-block:: nim
## var s = "abcdef"
## assert s[1..3] == "bcd"
result = s.substr(x.a, x.b)
result = s.substr(s ^^ x.a, s ^^ x.b)
proc `[]=`*(s: var string, x: Slice[int], b: string) =
proc `[]=`*[T, U](s: var string, x: Slice[T, U], b: string) =
## slice assignment for strings. If
## ``b.len`` is not exactly the number of elements that are referred to
## by `x`, a `splice`:idx: is performed:
@ -3472,75 +3475,69 @@ when hasAlloc or defined(nimscript):
## var s = "abcdef"
## s[1 .. ^2] = "xyz"
## assert s == "axyzf"
var a = x.a
var L = x.b - a + 1
var a = s ^^ x.a
var L = (s ^^ x.b) - a + 1
if L == b.len:
for i in 0..<L: s[i+a] = b[i]
else:
spliceImpl(s, a, L, b)
proc `[]`*[Idx, T](a: array[Idx, T], x: Slice[int]): seq[T] =
proc `[]`*[Idx, T, U, V](a: array[Idx, T], x: Slice[U, V]): seq[T] =
## slice operation for arrays.
## returns the inclusive range [a[x.a], a[x.b]]:
##
## .. code-block:: nim
## var a = [1,2,3,4]
## assert a[0..2] == @[1,2,3]
when low(a) < 0:
{.error: "Slicing for arrays with negative indices is unsupported.".}
var L = x.b - x.a + 1
let xa = a ^^ x.a
let L = (a ^^ x.b) - xa + 1
result = newSeq[T](L)
for i in 0..<L: result[i] = a[i + x.a]
for i in 0..<L: result[i] = a[Idx(i + xa + int low(a))]
proc `[]=`*[Idx, T](a: var array[Idx, T], x: Slice[int], b: openArray[T]) =
proc `[]=`*[Idx, T, U, V](a: var array[Idx, T], x: Slice[U, V], b: openArray[T]) =
## slice assignment for arrays.
when low(a) < 0:
{.error: "Slicing for arrays with negative indices is unsupported.".}
var L = x.b - x.a + 1
let xa = a ^^ x.a
let L = (a ^^ x.b) - xa + 1
if L == b.len:
for i in 0..<L: a[i+x.a] = b[i]
for i in 0..<L: a[Idx(i + xa + int low(a))] = b[i]
else:
sysFatal(RangeError, "different lengths for slice assignment")
proc `[]`*[Idx, T](a: array[Idx, T], x: Slice[Idx]): seq[T] =
## slice operation for arrays.
var L = ord(x.b) - ord(x.a) + 1
newSeq(result, L)
for i in 0..<L:
result[i] = a[Idx(ord(x.a) + i)]
proc `[]=`*[Idx, T](a: var array[Idx, T], x: Slice[Idx], b: openArray[T]) =
## slice assignment for arrays.
var L = ord(x.b) - ord(x.a) + 1
if L == b.len:
for i in 0..<L:
a[Idx(ord(x.a) + i)] = b[i]
else:
sysFatal(RangeError, "different lengths for slice assignment")
proc `[]`*[T](s: seq[T], x: Slice[int]): seq[T] =
proc `[]`*[T, U, V](s: seq[T], x: Slice[U, V]): seq[T] =
## slice operation for sequences.
## returns the inclusive range [s[x.a], s[x.b]]:
##
## .. code-block:: nim
## var s = @[1,2,3,4]
## assert s[0..2] == @[1,2,3]
var a = x.a
var L = x.b - a + 1
let a = s ^^ x.a
let L = (s ^^ x.b) - a + 1
newSeq(result, L)
for i in 0 ..< L: result[i] = s[i + a]
proc `[]=`*[T](s: var seq[T], x: Slice[int], b: openArray[T]) =
proc `[]=`*[T, U, V](s: var seq[T], x: Slice[U, V], b: openArray[T]) =
## slice assignment for sequences. If
## ``b.len`` is not exactly the number of elements that are referred to
## by `x`, a `splice`:idx: is performed.
var a = x.a
var L = x.b - a + 1
let a = s ^^ x.a
let L = (s ^^ x.b) - a + 1
if L == b.len:
for i in 0 ..< L: s[i+a] = b[i]
else:
spliceImpl(s, a, L, b)
proc `[]`*[T](s: seq[T]; i: BackwardsIndex): T = s[s.len - int(i)]
proc `[]`*[Idx, T](a: array[Idx, T]; i: BackwardsIndex): T =
a[Idx(a.len - int(i) + int low(a))]
proc `[]`*(s: string; i: BackwardsIndex): char = s[s.len - int(i)]
proc `[]`*[T](s: var seq[T]; i: BackwardsIndex; x: T) =
s[s.len - int(i)] = x
proc `[]`*[Idx, T](a: var array[Idx, T]; i: BackwardsIndex; x: T) =
a[Idx(a.len - int(i) + int low(a))] = x
proc `[]`*(s: var string; i: BackwardsIndex; x: char) =
s[s.len - int(i)] = x
proc slurp*(filename: string): string {.magic: "Slurp".}
## This is an alias for `staticRead <#staticRead>`_.