1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
when sizeof(int) <= 2:
type IntLikeForCount = int|int8|int16|char|bool|uint8|enum
else:
type IntLikeForCount = int|int8|int16|int32|char|bool|uint8|uint16|enum
iterator countdown*[T](a, b: T, step: Positive = 1): T {.inline.} =
## Counts from ordinal value `a` down to `b` (inclusive) with the given
## step count.
##
## `T` may be any ordinal type, `step` may only be positive.
##
## **Note**: This fails to count to `low(int)` if T = int for
## efficiency reasons.
runnableExamples:
import std/sugar
let x = collect(newSeq):
for i in countdown(7, 3):
i
assert x == @[7, 6, 5, 4, 3]
let y = collect(newseq):
for i in countdown(9, 2, 3):
i
assert y == @[9, 6, 3]
when T is (uint|uint64):
var res = a
while res >= b:
yield res
if res == b: break
dec(res, step)
elif T is IntLikeForCount and T is Ordinal:
var res = int(a)
while res >= int(b):
yield T(res)
dec(res, step)
else:
var res = a
while res >= b:
yield res
dec(res, step)
iterator countup*[T](a, b: T, step: Positive = 1): T {.inline.} =
## Counts from ordinal value `a` to `b` (inclusive) with the given
## step count.
##
## `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.
runnableExamples:
import std/sugar
let x = collect(newSeq):
for i in countup(3, 7):
i
assert x == @[3, 4, 5, 6, 7]
let y = collect(newseq):
for i in countup(2, 9, 3):
i
assert y == @[2, 5, 8]
mixin inc
when T is IntLikeForCount and T is Ordinal:
var res = int(a)
while res <= int(b):
yield T(res)
inc(res, step)
else:
var res = a
while res <= b:
yield res
inc(res, step)
iterator `..`*[T](a, b: T): T {.inline.} =
## An alias for `countup(a, b, 1)`.
##
## See also:
## * [..<](#..<.i,T,T)
runnableExamples:
import std/sugar
let x = collect(newSeq):
for i in 3 .. 7:
i
assert x == @[3, 4, 5, 6, 7]
mixin inc
when T is IntLikeForCount and T is Ordinal:
var res = int(a)
while res <= int(b):
yield T(res)
inc(res)
else:
var res = a
while res <= b:
yield res
inc(res)
template dotdotImpl(t) {.dirty.} =
iterator `..`*(a, b: t): t {.inline.} =
## A type specialized version of `..` for convenience so that
## mixing integer types works better.
##
## See also:
## * [..<](#..<.i,T,T)
var res = a
while res <= b:
yield res
inc(res)
dotdotImpl(int64)
dotdotImpl(int32)
dotdotImpl(uint64)
dotdotImpl(uint32)
iterator `..<`*[T](a, b: T): T {.inline.} =
mixin inc
var i = a
while i < b:
yield i
inc i
template dotdotLessImpl(t) {.dirty.} =
iterator `..<`*(a, b: t): t {.inline.} =
## A type specialized version of `..<` for convenience so that
## mixing integer types works better.
var res = a
while res < b:
yield res
inc(res)
dotdotLessImpl(int64)
dotdotLessImpl(int32)
dotdotLessImpl(uint64)
dotdotLessImpl(uint32)
iterator `||`*[S, T](a: S, b: T, annotation: static string = "parallel for"): T {.
inline, magic: "OmpParFor", sideEffect.} =
## OpenMP parallel loop iterator. Same as `..` but the loop may run in parallel.
##
## `annotation` is an additional annotation for the code generator to use.
## The default annotation is `parallel for`.
## Please refer to the `OpenMP Syntax Reference
## <https://www.openmp.org/wp-content/uploads/OpenMP-4.5-1115-CPP-web.pdf>`_
## for further information.
##
## Note that the compiler maps that to
## the `#pragma omp parallel for` construct of `OpenMP`:idx: and as
## such isn't aware of the parallelism in your code! Be careful! Later
## versions of `||` will get proper support by Nim's code generator
## and GC.
discard
iterator `||`*[S, T](a: S, b: T, step: Positive, annotation: static string = "parallel for"): T {.
inline, magic: "OmpParFor", sideEffect.} =
## OpenMP parallel loop iterator with stepping.
## Same as `countup` but the loop may run in parallel.
##
## `annotation` is an additional annotation for the code generator to use.
## The default annotation is `parallel for`.
## Please refer to the `OpenMP Syntax Reference
## <https://www.openmp.org/wp-content/uploads/OpenMP-4.5-1115-CPP-web.pdf>`_
## for further information.
##
## Note that the compiler maps that to
## the `#pragma omp parallel for` construct of `OpenMP`:idx: and as
## such isn't aware of the parallelism in your code! Be careful! Later
## versions of `||` will get proper support by Nim's code generator
## and GC.
discard
|