Algorithm-SkipList
view release on metacpan or search on metacpan
lib/Algorithm/SkipList.pm view on Meta::CPAN
=item _debug
$list->_debug;
Used for debugging skip lists by developer. The output of this
function is subject to change.
=back
=head2 Node Methods
Methods for the L<Algorithm::SkipList::Node> object are documented in
that module. They are for internal use by the main
C<Algorithm::SkipList> module.
=head1 SPECIAL FEATURES
=head2 Tied Hashes
Hashes can be tied to C<Algorithm::SkipList> objects:
tie %hash, 'Algorithm::SkipList';
$hash{'foo'} = 'bar';
$list = tied %hash;
print $list->find('foo'); # returns bar
See the L<perltie> manpage for more information.
=head2 Customizing the Node Class
The default node may not handle specialized data types. To define
your own custom class, you need to derive a child class from
C<Algorithm::SkipList::Node>.
Below is an example of a node which redefines the default type to use
numeric instead of string comparisons:
package NumericNode;
our @ISA = qw( Algorithm::SkipList::Node );
sub key_cmp {
my $self = shift;
my $left = $self->key; # node key
my $right = shift; # value to compare the node key with
unless ($self->validate_key($right)) {
die "Invalid key: \'$right\'"; }
return ($left <=> $right);
}
sub validate_key {
my $self = shift;
my $key = shift;
return ($key =~ s/\-?\d+(\.\d+)?$/); # test if key is numeric
}
To use this, we say simply
$number_list = new Algorithm::SkipList( node_class => 'NumericNode' );
This skip list should work normally, except that the keys must be
numbers.
For another example of customized nodes, see L<Tie::RangeHash> version
1.00_b1 or later.
=head2 About Search Fingers
A side effect of the search function is that it returns a I<finger> to
where the key is or should be in the list.
We can use this finger for future searches if the key that we are
searching for occurs I<after> the key that produced the finger. For
example,
($value, $finger) = $list->find('Turing');
If we are searching for a key that occurs after 'Turing' in the above
example, then we can use this finger:
$value = $list->find('VonNeuman', $finger);
If we use this finger to search for a key that occurs before 'Turing'
however, it may fail:
$value = $list->find('Goedel', $finger); # this may not work
Therefore, use search fingers with caution.
Search fingers are specific to particular instances of a skip list.
The following should not work:
($value1, $finger) = $list1->find('bar');
$value2 = $list2->find('foo', $finger);
One useful feature of fingers is with enumerating all keys using the
L</first_key> and L</next_key> methods:
($key, $finger) = $list->first_key;
while (defined $key) {
...
($key, $finger) = $list->next_key($key, $finger);
}
See also the L</keys> method for generating a list of keys.
=head2 Similarities to Tree Classes
This module intentionally has a subset of the interface in the
L<Tree::Base> and other tree-type data structure modules, since skip
lists can be used in place of trees.
Because pointers only point forward, there is no C<prev> method to
point to the previous key.
Some of these methods (least, greatest) are autoloading because they
( run in 0.663 second using v1.01-cache-2.11-cpan-483215c6ad5 )