| Filename | /homes/dcw/src/perl/coloroids_solver/NewBoard.pm |
| Statements | Executed 137237976 statements in 52.4s |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 14145230 | 4 | 1 | 21.3s | 21.3s | NewBoard::cellstatus |
| 5584 | 3 | 2 | 12.7s | 27.5s | NewBoard::changeregioncolour |
| 5585 | 4 | 3 | 12.3s | 25.1s | NewBoard::extendregion |
| 2811488 | 1 | 1 | 4.51s | 4.51s | NewBoard::markcellcolour |
| 674063 | 7 | 2 | 1.09s | 1.09s | NewBoard::cell |
| 247875 | 6 | 1 | 397ms | 397ms | NewBoard::markcellstatus |
| 57 | 2 | 1 | 166ms | 166ms | NewBoard::as_string |
| 5579 | 2 | 2 | 8.74ms | 8.74ms | NewBoard::solved |
| 6757 | 5 | 2 | 8.26ms | 8.26ms | NewBoard::region |
| 1 | 1 | 1 | 2.34ms | 2.47ms | NewBoard::BEGIN@25 |
| 1 | 1 | 1 | 2.10ms | 2.29ms | NewBoard::BEGIN@24 |
| 1 | 1 | 1 | 1.91ms | 2.06ms | NewBoard::BEGIN@26 |
| 1 | 1 | 1 | 1.49ms | 1.49ms | NewBoard::CORE:open (opcode) |
| 1 | 1 | 1 | 1.01ms | 1.01ms | NewBoard::BEGIN@17 |
| 1 | 1 | 1 | 547µs | 5.29ms | NewBoard::mkboard |
| 1 | 1 | 1 | 539µs | 7.36ms | NewBoard::newfromfile |
| 1 | 1 | 1 | 407µs | 2.44ms | NewBoard::BEGIN@23 |
| 1 | 1 | 1 | 231µs | 231µs | NewBoard::BEGIN@43 |
| 1 | 1 | 1 | 225µs | 3.30ms | NewBoard::BEGIN@21 |
| 37 | 3 | 1 | 27µs | 27µs | NewBoard::CORE:readline (opcode) |
| 1 | 1 | 1 | 11µs | 35µs | NewBoard::BEGIN@28 |
| 1 | 1 | 1 | 9µs | 81µs | NewBoard::BEGIN@30 |
| 1 | 1 | 1 | 6µs | 6µs | NewBoard::CORE:close (opcode) |
| 1 | 1 | 1 | 6µs | 99µs | NewBoard::BEGIN@18 |
| 1 | 1 | 1 | 5µs | 33µs | NewBoard::BEGIN@17.3 |
| 1 | 1 | 1 | 5µs | 23µs | NewBoard::BEGIN@19 |
| 1 | 1 | 1 | 1µs | 1µs | NewBoard::boardsize |
| 0 | 0 | 0 | 0s | 0s | NewBoard::as_plain_string |
| 0 | 0 | 0 | 0s | 0s | NewBoard::new |
| 0 | 0 | 0 | 0s | 0s | NewBoard::newrandom |
| 0 | 0 | 0 | 0s | 0s | NewBoard::size |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package NewBoard; | ||||
| 2 | |||||
| 3 | # | ||||
| 4 | # new version of board object... represents much more information about | ||||
| 5 | # the state of the game so we can lookup more answers and spend less | ||||
| 6 | # time calculating. | ||||
| 7 | # a NewBoard stores the following: | ||||
| 8 | # GRID: ref to array of row | ||||
| 9 | # [row = ref to even-length pair array (colour,status)] | ||||
| 10 | # R: no of rows | ||||
| 11 | # C: no of columns | ||||
| 12 | # SIZE: R * C: no of cells in board | ||||
| 13 | # REGSIZE: Region size (no of cells in region) | ||||
| 14 | # ADJCOLBAG: bag of colours of cells immediately | ||||
| 15 | # adjacent to region. | ||||
| 16 | |||||
| 17 | 4 | 64µs | 3 | 1.07ms | use v5.12; # spent 1.01ms making 1 call to NewBoard::BEGIN@17
# spent 33µs making 1 call to NewBoard::BEGIN@17.3
# spent 27µs making 1 call to feature::import |
| 18 | 2 | 21µs | 2 | 192µs | # spent 99µs (6+93) within NewBoard::BEGIN@18 which was called:
# once (6µs+93µs) by main::BEGIN@12 at line 18 # spent 99µs making 1 call to NewBoard::BEGIN@18
# spent 93µs making 1 call to Function::Parameters::import |
| 19 | 2 | 19µs | 2 | 40µs | # spent 23µs (5+18) within NewBoard::BEGIN@19 which was called:
# once (5µs+18µs) by main::BEGIN@12 at line 19 # spent 23µs making 1 call to NewBoard::BEGIN@19
# spent 18µs making 1 call to Exporter::import |
| 20 | |||||
| 21 | 2 | 85µs | 2 | 6.32ms | # spent 3.30ms (225µs+3.07) within NewBoard::BEGIN@21 which was called:
# once (225µs+3.07ms) by main::BEGIN@12 at line 21 # spent 3.30ms making 1 call to NewBoard::BEGIN@21
# spent 3.03ms making 1 call to parent::import |
| 22 | |||||
| 23 | 2 | 86µs | 2 | 4.45ms | # spent 2.44ms (407µs+2.04) within NewBoard::BEGIN@23 which was called:
# once (407µs+2.04ms) by main::BEGIN@12 at line 23 # spent 2.44ms making 1 call to NewBoard::BEGIN@23
# spent 2.01ms making 1 call to lib::import |
| 24 | 2 | 1.73ms | 2 | 2.33ms | # spent 2.29ms (2.10+198µs) within NewBoard::BEGIN@24 which was called:
# once (2.10ms+198µs) by main::BEGIN@12 at line 24 # spent 2.29ms making 1 call to NewBoard::BEGIN@24
# spent 34µs making 1 call to Exporter::import |
| 25 | 2 | 1.65ms | 2 | 2.51ms | # spent 2.47ms (2.34+127µs) within NewBoard::BEGIN@25 which was called:
# once (2.34ms+127µs) by main::BEGIN@12 at line 25 # spent 2.47ms making 1 call to NewBoard::BEGIN@25
# spent 37µs making 1 call to Exporter::import |
| 26 | 2 | 1.62ms | 1 | 2.06ms | # spent 2.06ms (1.91+152µs) within NewBoard::BEGIN@26 which was called:
# once (1.91ms+152µs) by main::BEGIN@12 at line 26 # spent 2.06ms making 1 call to NewBoard::BEGIN@26 |
| 27 | |||||
| 28 | 2 | 41µs | 2 | 59µs | # spent 35µs (11+24) within NewBoard::BEGIN@28 which was called:
# once (11µs+24µs) by main::BEGIN@12 at line 28 # spent 35µs making 1 call to NewBoard::BEGIN@28
# spent 24µs making 1 call to overload::import |
| 29 | |||||
| 30 | # spent 81µs (9+72) within NewBoard::BEGIN@30 which was called:
# once (9µs+72µs) by main::BEGIN@12 at line 35 | ||||
| 31 | 1 | 72µs | COLOUR => 0, # spent 72µs making 1 call to constant::import | ||
| 32 | STATUS => 1, | ||||
| 33 | INREGION => 1, | ||||
| 34 | ADJACENT => 2, | ||||
| 35 | 2 | 58µs | 1 | 81µs | }; # spent 81µs making 1 call to NewBoard::BEGIN@30 |
| 36 | |||||
| 37 | |||||
| 38 | # | ||||
| 39 | # my $board = Board->new( @boardarray ); | ||||
| 40 | # Build a new board object and return it. | ||||
| 41 | # @boardarray is an array of array refs of cells (colours) | ||||
| 42 | # | ||||
| 43 | 2 | 1.82ms | 3 | 6.60ms | # spent 231µs within NewBoard::BEGIN@43 which was called:
# once (231µs+0s) by main::BEGIN@12 at line 43 # spent 3.29ms making 1 call to utf8::AUTOLOAD
# spent 3.08ms making 1 call to utf8::SWASHNEW
# spent 231µs making 1 call to NewBoard::BEGIN@43 |
| 44 | { | ||||
| 45 | return mkboard( $class, @boardarray ); | ||||
| 46 | } | ||||
| 47 | |||||
| 48 | |||||
| 49 | # | ||||
| 50 | # my $board = Board->newfromfile( $filename ); | ||||
| 51 | # Build a new board object by opening and reading a file. | ||||
| 52 | # | ||||
| 53 | # spent 7.36ms (539µs+6.82) within NewBoard::newfromfile which was called:
# once (539µs+6.82ms) by main::RUNTIME at line 34 of testnewboardincnply | ||||
| 54 | 9 | 1.58ms | { | ||
| 55 | 1 | 1.49ms | open( my $in, '<', $filename ) || die; # spent 1.49ms making 1 call to NewBoard::CORE:open | ||
| 56 | 1 | 9µs | $_ = <$in>; # spent 9µs making 1 call to NewBoard::CORE:readline | ||
| 57 | my( $w, $h ) = split( /\s+/, $_, 2 ); | ||||
| 58 | my @array; | ||||
| 59 | 1 | 700ns | while( <$in> ) # spent 700ns making 1 call to NewBoard::CORE:readline | ||
| 60 | { | ||||
| 61 | 70 | 484µs | chomp; | ||
| 62 | 35 | 16µs | push @array, [ split( /\s+/, $_ ) ]; # spent 16µs making 35 calls to NewBoard::CORE:readline, avg 471ns/call | ||
| 63 | } | ||||
| 64 | 1 | 6µs | close( $in ); # spent 6µs making 1 call to NewBoard::CORE:close | ||
| 65 | 1 | 5.29ms | return mkboard( $class, @array ); # spent 5.29ms making 1 call to NewBoard::mkboard | ||
| 66 | 1 | 100ns | } | ||
| 67 | |||||
| 68 | |||||
| 69 | # | ||||
| 70 | # my $board = Board->newrandom( $w, $h, @colours ); | ||||
| 71 | # Build a new board object of width $w and height $h | ||||
| 72 | # with random colours from @colours. | ||||
| 73 | # | ||||
| 74 | method newrandom( $class: $w, $h, @colours ) | ||||
| 75 | { | ||||
| 76 | my @array; | ||||
| 77 | foreach my $i (1..$h) | ||||
| 78 | { | ||||
| 79 | my @row = map { $colours[int rand(@colours)] } 1..$w; | ||||
| 80 | push @array, \@row; | ||||
| 81 | } | ||||
| 82 | return mkboard( $class, @array ); | ||||
| 83 | } | ||||
| 84 | |||||
| 85 | |||||
| 86 | # | ||||
| 87 | # my $board = mkboard( $class, @array ); | ||||
| 88 | # Real constructor: turn @array (2-d array of colours) | ||||
| 89 | # into a NewBoard object | ||||
| 90 | # | ||||
| 91 | # spent 5.29ms (547µs+4.75) within NewBoard::mkboard which was called:
# once (547µs+4.75ms) by NewBoard::newfromfile at line 65 | ||||
| 92 | 15 | 536µs | { | ||
| 93 | my $rows = @array; | ||||
| 94 | my $cols = @{$array[0]}; | ||||
| 95 | my @grid = map { [ map { $_, 0 } @$_ ] } @array; | ||||
| 96 | #die Dumper \@grid; | ||||
| 97 | # make initial state: region is 0,0, adjacents are 0,1 and 1,0 | ||||
| 98 | my $regionsize = 1; | ||||
| 99 | 1 | 26µs | my $adjcolbag = Sorthash->new; # spent 26µs making 1 call to Sorthash::new | ||
| 100 | my $board = bless { | ||||
| 101 | R => $rows, | ||||
| 102 | C => $cols, | ||||
| 103 | GRID => \@grid, | ||||
| 104 | SIZE => $rows * $cols, | ||||
| 105 | REGSIZE => $regionsize, | ||||
| 106 | ADJCOLBAG => $adjcolbag, | ||||
| 107 | }, $class; | ||||
| 108 | 1 | 5µs | $board->markcellstatus( 0, 0, INREGION ); # mark 0,0 as inregion # spent 5µs making 1 call to NewBoard::markcellstatus | ||
| 109 | 1 | 2µs | $board->markcellstatus( 0, 1, ADJACENT ); # mark 0,1 as adjacent # spent 2µs making 1 call to NewBoard::markcellstatus | ||
| 110 | 1 | 2µs | $board->markcellstatus( 1, 0, ADJACENT ); # mark 1,0 as adjacent # spent 2µs making 1 call to NewBoard::markcellstatus | ||
| 111 | 1 | 3µs | $adjcolbag->{ $board->cell( 0, 1 ) }++; # spent 3µs making 1 call to NewBoard::cell | ||
| 112 | 1 | 2µs | $adjcolbag->{ $board->cell( 1, 0 ) }++; # spent 2µs making 1 call to NewBoard::cell | ||
| 113 | #die Dumper $board; | ||||
| 114 | 1 | 4.70ms | $board->extendregion; # spent 4.70ms making 1 call to NewBoard::extendregion | ||
| 115 | #die Dumper $board; | ||||
| 116 | #die $board; | ||||
| 117 | return $board; | ||||
| 118 | 1 | 0s | } | ||
| 119 | |||||
| 120 | |||||
| 121 | # | ||||
| 122 | # my $newboard = $board->clone; | ||||
| 123 | # Clone a new board object and return it. | ||||
| 124 | # Provided by 'Clone' parent class. | ||||
| 125 | # | ||||
| 126 | #DCWmethod clone | ||||
| 127 | #DCW{ | ||||
| 128 | #DCW my $adjcolbag = Sorthash->new; | ||||
| 129 | #DCW %$adjcolbag = %{$self->{ADJCOLBAG}}; | ||||
| 130 | #DCW my @grid = map { | ||||
| 131 | #DCW [ map { Tuple->new(@$_) } @$_ ] | ||||
| 132 | #DCW } @{$self->{GRID}}; | ||||
| 133 | #DCW my $newboard = bless { | ||||
| 134 | #DCW R => $self->{R}, | ||||
| 135 | #DCW C => $self->{C}, | ||||
| 136 | #DCW GRID => \@grid, | ||||
| 137 | #DCW SIZE => $self->{SIZE}, | ||||
| 138 | #DCW REGSIZE => $self->{REGSIZE}, | ||||
| 139 | #DCW ADJCOLBAG => $adjcolbag, | ||||
| 140 | #DCW }, ref($self); | ||||
| 141 | #DCW} | ||||
| 142 | |||||
| - - | |||||
| 145 | # | ||||
| 146 | # my $colour = $board->cell( $r, $c ); | ||||
| 147 | # Extract a specific cell colour ($r,$c) from the board | ||||
| 148 | # | ||||
| 149 | # spent 1.09s within NewBoard::cell which was called 674063 times, avg 2µs/call:
# 382848 times (617ms+0s) by NewBoard::extendregion at line 285, avg 2µs/call
# 180541 times (295ms+0s) by NewBoard::extendregion at line 329, avg 2µs/call
# 103908 times (168ms+0s) by NewBoard::extendregion at line 303, avg 2µs/call
# 5585 times (10.8ms+0s) by NewBoard::extendregion at line 272, avg 2µs/call
# 1179 times (3.06ms+0s) by main::fulllist_all_nply at line 65 of NewIncNPlyPicker.pm, avg 3µs/call
# once (3µs+0s) by NewBoard::mkboard at line 111
# once (2µs+0s) by NewBoard::mkboard at line 112 | ||||
| 150 | 4044378 | 1.41s | { | ||
| 151 | my $pos = $c<<1; | ||||
| 152 | my $row = $self->{GRID}[$r]; | ||||
| 153 | my $colour = $row->[$pos]; | ||||
| 154 | return $colour; | ||||
| 155 | } | ||||
| 156 | |||||
| 157 | |||||
| 158 | # | ||||
| 159 | # my $status = $board->cellstatus( $r, $c ); | ||||
| 160 | # Extract the status (0, INREGION or ADJACENT) of cell ($r,$c) | ||||
| 161 | # | ||||
| 162 | # spent 21.3s within NewBoard::cellstatus which was called 14145230 times, avg 2µs/call:
# 6841625 times (10.3s+0s) by NewBoard::extendregion at line 282, avg 2µs/call
# 6840400 times (10.3s+0s) by NewBoard::changeregioncolour at line 364, avg 2µs/call
# 359297 times (562ms+0s) by NewBoard::extendregion at line 326, avg 2µs/call
# 103908 times (161ms+0s) by NewBoard::extendregion at line 304, avg 2µs/call | ||||
| 163 | 84871380 | 27.7s | { | ||
| 164 | my $row = $self->{GRID}[$r]; | ||||
| 165 | my $pos = $c<<1|1; | ||||
| 166 | my $status = $row->[$pos]; | ||||
| 167 | return $status; | ||||
| 168 | 1 | 0s | } | ||
| 169 | |||||
| 170 | |||||
| 171 | # | ||||
| 172 | # $board->markcellcolour( $r, $c, $col ); | ||||
| 173 | # Mark ($r,$c)' cell as INREGION and colour $col | ||||
| 174 | # | ||||
| 175 | # spent 4.51s within NewBoard::markcellcolour which was called 2811488 times, avg 2µs/call:
# 2811488 times (4.51s+0s) by NewBoard::changeregioncolour at line 365, avg 2µs/call | ||||
| 176 | 14057440 | 5.73s | { | ||
| 177 | my $row = $self->{GRID}[$r]; | ||||
| 178 | my $pos = $c<<1; | ||||
| 179 | $row->[$pos] = $col; | ||||
| 180 | #print "debug: mcc($r,$c,$col): row $r [$pos] = $col\n"; | ||||
| 181 | } | ||||
| 182 | |||||
| 183 | |||||
| 184 | # | ||||
| 185 | # $board->markcellstatus( $r, $c, $status ); | ||||
| 186 | # Mark ($r,$c)' cellstatus as status | ||||
| 187 | # | ||||
| 188 | # spent 397ms within NewBoard::markcellstatus which was called 247875 times, avg 2µs/call:
# 92115 times (146ms+0s) by NewBoard::extendregion at line 313, avg 2µs/call
# 78893 times (134ms+0s) by NewBoard::extendregion at line 289, avg 2µs/call
# 76864 times (117ms+0s) by NewBoard::extendregion at line 342, avg 2µs/call
# once (5µs+0s) by NewBoard::mkboard at line 108
# once (2µs+0s) by NewBoard::mkboard at line 109
# once (2µs+0s) by NewBoard::mkboard at line 110 | ||||
| 189 | 1239375 | 515ms | { | ||
| 190 | my $row = $self->{GRID}[$r]; | ||||
| 191 | my $pos = $c<<1|1; | ||||
| 192 | $row->[$pos] = $status; | ||||
| 193 | #die "debug: markcellstatus($r, $c, $status): row[$pos] = $status\n"; | ||||
| 194 | 1 | 0s | } | ||
| 195 | |||||
| 196 | |||||
| 197 | # | ||||
| 198 | # my( $rows, $cols) = $board->size(); | ||||
| 199 | # Extract the size (rows x cols) of $board. | ||||
| 200 | # | ||||
| 201 | method size | ||||
| 202 | { | ||||
| 203 | return ( $self->{R}, $self->{C} ); | ||||
| 204 | } | ||||
| 205 | |||||
| 206 | |||||
| 207 | # | ||||
| 208 | # my $isover = $board->solved; | ||||
| 209 | # Is the game over? | ||||
| 210 | # | ||||
| 211 | method solved | ||||
| 212 | 11158 | 11.4ms | # spent 8.74ms within NewBoard::solved which was called 5579 times, avg 2µs/call:
# 5522 times (8.18ms+0s) by main::fulllist_all_nply at line 101 of NewIncNPlyPicker.pm, avg 1µs/call
# 57 times (557µs+0s) by main::solve_game at line 29 of NewSolveGame.pm, avg 10µs/call | ||
| 213 | return $self->{SIZE} == $self->{REGSIZE}; | ||||
| 214 | 1 | 0s | } | ||
| 215 | |||||
| 216 | |||||
| 217 | # | ||||
| 218 | # my $size = $board->boardsize; | ||||
| 219 | # how many cells are in the board? | ||||
| 220 | method boardsize | ||||
| 221 | 2 | 3µs | # spent 1µs within NewBoard::boardsize which was called:
# once (1µs+0s) by main::incnply_pick_best at line 192 of NewIncNPlyPicker.pm | ||
| 222 | return $self->{SIZE}; | ||||
| 223 | } | ||||
| 224 | |||||
| 225 | |||||
| 226 | # | ||||
| 227 | # my( $ninregion, $adjcolbag ) = $board->region; | ||||
| 228 | # return information about the region: | ||||
| 229 | # the regionsize and the adjacent colour bag | ||||
| 230 | # | ||||
| 231 | method region | ||||
| 232 | 13514 | 11.4ms | # spent 8.26ms within NewBoard::region which was called 6757 times, avg 1µs/call:
# 5515 times (6.60ms+0s) by main::fulllist_all_nply at line 104 of NewIncNPlyPicker.pm, avg 1µs/call
# 1179 times (1.50ms+0s) by main::fulllist_all_nply at line 68 of NewIncNPlyPicker.pm, avg 1µs/call
# 56 times (153µs+0s) by main::solve_game at line 31 of NewSolveGame.pm, avg 3µs/call
# 6 times (8µs+0s) by main::fulllist_all_nply at line 79 of NewIncNPlyPicker.pm, avg 1µs/call
# once (1µs+0s) by main::solve_game at line 42 of NewSolveGame.pm | ||
| 233 | return ( $self->{REGSIZE}, $self->{ADJCOLBAG} ); | ||||
| 234 | 1 | 0s | } | ||
| 235 | |||||
| 236 | |||||
| 237 | # | ||||
| 238 | # foreach_cell( $self, $property, $callback ); | ||||
| 239 | # For each cell in board that has $property (INREGION|ADJACENT) | ||||
| 240 | # invoke the coderef $callback with the row and the column and | ||||
| 241 | # the tuple. | ||||
| 242 | # | ||||
| 243 | #DCWmethod foreach_cell( $property, $callback ) | ||||
| 244 | #DCW{ | ||||
| 245 | #DCW my $rows = $self->{R}; | ||||
| 246 | #DCW my $cols = $self->{C}; | ||||
| 247 | #DCW my $g = $self->{GRID}; | ||||
| 248 | #DCW foreach my $r (0..$rows-1) | ||||
| 249 | #DCW { | ||||
| 250 | #DCW my $row = $self->{GRID}[$r]; | ||||
| 251 | #DCW foreach my $c (0..$cols-1) | ||||
| 252 | #DCW { | ||||
| 253 | #DCW my $colour = lc( substr($row, 2*$c, 1) ); | ||||
| 254 | #DCW my $tuple = $gr->[$c]; | ||||
| 255 | #DCW $callback->( $r, $c, $tuple ) if | ||||
| 256 | #DCW $tuple->[STATUS] == $property; | ||||
| 257 | #DCW } | ||||
| 258 | #DCW } | ||||
| 259 | #DCW} | ||||
| 260 | |||||
| 261 | 1 | 4µs | 4 | 43µs | my @delta = ( tuple(0,1), tuple(1,0), tuple(0,-1), tuple(-1,0) ); # spent 43µs making 4 calls to Tuple::tuple, avg 11µs/call |
| 262 | |||||
| 263 | # | ||||
| 264 | # $board->extendregion; | ||||
| 265 | # After changing the board, locate all cells that should join the | ||||
| 266 | # region, ie. which are now of the same colour as the region. | ||||
| 267 | # Start by locating the existing adjacent cells of the region colour, | ||||
| 268 | # then flood fill out from there. also updates the adjacent information. | ||||
| 269 | # | ||||
| 270 | method extendregion | ||||
| 271 | 50265 | 27.5ms | # spent 25.1s (12.3+12.8) within NewBoard::extendregion which was called 5585 times, avg 4.50ms/call:
# 5522 times (12.1s+12.7s) by main::fulllist_all_nply at line 98 of NewIncNPlyPicker.pm, avg 4.50ms/call
# 56 times (129ms+134ms) by main::solve_game at line 38 of NewSolveGame.pm, avg 4.71ms/call
# 6 times (10.3ms+11.1ms) by main::fulllist_all_nply at line 78 of NewIncNPlyPicker.pm, avg 3.57ms/call
# once (2.28ms+2.42ms) by NewBoard::mkboard at line 114 | ||
| 272 | 5585 | 10.8ms | my $regioncolour = $self->cell(0,0); # spent 10.8ms making 5585 calls to NewBoard::cell, avg 2µs/call | ||
| 273 | my $adjcolbag = $self->{ADJCOLBAG}; | ||||
| 274 | my $rows = $self->{R}; | ||||
| 275 | my $cols = $self->{C}; | ||||
| 276 | my @todo; | ||||
| 277 | # foreach adjacent cell of the regioncolour | ||||
| 278 | foreach my $r (0..$rows-1) | ||||
| 279 | { | ||||
| 280 | 195475 | 116ms | foreach my $c (0..$cols-1) | ||
| 281 | { | ||||
| 282 | 13683250 | 6.49s | 6841625 | 10.3s | my $status = $self->cellstatus( $r, $c ); # spent 10.3s making 6841625 calls to NewBoard::cellstatus, avg 2µs/call |
| 283 | 923482 | 514ms | if( $status == ADJACENT ) | ||
| 284 | { | ||||
| 285 | 382848 | 617ms | my $colour = $self->cell( $r, $c ); # spent 617ms making 382848 calls to NewBoard::cell, avg 2µs/call | ||
| 286 | next unless $colour eq $regioncolour; | ||||
| 287 | 78893 | 286ms | push @todo, tuple($r,$c); # spent 286ms making 78893 calls to Tuple::tuple, avg 4µs/call | ||
| 288 | # no longer adjacent | ||||
| 289 | 78893 | 134ms | $self->markcellstatus( $r, $c, 0 ); # spent 134ms making 78893 calls to NewBoard::markcellstatus, avg 2µs/call | ||
| 290 | } | ||||
| 291 | } | ||||
| 292 | } | ||||
| 293 | #print "debug: extendregion, extracted adjacent cells and neutered them\n"; | ||||
| 294 | #print "board is now:\n$self\ntodo is ", join(',', @todo), "\n"; | ||||
| 295 | # now @todo is all adjacent cells of regioncolour | ||||
| 296 | delete $adjcolbag->{$regioncolour}; # nothing adjacent | ||||
| 297 | # adapted find linked colour code (aka flood fill) from here | ||||
| 298 | while( @todo ) | ||||
| 299 | { | ||||
| 300 | 899793 | 407ms | my $pair = shift @todo; | ||
| 301 | #DCWmy( $r, $c ) = $pair->detuple; | ||||
| 302 | my( $r, $c ) = @$pair; | ||||
| 303 | 103908 | 168ms | my $cellcolour = $self->cell( $r, $c ); # spent 168ms making 103908 calls to NewBoard::cell, avg 2µs/call | ||
| 304 | 103908 | 161ms | my $status = $self->cellstatus( $r, $c ); # spent 161ms making 103908 calls to NewBoard::cellstatus, avg 2µs/call | ||
| 305 | #print "debug: floodfill: cell $r,$c, colour $cellcolour, status $status\n"; | ||||
| 306 | die "extendregion: cell($r,$c), colour $cellcolour, ". | ||||
| 307 | "should be $regioncolour\n" unless | ||||
| 308 | $cellcolour eq $regioncolour; | ||||
| 309 | # mark this cell as in the region - skipping cells already | ||||
| 310 | # in the region (eg. one cell reachable in the same move | ||||
| 311 | # via two different paths) | ||||
| 312 | next if $status == INREGION; | ||||
| 313 | 92115 | 146ms | $self->markcellstatus( $r, $c, INREGION ); # spent 146ms making 92115 calls to NewBoard::markcellstatus, avg 2µs/call | ||
| 314 | $self->{REGSIZE}++; | ||||
| 315 | # look at NSEW neighbours | ||||
| 316 | foreach my $dp (@delta) | ||||
| 317 | { | ||||
| 318 | #DCWmy( $dr, $dc ) = $dp->detuple; | ||||
| 319 | 2553516 | 872ms | my( $dr, $dc ) = @$dp; | ||
| 320 | my $tr = $r + $dr; | ||||
| 321 | my $tc = $c + $dc; | ||||
| 322 | # are we off the edge? | ||||
| 323 | next if $tr < 0 || $tr >= $rows | ||||
| 324 | || $tc < 0 || $tc >= $cols; | ||||
| 325 | # have we already seen this cell? | ||||
| 326 | 359297 | 562ms | my $status = $self->cellstatus( $tr, $tc ); # spent 562ms making 359297 calls to NewBoard::cellstatus, avg 2µs/call | ||
| 327 | next if $status == INREGION; | ||||
| 328 | # ok, new cell.. process | ||||
| 329 | 180541 | 295ms | my $neighbourcolour = $self->cell($tr,$tc); # spent 295ms making 180541 calls to NewBoard::cell, avg 2µs/call | ||
| 330 | #print "debug: process neighbour cell ($tr,$tc), colour=$neighbourcolour\n"; | ||||
| 331 | 359284 | 140ms | if( $neighbourcolour eq $regioncolour ) | ||
| 332 | { | ||||
| 333 | 25015 | 89.1ms | push @todo, tuple($tr,$tc); # spent 89.1ms making 25015 calls to Tuple::tuple, avg 4µs/call | ||
| 334 | next unless $status == ADJACENT; | ||||
| 335 | # no longer ADJACENT | ||||
| 336 | $self->markcellstatus( $tr, $tc, 0 ); | ||||
| 337 | # nb: cells on todo list will end up INREGION | ||||
| 338 | $adjcolbag->{$regioncolour}--; | ||||
| 339 | } else | ||||
| 340 | { | ||||
| 341 | next if $status == ADJACENT; | ||||
| 342 | 76864 | 117ms | $self->markcellstatus( $tr, $tc, ADJACENT ); # spent 117ms making 76864 calls to NewBoard::markcellstatus, avg 2µs/call | ||
| 343 | $adjcolbag->{$neighbourcolour}++; | ||||
| 344 | } | ||||
| 345 | } | ||||
| 346 | } | ||||
| 347 | } | ||||
| 348 | |||||
| 349 | |||||
| 350 | # | ||||
| 351 | # $board->changeregioncolour( $newcolour ); | ||||
| 352 | # Change the colour of the whole region to $newcolour | ||||
| 353 | # NB: we do not extend the region at the end of this, | ||||
| 354 | # the caller should do that again. | ||||
| 355 | # | ||||
| 356 | # spent 27.5s (12.7+14.8) within NewBoard::changeregioncolour which was called 5584 times, avg 4.93ms/call:
# 5522 times (12.6s+14.6s) by main::fulllist_all_nply at line 97 of NewIncNPlyPicker.pm, avg 4.93ms/call
# 56 times (126ms+147ms) by main::solve_game at line 37 of NewSolveGame.pm, avg 4.88ms/call
# 6 times (18.0ms+22.8ms) by main::fulllist_all_nply at line 77 of NewIncNPlyPicker.pm, avg 6.80ms/call | ||||
| 357 | 27920 | 20.7ms | { | ||
| 358 | my $rows = $self->{R}; | ||||
| 359 | my $cols = $self->{C}; | ||||
| 360 | foreach my $r (0..$rows-1) | ||||
| 361 | { | ||||
| 362 | 195440 | 125ms | foreach my $c (0..$cols-1) | ||
| 363 | { | ||||
| 364 | 13680800 | 8.20s | 6840400 | 10.3s | my $status = $self->cellstatus( $r, $c ); # spent 10.3s making 6840400 calls to NewBoard::cellstatus, avg 2µs/call |
| 365 | 2811488 | 4.51s | $self->markcellcolour( $r, $c, $newcolour ) # spent 4.51s making 2811488 calls to NewBoard::markcellcolour, avg 2µs/call | ||
| 366 | if $status == INREGION; | ||||
| 367 | } | ||||
| 368 | } | ||||
| 369 | #print "debug crc board now $self\n"; | ||||
| 370 | 1 | 100ns | } | ||
| 371 | |||||
| 372 | |||||
| 373 | # | ||||
| 374 | # my $str = $board->as_string; | ||||
| 375 | # Generate a printable string form of a complete board | ||||
| 376 | # Return the printable firm of a board | ||||
| 377 | # | ||||
| 378 | method as_string | ||||
| 379 | 456 | 114ms | # spent 166ms within NewBoard::as_string which was called 57 times, avg 2.91ms/call:
# 56 times (165ms+0s) by main::solve_game at line 32 of NewSolveGame.pm, avg 2.94ms/call
# once (906µs+0s) by main::solve_game at line 43 of NewSolveGame.pm | ||
| 380 | my $rows = $self->{R}; | ||||
| 381 | my $cols = $self->{C}; | ||||
| 382 | my $str = "\t"; | ||||
| 383 | foreach my $col (0..$cols-1) | ||||
| 384 | { | ||||
| 385 | 1995 | 648µs | $str .= sprintf( "%2d", $col ); | ||
| 386 | } | ||||
| 387 | $str .= "\n"; | ||||
| 388 | foreach my $row (0..$rows-1) | ||||
| 389 | { | ||||
| 390 | 9975 | 2.92ms | $str .= sprintf( " %2d ", $row ); | ||
| 391 | my $rowarr = $self->{GRID}[$row]; | ||||
| 392 | my $pos = 0; | ||||
| 393 | foreach my $col (0..$cols-1) | ||||
| 394 | { | ||||
| 395 | 418950 | 48.3ms | my $colour = $rowarr->[$pos++]; | ||
| 396 | my $status = $rowarr->[$pos++]; | ||||
| 397 | my $extra = " "; | ||||
| 398 | $colour = uc($colour) if $status == INREGION; | ||||
| 399 | $extra = '+' if $status == ADJACENT; | ||||
| 400 | $str .= "$colour$extra"; | ||||
| 401 | } | ||||
| 402 | $str .= "\n"; | ||||
| 403 | } | ||||
| 404 | return $str; | ||||
| 405 | } | ||||
| 406 | |||||
| 407 | |||||
| 408 | # | ||||
| 409 | # my $str = $board->as_plain_string; | ||||
| 410 | # Build and return a printable plain string form of a board, | ||||
| 411 | # with no status information | ||||
| 412 | # | ||||
| 413 | method as_plain_string | ||||
| 414 | { | ||||
| 415 | my $rows = $self->{R}; | ||||
| 416 | my $cols = $self->{C}; | ||||
| 417 | my $str = "$rows $cols\n"; | ||||
| 418 | foreach my $row (0..$rows-1) | ||||
| 419 | { | ||||
| 420 | foreach my $col (0..$cols-1) | ||||
| 421 | { | ||||
| 422 | $str .= $self->cell($row,$col). " "; | ||||
| 423 | } | ||||
| 424 | $str .= "\n"; | ||||
| 425 | } | ||||
| 426 | return $str; | ||||
| 427 | 1 | 100ns | } | ||
| 428 | |||||
| 429 | |||||
| 430 | 1 | 5µs | 1; | ||
# spent 6µs within NewBoard::CORE:close which was called:
# once (6µs+0s) by NewBoard::newfromfile at line 64 | |||||
# spent 1.49ms within NewBoard::CORE:open which was called:
# once (1.49ms+0s) by NewBoard::newfromfile at line 55 | |||||
sub NewBoard::CORE:readline; # opcode |