-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIterDup.hx
More file actions
49 lines (44 loc) · 1.43 KB
/
IterDup.hx
File metadata and controls
49 lines (44 loc) · 1.43 KB
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
package finitudeiterable;
/**
* IterDup, an iterator that duplicates entries of an iterable n times.
*
* @example
* using IterDup;
* trace( [0,1,2].dup(2) );
*
* // Or:
* for (i in new IterDup([0,1,2], 2)) trace(i); // 0, 0, 1, 1, 2, 2
* @endexample
*
* .dup(0) is eq to an IterVoid()
* .dup(1) is eq to not using dup()
* .dup(2) is eq to dup(), duplicating each entry
* .dup(n) produces each entry n times.
*
* Negative n are forbidden at this time and throw exception.
*/
class IterDup<T> {
var a : Iterable<T>; ///< Iter to dup
var n : Int; ///< n
var it : Iterator<T>; ///< a's iter
var c : Int; ///< dups done
var t : T;
/** @throw if count < 0 (reserved) */
public function new( itr:Iterable<T>, count:Int=2 ) {
a = itr;
n = count;
if (n < 0) throw "fkwr2 reserved";
c = 0;
it = a.iterator();
if (it.hasNext()) t = it.next();
}
public inline static function dup<T>( itr:Iterable<T>, count:Int=2 ) : IterableIterator<T>
return new IterDup(itr, count);
public inline function hasNext() return n > 0 && (c < n || it.hasNext());
public function next() {
if (c++ < n) return t;
c = 1;
return t = it.next();
}
public function iterator():IterableIterator<T> return new IterDup<T>(a, n);
}