Algorithm-SkipList
view release on metacpan or search on metacpan
lib/Algorithm/SkipList.pm view on Meta::CPAN
=item first_key
$key = $list->first_key;
Returns the first key in the list.
If called in a list context, will return a
L<search finger|/"About Search Fingers">:
($key, $finger) = $list->first_key;
A call to L</first_key> implicitly calls L</reset>.
=item next_key
$key = $list->next_key( $last_key );
Returns the key following the previous key. List nodes are always
maintained in sorted order.
Search fingers may also be used to improve performance:
$key = $list->next_key( $last_key, $finger );
If called in a list context, will return a
L<search finger|/"About Search Fingers">:
($key, $finger) = $list->next_key( $last_key, $finger );
If no arguments are called,
$key = $list->next_key;
then the value of L</last_key> is assumed:
$key = $list->next_key( $list->last_key );
Note: calls to L</delete> will L</reset> the last key.
=item next
($key, $value) = $list->next( $last_key, $finger );
Returns the next key-value pair.
C<$last_key> and C<$finger> are optional.
This is an autoloading method.
=item last_key
$key = $list->last_key;
($key, $finger, $value) = $list->last_key;
Returns the last key or the last key and finger returned by a call to
L</first_key>, L</next_key>, L</index_by_key>, L</key_by_index> or
L</value_by_index>. This is not the greatest key.
Deletions and inserts may invalidate the L</last_key> value.
(Deletions will actually L</reset> the value.)
Values for L</last_key> can also be set by including parameters,
however this feature is meant for I<internal use only>:
$list->last_key( $node );
Note that this is a change form versions prior to 0.71.
=item reset
$list->reset;
Resets the L</last_key> to C<undef>.
=item index_by_key
$index = $list->index_by_key( $key );
Returns the 0-based index of the key (as if the list were an array).
I<This is not an efficient method of access.>
This is an autoloading method.
=item key_by_index
$key = $list->key_by_index( $index );
Returns the key associated with an index (as if the list were an
array). Negative indices return the key from the end. I<This is not
an efficient method of access.>
This is an autoloading method.
=item value_by_index
$value = $list->value_by_index( $index );
Returns the value associated with an index (as if the list were an
array). Negative indices return the value from the end. I<This is not
an efficient method of access.>
This is an autoloading method.
=item delete
$value = $list->delete( $key );
Deletes the node associated with the key, and returns the value. If
the key cannot be found, returns C<undef>.
L<Search fingers|/"About Search Fingers"> may also be used:
$value = $list->delete( $key, $finger );
Calling L</delete> in a list context I<will not> return a search
finger.
=item clear
lib/Algorithm/SkipList.pm view on Meta::CPAN
to L</_set_p> and L</_set_k>.
=item _set_node_class
=item _set_max_level
=item _set_p
=item _set_k
These methods are used during initialization of the object.
=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
( run in 2.146 seconds using v1.01-cache-2.11-cpan-140bd7fdf52 )