~eda-qa/dhlib/main

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
/* <license>
 * This file is part of the dis-Emi-A HaXe Library. Copyright © edA-qa mort-ora-y
 * For full copyright and license information please refer to doc/license.txt.
 * </license> 
 */
/**
 * This provides a simple iterator base which allows a class to simply
 * define "_next" to fully implement the iterator.  This is desired since
 * in many cases the normal structure of hasNext, and next, is simply
 * difficult, or costly to do.
 *
 * NOTE: This has been done as a Mixin for two reasons:
 *	-function calls can be expensive in iterators (see Matrix) and thus need
 * to be avoided
 * -HaXe was giving me problems trying to override _next with a specific
 * type (quite annoying actually)
 */

	/*hidden*/ var __haveNext : Bool;
	/*hidden*/ var __next : SimpleIteratorType;

	public function MixinSimpleIterator()
	{
		__haveNext = false;
	}
	
	//Avoid expensive function call, mainly for perf critical areas
	define(`__ensureNext',`
		if( !__haveNext )
		{
			__next = _next();
			__haveNext = __next != null;
		}
	')
	
	public function hasNext() : Bool
	{
		__ensureNext
		return __haveNext;
	}
	
	public function next() : SimpleIteratorType
	{
		__ensureNext
		__haveNext = false;
		return __next;
	}