Ambrosia

 view release on metacpan or  search on metacpan

lib/Ambrosia/Utils/Queue.pm  view on Meta::CPAN

package Ambrosia::Utils::Queue;
use strict;
use warnings;

use Ambrosia::Meta;

class sealed
{
    private => [qw/__list __strategy/]
};

our $VERSION = 0.010;

use Ambrosia::Utils::Enumeration property => __strategy => (STRATEGY_LIFO => 1, STRATEGY_FIFO => 2);

sub _init
{
    my $self = shift;
    $self->SUPER::_init(@_);
    $self->__list ||= [];
    $self->SET_STRATEGY_FIFO();
}

sub add
{
    my $self = shift;
    push @{$self->__list}, @_;
    $self;
}

sub inhead
{
    my $self = shift;
    unshift @{$self->__list}, @_;
    $self;
}

sub next()
{
    my $self = shift;

    if ( $self->IS_STRATEGY_LIFO )
    {
        return pop @{$self->__list};
    }
    else
    {
        return shift @{$self->__list};
    }
}

sub last()
{
    my $self = shift;

    return undef unless $self->size();

    if ( $self->IS_STRATEGY_LIFO )
    {
        return $self->__list->[0];
    }
    else
    {
        return $self->__list->[-1];
    }
}

sub head()
{
    my $self = shift;

    if ( $self->IS_STRATEGY_LIFO )
    {
        return pop @{$self->__list};
    }
    else
    {
        return shift @{$self->__list};
    }



( run in 0.923 second using v1.01-cache-2.11-cpan-0b5f733616e )