This commit is contained in:
Andreas Rumpf 2017-10-30 14:45:57 +01:00
commit b14cc1e3b2
8 changed files with 65 additions and 38 deletions

View file

@ -308,14 +308,12 @@ when defined(nimArrIdx):
shallowCopy(x, y)
type
Slice*[T, U] = object ## builtin slice type
HSlice*[T, U] = object ## "heterogenous" slice type
a*: T ## the lower bound (inclusive)
b*: U ## the upper bound (inclusive)
Slice*[T] = HSlice[T, T] ## an alias for ``HSlice[T, T]``
when defined(nimalias):
{.deprecated: [TSlice: Slice].}
proc `..`*[T, U](a: T, b: U): Slice[T, U] {.noSideEffect, inline, magic: "DotDot".} =
proc `..`*[T, U](a: T, b: U): HSlice[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
@ -323,7 +321,7 @@ proc `..`*[T, U](a: T, b: U): Slice[T, U] {.noSideEffect, inline, magic: "DotDot
result.a = a
result.b = b
proc `..`*[T](b: T): Slice[int, T] {.noSideEffect, inline, magic: "DotDot".} =
proc `..`*[T](b: T): HSlice[int, T] {.noSideEffect, inline, magic: "DotDot".} =
## `slice`:idx: operator that constructs an interval ``[default(T), b]``
result.b = b
@ -1169,7 +1167,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, T], value: T): bool {.noSideEffect, inline.} =
proc contains*[T](s: HSlice[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`
##
@ -2088,7 +2086,7 @@ proc clamp*[T](x, a, b: T): T =
if x > b: return b
return x
proc len*[T: Ordinal](x: Slice[T, T]): int {.noSideEffect, inline.} =
proc len*[T: Ordinal](x: HSlice[T, T]): int {.noSideEffect, inline.} =
## length of ordinal slice, when x.b < x.a returns zero length
##
## .. code-block:: Nim
@ -2156,7 +2154,7 @@ iterator items*(E: typedesc[enum]): E =
for v in low(E)..high(E):
yield v
iterator items*[T](s: Slice[T, T]): T =
iterator items*[T](s: HSlice[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:
@ -3457,7 +3455,7 @@ template `^^`(s, i: untyped): untyped =
(when i is BackwardsIndex: s.len - int(i) else: int(i))
when hasAlloc or defined(nimscript):
proc `[]`*[T, U](s: string, x: Slice[T, U]): string {.inline.} =
proc `[]`*[T, U](s: string, x: HSlice[T, U]): string {.inline.} =
## slice operation for strings.
## returns the inclusive range [s[x.a], s[x.b]]:
##
@ -3466,7 +3464,7 @@ when hasAlloc or defined(nimscript):
## assert s[1..3] == "bcd"
result = s.substr(s ^^ x.a, s ^^ x.b)
proc `[]=`*[T, U](s: var string, x: Slice[T, U], b: string) =
proc `[]=`*[T, U](s: var string, x: HSlice[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:
@ -3482,7 +3480,7 @@ when hasAlloc or defined(nimscript):
else:
spliceImpl(s, a, L, b)
proc `[]`*[Idx, T, U, V](a: array[Idx, T], x: Slice[U, V]): seq[T] =
proc `[]`*[Idx, T, U, V](a: array[Idx, T], x: HSlice[U, V]): seq[T] =
## slice operation for arrays.
## returns the inclusive range [a[x.a], a[x.b]]:
##
@ -3494,7 +3492,7 @@ proc `[]`*[Idx, T, U, V](a: array[Idx, T], x: Slice[U, V]): seq[T] =
result = newSeq[T](L)
for i in 0..<L: result[i] = a[Idx(i + xa + int low(a))]
proc `[]=`*[Idx, T, U, V](a: var array[Idx, T], x: Slice[U, V], b: openArray[T]) =
proc `[]=`*[Idx, T, U, V](a: var array[Idx, T], x: HSlice[U, V], b: openArray[T]) =
## slice assignment for arrays.
let xa = a ^^ x.a
let L = (a ^^ x.b) - xa + 1
@ -3503,7 +3501,7 @@ proc `[]=`*[Idx, T, U, V](a: var array[Idx, T], x: Slice[U, V], b: openArray[T])
else:
sysFatal(RangeError, "different lengths for slice assignment")
proc `[]`*[T, U, V](s: seq[T], x: Slice[U, V]): seq[T] =
proc `[]`*[T, U, V](s: openArray[T], x: HSlice[U, V]): seq[T] =
## slice operation for sequences.
## returns the inclusive range [s[x.a], s[x.b]]:
##
@ -3515,7 +3513,7 @@ proc `[]`*[T, U, V](s: seq[T], x: Slice[U, V]): seq[T] =
newSeq(result, L)
for i in 0 ..< L: result[i] = s[i + a]
proc `[]=`*[T, U, V](s: var seq[T], x: Slice[U, V], b: openArray[T]) =
proc `[]=`*[T, U, V](s: var seq[T], x: HSlice[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.
@ -3526,12 +3524,16 @@ proc `[]=`*[T, U, V](s: var seq[T], x: Slice[U, V], b: openArray[T]) =
else:
spliceImpl(s, a, L, b)
proc `[]`*[T](s: seq[T]; i: BackwardsIndex): T = s[s.len - int(i)]
proc `[]`*[T](s: openArray[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) =
proc `[]`*[T](s: var openArray[T]; i: BackwardsIndex): var T = s[s.len - int(i)]
proc `[]`*[Idx, T](a: var array[Idx, T]; i: BackwardsIndex): var T =
a[Idx(a.len - int(i) + int low(a))]
proc `[]=`*[T](s: var openArray[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