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,34 +1985,65 @@ 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):
## Counts from ordinal value `a` up to `b` (inclusive) with the given iterator countup*[T](a, b: T, step = 1): T {.inline.} =
## step count. `S`, `T` may be any ordinal type, `step` may only ## Counts from ordinal value `a` up to `b` (inclusive) with the given
## be positive. **Note**: This fails to count to ``high(int)`` if T = int for ## step count. `S`, `T` may be any ordinal type, `step` may only
## efficiency reasons. ## be positive. **Note**: This fails to count to ``high(int)`` if T = int for
when T is IntLikeForCount: ## efficiency reasons.
var res = int(a) when T is IntLikeForCount:
while res <= int(b): var res = int(a)
yield T(res) while res <= int(b):
inc(res, step) yield T(res)
else: inc(res, step)
var res: T = T(a) else:
while res <= b: var res: T = T(a)
yield res while res <= b:
inc(res, step) yield res
inc(res, step)
iterator `..`*[T](a, 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)
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 `..`*[T](a, 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,11 +3464,18 @@ 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):
var i = T(a) iterator `..<`*[T](a, b: T): T =
while i < b: var i = T(a)
yield i while i < b:
inc i yield i
inc i
else:
iterator `..<`*[S, T](a: S, b: T): T =
var i = T(a)
while i < b:
yield i
inc i
template spliceImpl(s, a, L, b: untyped): untyped = template spliceImpl(s, a, L, b: untyped): untyped =
# make room for additional elements or cut: # make room for additional elements or cut: