| 1 | package DateTime::Event::Cron::Quartz::TreeSet; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use 5.006001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | our $VERSION = '0.05'; | |||||||||
| 8 | |||||||||||
| 9 | 1923 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 10 | 1923 | my $class = shift; | |||||||||
| 11 | |||||||||||
| 12 | 1923 | my $list = shift; | |||||||||
| 13 | |||||||||||
| 14 | 1923 | ▶ | my $self = { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | _sorted => 0, | ||||||||||
| 16 | _array => defined $list ? $list : [], | ||||||||||
| 17 | _index => {} | ||||||||||
| 18 | }; | ||||||||||
| 19 | |||||||||||
| 20 | 1923 | return bless $self, $class; | |||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | 6883 | ▶ | sub size { | ||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 24 | 6883 | return ( int @{ shift->{_array} } ); | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | 9325 | ▶ | sub contains { | ||||||||
|
Subroutine
contains: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 28 | 9325 | my $self = shift; | |||||||||
| 29 | |||||||||||
| 30 | 9325 | my $value = shift; | |||||||||
| 31 | |||||||||||
| 32 | 9325 | return ( exists $self->{_index}->{$value} ); | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 1906 | ▶ | sub first_item { | ||||||||
|
Subroutine
first_item: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 36 | 1906 | my $self = shift; | |||||||||
| 37 | |||||||||||
| 38 | 1906 | ▶ | if ( !$self->{_sorted} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 1586 | $self->_sort; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 1906 | ▶ | return ( $self->size > 0 ? $self->{_array}->[0] : undef ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 19 | ▶ | sub last_item { | ||||||||
|
Subroutine
last_item: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||
| 46 | 19 | my $self = shift; | |||||||||
| 47 | 19 | ▶ | if ( !$self->{_sorted} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 19 | $self->_sort; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 19 | ▶ | return ( $self->size > 0 ? $self->{_array}->[ $self->size - 1 ] : undef ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 7036 | ▶ | sub add { | ||||||||
|
Subroutine
add: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 55 | 7036 | my $self = shift; | |||||||||
| 56 | 7036 | my $value = shift; | |||||||||
| 57 | |||||||||||
| 58 | 7036 | ▶ | if ( !$self->contains($value) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 7036 | push @{ $self->{_array} }, $value; | |||||||||
| 60 | 7036 | $self->{_index}->{$value} = undef; | |||||||||
| 61 | 7036 | $self->{_sorted} = 0; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 7036 | return; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | sub to_array { | ||||||||
|
Subroutine
to_array: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 68 | 0 | my $self = shift; | |||||||||
| 69 | |||||||||||
| 70 | 0 | ▶ | if (!$self->{_sorted}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 0 | $self->_sort; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 0 | return $self->{_array}; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 1833 | ▶ | sub tail_set { | ||||||||
|
Subroutine
tail_set: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||
| 78 | 1833 | my $self = shift; | |||||||||
| 79 | 1833 | my $from_element = shift; | |||||||||
| 80 | |||||||||||
| 81 | 1833 | ▶ | if ( !$self->{_sorted} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 192 | $self->_sort; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 1833 | my $idx; | |||||||||
| 86 | |||||||||||
| 87 | 1833 | ▶ | if ( $self->contains($from_element) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | |||||||||||
| 89 | # try to find in index | ||||||||||
| 90 | 1296 | $idx = $self->{_index}->{$from_element}; | |||||||||
| 91 | } | ||||||||||
| 92 | else { | ||||||||||
| 93 | |||||||||||
| 94 | # find closest in array | ||||||||||
| 95 | 537 | foreach my $val ( @{ $self->{_array} } ) { | |||||||||
| 96 | 568 | ▶ | if ( $val >= $from_element ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 277 | $idx = int $self->{_index}->{$val}; | |||||||||
| 98 | 277 | CORE::last; | |||||||||
| 99 | } | ||||||||||
| 100 | } | ||||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 1833 | my $result_set; | |||||||||
| 104 | |||||||||||
| 105 | 1833 | ▶ | if ( defined $idx ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | 1573 | my $size = $self->size - 1; | |||||||||
| 107 | |||||||||||
| 108 | 1573 | $result_set = [ @{ $self->{_array} }[ $idx .. $size ] ]; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 1833 | ▶ | return ( defined $result_set ? ( ref $self )->new($result_set) : undef ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | # performs internal sorting and search index building | ||||||||||
| 115 | 1797 | ▶ | sub _sort { | ||||||||
|
Subroutine
_sort: called CC=3 SCAR=11.0
| |||||||||||
| 116 | 1797 | my $self = shift; | |||||||||
| 117 | |||||||||||
| 118 | 1797 | ▶ | if ( ( my $size = $self->size ) > 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 1795 | $self->{_array} = [ CORE::sort { $a <=> $b } @{ $self->{_array} } ]; | |||||||||
| 120 | |||||||||||
| 121 | # build search index | ||||||||||
| 122 | 1795 | foreach my $i ( 0 .. ( $size - 1 ) ) { | |||||||||
| 123 | 18655 | $self->{_index}->{ $self->{_array}->[$i] } = $i; | |||||||||
| 124 | } | ||||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | 1797 | $self->{_sorted} = 1; | |||||||||
| 128 | |||||||||||
| 129 | 1797 | return; | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 1 | 1; | |||||||||
| 133 | |||||||||||
| 134 | __END__ | ||||||||||