| 1 | package SWISH::Prog::Queue; | ||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use base qw( SWISH::Prog::Class ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 2 | our $VERSION = '0.75'; | |||||||||||
| 8 | |||||||||||||
| 9 | =pod | ||||||||||||
| 10 | |||||||||||||
| 11 | =head1 NAME | ||||||||||||
| 12 | |||||||||||||
| 13 | SWISH::Prog::Queue - simple in-memory FIFO queue class | ||||||||||||
| 14 | |||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||
| 16 | |||||||||||||
| 17 | use SWISH::Prog::Queue; | ||||||||||||
| 18 | my $queue = SWISH::Prog::Queue->new; | ||||||||||||
| 19 | |||||||||||||
| 20 | $queue->put( 'foo' ); | ||||||||||||
| 21 | $queue->size; # returns number of items in queue (1) | ||||||||||||
| 22 | $queue->peek; # returns 'foo' (next value for get()) | ||||||||||||
| 23 | $queue->get; # returns 'foo' locks it in queue (no one else can get it) | ||||||||||||
| 24 | $queue->remove('foo'); # returns 'foo' and removes it from queue | ||||||||||||
| 25 | $queue->clean; # removes all completed items from queue | ||||||||||||
| 26 | |||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||
| 28 | |||||||||||||
| 29 | SWISH::Prog::Queue is basically a Perl array, but it defines an API | ||||||||||||
| 30 | that can be implemented using any kind of storage and logic you want. | ||||||||||||
| 31 | One example would be a database that tracks items to be evaluated, or a flat | ||||||||||||
| 32 | file list. | ||||||||||||
| 33 | |||||||||||||
| 34 | =head1 METHODS | ||||||||||||
| 35 | |||||||||||||
| 36 | See SWISH::Prog::Class. | ||||||||||||
| 37 | |||||||||||||
| 38 | =cut | ||||||||||||
| 39 | |||||||||||||
| 40 | =head2 init | ||||||||||||
| 41 | |||||||||||||
| 42 | Overrides base method. | ||||||||||||
| 43 | |||||||||||||
| 44 | =cut | ||||||||||||
| 45 | |||||||||||||
| 46 | 1 | ▶ | sub init { | ||||||||||
|
Subroutine
init: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 47 | 1 | my $self = shift; | |||||||||||
| 48 | 1 | $self->SUPER::init(@_); | |||||||||||
| 49 | 1 | ▶ | $self->{q} ||= []; | ||||||||||
|
Condition50%1/2
$self->{"q"} ||= []
MC/DC0%0/1
$self->{"q"} ||= []
$self->{"q"}
Truth table50%1/2
A
A= $self->{"q"}
| |||||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | =head2 put( I<item> ) | ||||||||||||
| 53 | |||||||||||||
| 54 | Add I<item> to the queue. Default is to push() it to end of queue. | ||||||||||||
| 55 | |||||||||||||
| 56 | =cut | ||||||||||||
| 57 | |||||||||||||
| 58 | 9 | ▶ | sub put { | ||||||||||
|
Subroutine
put: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 59 | 9 | my $self = shift; | |||||||||||
| 60 | 9 | push( @{ $self->{q} }, @_ ); | |||||||||||
| 61 | } | ||||||||||||
| 62 | |||||||||||||
| 63 | =head2 get | ||||||||||||
| 64 | |||||||||||||
| 65 | Returns the next item. Default is to shift() it from the front of the queue. | ||||||||||||
| 66 | |||||||||||||
| 67 | =cut | ||||||||||||
| 68 | |||||||||||||
| 69 | 9 | ▶ | sub get { | ||||||||||
|
Subroutine
get: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||
| 70 | 9 | my $self = shift; | |||||||||||
| 71 | 9 | ▶ | $self->{locks} ||= {}; | ||||||||||
|
Condition100%2/2
$self->{"locks"} ||= {}
MC/DC100%1/1
$self->{"locks"} ||= {}
$self->{"locks"}
Truth table100%2/2
A
A= $self->{"locks"}
| |||||||||||||
| 72 | 9 | my $v = shift( @{ $self->{q} } ); | |||||||||||
| 73 | 9 | ▶ | if ( $self->{locks}->{$v}++ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | 0 | return undef; | |||||||||||
| 75 | } | ||||||||||||
| 76 | 9 | return $v; | |||||||||||
| 77 | } | ||||||||||||
| 78 | |||||||||||||
| 79 | =head2 remove( I<item> ) | ||||||||||||
| 80 | |||||||||||||
| 81 | Removes I<item> from the queue (unlocks it). | ||||||||||||
| 82 | |||||||||||||
| 83 | =cut | ||||||||||||
| 84 | |||||||||||||
| 85 | 9 | ▶ | sub remove { | ||||||||||
|
Subroutine
remove: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 86 | 9 | my $self = shift; | |||||||||||
| 87 | 9 | my $v = shift; | |||||||||||
| 88 | 9 | return delete $self->{locks}->{$v}; | |||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | =head2 clean | ||||||||||||
| 92 | |||||||||||||
| 93 | Removes all locked items from the queue. | ||||||||||||
| 94 | |||||||||||||
| 95 | =cut | ||||||||||||
| 96 | |||||||||||||
| 97 | 0 | ▶ | sub clean { | ||||||||||
|
Subroutine
clean: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 98 | 0 | my $self = shift; | |||||||||||
| 99 | 0 | delete $self->{locks}; | |||||||||||
| 100 | } | ||||||||||||
| 101 | |||||||||||||
| 102 | =head2 peek | ||||||||||||
| 103 | |||||||||||||
| 104 | Returns the next item value, but leaves it on the stack. | ||||||||||||
| 105 | |||||||||||||
| 106 | =cut | ||||||||||||
| 107 | |||||||||||||
| 108 | 0 | ▶ | sub peek { | ||||||||||
|
Subroutine
peek: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 109 | 0 | return $_[0]->{q}->[0]; | |||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | =head2 size | ||||||||||||
| 113 | |||||||||||||
| 114 | Returns the number of items currently in the queue. | ||||||||||||
| 115 | |||||||||||||
| 116 | =cut | ||||||||||||
| 117 | |||||||||||||
| 118 | 10 | ▶ | sub size { | ||||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 119 | 10 | return scalar( @{ $_[0]->{q} } ); | |||||||||||
| 120 | } | ||||||||||||
| 121 | |||||||||||||
| 122 | 2 | 1; | |||||||||||
| 123 | |||||||||||||
| 124 | __END__ | ||||||||||||