| 1 | package Tree::Interval::Fast; | ||
| 2 | |||
| 3 | 4 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 4 | our $VERSION = '0.0.1'; | |
| 8 | 4 | our $ENABLE_DEBUG = 0; | |
| 9 | |||
| 10 | 4 | require XSLoader; | |
| 11 | 4 | XSLoader::load('Tree::Interval::Fast', $VERSION); | |
| 12 | |||
| 13 | 4 | 1; # End of Tree::Interval::Fast | |
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Tree::Interval::Fast - Perl extension for efficient creation and manipulation of interval trees. | ||
| 18 | |||
| 19 | =head1 VERSION | ||
| 20 | |||
| 21 | Version 0.0.1 | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This module provides a simple and fast implementation of B<interval trees>. | ||
| 26 | It uses the Perl XS extension mechanism by providing a tiny wrapper around | ||
| 27 | an efficient C library which does the core of the work. | ||
| 28 | |||
| 29 | =head1 SEE ALSO | ||
| 30 | |||
| 31 | Tree::Interval::Fast::Interval - implements an interval stored in the tree | ||
| 32 | |||
| 33 | =head1 SYNOPSIS | ||
| 34 | |||
| 35 | You can create an interval tree, add intervals to it and ask for intervals | ||
| 36 | overlapping with a certain range. | ||
| 37 | |||
| 38 | use Tree::Interval::Fast; | ||
| 39 | use Tree::Interval::Fast::Interval; | ||
| 40 | |||
| 41 | # create the tree | ||
| 42 | my $foo = Tree::Interval::Fast->new(); | ||
| 43 | |||
| 44 | # add some intervals | ||
| 45 | $tree->insert(Tree::Interval::Fast::Interval->new(15, 20, 10)); | ||
| 46 | $tree->insert(Tree::Interval::Fast::Interval->new(10, 30, 20)); | ||
| 47 | $tree->insert(Tree::Interval::Fast::Interval->new(17, 19, 30)); | ||
| 48 | ... | ||
| 49 | |||
| 50 | # query the tree for an overlapping interval | ||
| 51 | my $result = $tree->find(6., 7.); | ||
| 52 | printf "(%.2f, %.2f)", $result->low, $result->high; | ||
| 53 | print Dumper $result->data; # overlapping interval might store data | ||
| 54 | |||
| 55 | # another (unsuccesful) query | ||
| 56 | $result = $tree->find(1, 4); | ||
| 57 | if(!$result) { | ||
| 58 | print "No overlapping interval with the query."; | ||
| 59 | } | ||
| 60 | |||
| 61 | # query the tree for all overlapping intervals | ||
| 62 | my $results = $tree->findall(8, 11); | ||
| 63 | foreach my $item (@{$results}) { | ||
| 64 | print "Query overlaps with (%.2f, %.2f)\n", $item->low, $item->high; | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | =head1 METHODS | ||
| 69 | |||
| 70 | =head2 C<new> | ||
| 71 | |||
| 72 | Arg [...] : None | ||
| 73 | |||
| 74 | Example : my $tree = Tree::Interval::Fast->new(); | ||
| 75 | carp "Unable to instantiate tree" unless defined $tree; | ||
| 76 | |||
| 77 | Description : Creates a new empty interval tree object. | ||
| 78 | |||
| 79 | Returntype : An instance of Tree::Interval::Fast or undef | ||
| 80 | Exceptions : None | ||
| 81 | Caller : General | ||
| 82 | Status : Stable | ||
| 83 | |||
| 84 | =head2 C<find> | ||
| 85 | |||
| 86 | Arg [1] : Number; query interval left boundary | ||
| 87 | Arg [2] : Number; query interval right boundary | ||
| 88 | |||
| 89 | Example : $result = $tree->find(6, 7); | ||
| 90 | if($result) { | ||
| 91 | printf "Found an overlapping interval: (%.2f, %.2f)\n", $result->low, $result->high | ||
| 92 | } else { print "No overlapping interval with (6, 7)\n"; } | ||
| 93 | |||
| 94 | Description : Query the tree for an overlapping interval with the specified range. | ||
| 95 | |||
| 96 | Returntype : An instance of Tree::Interval::Fast::Interval, representing the overlapping | ||
| 97 | interval, if found, as stored in the tree or undef. | ||
| 98 | Exceptions : None | ||
| 99 | Caller : General | ||
| 100 | Status : Unstable | ||
| 101 | |||
| 102 | =head2 C<findall> | ||
| 103 | |||
| 104 | Arg [1] : Number; query interval left boundary | ||
| 105 | Arg [2] : Number; query interval right boundary | ||
| 106 | |||
| 107 | Example : $results = $tree->findall(6, 7); | ||
| 108 | if(!$result) { | ||
| 109 | print "No overlapping intervals with (6, 7)\n"; | ||
| 110 | } else { | ||
| 111 | foreach my $item (@{$results}) { | ||
| 112 | print "Found (%.2f, %.2f)\n", $item->low, $item->high; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | Description : Query the tree for all overlapping intervals with the specified range. | ||
| 117 | |||
| 118 | Returntype : ArraryRef; the list of Tree::Interval::Fast::Interval instances, representing | ||
| 119 | each one an overlapping interval, or undef if no interval is found. | ||
| 120 | Exceptions : None | ||
| 121 | Caller : General | ||
| 122 | Status : Stable | ||
| 123 | |||
| 124 | =head2 C<insert> | ||
| 125 | |||
| 126 | Arg [1] : Tree::Interval::Fast::Interval; the interval to insert in the tree | ||
| 127 | |||
| 128 | Example : my $interval = Tree::Interval::Fast::Interval->new(100, 1000, [1, 2, 3]); | ||
| 129 | my $ok = $tree->insert($interval); | ||
| 130 | if($ok) { | ||
| 131 | printf "Could insert (100, 1000)\n"; | ||
| 132 | } else { print "Something went wrong\n"; } | ||
| 133 | |||
| 134 | Description : Insert an interval in the tree. | ||
| 135 | |||
| 136 | Returntype : True/False, depending on whether the insertion was successful or not | ||
| 137 | Exceptions : None | ||
| 138 | Caller : General | ||
| 139 | Status : Unstable | ||
| 140 | |||
| 141 | =head2 C<remove> | ||
| 142 | |||
| 143 | Arg [1] : Tree::Interval::Fast::Interval; the interval to remove from the tree | ||
| 144 | |||
| 145 | Example : my $interval = Tree::Interval::Fast::Interval->new(100, 1000, undef); | ||
| 146 | my $ok = $tree->remove($interval); | ||
| 147 | if($ok) { | ||
| 148 | printf "Could remove (100, 1000)\n"; | ||
| 149 | } else { print "Something went wrong\n"; } | ||
| 150 | |||
| 151 | Description : Remove an interval in the tree. | ||
| 152 | |||
| 153 | Returntype : True/False, depending on whether the operation was successful or not | ||
| 154 | Exceptions : None | ||
| 155 | Caller : General | ||
| 156 | Status : Unstable | ||
| 157 | |||
| 158 | =head2 C<size> | ||
| 159 | |||
| 160 | Arg [...] : None | ||
| 161 | |||
| 162 | Example : printf "Size of the tree: %d\n", $tree->size(); | ||
| 163 | |||
| 164 | Description : Get the size (number of intervals) of the tree. | ||
| 165 | |||
| 166 | Returntype : Int; the number of intervals currently stored in the tree. | ||
| 167 | Exceptions : None | ||
| 168 | Caller : General | ||
| 169 | Status : Stable | ||
| 170 | |||
| 171 | =head1 EXPORT | ||
| 172 | |||
| 173 | None | ||
| 174 | |||
| 175 | =head1 AUTHOR | ||
| 176 | |||
| 177 | Alessandro Vullo, C<< <avullo at cpan.org> >> | ||
| 178 | |||
| 179 | =head1 BUGS | ||
| 180 | |||
| 181 | Please report any bugs or feature requests to C<bug-tree-interval-fast at rt.cpan.org>, or through | ||
| 182 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Tree-Interval-Fast>. I will be notified, and then you'll | ||
| 183 | automatically be notified of progress on your bug as I make changes. | ||
| 184 | |||
| 185 | =head1 CONTRIBUTING | ||
| 186 | |||
| 187 | You can obtain the most recent development version of this module via the GitHub | ||
| 188 | repository at https://github.com/avullo/AVLTree. Please feel free to submit bug | ||
| 189 | reports, patches etc. | ||
| 190 | |||
| 191 | =head1 SUPPORT | ||
| 192 | |||
| 193 | You can find documentation for this module with the perldoc command. | ||
| 194 | |||
| 195 | perldoc Tree::Interval::Fast | ||
| 196 | |||
| 197 | |||
| 198 | You can also look for information at: | ||
| 199 | |||
| 200 | =over 4 | ||
| 201 | |||
| 202 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 203 | |||
| 204 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Tree-Interval-Fast> | ||
| 205 | |||
| 206 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 207 | |||
| 208 | L<http://annocpan.org/dist/Tree-Interval-Fast> | ||
| 209 | |||
| 210 | =item * CPAN Ratings | ||
| 211 | |||
| 212 | L<http://cpanratings.perl.org/d/Tree-Interval-Fast> | ||
| 213 | |||
| 214 | =item * Search CPAN | ||
| 215 | |||
| 216 | L<http://search.cpan.org/dist/Tree-Interval-Fast/> | ||
| 217 | |||
| 218 | =back | ||
| 219 | |||
| 220 | =head1 ACKNOWLEDGEMENTS | ||
| 221 | |||
| 222 | I am very grateful to Julienne Walker for generously providing the source | ||
| 223 | code of his production quality C library for handling AVL balanced trees. | ||
| 224 | The library has been adapted to implement interval trees. | ||
| 225 | |||
| 226 | Julienne's library can be found at: | ||
| 227 | |||
| 228 | http://www.eternallyconfuzzled.com/Libraries.aspx | ||
| 229 | |||
| 230 | |||
| 231 | =head1 LICENSE AND COPYRIGHT | ||
| 232 | |||
| 233 | Copyright 2018 Alessandro Vullo. | ||
| 234 | |||
| 235 | This program is free software; you can redistribute it and/or modify it | ||
| 236 | under the terms of either: the GNU General Public License as published | ||
| 237 | by the Free Software Foundation; or the Artistic License. | ||
| 238 | |||
| 239 | See L<http://dev.perl.org/licenses/> for more information. | ||
| 240 | |||
| 241 | |||
| 242 | =cut | ||