Tree::BPTree 1.07 review
DownloadTree::BPTree is a Perl implementation of B+ trees. SYNOPSIS use Tree::BPTree; # These arguments are actually the defau
|
|
Tree::BPTree is a Perl implementation of B+ trees.
SYNOPSIS
use Tree::BPTree;
# These arguments are actually the defaults
my $tree = new Tree::BPTree(
-n => 3,
-unique => 0,
-keycmp => sub { $_[0] cmp $_[1] },
-valuecmp => sub { $_[0] $_[1] },
);
# index the entries in this string:
my $string = "THERE'S MORE THAN ONE WAY TO DO IT"; # TMTOWTDI
my $i = 0;
$tree->insert($_, $i++) foreach (split //, $string);
# find the index of the first 'T'
my $t = $tree->find('T');
# find the indexes of every 'T'
my @t = $tree->find('T');
# We don't like the word 'WAY ', so let's remove it
my $i = index $string, 'W';
$tree->delete($_, $i++) foreach (split //, substr($string, $i, 4));
# Reverse the sort order
$tree->reverse;
# Iterate through each key/value pair just like built-in each operator
while (my ($key, $value) = $tree->each) {
print "$key => $valuen";
}
# Reset the iterator when we quit from an "each-loop" early
$tree->reset;
# You might also be interested in using multiple each loops at once, which is
# possible through the cursor syntax. You can even delete individual pairs
# from the list during iteration.
my $cursor = $tree->new_cursor;
while (my ($key, $value) = $cursor->each) {
my $nested = $tree->new_cursor;
while (my ($nkey, $nvalue) = $nested->each) {
if ($key->shouldnt_be_in_this_tree_with($nkey)) {
$nested->delete;
}
}
}
# Iterate using an iterator subroutine
$tree->iterate(sub { print "$_[0] => $_[1]n" });
# Iterate using an iterator subroutine that returns the list of return values
# returned by the iterator
print join(', ', $tree->map(sub { "$_[0] => $_[1]" })),"n";
# Grep-like operations
my @pairs = $tree->grep (sub { $_[0] =~ /S/ });
my @keys = $tree->grep_keys (sub { $_[0] =~ /S/ });
my @values = $tree->grep_values (sub { $_[0] =~ /S/ });
# Get all keys, values
my @all_keys = $tree->keys;
my @all_values = $tree->values;
# Clear it out and start over
$tree->clear;
B+ trees are balanced trees which provide an ordered map from keys to values. They are useful for indexing large bodies of data. They are similar to 2-3-4 Trees and Red-Black Trees. This implementation supports B+ trees using an arbitrary n value.
Requirements:
Perl
Tree::BPTree 1.07 keywords