List::Sorted - Class representing a list that is always sorted
version 0.001
my $l = new List::Sorted;
$l->insert(2,1,3);
say $l->[1]; # says 2
This class represents a list that remains sorted.
$l = new List::Sorted [ccmp => sub {...}];
Create a new object. If ccmp is given, it should be a coderef that compares
two elements in the list (as in the comparator
argument in
List::BinarySearch). If it is not given, it defaults to the class's "ccmp"
method (which can be overriden in derived classes).
if ( $self->ccmp($a,$b) ) { ... }
The method used to compare elements for sorting.
$pos = $self->find_pos($i)
Find the position of the argument $i in the list. If the element is not in the list, finds the place where it can be inserted to keep the list sorted.
$self->insert(3,1,2);
Insert the given elements into the list, keeping it sorted. Returns the list.
$err = $self->store("foo.yml", 0.1);
Store the content of the list in the given file. The second argument is a data version, which should be changed when the structure of the elements stored in the list changes.
Returns the error, if any. This method requires YAML::Any.
$err = $self->load("foo.yml", 0.1);
Load the content of the list from the given file, assuming the data version in the file is exactly the one given. Any previous content of $self is removed. Returns the error, if any.
This method requires YAML::Any.
# get a List::Sorted $l
my @data = @$l; # @data is a regular (sorted) array
Objects can be dereferenced to obtain the underlying array. Note that this should not be used to modify the array, since the list must remain sorted.
Moshe Kamensky [email protected]
This software is copyright (c) 2013 by Moshe Kamensky.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.