-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathIterableIterator.hx
More file actions
57 lines (50 loc) · 1.25 KB
/
IterableIterator.hx
File metadata and controls
57 lines (50 loc) · 1.25 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
50
51
52
53
54
55
56
57
package moon.data.iterators;
import moon.core.Seq;
/**
* An Iterator of Iterables.
* Also see IteratorIterator for an Iterator of Iterators.
*
* @author Munir Hussin
*/
class IterableIterator<T>
{
public var outer:Iterator<Iterable<T>>;
public var inner:Iterator<T>;
public function new(it:Iterator<Iterable<T>>)
{
outer = it;
nextOuter();
}
public static inline function of<T>(it:Seq<Seq<T>>):IterableIterator<T>
{
return new IterableIterator<T>(it.iterator());
}
private inline function nextOuter():Void
{
if (outer.hasNext())
inner = outer.next().iterator();
}
public function hasNext():Bool
{
// there's more in the current iterator, so its true
if (inner != null && inner.hasNext())
{
return true;
}
// no more in current iterator. are there more outer?
else if (outer.hasNext())
{
nextOuter();
return hasNext();
}
// no more outers and no more inners, so that's the end
else
{
return false;
}
}
public inline function next():T
{
return hasNext() ? inner.next() : null;
}
}