-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRange.hx
315 lines (267 loc) · 7.42 KB
/
Range.hx
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package moon.core;
import moon.core.Range.MappedRange;
/**
* Iterator for python-esque Range.
* Note that Range is an Iterable, not an Iterator.
* Since it's an Iterable, once a range is created,
* you can use it multiple times.
*
* The output of Range.from(a, b, c) should match
* python's range(a, b, c) exactly.
*
* Usage:
* for (i in Range.from(5, 19, 3))
* trace(i); // [5, 8, 11, 14, 17]
*
* for (i in Range.count(4))
* trace(i); // [0, 1, 2, 3]
*
* @author Munir Hussin
*/
class Range
{
public var length(default, null):Int;
private var start:Int;
private var stop:Int;
private var step:Int;
/*==================================================
Constructors
==================================================*/
public function new(start:Int=0, stop:Int, step:Int=1)
{
this.start = start;
this.stop = stop;
this.step = step;
this.length = Math.ceil((stop - start) / step);
}
/**
* generate range from [0 ... n-1]
* count(3) ==> 0, 1, 2
*/
public static inline function count(stop:Int, step:Int=1):Range
{
return step < 0 ?
new Range(stop, 0, step):
new Range(0, stop, step);
}
/**
* generate inclusive range from [0 ... n]
* zeroTo(3) ==> 0, 1, 2, 3
*/
public static inline function zeroTo(stop:Int, step:Int=1):Range
{
return step < 0 ?
new Range(stop, -1, step):
new Range(0, stop + 1, step);
}
/**
* generate range from [m ... n-1] with step amount
* from(3, 14, 3) ==> 3, 6, 9, 12
*/
public static inline function from(start:Int, stop:Int, step:Int=1):Range
{
return new Range(start, stop, step);
}
/**
* generate range that goes in decreasing order. step is negated for you.
* for (x in Range.downTo(10, 2))
*/
public static inline function downTo(start:Int, stop:Int, step:Int=1):Range
{
return new Range(start, stop, -step);
}
/**
* negates step if start is bigger than stop
*/
public static inline function auto(start:Int, stop:Int, step:Int=1):Range
{
return start < stop ?
new Range(start, stop, step):
new Range(start, stop, -step);
}
/*==================================================
Methods
==================================================*/
public function iterator():Iterator<Int>
{
return step > 0 ?
new RangeForwardIterator(start, stop, step):
new RangeBackwardIterator(start, stop, step);
}
private function index(i:Int):Int
{
var idx = i < 0 ? i + length : i;
if (idx < 0 || idx >= length)
throw 'Index $i out of bounds';
return idx;
}
private inline function pos(i:Int):Int
{
return start + step * i;
}
public inline function get(i:Int):Int
{
return pos(index(i));
}
public function slice(range:Range):Range
{
return new Range(pos(range.start), pos(range.stop), range.step * step);
}
/*==================================================
Other stuff
==================================================*/
public inline function map<U>(fn:Int->U):MappedRange<U>
{
// T->U ==> Int->U
return new MappedRange<U>(this, fn);
}
/**
* Creates a copy of `this` Range object.
*/
public function copy():Range
{
return new Range(start, stop, step);
}
/*==================================================
Conversions
==================================================*/
public function toArray():Array<Int>
{
return [for (x in iterator()) x];
}
}
/**
* Range's map returns a MappedRange, which works like a range,
* but its integer index can be mapped to a function returning
* some other type.
*
* Usage:
* var chars = "abcdefghijklmnopqrstuvwxyz";
* var r = Range.zeroTo(26, 5)
* .map(function(x) return chars.substr(x, 1))
* .map(function(x) return x.charCodeAt(0));
*
* for (c in r)
* trace(c); // [97, 102, 107, 112, 117, 122]
*/
class MappedRange<T>
{
public var length(get, never):Int;
public var transform:Int->T;
public var range:Range;
/*==================================================
Constructors
==================================================*/
public function new(range:Range, transform:Int->T)
{
this.range = range;
this.transform = transform;
}
private inline function get_length():Int
{
return range.length;
}
/*==================================================
Methods
==================================================*/
public function iterator():Iterator<T>
{
return new MappedRangeIterator<T>(this);
}
public inline function get(i:Int):T
{
return transform(range.get(i));
}
public inline function slice(range:Range):MappedRange<T>
{
return new MappedRange<T>(this.range.slice(range), transform);
}
/*==================================================
Other stuff
==================================================*/
public inline function map<U>(fn:T->U):MappedRange<U>
{
// T->U ==> Int->U
return new MappedRange<U>(range, function(i:Int) return fn(transform(i)));
}
public inline function mapi<U>(fn:Int->T->U):MappedRange<U>
{
return new MappedRange<U>(range, function(i:Int) return fn(i, transform(i)));
}
/**
* Creates a copy of `this` Range object.
*/
public function copy():MappedRange<T>
{
return new MappedRange<T>(range.copy(), transform);
}
/*==================================================
Conversions
==================================================*/
public function toArray():Array<T>
{
return [for (x in iterator()) x];
}
}
class RangeForwardIterator
{
public var i:Int;
public var stop:Int;
public var step:Int;
public inline function new(i:Int, stop:Int, step:Int)
{
this.i = i;
this.stop = stop;
this.step = step;
}
public inline function hasNext():Bool
{
return i < stop;
}
public inline function next():Int
{
var t = i;
i += step;
return t;
}
}
class RangeBackwardIterator
{
public var i:Int;
public var stop:Int;
public var step:Int;
public inline function new(i:Int, stop:Int, step:Int)
{
this.i = i;
this.stop = stop;
this.step = step;
}
public inline function hasNext():Bool
{
return i > stop;
}
public inline function next():Int
{
var t = i;
i += step;
return t;
}
}
class MappedRangeIterator<T>
{
public var tx:Int->T;
public var it:Iterator<Int>;
public inline function new(mrange:MappedRange<T>)
{
this.it = mrange.range.iterator();
this.tx = mrange.transform;
}
public inline function hasNext():Bool
{
return it.hasNext();
}
public inline function next():T
{
return tx(it.next());
}
}