~extremepopcorn/dhlib/dhlib_ep

« back to all changes in this revision

Viewing changes to haxe/SWFCompSpeed.hx

  • Committer: edA-qa mort-ora-y
  • Date: 2010-02-16 05:36:32 UTC
  • Revision ID: eda-qa@disemia.com-20100216053632-60lt7fndfi3fgblw
first

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* <license>
 
2
 * This file is part of the dis-Emi-A HaXe Library. Copyright (c) edA-qa mort-ora-y
 
3
 * For full copyright and license information please refer to doc/license.txt.
 
4
 * </license> 
 
5
 */
 
6
typedef FastList<T> = Array<T>;
 
7
 
 
8
/**
 
9
 * Simple array utilities that seem to be missing from haxe...?
 
10
 */
 
11
class MatrixUtil
 
12
{
 
13
        static public function seedFill_8<T>( colorMap : Matrix<T>, 
 
14
                seeds : FastList<MatPoint>, match1 : T, fill : T )
 
15
        {
 
16
                //PERFORMANCE: In AS3 Compiler Array is far faster than List! (TODO: choose based on compiler options?)
 
17
                var sX = new FastList<Int>();
 
18
                var sY = new FastList<Int>();
 
19
                for( i in seeds )
 
20
                {
 
21
                        sX.push( i.x );
 
22
                        sY.push( i.y );
 
23
                        colorMap.set( i.x, i.y, fill ); //pre-fill these (to avoid duplicate fills later) (TODO: promote to others)
 
24
                }
 
25
                
 
26
                while( sX.length > 0 )
 
27
                {
 
28
                        var ccX = sX.pop();
 
29
                        var ccY = sY.pop();
 
30
                        
 
31
                        colorMap.set( ccX, ccY, fill );
 
32
                        for( off in 0... 8 )
 
33
                        {
 
34
                                //PERFORANCE: No static arrays, and minimize if comparisons
 
35
                                var nx = ccX;
 
36
                                var ny = ccY;
 
37
                                
 
38
                                switch( off )
 
39
                                {
 
40
                                        case 0:
 
41
                                                nx--;
 
42
                                                ny--;
 
43
                                        case 1:
 
44
                                                nx--;
 
45
                                        case 2:
 
46
                                                nx--;
 
47
                                                ny++;
 
48
                                        case 3:
 
49
                                                ny--;
 
50
                                        case 4:
 
51
                                                ny++;
 
52
                                        case 5:
 
53
                                                nx++;
 
54
                                                ny--;
 
55
                                        case 6:
 
56
                                                nx++;
 
57
                                        case 7:
 
58
                                                nx++;
 
59
                                                ny++;
 
60
                                }
 
61
         
 
62
                                
 
63
                                //Strangely, this is better than putting continues in the switch
 
64
                                //statement on possibly invalid items (at least for size 8, 4 the continues were fine)
 
65
                                if( nx >= 0 && ny >= 0
 
66
                                        && nx < colorMap.size.x && ny < colorMap.size.y
 
67
                                        && colorMap.get(nx,ny) == match1
 
68
                                        ) 
 
69
                                {
 
70
                                        colorMap.set( nx, ny, fill );
 
71
                                        sX.push( nx );
 
72
                                        sY.push( ny );
 
73
                                }
 
74
                        }
 
75
                }       //while(open)
 
76
        }
 
77
        
 
78
}
 
79
 
 
80
class MatPoint
 
81
{
 
82
        public var x : Int;
 
83
        public var y : Int;
 
84
        
 
85
        public function new( ax : Int, ay : Int )
 
86
        {
 
87
                x = ax;
 
88
                y = ay;
 
89
        }
 
90
}
 
91
 
 
92
/**
 
93
 * A matrix class...
 
94
 */
 
95
class Matrix<T>
 
96
{
 
97
        private var data : Array<T>;
 
98
        public var sizeX(default,null) : Int;
 
99
        public var sizeY(default,null) : Int;
 
100
        
 
101
        public function get( x : Int, y : Int ) : T
 
102
        {
 
103
                //assert validIndex(x,y);
 
104
                return data[ y*sizeX + x ];
 
105
        }
 
106
        
 
107
        public function set( x : Int, y : Int, val : T )
 
108
        {
 
109
                //assert validIndex(x, y );
 
110
                data[y*sizeX + x] = val;
 
111
        }
 
112
        
 
113
        //only static functions can create a new matrix
 
114
        private function new()
 
115
        {
 
116
        }
 
117
        
 
118
        /**
 
119
         * Creates a matrix with the default value given
 
120
         */
 
121
        static public function create<Q>( cols : Int, rows : Int, init : Q ) : Matrix<Q>
 
122
        {
 
123
                var m = new Matrix<Q>();
 
124
                m.size.x = cols;
 
125
                m.size.y = rows;
 
126
                m.data = new Array<Q>();
 
127
                
 
128
                for( i in 0...m.size.y )
 
129
                        for( j in 0...m.size.x )
 
130
                                m.data.push( init );
 
131
                                
 
132
                return m;
 
133
        }
 
134
}
 
135
 
 
136
 
 
137
class SWFCompSpeed
 
138
{
 
139
        static var matSize = 50;
 
140
        static var loop = 10;
 
141
        static function main()
 
142
        {
 
143
                var norm = Matrix.create( matSize, matSize, 0 );
 
144
                var lastTime = flash.Lib.getTimer();
 
145
                for( i in 0...loop )
 
146
                {
 
147
                        var lo = new FastList<MatPoint>();
 
148
                        lo.push( new MatPoint( 0, 0 ) );
 
149
                        MatrixUtil.seedFill_8( norm, lo, i, i+1 );
 
150
                }
 
151
 
 
152
                //show time
 
153
                var nTime = flash.Lib.getTimer();
 
154
                trace( "time: " +  (nTime - lastTime) );
 
155
        }
 
156
}