| 1 | package Algorithm::VectorClocks; | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use version; our $VERSION = qv('0.1.2'); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use JSON::Any; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use List::MoreUtils qw(uniq); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use List::Util qw(max); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Perl6::Export::Attrs; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Sys::Hostname; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use overload ( | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 16 | 3 | '""' => \&serialize, | |||||||||||||||||||||||||||||
| 17 | '++' => \&increment, | ||||||||||||||||||||||||||||||
| 18 | '+=' => \&merge, | ||||||||||||||||||||||||||||||
| 19 | '==' => \&equal, | ||||||||||||||||||||||||||||||
| 20 | 'eq' => \&equal, | ||||||||||||||||||||||||||||||
| 21 | '!=' => \¬_equal, | ||||||||||||||||||||||||||||||
| 22 | 'ne' => \¬_equal, | ||||||||||||||||||||||||||||||
| 23 | fallback => undef, | ||||||||||||||||||||||||||||||
| 24 | 3 | ); | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||
| 26 | 3 | ▶ | use base qw(Class::Accessor::Fast Class::Data::Inheritable); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||
| 28 | 3 | __PACKAGE__->mk_accessors(qw(clocks)); | |||||||||||||||||||||||||||||
| 29 | 3 | __PACKAGE__->mk_classdata($_) for qw(id json); | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||
| 31 | 3 | __PACKAGE__->id(hostname); | |||||||||||||||||||||||||||||
| 32 | 3 | __PACKAGE__->json(JSON::Any->new); | |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||
| 34 | 39 | ▶ | sub new { | ||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 35 | 39 | my $class = shift; | |||||||||||||||||||||||||||||
| 36 | 39 | my($arg) = @_; | |||||||||||||||||||||||||||||
| 37 | 39 | ▶ | my $self = UNIVERSAL::isa($arg, $class) ? $arg | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$arg || '{}'
MC/DC100%1/1
$arg || '{}'
$arg
Truth table100%2/2
A
A= $arg
| |||||||||||||||||||||||||||||||
| 38 | : { clocks => $class->json->jsonToObj($arg || '{}') }; | ||||||||||||||||||||||||||||||
| 39 | 39 | bless $self, $class; | |||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||
| 42 | 4 | ▶ | sub serialize { | ||||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 43 | 4 | my $self = shift; | |||||||||||||||||||||||||||||
| 44 | 4 | $self->json->objToJson($self->clocks); | |||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||
| 47 | 4 | ▶ | sub increment { | ||||||||||||||||||||||||||||
|
Subroutine
increment: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 48 | 4 | my $self = shift; | |||||||||||||||||||||||||||||
| 49 | 4 | $self->clocks->{ $self->id }++; # increment its own clock | |||||||||||||||||||||||||||||
| 50 | 4 | $self; | |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||
| 53 | 3 | ▶ | sub merge { | ||||||||||||||||||||||||||||
|
Subroutine
merge: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 54 | 3 | my $self = shift; | |||||||||||||||||||||||||||||
| 55 | 3 | my($other) = @_; | |||||||||||||||||||||||||||||
| 56 | 3 | $other = __PACKAGE__->new($other); | |||||||||||||||||||||||||||||
| 57 | 3 | my @ids = _list_ids($self, $other); | |||||||||||||||||||||||||||||
| 58 | 3 | for my $id (@ids) { | |||||||||||||||||||||||||||||
| 59 | $self->clocks->{$id} | ||||||||||||||||||||||||||||||
| 60 | 5 | ▶ | = max( ($self->clocks->{$id} || 0), ($other->clocks->{$id} || 0) ); | ||||||||||||||||||||||||||||
|
Condition75%3/4
$self->clocks->{$id} || 0
$other->clocks->{$id} || 0
MC/DC100%1/1
$self->clocks->{$id} || 0
$self->clocks->{$id}
MC/DC0%0/1
$other->clocks->{$id} || 0
$other->clocks->{$id}
Truth table100%2/2
A
A= $self->clocks->{$id}
Truth table50%1/2
A
A= $other->clocks->{$id}
| |||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||
| 62 | 3 | $self; | |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||
| 65 | 6 | ▶ | sub equal { | ||||||||||||||||||||||||||||
|
Subroutine
equal: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||||
| 66 | 6 | my @vcs = @_; | |||||||||||||||||||||||||||||
| 67 | 6 | $_ = __PACKAGE__->new($_) for @vcs; | |||||||||||||||||||||||||||||
| 68 | 6 | my @ids = _list_ids(@vcs); | |||||||||||||||||||||||||||||
| 69 | 6 | for my $id (@ids) { | |||||||||||||||||||||||||||||
| 70 | return 0 | ||||||||||||||||||||||||||||||
| 71 | 10 | ▶ | unless ($vcs[0]->clocks->{$id} || 0) == ($vcs[1]->clocks->{$id} || 0); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$vcs[0]->clocks->{$id} || 0
$vcs[1]->clocks->{$id} || 0
MC/DC0%0/1
$vcs[0]->clocks->{$id} || 0
$vcs[0]->clocks->{$id}
MC/DC0%0/1
$vcs[1]->clocks->{$id} || 0
$vcs[1]->clocks->{$id}
Truth table50%1/2
A
A= $vcs[0]->clocks->{$id}
Truth table50%1/2
A
A= $vcs[1]->clocks->{$id}
| |||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||
| 73 | 4 | return 1; | |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||
| 76 | 3 | ▶ | sub not_equal { !equal(@_) } | ||||||||||||||||||||||||||||
|
Subroutine
not_equal: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||
| 78 | 4 | ▶ | sub order_vector_clocks :Export(:DEFAULT) { | ||||||||||||||||||||||||||||
|
Subroutine
order_vector_clocks: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 79 | 4 | my($vcs) = @_; | |||||||||||||||||||||||||||||
| 80 | 4 | my @vcs; | |||||||||||||||||||||||||||||
| 81 | 4 | ▶ | while (my($id, $vc) = each %$vcs) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 82 | 8 | $vc = __PACKAGE__->new($vc); | |||||||||||||||||||||||||||||
| 83 | 8 | $vc->{_id} = $id; | |||||||||||||||||||||||||||||
| 84 | 8 | push @vcs, $vc; | |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||
| 86 | 4 | @vcs = sort { _compare($b, $a) } @vcs; | |||||||||||||||||||||||||||||
| 87 | 4 | _pack_independent_vector_clocks(@vcs); | |||||||||||||||||||||||||||||
| 88 | 3 | ▶ | } | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||
| 90 | 4 | ▶ | sub _pack_independent_vector_clocks { | ||||||||||||||||||||||||||||
|
Subroutine
_pack_independent_vector_clocks: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||
| 91 | 4 | my @vcs = @_; | |||||||||||||||||||||||||||||
| 92 | 4 | my @ret; | |||||||||||||||||||||||||||||
| 93 | 4 | my $i = 0; | |||||||||||||||||||||||||||||
| 94 | 4 | ▶ | while ($i < @vcs) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 95 | my @equals = ( | ||||||||||||||||||||||||||||||
| 96 | $vcs[$i], | ||||||||||||||||||||||||||||||
| 97 | 6 | (grep { _compare($vcs[$i], $_) == 0 } @vcs[($i+1)..$#vcs]), | |||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||||
| 99 | 6 | ▶ | push @ret, @equals == 1 ? $equals[0]->{_id} : [ map $_->{_id}, @equals ]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 100 | 6 | $i += @equals; | |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||
| 102 | 4 | @ret; | |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub are_independent { | ||||||||||||||||||||||||||||
|
Subroutine
are_independent: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 106 | 0 | my @vcs = @_; | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | for (my $j = 0; $j < @vcs; $j++) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | for (my $k = $j+1; $k < @vcs; $k++) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | return 1 if $vcs[$j]->_is_independent($vcs[$k]); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub _is_independent { | ||||||||||||||||||||||||||||
|
Subroutine
_is_independent: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||
| 115 | 0 | my $self = shift; | |||||||||||||||||||||||||||||
| 116 | 0 | my($other) = @_; | |||||||||||||||||||||||||||||
| 117 | 0 | $other = __PACKAGE__->new($other); | |||||||||||||||||||||||||||||
| 118 | 0 | my @ids = _list_ids($self, $other); | |||||||||||||||||||||||||||||
| 119 | 0 | my $res = 0; | |||||||||||||||||||||||||||||
| 120 | 0 | for my $id (@ids) { | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | my $r = ($self->clocks->{$id} || 0) - ($other->clocks->{$id} || 0); | ||||||||||||||||||||||||||||
|
Condition0%0/4
$self->clocks->{$id} || 0
$other->clocks->{$id} || 0
MC/DC0%0/1
$self->clocks->{$id} || 0
$self->clocks->{$id}
MC/DC0%0/1
$other->clocks->{$id} || 0
$other->clocks->{$id}
Truth table0%0/2
A
A= $self->clocks->{$id}
Truth table0%0/2
A
A= $other->clocks->{$id}
| |||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ($res == 0 ) { $res = $r } | ||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||
| 123 | elsif ($r == 0 ) { } | ||||||||||||||||||||||||||||||
| 124 | 0 | elsif ($res != $r) { return 1 } | |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||
| 126 | 0 | 0; | |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||
| 129 | 9 | ▶ | sub _compare { | ||||||||||||||||||||||||||||
|
Subroutine
_compare: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||
| 130 | 9 | my $self = shift; | |||||||||||||||||||||||||||||
| 131 | 9 | my($other) = @_; | |||||||||||||||||||||||||||||
| 132 | 9 | $other = __PACKAGE__->new($other); | |||||||||||||||||||||||||||||
| 133 | 9 | my @ids = _list_ids($self, $other); | |||||||||||||||||||||||||||||
| 134 | 9 | my $res = 0; | |||||||||||||||||||||||||||||
| 135 | 9 | for my $id (@ids) { | |||||||||||||||||||||||||||||
| 136 | 24 | ▶ | my $r = ($self->clocks->{$id} || 0) - ($other->clocks->{$id} || 0); | ||||||||||||||||||||||||||||
|
Condition100%4/4
$self->clocks->{$id} || 0
$other->clocks->{$id} || 0
MC/DC100%1/1
$self->clocks->{$id} || 0
$self->clocks->{$id}
MC/DC100%1/1
$other->clocks->{$id} || 0
$other->clocks->{$id}
Truth table100%2/2
A
A= $self->clocks->{$id}
Truth table100%2/2
A
A= $other->clocks->{$id}
| |||||||||||||||||||||||||||||||
| 137 | 24 | ▶ | if ($res == 0 ) { $res = $r } | ||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||
| 138 | elsif ($r == 0 ) { } | ||||||||||||||||||||||||||||||
| 139 | 4 | elsif ($res != $r) { return 0 } # independent | |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||
| 141 | 5 | $res; | |||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||
| 144 | 18 | ▶ | sub _list_ids { uniq map { keys %{ $_->clocks } } @_ } | ||||||||||||||||||||||||||||
|
Subroutine
_list_ids: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||
| 146 | 3 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||||
| 147 | __END__ | ||||||||||||||||||||||||||||||