| 1 | package Games::Go::Cinderblock::NodeSet; | ||||||||||
| 2 | 6 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | use overload | ||||||||||
| 5 | 6 | ▶ | '==' => \&test_equality, | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | '!=' => \&test_inequality, | ||||||||||
| 7 | 6 | '""' => \&stringify; | |||||||||
| 8 | |||||||||||
| 9 | has _nodes => ( | ||||||||||
| 10 | isa => 'HashRef', | ||||||||||
| 11 | is => 'ro', | ||||||||||
| 12 | 175 | ▶ | default => sub{{}}, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 6 | ); | |||||||||
| 14 | 6 | has rulemap => ( | |||||||||
| 15 | isa => 'Games::Go::Cinderblock::Rulemap', | ||||||||||
| 16 | is => 'ro', #shouldn't change. | ||||||||||
| 17 | required => 1, | ||||||||||
| 18 | ); | ||||||||||
| 19 | |||||||||||
| 20 | 3 | ▶ | sub copy{ | ||||||||
|
Subroutine
copy: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 21 | 3 | my $self = shift; | |||||||||
| 22 | 3 | my %set = %{$self->_nodes}; | |||||||||
| 23 | 3 | return Games::Go::Cinderblock::NodeSet->new( | |||||||||
| 24 | _nodes => \%set, | ||||||||||
| 25 | rulemap => $self->rulemap, | ||||||||||
| 26 | ); | ||||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 76 | ▶ | sub nodes{ | ||||||||
|
Subroutine
nodes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 30 | 76 | my $self = shift; | |||||||||
| 31 | 76 | return values %{$self->_nodes}; | |||||||||
| 32 | } | ||||||||||
| 33 | 185 | ▶ | sub remove{ | ||||||||
|
Subroutine
remove: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 34 | 185 | my $self = shift; | |||||||||
| 35 | 185 | ▶ | return unless $_[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 185 | ▶ | if(ref($_[0]) eq 'Games::Go::Cinderblock::NodeSet'){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 74 | my $ns = shift; | |||||||||
| 38 | 74 | for my $nid (keys %{$ns->_nodes}){ | |||||||||
| 39 | 144 | delete $self->_nodes->{$nid} | |||||||||
| 40 | } | ||||||||||
| 41 | } | ||||||||||
| 42 | else{ | ||||||||||
| 43 | 111 | for my $node (@_){ | |||||||||
| 44 | 111 | my $node_id = $self->rulemap->node_to_id($node); | |||||||||
| 45 | 111 | delete $self->_nodes->{$node_id}; | |||||||||
| 46 | } | ||||||||||
| 47 | } | ||||||||||
| 48 | } | ||||||||||
| 49 | 513 | ▶ | sub add { #stones or a nodeset. | ||||||||
|
Subroutine
add: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 50 | 513 | my $self = shift; | |||||||||
| 51 | 513 | ▶ | return unless $_[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 513 | ▶ | if(ref($_[0]) eq 'Games::Go::Cinderblock::NodeSet'){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 37 | my $ns = shift; | |||||||||
| 54 | 37 | for my $nid (keys %{$ns->_nodes}){ | |||||||||
| 55 | 68 | $self->_nodes->{$nid} = $ns->_nodes->{$nid}; | |||||||||
| 56 | } | ||||||||||
| 57 | } | ||||||||||
| 58 | else{ | ||||||||||
| 59 | 476 | for my $node (@_){ | |||||||||
| 60 | 516 | my $node_id = $self->rulemap->node_to_id($node); | |||||||||
| 61 | 516 | $self->_nodes->{$node_id} = $node; | |||||||||
| 62 | } | ||||||||||
| 63 | } | ||||||||||
| 64 | } | ||||||||||
| 65 | 202 | ▶ | sub has_node{ | ||||||||
|
Subroutine
has_node: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 66 | 202 | my ($self,$node) = @_; | |||||||||
| 67 | 202 | my $node_id = $self->rulemap->node_to_id($node); | |||||||||
| 68 | 202 | ▶ | return 1 if defined $self->_nodes->{$node_id}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 114 | return 0; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 6 | ▶ | sub test_equality{ | ||||||||
|
Subroutine
test_equality: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||
| 73 | 6 | my $self = shift; | |||||||||
| 74 | 6 | my $other = shift; | |||||||||
| 75 | 6 | ▶ | Carp::confess unless ref($other) eq 'Games::Go::Cinderblock::NodeSet'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 6 | ▶ | return 0 if scalar(keys %{$self->_nodes}) != scalar(keys %{$other->_nodes}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 6 | for my $key (keys %{$self->_nodes}){ | |||||||||
| 78 | 18 | ▶ | return 0 unless defined $other->_nodes->{$key}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | } | ||||||||||
| 80 | 4 | return 1; | |||||||||
| 81 | } | ||||||||||
| 82 | 2 | ▶ | sub test_inequality{ | ||||||||
|
Subroutine
test_inequality: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 83 | 2 | my $self = shift; | |||||||||
| 84 | 2 | my $other = shift; | |||||||||
| 85 | 2 | ▶ | return 0 if $self == $other; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 2 | return 1; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 129 | ▶ | sub stringify{ | ||||||||
|
Subroutine
stringify: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 90 | 129 | my $self = shift; | |||||||||
| 91 | 129 | return '<' . join( ',', keys %{$self->_nodes}) . '>'; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 206 | ▶ | sub count{ | ||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 95 | 206 | my $self = shift; | |||||||||
| 96 | 206 | return scalar keys %{$self->_nodes}; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | # return an arbitrary element. not random. | ||||||||||
| 100 | 130 | ▶ | sub choose{ | ||||||||
|
Subroutine
choose: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 101 | 130 | my $self = shift; | |||||||||
| 102 | 130 | my ($key,$val) = each %{$self->_nodes}; | |||||||||
| 103 | 130 | return $val; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 20 | ▶ | sub adjacent{ | ||||||||
|
Subroutine
adjacent: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 107 | 20 | my $self = shift; | |||||||||
| 108 | 20 | my $res = $self->rulemap->nodeset; | |||||||||
| 109 | 20 | for my $n ($self->nodes){ | |||||||||
| 110 | 29 | my @adj = $self->rulemap->adjacent_nodes($n); | |||||||||
| 111 | 29 | $res->add(@adj); | |||||||||
| 112 | } | ||||||||||
| 113 | 20 | $res->remove($self); | |||||||||
| 114 | 20 | return $res; | |||||||||
| 115 | } | ||||||||||
| 116 | 9 | ▶ | sub union{ | ||||||||
|
Subroutine
union: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 117 | 9 | my ($self,$other) = @_; | |||||||||
| 118 | 9 | my $result = $self->rulemap->nodeset; | |||||||||
| 119 | 9 | for my $n ($self->nodes){ | |||||||||
| 120 | 12 | $result->add($n); | |||||||||
| 121 | } | ||||||||||
| 122 | 9 | for my $n ($other->nodes){ | |||||||||
| 123 | 9 | $result->add($n); | |||||||||
| 124 | } | ||||||||||
| 125 | $result | ||||||||||
| 126 | 9 | } | |||||||||
| 127 | 18 | ▶ | sub intersect{ | ||||||||
|
Subroutine
intersect: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 128 | 18 | my ($self,$other) = @_; | |||||||||
| 129 | 18 | my $result = $self->rulemap->nodeset; | |||||||||
| 130 | 18 | for my $n ($self->nodes){ | |||||||||
| 131 | 50 | ▶ | next unless $other->has_node($n); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 3 | $result->add($n); | |||||||||
| 133 | } | ||||||||||
| 134 | $result | ||||||||||
| 135 | 18 | } | |||||||||
| 136 | |||||||||||
| 137 | 3 | ▶ | sub disjoint_split{ | ||||||||
|
Subroutine
disjoint_split: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 138 | 3 | my $self = shift; | |||||||||
| 139 | 3 | my @disjoints; | |||||||||
| 140 | 3 | my $remaining = $self->copy; | |||||||||
| 141 | 3 | ▶ | while($remaining->count){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | 5 | my $choice_node = $remaining->choose; | |||||||||
| 143 | 5 | my $subset = $self->rulemap->nodeset;#($choice_node); | |||||||||
| 144 | 5 | my $flood_iter = $self->rulemap->nodeset ($choice_node); | |||||||||
| 145 | 5 | ▶ | while($flood_iter->count){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 146 | 6 | $subset->add($flood_iter); | |||||||||
| 147 | 6 | $remaining->remove($flood_iter); | |||||||||
| 148 | 6 | $flood_iter = $flood_iter->adjacent->intersect($remaining); | |||||||||
| 149 | } | ||||||||||
| 150 | 5 | push @disjoints, $subset; | |||||||||
| 151 | } | ||||||||||
| 152 | 3 | return @disjoints; | |||||||||
| 153 | } | ||||||||||
| 154 | 6 | 1; | |||||||||
| 155 | |||||||||||
| 156 | __END__ | ||||||||||