/usr/share/perl5/Tie/Array/Sorted.pm is in libtie-array-sorted-perl 1.41-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | package Tie::Array::Sorted;
use 5.006;
use strict;
use warnings;
use base 'Tie::Array';
our $VERSION = '1.41';
=head1 NAME
Tie::Array::Sorted - An array which is kept sorted
=head1 SYNOPSIS
use Tie::Array::Sorted;
tie @a, "Tie::Array::Sorted", sub { $_[0] <=> $_[1] };
push @a, 10, 4, 7, 3, 4;
print "@a"; # "3 4 4 7 10"
=head1 DESCRIPTION
This presents an ordinary array, but is kept sorted. All pushes and
unshifts cause the elements in question to be inserted in the
appropriate location to maintain order.
Direct stores (C<$a[10] = "wibble">) effectively splice out the original
value and insert the new element. It's not clear why you'd want to use
direct stores like that, but this module does the right thing if you do.
If you don't like the ordinary lexical comparator, you can provide your
own; it should compare the two elements it is given. For instance, a
numeric comparator would look like this:
tie @a, "Tie::Array::Sorted", sub { $_[0] <=> $_[1] }
Whereas to compare a list of files by their sizes, you'd so something
like:
tie @a, "Tie::Array::Sorted", sub { -s $_[0] <=> -s $_[1] }
=head1 LAZY SORTING
If you do more stores than fetches, you may find
L<Tie::Array::Sorted::Lazy> more efficient.
=cut
sub TIEARRAY {
my ($class, $comparator) = @_;
bless {
array => [],
comp => (defined $comparator ? $comparator : sub { $_[0] cmp $_[1] })
}, $class;
}
sub STORE {
my ($self, $index, $elem) = @_;
splice @{ $self->{array} }, $index, 0;
$self->PUSH($elem);
}
sub PUSH {
my ($self, @elems) = @_;
ELEM: for my $elem (@elems) {
my ($lo, $hi) = (0, $#{ $self->{array} });
while ($hi >= $lo) {
my $mid = int(($lo + $hi) / 2);
my $mid_val = $self->{array}[$mid];
my $cmp = $self->{comp}($elem, $mid_val);
if ($cmp == 0) {
splice(@{ $self->{array} }, $mid, 0, $elem);
next ELEM;
} elsif ($cmp > 0) {
$lo = $mid + 1;
} elsif ($cmp < 0) {
$hi = $mid - 1;
}
}
splice(@{ $self->{array} }, $lo, 0, $elem);
}
}
sub UNSHIFT { goto &PUSH }
sub FETCHSIZE { scalar @{ $_[0]->{array} } }
sub STORESIZE { $#{ $_[0]->{array} } = $_[1] - 1 }
sub FETCH { $_[0]->{array}->[ $_[1] ] }
sub CLEAR { @{ $_[0]->{array} } = () }
sub POP { pop(@{ $_[0]->{array} }) }
sub SHIFT { shift(@{ $_[0]->{array} }) }
sub EXISTS { exists $_[0]->{array}->[ $_[1] ] }
sub DELETE { delete $_[0]->{array}->[ $_[1] ] }
1;
=head1 AUTHOR
Original author: Simon Cozens
Current maintainer: Tony Bowden
=head1 BUGS and QUERIES
Please direct all correspondence regarding this module to:
bug-Tie-Array-Sorted@rt.cpan.org
This module was originall written as part of the L<Plucene> project.
However, as Plucene no longer uses this, it is effectively unmaintained.
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2003-2006 Simon Cozens and Tony Bowden.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License; either version 2 of the License,
or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.
=cut
|