Set::I...valTree(3pm)
π Set::IntervalTree β the whole vibe (from the Set::I...valTree(3pm) page, User Contributed Perl Documentation) π
okay bestie, gather round, because this one is genuinely adorable: it's a Perl module for doing range-based lookups on sets of ranges. Yes. Ranges. On ranges. Iconic behaviour, honestly.
β¨ NAME β¨
Set::IntervalTree β perform range-based lookups on sets of ranges. that's it, that's the whole personality π
πΈ VERSION πΈ
version 0.12 β she's grown, she's stable, she's ready.
π¦ SYNOPSIS π¦
here's the whole thing in action, copy-paste ready, no cap:
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->fetch_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;
});
(also: yes, the remove line above is written exactly like that in the original. bestie, I do not make the rules, I just report them π)
π DESCRIPTION π
Set::IntervalTree uses Interval Trees to store ranges and then look them up efficiently using a range-based lookup. that's the whole serve. that's the assignment.
and the one thing you MUST internalise, or nothing will make sense ever again:
all intervals are half-open, i.e.[1,3),[2,6), etc. the low end is in, the high end is out. periodt. π
π«Ά EXPORTS π«Ά
Nothing. she gives you nothing and expects nothing back. boundaries queen. π
π METHODS π
π my $tree = Set::IntervalTree->new;
Creates a new interval tree object. brand new, fresh out the box, main character energy. β¨
π $tree->insert($object, $low, $high);
Insert a range into the interval tree and associate it with a perl scalar.
$objectcan be any perl scalar. This is what will be returned byfetch(). so put literally whatever you want in there. a string, a number, a hashref, your whole situationship.$lowis the lower bound of the range.$highis the upper bound of the range.
Ranges are represented as half-closed integer intervals. (remember [low, high) bestie!! the high end is NOT invited to the party.)
πΈ my $results = $tree->fetch($low, $high)
Return an arrayref of perl objects whose ranges overlap the specified range. overlap, not contain β even a little bit of touching counts, it's giving "we were in the same room once".
$lowis the lower bound of the region to query.$highis 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. yes: "witin". that's the original spelling, I'm not touching it, it's part of the lore π
$lowis the lower bound of the region to query.$highis the upper bound of the region to query.
π¦ my $nearest_up = $tree->fetch_nearest_up($query)
Search for the closest interval in upstream that does not contain the query, and returns the perl object associated with it.
$queryis the position to use for the search
π§ my $nearest_down = $tree->fetch_nearest_down($query)
Search for the closest interval in downstream that does not contain the query, and returns the perl object associated with it.
$queryis the position to use for the search
π
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. it's a little bouncer at the door, deciding who gets to leave π
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.
π LIMITATIONS π
A $tree->print() serialization method might be useful for debugging.
that's it. that's the limitation. she simply does not have one, and the docs are just out here like "hmm, that would've been nice huh" π
π₯ SEE ALSO π₯
The source code for this module contains a reusable template-based C++ header for Interval trees that might be useful. iykyk. go read the source if you want the deep lore.
π AUTHORS π
- Benjamin Booth <benbooth@cpan.org>
- Stephan Loyd <sloyd@cpan.org>
two legends, one module. ate and left no crumbs π«Ά
β COPYRIGHT AND LICENSE β
This software is copyright (c) 2012 by Benjamin Booth.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
and that's the whole page bestie!! π this documentation shipped as part of perl v5.42.3, dated 2026-08-19 β mwah, go insert some intervals, you're gonna slay πβ¨π
π¦ where this page comes from
- ubuntu stonking: libset-intervaltree-perl 0.12-2build6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms