Skip to content

Commit

Permalink
Re-write based on AvlTreeSet from quiver.collection
Browse files Browse the repository at this point in the history
  • Loading branch information
jpnurmi committed Apr 27, 2020
1 parent bb98851 commit 17662c8
Show file tree
Hide file tree
Showing 8 changed files with 793 additions and 272 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
## [0.2.0] - 2020-04-27

* Re-wrote IntervalTree based on AvlTreeSet from quiver.collection
* Made IntervalTree automatically join and split appropriate intervals at
insertion and removal, respectively
* Added IntervalTree.of() and from() factory and named constructors
* Added IntervalTree.add(), addAll(), remove(), removeAll(), and clear()
methods for managing intervals in the tree
* Added IntervalTree.union(), intersection() and difference() methods
* Made IntervalTree accept array literals like `[a,b]` in place of Interval
objects
* Made IntervalTree inherit IterableMixin<Interval> to offer all standard
iterable operations
* Made Interval immutable
* Renamed Interval.stop to end
* Added Interval.length property
* Added Interval.union(), intersection() and difference() methods

## [0.1.0] - 2020-04-23

* Initial Dart port of a minimal C++ interval tree implementation
Expand Down
5 changes: 0 additions & 5 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
Copyright (c) 2020 J-P Nurmi <jpnurmi@gmail.com>

Based on a minimal C++ interval tree implementation:
https://github.com/ekg/intervaltree

Copyright (c) 2011 Erik Garrison <erik.garrison@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
Expand Down
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
[![pub](https://img.shields.io/pub/v/interval_tree.svg)](https://pub.dev/packages/interval_tree)
[![license: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
![coverage](https://raw.githubusercontent.com/jpnurmi/interval_tree/master/coverage_badge.svg)

# interval_tree

This is a Dart port of [a minimal C++ interval tree implementation](https://github.com/ekg/intervaltree).
A [Dart][1] implementation of an [interval tree][2], with support for
calculating unions, intersections, and differences between individual
intervals, or entire iterable collections of intervals, such as other
interval trees.

## Mutable

IntervalTree has support for adding and removing intervals, or entire
iterable collections of intervals, such as other interval trees.

## Non-overlapping

IntervalTree automatically joins and splits appropriate intervals at
insertions and removals, respectively, whilst maintaining a collection
of non-overlapping intervals.

## Iterable

IntervalTree is an [iterable collection][3] offering all standard
iterable operations, such as easily accessing the first and last
interval.

## History

IntervalTree started off as a quick and dirty Dart port of Erik
Garrison's [simple C++ interval tree implementation][4], but was soon
re-written and based on [quiver.collection's][6] AVL implementation of
a self-balancing binary tree [AvlTreeSet][7].

## License

IntervalTree is licensed under the [MIT][5] license.

[1]: https://dart.dev
[2]: https://en.wikipedia.org/wiki/Interval_tree
[3]: https://dart.dev/codelabs/iterables
[4]: https://github.com/ekg/intervaltree
[5]: https://opensource.org/licenses/MIT
[6]: https://pub.dev/packages/quiver
[7]: https://pub.dev/documentation/quiver/latest/quiver.collection/AvlTreeSet-class.html
64 changes: 56 additions & 8 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,65 @@
* SOFTWARE.
*/

import 'package:interval_tree/interval_tree.dart';
import 'package:interval_tree/interval_tree.dart' as iv;

void main() {
// Construct a tree:
IntervalTree tree = IntervalTree([Interval(1, 5), Interval(5, 10), Interval(10, 15)]);
final iv.IntervalTree tree = iv.IntervalTree.from([
[1, 3],
[5, 8],
[10, 15]
]);

// Find intervals contained within given points or intervals:
print(tree.findContained(5)); // [Interval(1, 5), Interval(5, 10)]
print(tree.findContained(5, 10)); // [Interval(5, 10)]
// Add an interval:
tree.add([2, 6]);
print(tree); // IntervalTree([1, 8], [10, 15])

// Find intervals overlapping with given points or intervals:
print(tree.findOverlapping(5)); // [Interval(1, 5), Interval(5, 10)]
print(tree.findOverlapping(5, 10)); // [Interval(1, 5), Interval(5, 10), Interval(10, 15)]
// Remove an interval:
tree.remove([12, 16]);
print(tree); // IntervalTree([1, 8], [10, 12])

// Calculate union/intersection/difference:
final iv.IntervalTree other = iv.IntervalTree.from([
[0, 2],
[5, 7]
]);
print(tree.union(other)); // IntervalTree([0, 8], [10, 12])
print(tree.intersection(other)); // IntervalTree([1, 2], [5, 7])
print(tree.difference(other)); // IntervalTree([2, 5], [7, 8], [10, 12])

{
final a = iv.Interval(0, 3);
final b = iv.Interval(2, 5);
print(a.union(b)); // [[0, 5]]
print(a.intersection(b)); // [2,3]
print(a.difference(b)); // [[0, 2]]
}

{
final iv.IntervalTree tree = iv.IntervalTree.from([
[1, 3],
[5, 8],
[10, 15]
]);
print(tree); // IntervalTree([1, 3], [5, 8], [10, 15])
tree.add([2, 6]);
print(tree); // IntervalTree([1, 8], [10, 15])
tree.remove([12, 16]);
print(tree); // IntervalTree([1, 8], [10, 12])

final iv.IntervalTree other = iv.IntervalTree.from([
[0, 2],
[5, 7]
]);
print(tree.union(other)); // IntervalTree([0, 8], [10, 12])
print(tree.intersection(other)); // IntervalTree([1, 2], [5, 7])
print(tree.difference(other)); // IntervalTree([2, 5], [7, 8], [10, 12])

for (final interval in tree) {
print(interval); // [1, 8] \n [10, 12]
}
print(tree.first); // [1, 8]
print(tree.last); // [10, 12]
}
}
Loading

0 comments on commit 17662c8

Please sign in to comment.