![Build Status] ![Latest Version] ![Docs Badge]
This crate provides set and relational operations for all iterators in the standard library that are known at compile time to be sorted.
```rust use sorted_iter::SortedIterator;
let primes = btreeset! { 2, 3, 5, 7, 11, 13u64 }.intoiter(); let fibs = btreeset! { 1, 2, 3, 5, 8, 13u64 }.intoiter(); let fib_primes = primes.intersection(fibs); ```
It is possible to efficiently define set operations on sorted iterators. Sorted iterators are
very common in the standard library. E.g. the elements of a [BTreeSet] or the keys of a [BTreeMap]
are guaranteed to be sorted according to the element order, as are iterable ranges like 0..100
.
There are also a number of operations on iterators that preserve the sort order. E.g. if an iterator is sorted, [take], [take_while] etc. are going to result in a sorted iterator as well.
Since the complete types of iterators are typically visible in rust, it is possible to encode these rules at type level. This is what this crate does.
For available set operations, see [SortedIterator]. For sorted iterators in the std lib, see instances the for [SortedByItem] marker trait.
```rust use sorted_iter::SortedPairIterator;
let cities = btreemap! { 1 => "New York", 2 => "Tokyo", 3u8 => "Berlin" }.intoiter(); let countries = btreemap! { 1 => "USA", 2 => "Japan", 3u8 => "Germany" }.intoiter(); let citiesandcountries = cities.join(countries); ```
Iterators of pairs that are sorted according to the first element / key are also very common in the standard library and elsewhere. E.g. the elements of a [BTreeMap] are guaranteed to be sorted according to the key order.
The same rules as for sorted iterators apply for preservation of the sort order, except that there are some additional operations that preserve sort order. Anything that only operates on the value, like e.g. map or filter_map on the value, is guaranteed to preserve the sort order.
The operations that can be defined on sorted pair operations are the relational operations known from relational algebra / SQL, namely join, leftjoin, rightjoin and outer_join.
For available relational operations, see [SortedPairIterator]. For sorted iterators in the std lib, see instances the for [SortedByKey] marker trait.
```rust use sorted_iter::*;
let odd = (1..31).stepby(2); let multiplesof3 = (3..30).stepby(3); let either = odd.union(multiplesof3); ```
```compilefail use sortediter::*;
// we have no idea what map does to the order. could be anything! let a = (1..31).map(|x| -x); let b = (3..30).step_by(3); let either = a.union(b); // does not compile! ```
For most std lib iterators, this library already provides instances. But there will occasionally be an iterator from a third party library where you know that it is properly sorted.
For this case, there is an escape hatch:
```rust // the assume_ extensions have to be implicitly imported use sortediter::*; use sortediter::assume::*; let odd = vec![1,3,5,7u8].intoiter().assumesortedbyitem(); let even = vec![2,4,6,8u8].intoiter().assumesortedbyitem(); let all = odd.union(even);
let cities = vec![(1u8, "New York")].intoiter().assumesortedbykey(); let countries = vec![(1u8, "USA")].intoiter().assumesortedbykey(); let citiesandcountries = cities.join(countries); ```
If you have a library and want to mark some iterators as sorted, this is possible by implementing the appropriate marker trait, [SortedByItem] or [SortedByKey].
```rust // marker traits are not at top level, since usually you don't need them use sortediter::sortediterator::SortedByItem; use sortediter::sortedpair_iterator::SortedByKey;
pub struct MySortedIter
impl
By reexporting the extension traits, you get a seamless experience for people using your library.
rust
extern crate sorted_iter;
pub use sorted_iter::{SortedIterator, SortedPairIterator};
Tests are done using the fantastic [quickcheck] crate, by comparing against the operations defined on [BTreeSet] and [BTreeMap].