Set::IntervalTree - Perform range-based lookups on sets of ranges.
use Set::IntervalTree; my $tree = Set::IntervalTree->new; $tree->insert("ID1",100,200); $tree->insert(2,50,100); $tree->insert({id=>3},520,700); $tree->insert($some_obj,1000,1100); my $results = $tree->fetch(400,800); my $window = $tree->window(100,200); print scalar(@$results)." intervals found.\n"; # remove only items overlapping location 100..200 with values # less than 100; my $removed = $tree->remove(100,200 sub { my ($item, $low, $high) = @_; return $item < 100; });
Set::IntervalTree uses Interval Trees to store and efficiently look up ranges using a range-based lookup.
All intervals are half-open, i.e. [1,3), [2,6), etc.
Nothing.
my $tree = Set::IntervalTree->new;
Creates a new interval tree object.
$tree->insert($object, $low, $high);
Insert a range into the interval tree and associate it with a perl scalar. $object can be any perl scalar. This is what will be returned by fetch(). $low is the lower bound of the range. $high is the upper bound of the range. Ranges are represented as half-closed integer intervals.
my $results = $tree->fetch($low, $high)
Return an arrayref of perl objects whose ranges overlap the specified range. $low is the lower bound of the region to query. $high is the upper bound of the region to query.
my $results = $tree->fetch_window($low, $high)
Return an arrayref of perl objects whose ranges are completely contained witin the specified range. $low is the lower bound of the region to query. $high is the upper bound of the region to query.
my $removed = $tree->remove($low, $high [, optional \&coderef]);
Remove items in the tree that overlap the region from $low to $high. A coderef can be passed in as an optional third argument for filtering what is removed. The coderef receives the stored item, the low point, and the high point as its arguments. If the result value of the coderef is true, the item is removed, otherwise the item remains in the tree. Returns the list of removed items.
my $removed = $tree->remove_window($low, $high [, optional \&coderef]);
Remove items in the tree that are contained within the region from $low to $high. A coderef can be passed in as an optional third argument for filtering what is removed. The coderef receives the stored item, the low point, and the high point as its arguments. If the result value of the coderef is true, the item is removed, otherwise the item remains in the tree. Returns the list of removed items.
You will need a C++11 capable compiler to compile this module.
A $tree->print() serialization method might be useful for debugging.
The source code for this module contains a reusable template-based C++ header for Interval trees that might be useful.
Ben Booth, <bbooth@>
Copyright (C) 2010 by Ben Booth
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.1 or, at your option, any later version of Perl 5 you may have available.
To install Set::IntervalTree, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Set::IntervalTree
CPAN shell
perl -MCPAN -e shell install Set::IntervalTree
For more information on module installation, please visit the detailed CPAN module installation guide.