~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
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
/* <license>
 * This file is part of the dis-Emi-A HaXe Library. Copyright (c) edA-qa mort-ora-y
 * For full copyright and license information please refer to doc/license.txt.
 * </license> 
 */
typedef FastList<T> = Array<T>;

/**
 * Simple array utilities that seem to be missing from haxe...?
 */
class MatrixUtil
{
	static public function seedFill_8<T>( colorMap : Matrix<T>, 
		seeds : FastList<MatPoint>, match1 : T, fill : T )
	{
		//PERFORMANCE: In AS3 Compiler Array is far faster than List! (TODO: choose based on compiler options?)
		var sX = new FastList<Int>();
		var sY = new FastList<Int>();
		for( i in seeds )
		{
			sX.push( i.x );
			sY.push( i.y );
			colorMap.set( i.x, i.y, fill );	//pre-fill these (to avoid duplicate fills later) (TODO: promote to others)
		}
		
		while( sX.length > 0 )
		{
			var ccX = sX.pop();
			var ccY = sY.pop();
			
			colorMap.set( ccX, ccY, fill );
			for( off in 0... 8 )
			{
				//PERFORANCE: No static arrays, and minimize if comparisons
				var nx = ccX;
				var ny = ccY;
				
				switch( off )
				{
					case 0:
						nx--;
						ny--;
					case 1:
						nx--;
					case 2:
						nx--;
						ny++;
					case 3:
						ny--;
					case 4:
						ny++;
					case 5:
						nx++;
						ny--;
					case 6:
						nx++;
					case 7:
						nx++;
						ny++;
				}
	 
				
				//Strangely, this is better than putting continues in the switch
				//statement on possibly invalid items (at least for size 8, 4 the continues were fine)
				if( nx >= 0 && ny >= 0
					&& nx < colorMap.size.x	&& ny < colorMap.size.y
					&& colorMap.get(nx,ny) == match1
					) 
				{
					colorMap.set( nx, ny, fill );
					sX.push( nx );
					sY.push( ny );
				}
			}
		}	//while(open)
	}
	
}

class MatPoint
{
	public var x : Int;
	public var y : Int;
	
	public function new( ax : Int, ay : Int )
	{
		x = ax;
		y = ay;
	}
}

/**
 * A matrix class...
 */
class Matrix<T>
{
	private var data : Array<T>;
	public var sizeX(default,null) : Int;
	public var sizeY(default,null) : Int;
	
	public function get( x : Int, y : Int ) : T
	{
		//assert validIndex(x,y);
		return data[ y*sizeX + x ];
	}
	
	public function set( x : Int, y : Int, val : T )
	{
		//assert validIndex(x, y );
		data[y*sizeX + x] = val;
	}
	
	//only static functions can create a new matrix
	private function new()
	{
	}
	
	/**
	 * Creates a matrix with the default value given
	 */
	static public function create<Q>( cols : Int, rows : Int, init : Q ) : Matrix<Q>
	{
		var m = new Matrix<Q>();
		m.size.x = cols;
		m.size.y = rows;
		m.data = new Array<Q>();
		
		for( i in 0...m.size.y )
			for( j in 0...m.size.x )
				m.data.push( init );
				
		return m;
	}
}


class SWFCompSpeed
{
	static var matSize = 50;
	static var loop = 10;
	static function main()
	{
		var norm = Matrix.create( matSize, matSize, 0 );
		var lastTime = flash.Lib.getTimer();
		for( i in 0...loop )
		{
			var lo = new FastList<MatPoint>();
			lo.push( new MatPoint( 0, 0 ) );
			MatrixUtil.seedFill_8( norm, lo, i, i+1 );
		}

		//show time
		var nTime = flash.Lib.getTimer();
		trace( "time: " +  (nTime - lastTime) );
	}
}