[greg@mccarroll.demon.co.uk: Re: Tie::Hash::Cannabinol]

Chris Benson chrisb at jesmond.demon.co.uk
Wed Jun 6 13:55:05 CDT 2001


And ...  what jolly fun!  It rains and London.pm stay in and write 
strange code.

----- Forwarded message from Greg McCarroll <greg at mccarroll.demon.co.uk> -----

Date: Wed, 6 Jun 2001 11:46:51 +0100
From: Greg McCarroll <greg at mccarroll.demon.co.uk>
To: london-pm at lists.dircon.co.uk
Subject: Re: Tie::Hash::Cannabinol
X-Operating-System: Linux scully.mccarroll.demon.co.uk 2.2.13-7mdk
Reply-To: london-pm at lists.dircon.co.uk

* Richard Clyne (richard_clyne at anadarko.COM) wrote:
> I always thought that a data structure that mimicked a bus queue would
> be useful.
> 
> If you request more items than are in the queue (e.g. lots of empty
> seats) the queue returns the items in order.  If you request less items
> than are in the queue (Bus almost full) the largest items push through
> and are selected.

Fun!

the following should do what you want, although i'm not sure if freezing
non-references is fair on them and i'm sure the sort condition syntax
can be shortened by the perl golfers on the list ...

package BusQueue;

use strict;
use Storable qw(freeze);

sub new {
    my $class = shift;
    my $self  = [];
    return bless $self, $class;
}

sub insert {
    my $self = shift;
    push(@$self, @_);
}

sub remove {
    my $self = shift;
    my ($num) = @_;
    @$self = sort {
        my $sa;
        my $sb;
        if (ref($a)) {
            $sa = length(freeze($a));
        } else {
            $sa = length(freeze(\$a));
        }
        if (ref($b)) {
            $sb = length(freeze($b));
        } else {
            $sb = length(freeze(\$b));
        }
        $sb <=> $sa;
    } @$self;
    return splice @$self, 0, $num;
}

1;


-- 
Greg McCarroll                                http://217.34.97.146/~gem/

----- End forwarded message -----

-- 
Chris Benson



More information about the Tyneside-pm mailing list