make countup et al take only a single type parameter in order to prevent type guesses

This commit is contained in:
Andreas Rumpf 2017-11-02 19:02:08 +01:00
commit eadba5a345
3 changed files with 73 additions and 35 deletions

View file

@ -17,7 +17,7 @@ type
proc newMersenneTwister*(seed: uint32): MersenneTwister = proc newMersenneTwister*(seed: uint32): MersenneTwister =
result.index = 0 result.index = 0
result.mt[0] = seed result.mt[0] = seed
for i in 1..623'u32: for i in 1'u32 .. 623'u32:
result.mt[i] = (0x6c078965'u32 * (result.mt[i-1] xor (result.mt[i-1] shr 30'u32)) + i) result.mt[i] = (0x6c078965'u32 * (result.mt[i-1] xor (result.mt[i-1] shr 30'u32)) + i)
proc generateNumbers(m: var MersenneTwister) = proc generateNumbers(m: var MersenneTwister) =

View file

@ -563,12 +563,12 @@ macro scanp*(input, idx: typed; pattern: varargs[untyped]): bool =
of nnkCurlyExpr: of nnkCurlyExpr:
if it.len == 3 and it[1].kind == nnkIntLit and it[2].kind == nnkIntLit: if it.len == 3 and it[1].kind == nnkIntLit and it[2].kind == nnkIntLit:
var h = newTree(nnkPar, it[0]) var h = newTree(nnkPar, it[0])
for count in 2..it[1].intVal: h.add(it[0]) for count in 2i64 .. it[1].intVal: h.add(it[0])
for count in it[1].intVal .. it[2].intVal-1: h.add(newTree(nnkPrefix, ident"?", it[0])) for count in it[1].intVal .. it[2].intVal-1: h.add(newTree(nnkPrefix, ident"?", it[0]))
result = atm(h, input, idx, attached) result = atm(h, input, idx, attached)
elif it.len == 2 and it[1].kind == nnkIntLit: elif it.len == 2 and it[1].kind == nnkIntLit:
var h = newTree(nnkPar, it[0]) var h = newTree(nnkPar, it[0])
for count in 2..it[1].intVal: h.add(it[0]) for count in 2i64 .. it[1].intVal: h.add(it[0])
result = atm(h, input, idx, attached) result = atm(h, input, idx, attached)
else: else:
error("invalid pattern") error("invalid pattern")

View file

@ -1985,7 +1985,8 @@ iterator countdown*[T](a, b: T, step = 1): T {.inline.} =
yield res yield res
dec(res, step) dec(res, step)
iterator countup*[T](a, b: T, step = 1): T {.inline.} = when defined(nimNewRoof):
iterator countup*[T](a, b: T, step = 1): T {.inline.} =
## Counts from ordinal value `a` up to `b` (inclusive) with the given ## Counts from ordinal value `a` up to `b` (inclusive) with the given
## step count. `S`, `T` may be any ordinal type, `step` may only ## step count. `S`, `T` may be any ordinal type, `step` may only
## be positive. **Note**: This fails to count to ``high(int)`` if T = int for ## be positive. **Note**: This fails to count to ``high(int)`` if T = int for
@ -2001,7 +2002,7 @@ iterator countup*[T](a, b: T, step = 1): T {.inline.} =
yield res yield res
inc(res, step) inc(res, step)
iterator `..`*[T](a, b: T): T {.inline.} = iterator `..`*[T](a, b: T): T {.inline.} =
## An alias for `countup`. ## An alias for `countup`.
when T is IntLikeForCount: when T is IntLikeForCount:
var res = int(a) var res = int(a)
@ -2013,6 +2014,36 @@ iterator `..`*[T](a, b: T): T {.inline.} =
while res <= b: while res <= b:
yield res yield res
inc(res) inc(res)
else:
iterator countup*[S, T](a: S, b: T, step = 1): T {.inline.} =
## Counts from ordinal value `a` up to `b` (inclusive) with the given
## step count. `S`, `T` may be any ordinal type, `step` may only
## be positive. **Note**: This fails to count to ``high(int)`` if T = int for
## efficiency reasons.
when T is IntLikeForCount:
var res = int(a)
while res <= int(b):
yield T(res)
inc(res, step)
else:
var res: T = T(a)
while res <= b:
yield res
inc(res, step)
iterator `..`*[S, T](a: S, b: T): T {.inline.} =
## An alias for `countup`.
when T is IntLikeForCount:
var res = int(a)
while res <= int(b):
yield T(res)
inc(res)
else:
var res: T = T(a)
while res <= b:
yield res
inc(res)
iterator `||`*[S, T](a: S, b: T, annotation=""): T {. iterator `||`*[S, T](a: S, b: T, annotation=""): T {.
inline, magic: "OmpParFor", sideEffect.} = inline, magic: "OmpParFor", sideEffect.} =
@ -3433,7 +3464,14 @@ template `..<`*(a, b: untyped): untyped =
## a shortcut for 'a..pred(b)'. ## a shortcut for 'a..pred(b)'.
a .. pred(b) a .. pred(b)
iterator `..<`*[T](a, b: T): T = when defined(nimNewRoof):
iterator `..<`*[T](a, b: T): T =
var i = T(a)
while i < b:
yield i
inc i
else:
iterator `..<`*[S, T](a: S, b: T): T =
var i = T(a) var i = T(a)
while i < b: while i < b:
yield i yield i