| 1 | package Geo::Coder::Many::Scheduler::UniquenessScheduler; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use List::MoreUtils qw( first_index ); | ||
| 7 | |||
| 8 | use base 'Geo::Coder::Many::Scheduler'; | ||
| 9 | |||
| 10 | our $VERSION = '0.01'; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Geo::Coder::Many::Scheduler::UniquenessScheduler - Scheduler base class which | ||
| 15 | ensures uniqueness | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | A base class for enforcing correct behaviour of get_next_unique (and the other | ||
| 20 | methods) even when a scheduling scheme might not take this into account. Note: | ||
| 21 | this may alter the properties of the scheduling scheme! | ||
| 22 | |||
| 23 | Subclasses should | ||
| 24 | |||
| 25 | =head1 METHODS | ||
| 26 | |||
| 27 | =head2 new | ||
| 28 | |||
| 29 | Creates a UniquenessScheduler object and returns it. This should not be called | ||
| 30 | directly, but by any subclasses, via SUPER. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | sub new { | ||
| 35 | my $class = shift; | ||
| 36 | my $args = shift; | ||
| 37 | |||
| 38 | # (Map from geocoder+weight hash, for the time being) | ||
| 39 | my @items_copy = map { $_->{name} } @{$args->{items}}; | ||
| 40 | |||
| 41 | my $self = { items => \@items_copy}; | ||
| 42 | bless $self, $class; | ||
| 43 | |||
| 44 | # Initialize available_items | ||
| 45 | $self->reset_available(); | ||
| 46 | return $self; | ||
| 47 | }; | ||
| 48 | |||
| 49 | =head2 reset_available | ||
| 50 | |||
| 51 | Update the set of currently available items to the full set of items initially | ||
| 52 | provided. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | sub reset_available { | ||
| 57 | my $self = shift; | ||
| 58 | @{$self->{available_items}} = @{$self->{items}}; | ||
| 59 | return; | ||
| 60 | }; | ||
| 61 | |||
| 62 | =head2 get_next_unique | ||
| 63 | |||
| 64 | Uses _get_next (which has been overridden) to obtain the next scheduled item, | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub get_next_unique { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | my ($item, $item_pos); | ||
| 72 | while ( 0 < @{$self->{available_items}} ) { | ||
| 73 | |||
| 74 | # Get the next element (possibly one we've seen before) | ||
| 75 | $item = $self->_get_next(); | ||
| 76 | |||
| 77 | # Return undef if _get_next has no more items | ||
| 78 | return if !defined $item; | ||
| 79 | |||
| 80 | # Check whether we've seen this item before | ||
| 81 | $item_pos = first_index { $_ eq $item } @{$self->{available_items}}; | ||
| 82 | |||
| 83 | # Finish if we haven't seen it | ||
| 84 | last if ($item_pos > -1); | ||
| 85 | } | ||
| 86 | |||
| 87 | # If we ran out of items, return undef. | ||
| 88 | if ( !@{$self->{available_items}} ) { | ||
| 89 | return; | ||
| 90 | } | ||
| 91 | |||
| 92 | # Remember that we've seen this item, by removing it from the list of those | ||
| 93 | # available | ||
| 94 | if ( $item_pos > -1 ) { | ||
| 95 | splice @{$self->{available_items}},$item_pos,1; | ||
| 96 | } | ||
| 97 | return $item; | ||
| 98 | } | ||
| 99 | |||
| 100 | =head2 next_available | ||
| 101 | |||
| 102 | Zero if there are items remaining; undef if there aren't. | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub next_available { | ||
| 107 | my $self = shift; | ||
| 108 | if ( 0 == @{$self->{available_items}} ) { | ||
| 109 | return; | ||
| 110 | } | ||
| 111 | else { | ||
| 112 | return 0; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | =head2 _get_next | ||
| 117 | |||
| 118 | Shoudl be implemented by a subclass | ||
| 119 | |||
| 120 | =cut | ||
| 121 | |||
| 122 | sub _get_next { | ||
| 123 | die "This method must be over-ridden.\n"; | ||
| 124 | } | ||
| 125 | |||
| 126 | =head2 process_feedback | ||
| 127 | |||
| 128 | Does nothing by default; may be overridden | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub process_feedback { | ||
| 133 | return; | ||
| 134 | } | ||
| 135 | |||
| 136 | 1; | ||
| 137 |