| 1 | package Backup::Hanoi; | ||||||||||||
| 2 | # ABSTRACT: select backup according to algo | ||||||||||||
| 3 | 4 | $Backup::Hanoi::VERSION = '0.003'; | |||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | sub new { | ||||||||||||
| 8 | 7 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||
| 9 | 7 | ▶ | my $devices = shift // []; | ||||||||||
|
Condition50%1/2
shift() // []
MC/DC0%0/1
shift() // []
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 10 | |||||||||||||
| 11 | 7 | ▶ | die "You need at least three devices, for this to work.\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 12 | if ($devices < 3); | ||||||||||||
| 13 | |||||||||||||
| 14 | # the number of devices predicts the size of the cycles | ||||||||||||
| 15 | 7 | my $device_count = scalar @{$devices}; | |||||||||||
| 16 | |||||||||||||
| 17 | # half a hanoi cycle is just what we need for backup | ||||||||||||
| 18 | 7 | my $hanoi_cycles_half = (2**$device_count) / 2; | |||||||||||
| 19 | |||||||||||||
| 20 | 7 | my $self = { devices => $devices, | |||||||||||
| 21 | hanoi_cycles_half => $hanoi_cycles_half, | ||||||||||||
| 22 | }; | ||||||||||||
| 23 | |||||||||||||
| 24 | 7 | bless $self, $class; | |||||||||||
| 25 | |||||||||||||
| 26 | 7 | return $self; | |||||||||||
| 27 | } | ||||||||||||
| 28 | |||||||||||||
| 29 | sub get_device_for_cycle { | ||||||||||||
| 30 | |||||||||||||
| 31 | 96 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
get_device_for_cycle: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 32 | 96 | ▶ | my $cycle = shift // 0; | ||||||||||
|
Condition50%1/2
shift() // 0
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 33 | |||||||||||||
| 34 | # treat negative numbers as normal FIFO | ||||||||||||
| 35 | # e.g. index -1 gives the second last element | ||||||||||||
| 36 | return $self->{devices} | ||||||||||||
| 37 | 96 | ▶ | ->[(scalar @{$self->{devices}}) + $cycle -1] | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | if ($cycle < 0); | ||||||||||||
| 39 | |||||||||||||
| 40 | # allow cycle to cross hanoi limit, map it to first half | ||||||||||||
| 41 | 91 | my $modulo_cycle = $cycle % $self->{hanoi_cycles_half}; | |||||||||||
| 42 | |||||||||||||
| 43 | # change zero to maximum, so that zero gets highest device | ||||||||||||
| 44 | $modulo_cycle = $self->{hanoi_cycles_half} | ||||||||||||
| 45 | 91 | ▶ | if ($modulo_cycle == 0); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 46 | |||||||||||||
| 47 | # calculate which device is used for given cycle | ||||||||||||
| 48 | 91 | my $hanoi_number = | |||||||||||
| 49 | _get_right_zeros_from_digital_representation( | ||||||||||||
| 50 | $modulo_cycle); | ||||||||||||
| 51 | |||||||||||||
| 52 | # select and return device | ||||||||||||
| 53 | 91 | return $self->{devices}->[$hanoi_number]; | |||||||||||
| 54 | |||||||||||||
| 55 | } | ||||||||||||
| 56 | |||||||||||||
| 57 | sub _get_right_zeros_from_digital_representation { | ||||||||||||
| 58 | |||||||||||||
| 59 | 91 | ▶ | my $number = shift; | ||||||||||
|
Subroutine
_get_right_zeros_from_digital_representation: called CC=3 SCAR=11.0
| |||||||||||||
| 60 | |||||||||||||
| 61 | # convert to binary format: e.g. number 5 -> 101 | ||||||||||||
| 62 | 91 | my $binary = sprintf "%b", $number; | |||||||||||
| 63 | |||||||||||||
| 64 | # represent number zero as empty string | ||||||||||||
| 65 | 91 | ▶ | $binary = '' if ($binary == 0); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | |||||||||||||
| 67 | |||||||||||||
| 68 | # count the zeros, at the right from the binary number | ||||||||||||
| 69 | |||||||||||||
| 70 | 91 | my $zeros_from_the_right = 0; | |||||||||||
| 71 | |||||||||||||
| 72 | 91 | ▶ | if ( $binary =~ /(0+)$/ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | 50 | my $zero_capture = $1; | |||||||||||
| 74 | |||||||||||||
| 75 | 50 | $zeros_from_the_right = length $zero_capture; | |||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | 91 | return $zeros_from_the_right; | |||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | 4 | 1; | |||||||||||
| 82 | |||||||||||||
| 83 | __END__ | ||||||||||||