| 1 | |
| 2 | [![Build Status]][travis] [![Latest Version]][crates.io] [![Docs Badge]][docs.rs] |
| 3 | |
| 4 | # Sorted Iter |
| 5 | |
| 6 | [Build Status]: https://api.travis-ci.org/rklaehn/sorted-iter.svg?branch=master |
| 7 | [travis]: https://travis-ci.org/rklaehn/sorted-iter |
| 8 | [Latest Version]: https://img.shields.io/crates/v/sorted-iter.svg |
| 9 | [crates.io]: https://crates.io/crates/sorted-iter |
| 10 | [Docs Badge]: https://img.shields.io/badge/docs-docs.rs-green |
| 11 | [docs.rs]: https://docs.rs/sorted-iter |
| 12 | |
| 13 | # About |
| 14 | |
| 15 | <!-- cargo-sync-readme start --> |
| 16 | |
| 17 | This crate provides set and relational operations for all iterators in the standard library that are known |
| 18 | at compile time to be sorted. |
| 19 | |
| 20 | # Set operations |
| 21 | ```rust |
| 22 | use sorted_iter::SortedIterator; |
| 23 | |
| 24 | let primes = btreeset! { 2, 3, 5, 7, 11, 13u64 }.into_iter(); |
| 25 | let fibs = btreeset! { 1, 2, 3, 5, 8, 13u64 }.into_iter(); |
| 26 | let fib_primes = primes.intersection(fibs); |
| 27 | ``` |
| 28 | |
| 29 | It is possible to efficiently define set operations on sorted iterators. Sorted iterators are |
| 30 | very common in the standard library. E.g. the elements of a [BTreeSet] or the keys of a [BTreeMap] |
| 31 | are guaranteed to be sorted according to the element order, as are iterable ranges like `0..100`. |
| 32 | |
| 33 | There are also a number of operations on iterators that preserve the sort order. E.g. if an |
| 34 | iterator is sorted, [take], [take_while] etc. are going to result in a sorted iterator as well. |
| 35 | |
| 36 | Since the complete types of iterators are typically visible in rust, it is possible to encode these |
| 37 | rules at type level. This is what this crate does. |
| 38 | |
| 39 | For available set operations, see [SortedIterator]. |
| 40 | For sorted iterators in the std lib, see instances the for [SortedByItem] marker trait. |
| 41 | |
| 42 | # Relational operations |
| 43 | ```rust |
| 44 | use sorted_iter::SortedPairIterator; |
| 45 | |
| 46 | let cities = btreemap! { |
| 47 | 1 => "New York", |
| 48 | 2 => "Tokyo", |
| 49 | 3u8 => "Berlin" |
| 50 | }.into_iter(); |
| 51 | let countries = btreemap! { |
| 52 | 1 => "USA", |
| 53 | 2 => "Japan", |
| 54 | 3u8 => "Germany" |
| 55 | }.into_iter(); |
| 56 | let cities_and_countries = cities.join(countries); |
| 57 | ``` |
| 58 | |
| 59 | Iterators of pairs that are sorted according to the first element / key are also very common in |
| 60 | the standard library and elsewhere. E.g. the elements of a [BTreeMap] are guaranteed to be sorted |
| 61 | according to the key order. |
| 62 | |
| 63 | The same rules as for sorted iterators apply for preservation of the sort order, except that there |
| 64 | are some additional operations that preserve sort order. Anything that only operates on the value, |
| 65 | like e.g. map or filter_map on the value, is guaranteed to preserve the sort order. |
| 66 | |
| 67 | The operations that can be defined on sorted pair operations are the relational operations known |
| 68 | from relational algebra / SQL, namely join, left_join, right_join and outer_join. |
| 69 | |
| 70 | For available relational operations, see [SortedPairIterator]. |
| 71 | For sorted iterators in the std lib, see instances the for [SortedByKey] marker trait. |
| 72 | |
| 73 | # Transformations that retain order are allowed |
| 74 | ```rust |
| 75 | use sorted_iter::*; |
| 76 | |
| 77 | let odd = (1..31).step_by(2); |
| 78 | let multiples_of_3 = (3..30).step_by(3); |
| 79 | let either = odd.union(multiples_of_3); |
| 80 | ``` |
| 81 | |
| 82 | # Transformations that can change the order lose the sorted property |
| 83 | ```compile_fail |
| 84 | use sorted_iter::*; |
| 85 | |
| 86 | // we have no idea what map does to the order. could be anything! |
| 87 | let a = (1..31).map(|x| -x); |
| 88 | let b = (3..30).step_by(3); |
| 89 | let either = a.union(b); // does not compile! |
| 90 | ``` |
| 91 | |
| 92 | # Assuming sort ordering |
| 93 | |
| 94 | For most std lib iterators, this library already provides instances. But there will occasionally be an iterator |
| 95 | from a third party library where you *know* that it is properly sorted. |
| 96 | |
| 97 | For this case, there is an escape hatch: |
| 98 | |
| 99 | ```rust |
| 100 | // the assume_ extensions have to be implicitly imported |
| 101 | use sorted_iter::*; |
| 102 | use sorted_iter::assume::*; |
| 103 | let odd = vec![1,3,5,7u8].into_iter().assume_sorted_by_item(); |
| 104 | let even = vec![2,4,6,8u8].into_iter().assume_sorted_by_item(); |
| 105 | let all = odd.union(even); |
| 106 | |
| 107 | let cities = vec![(1u8, "New York")].into_iter().assume_sorted_by_key(); |
| 108 | let countries = vec![(1u8, "USA")].into_iter().assume_sorted_by_key(); |
| 109 | let cities_and_countries = cities.join(countries); |
| 110 | ``` |
| 111 | |
| 112 | # Marking your own iterators |
| 113 | |
| 114 | If you have a library and want to mark some iterators as sorted, this is possible by implementing the |
| 115 | appropriate marker trait, [SortedByItem] or [SortedByKey]. |
| 116 | |
| 117 | ```rust |
| 118 | // marker traits are not at top level, since usually you don't need them |
| 119 | use sorted_iter::sorted_iterator::SortedByItem; |
| 120 | use sorted_iter::sorted_pair_iterator::SortedByKey; |
| 121 | |
| 122 | pub struct MySortedIter<T> { whatever: T } |
| 123 | pub struct MySortedPairIter<K, V> { whatever: (K, V) } |
| 124 | |
| 125 | impl<T> SortedByItem for MySortedIter<T> {} |
| 126 | impl<K, V> SortedByKey for MySortedPairIter<K, V> {} |
| 127 | ``` |
| 128 | |
| 129 | By reexporting the extension traits, you get a seamless experience for people using your library. |
| 130 | |
| 131 | ```rust |
| 132 | extern crate sorted_iter; |
| 133 | pub use sorted_iter::{SortedIterator, SortedPairIterator}; |
| 134 | ``` |
| 135 | |
| 136 | ## Tests |
| 137 | |
| 138 | Tests are done using the fantastic [quickcheck] crate, by comparing against the operations defined on |
| 139 | [BTreeSet] and [BTreeMap]. |
| 140 | |
| 141 | [SortedIterator]: trait.SortedIterator.html |
| 142 | [SortedPairIterator]: trait.SortedPairIterator.html |
| 143 | [SortedByItem]: sorted_iterator/trait.SortedByItem.html |
| 144 | [SortedByKey]: sorted_pair_iterator/trait.SortedByKey.html |
| 145 | [quickcheck]: https://github.com/BurntSushi/quickcheck |
| 146 | [BTreeSet]: https://doc.rust-lang.org/std/collections/struct.BTreeSet.html |
| 147 | [BTreeMap]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html |
| 148 | [take]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.take |
| 149 | [take_while]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.take_while |
| 150 | [Ord]: https://doc.rust-lang.org/std/cmp/trait.Ord.html |
| 151 | |
| 152 | <!-- cargo-sync-readme end --> |