├── .gitignore ├── .travis.yml ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── devbox └── Dockerfile ├── docker-dev.yml └── src └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /Cargo.lock 3 | *.bk -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | 3 | rust: 4 | - stable 5 | - beta 6 | - nightly -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | 3 | name = "lazysort" 4 | version = "0.2.1" 5 | authors = ["Ben Ashford"] 6 | license = "MIT/Apache-2.0" 7 | repository = "https://github.com/benashford/rust-lazysort" 8 | description = "Lazy sorting for iterators" 9 | readme = "README.md" 10 | 11 | [features] 12 | nightly = [] 13 | 14 | [dev-dependencies] 15 | 16 | rand = ">= 0.3, <= 0.5" 17 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright 2016 rust-lazysort developers 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Lazysort 2 | 3 | [![Build Status](https://travis-ci.org/benashford/rust-lazysort.svg?branch=master)](https://travis-ci.org/benashford/rust-lazysort) 4 | [![](http://meritbadge.herokuapp.com/lazysort)](https://crates.io/crates/lazysort) 5 | [![](https://img.shields.io/crates/d/lazysort.svg)](https://crates.io/crates/lazysort) 6 | [![](https://img.shields.io/crates/dv/lazysort.svg)](https://crates.io/crates/lazysort) 7 | [![](https://docs.rs/lazysort/badge.svg)](https://docs.rs/lazysort/) 8 | 9 | Adds a method to iterators that returns a sorted iterator over the data. The sorting is achieved lazily using a quicksort algorithm. 10 | 11 | Available via [crates.io](https://crates.io/crates/lazysort). 12 | 13 | ## Usage 14 | 15 | ```rust 16 | extern crate lazysort; 17 | 18 | use lazysort::Sorted; 19 | 20 | use lazysort::SortedBy; 21 | 22 | use lazysort::SortedPartial; 23 | ``` 24 | 25 | The `Sorted` trait adds a method `sorted` to all `Iterator` which returns an iterator over the same data in default order. 26 | 27 | The `SortedBy` trait adds a method `sorted_by` to all `Iterator` which returns an iterator over the same data ordered according to the provided closure/function of type `Fn(&T, &T) -> Ordering` 28 | 29 | The `SortedPartial` trait adds two methods `sorted_partial_first` and `sorted_partial_last` to all `Iterator` which returns an iterator over the same data in the default order. The difference between the two is whether non-comparable values go first or last in the results. 30 | 31 | For example: 32 | 33 | ```rust 34 | let data: Vec = vec![9, 1, 3, 4, 4, 2, 4]; 35 | for x in data.iter().sorted() { 36 | println!("{}", x); 37 | } 38 | ``` 39 | 40 | Will print: 1, 2, 3, 4, 4, 4, 9 41 | 42 | A more complex example. Sort strings by length, then in default string order: 43 | 44 | ```rust 45 | let before: Vec<&str> = vec!["a", "cat", "sat", "on", "the", "mat"]; 46 | before.iter().sorted_by(|a, b| { 47 | match a.len().cmp(&b.len()) { 48 | Equal => a.cmp(b), 49 | x => x 50 | } 51 | }) 52 | ``` 53 | 54 | This returns an iterator which yields: `a`, `on`, `cat`, `mat`, `sat`, `the`. 55 | 56 | ## Implementation details and performance 57 | 58 | The algorithm is essentially the same as described in my blog post [using a lazy sort as an example of Clojure's lazy sequences](http://benashford.github.io/blog/2014/03/22/the-power-of-lazy-sequences/). But made to fit in with Rust's iterators. 59 | 60 | The full sequence from the parent iterator is read, then each call to `next` returns the next value in the sorted sequence. The sort is done element-by-element so the full order is only realised by iterating all the way through to the end. 61 | 62 | The algorithm is the quicksort, but depth-first; upon each call to `next` it does the work necessary to find the next item then pauses the state until the next call to `next`. 63 | 64 | To test performance we compare it against sorting the full vector, using the `sort` function from the standard library, and also against `std::collections::BinaryHeap`. 65 | 66 | First we compare what happens when sorting the entire vector: 67 | 68 | ``` 69 | test benches::c_heap_bench ... bench: 3,703,166 ns/iter (+/- 454,189) 70 | test benches::c_lazy_bench ... bench: 3,961,047 ns/iter (+/- 603,083) 71 | test benches::c_standard_bench ... bench: 3,093,873 ns/iter (+/- 430,401) 72 | ``` 73 | 74 | There are differences between the three, and not surprisingly the built-in sort is fastest. 75 | 76 | These benchmarks are for sorting 50,000 random `uint`s in the range 0 <= x < 1000000. Run `cargo bench` to run them. 77 | 78 | So what's the point of lazy sorting? As per the linked blog post, they're useful when you do not need or intend to need every value; for example you may only need the first 1,000 ordered values from a larger set. 79 | 80 | Comparing the lazy approach `data.iter().sorted().take(x)` vs a standard approach of sorting a vector then taking the first `x` values gives the following. 81 | 82 | The first 1,000 out of 50,000: 83 | 84 | ``` 85 | test benches::a_heap_bench ... bench: 366,767 ns/iter (+/- 55,393) 86 | test benches::a_lazy_bench ... bench: 171,923 ns/iter (+/- 52,784) 87 | test benches::a_standard_bench ... bench: 3,055,734 ns/iter (+/- 348,407) 88 | ``` 89 | 90 | The lazy approach is quite a bit faster; this is due to the 50,000 only being sorted enough to identify the first 1,000, the rest remain unsorted. `BinaryHeap` is also quite fast, for the same reason. 91 | 92 | The first 10,000 out of 50,000: 93 | 94 | ``` 95 | test benches::b_heap_bench ... bench: 1,126,774 ns/iter (+/- 156,833) 96 | test benches::b_lazy_bench ... bench: 993,954 ns/iter (+/- 208,188) 97 | test benches::b_standard_bench ... bench: 3,054,598 ns/iter (+/- 285,970) 98 | ``` 99 | 100 | The lazy approach is still faster in this situation. 101 | 102 | ## License 103 | 104 | Licensed under either of 105 | 106 | * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) 107 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) 108 | 109 | at your option. 110 | 111 | ### Contribution 112 | 113 | Unless you explicitly state otherwise, any contribution intentionally submitted 114 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any 115 | additional terms or conditions. 116 | -------------------------------------------------------------------------------- /devbox/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM devbox-rust-nightly -------------------------------------------------------------------------------- /docker-dev.yml: -------------------------------------------------------------------------------- 1 | dev: 2 | build: devbox/ 3 | ports: 4 | - "2224:4444" 5 | volumes: 6 | - .:/home/ben/src -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2018 rust-lazysort developers 3 | * 4 | * Licensed under the Apache License, Version 2.0 or the MIT license 6 | * , at your 7 | * option. This file may not be copied, modified, or distributed 8 | * except according to those terms. 9 | */ 10 | 11 | #![crate_type = "lib"] 12 | #![crate_name = "lazysort"] 13 | #![cfg_attr(feature = "nightly", feature(test))] 14 | 15 | use std::cmp::Ordering; 16 | use std::cmp::Ordering::{Greater, Less}; 17 | 18 | fn pivot(lower: usize, upper: usize) -> usize { 19 | upper + ((lower - upper) / 2) 20 | } 21 | 22 | #[inline(always)] 23 | unsafe fn cmp_by(by: &F, data: &mut [T], a: usize, b: usize) -> Ordering 24 | where 25 | F: Fn(&T, &T) -> Ordering, 26 | { 27 | debug_assert!(a < data.len()); 28 | debug_assert!(b < data.len()); 29 | by(data.get_unchecked(a), data.get_unchecked(b)) 30 | } 31 | 32 | fn partition(by: &F, data: &mut [T], lower: usize, upper: usize, p: usize) -> usize 33 | where 34 | F: Fn(&T, &T) -> Ordering, 35 | { 36 | // To make things more fun - well there is a real reason, which is that we can 37 | // simply `pop` values to remove the lowest value - the lower values are stored 38 | // at the higher indexes. So in this function `lower` will actually be higher 39 | // than `upper` 40 | 41 | unsafe { 42 | let mut i = upper; 43 | let mut nextp = upper; 44 | 45 | data.swap(lower, p); 46 | 47 | while i < lower { 48 | if cmp_by(by, data, i, lower) == Greater { 49 | if i != nextp { 50 | data.swap(i, nextp); 51 | } 52 | nextp += 1; 53 | } 54 | i += 1; 55 | } 56 | 57 | data.swap(nextp, lower); 58 | nextp 59 | } 60 | } 61 | 62 | fn qsort( 63 | by: &F, 64 | data: &mut Vec, 65 | work: &mut Vec<(usize, usize)>, 66 | lower: usize, 67 | upper: usize, 68 | ) -> T 69 | where 70 | F: Fn(&T, &T) -> Ordering, 71 | { 72 | // If lower and upper are the same, then just pop the next value 73 | // If lower and upper are adjacent, then manually swap depending on ordering 74 | // everything else, do the next stage of a quick sort 75 | match lower - upper { 76 | 0 => data.pop().expect("Non empty vector"), 77 | 1 => unsafe { 78 | if cmp_by(by, data, lower, upper) == Greater { 79 | data.swap(lower, upper); 80 | } 81 | work.push((upper, upper)); 82 | data.pop().expect("Non empty vector") 83 | }, 84 | _ => { 85 | let p = pivot(lower, upper); 86 | let p = partition(by, data, lower, upper, p); 87 | if p == lower { 88 | work.push((p - 1, upper)); 89 | qsort(by, data, work, lower, p) 90 | } else { 91 | work.push((p, upper)); 92 | qsort(by, data, work, lower, p + 1) 93 | } 94 | } 95 | } 96 | } 97 | 98 | fn make_work(len: usize) -> Vec<(usize, usize)> { 99 | let mut work = Vec::with_capacity(len / 4); 100 | if len > 0 { 101 | work.push((len - 1, 0)); 102 | } 103 | work 104 | } 105 | 106 | macro_rules! lazy_sort_iter_struct { 107 | ($name:ident) => { 108 | pub struct $name { 109 | data: Vec, 110 | work: Vec<(usize, usize)>, 111 | } 112 | }; 113 | } 114 | 115 | macro_rules! lazy_sort_iter_struct_new { 116 | () => { 117 | fn new(data: Vec) -> Self { 118 | let work = make_work(data.len()); 119 | Self { 120 | data: data, 121 | work: work, 122 | } 123 | } 124 | }; 125 | } 126 | 127 | macro_rules! lazy_sort_iter_struct_qsort { 128 | ($cmp_f:path) => { 129 | fn qsort(&mut self, lower: usize, upper: usize) -> T { 130 | qsort(&$cmp_f, &mut self.data, &mut self.work, lower, upper) 131 | } 132 | } 133 | } 134 | 135 | lazy_sort_iter_struct!(LazySortIterator); 136 | 137 | impl LazySortIterator 138 | where 139 | T: Ord, 140 | { 141 | lazy_sort_iter_struct_new!(); 142 | lazy_sort_iter_struct_qsort!(Ord::cmp); 143 | } 144 | 145 | fn partial_cmp_first(a: &T, b: &T) -> Ordering { 146 | match a.partial_cmp(b) { 147 | Some(order) => order, 148 | None => Less, 149 | } 150 | } 151 | 152 | fn partial_cmp_last(a: &T, b: &T) -> Ordering { 153 | match a.partial_cmp(b) { 154 | Some(order) => order, 155 | None => Greater, 156 | } 157 | } 158 | 159 | lazy_sort_iter_struct!(LazySortIteratorPartialFirst); 160 | lazy_sort_iter_struct!(LazySortIteratorPartialLast); 161 | 162 | impl LazySortIteratorPartialFirst 163 | where 164 | T: PartialOrd, 165 | { 166 | lazy_sort_iter_struct_new!(); 167 | lazy_sort_iter_struct_qsort!(partial_cmp_first); 168 | } 169 | 170 | impl LazySortIteratorPartialLast 171 | where 172 | T: PartialOrd, 173 | { 174 | lazy_sort_iter_struct_new!(); 175 | lazy_sort_iter_struct_qsort!(partial_cmp_last); 176 | } 177 | 178 | pub struct LazySortIteratorBy { 179 | data: Vec, 180 | work: Vec<(usize, usize)>, 181 | by: F, 182 | } 183 | 184 | impl LazySortIteratorBy 185 | where 186 | F: Fn(&T, &T) -> Ordering, 187 | { 188 | fn new(data: Vec, by: F) -> Self { 189 | let work = make_work(data.len()); 190 | LazySortIteratorBy { 191 | data: data, 192 | work: work, 193 | by: by, 194 | } 195 | } 196 | 197 | fn qsort(&mut self, lower: usize, upper: usize) -> T { 198 | qsort(&self.by, &mut self.data, &mut self.work, lower, upper) 199 | } 200 | } 201 | 202 | pub trait Sorted { 203 | type Item: Ord; 204 | 205 | fn sorted(self) -> LazySortIterator; 206 | } 207 | 208 | pub trait SortedPartial { 209 | type Item: PartialOrd; 210 | 211 | fn sorted_partial_first(self) -> LazySortIteratorPartialFirst; 212 | fn sorted_partial_last(self) -> LazySortIteratorPartialLast; 213 | } 214 | 215 | pub trait SortedBy { 216 | type Item; 217 | 218 | fn sorted_by(self, F) -> LazySortIteratorBy 219 | where 220 | F: Fn(&Self::Item, &Self::Item) -> Ordering; 221 | } 222 | 223 | impl Sorted for I 224 | where 225 | T: Eq + Ord, 226 | I: Iterator, 227 | { 228 | type Item = T; 229 | 230 | fn sorted(self) -> LazySortIterator { 231 | LazySortIterator::new(self.collect()) 232 | } 233 | } 234 | 235 | impl SortedPartial for I 236 | where 237 | T: PartialOrd, 238 | I: Iterator, 239 | { 240 | type Item = T; 241 | 242 | fn sorted_partial_first(self) -> LazySortIteratorPartialFirst { 243 | LazySortIteratorPartialFirst::new(self.collect()) 244 | } 245 | 246 | fn sorted_partial_last(self) -> LazySortIteratorPartialLast { 247 | LazySortIteratorPartialLast::new(self.collect()) 248 | } 249 | } 250 | 251 | impl SortedBy for I 252 | where 253 | I: Iterator, 254 | { 255 | type Item = T; 256 | 257 | fn sorted_by(self, by: F) -> LazySortIteratorBy 258 | where 259 | F: Fn(&T, &T) -> Ordering, 260 | { 261 | LazySortIteratorBy::new(self.collect(), by) 262 | } 263 | } 264 | 265 | macro_rules! add_next { 266 | () => { 267 | #[inline] 268 | fn next(&mut self) -> Option { 269 | match self.work.pop() { 270 | Some((lower, upper)) => Some(self.qsort(lower, upper)), 271 | None => None 272 | } 273 | } 274 | } 275 | } 276 | 277 | macro_rules! add_size_hint { 278 | () => { 279 | #[inline] 280 | fn size_hint(&self) -> (usize, Option) { 281 | let l = self.data.len(); 282 | (l, Some(l)) 283 | } 284 | } 285 | } 286 | 287 | impl Iterator for LazySortIterator 288 | where 289 | T: Ord, 290 | { 291 | type Item = T; 292 | 293 | add_next!(); 294 | add_size_hint!(); 295 | } 296 | 297 | impl Iterator for LazySortIteratorPartialFirst 298 | where 299 | T: PartialOrd, 300 | { 301 | type Item = T; 302 | 303 | add_next!(); 304 | add_size_hint!(); 305 | } 306 | 307 | impl Iterator for LazySortIteratorPartialLast 308 | where 309 | T: PartialOrd, 310 | { 311 | type Item = T; 312 | 313 | add_next!(); 314 | add_size_hint!(); 315 | } 316 | 317 | impl Iterator for LazySortIteratorBy 318 | where 319 | F: Fn(&T, &T) -> Ordering, 320 | { 321 | type Item = T; 322 | 323 | add_next!(); 324 | add_size_hint!(); 325 | } 326 | 327 | #[cfg(test)] 328 | mod tests { 329 | extern crate rand; 330 | 331 | use super::Sorted; 332 | use super::SortedBy; 333 | use super::SortedPartial; 334 | 335 | use std::cmp::Ordering::Equal; 336 | 337 | #[test] 338 | fn single_test() { 339 | let expected: Vec = vec![1]; 340 | let before: Vec = vec![1]; 341 | let after: Vec = before.into_iter().sorted().collect(); 342 | 343 | assert_eq!(expected, after); 344 | } 345 | 346 | #[test] 347 | fn pair_test() { 348 | let expected: Vec = vec![1, 2]; 349 | let before_ordered: Vec = vec![1, 2]; 350 | let before_unordered: Vec = vec![2, 1]; 351 | 352 | let after_ordered: Vec = before_ordered.into_iter().sorted().collect(); 353 | assert_eq!(expected, after_ordered); 354 | 355 | let after_unordered: Vec = before_unordered.into_iter().sorted().collect(); 356 | assert_eq!(expected, after_unordered); 357 | } 358 | 359 | #[test] 360 | fn sorted_test() { 361 | let expected: Vec = vec![1u64, 1, 1, 3, 4, 6, 7, 9, 22]; 362 | let before: Vec = vec![9u64, 7, 1, 1, 6, 3, 1, 4, 22]; 363 | let after: Vec = before.iter().sorted().cloned().collect(); 364 | 365 | assert_eq!(expected, after); 366 | } 367 | 368 | #[test] 369 | fn sorted_strings_test() { 370 | let expected: Vec<&str> = vec!["a", "cat", "mat", "on", "sat", "the"]; 371 | let before: Vec<&str> = vec!["a", "cat", "sat", "on", "the", "mat"]; 372 | let after: Vec<&str> = before.iter().sorted().cloned().collect(); 373 | 374 | assert_eq!(expected, after); 375 | } 376 | 377 | #[test] 378 | fn sorted_string_length() { 379 | let expected: Vec<&str> = vec!["a", "on", "cat", "mat", "sat", "the"]; 380 | let before: Vec<&str> = vec!["a", "cat", "sat", "on", "the", "mat"]; 381 | let after: Vec<&str> = before 382 | .iter() 383 | .sorted_by(|a, b| match a.len().cmp(&b.len()) { 384 | Equal => a.cmp(b), 385 | x => x, 386 | }) 387 | .cloned() 388 | .collect(); 389 | assert_eq!(expected, after); 390 | } 391 | 392 | #[test] 393 | fn empty_test() { 394 | let before: Vec = vec![]; 395 | let after: Vec = before.iter().sorted().cloned().collect(); 396 | assert_eq!(before, after); 397 | } 398 | 399 | #[test] 400 | fn sorted_partial_test() { 401 | let expected: Vec = vec![0.9_f64, 1.0, 1.0, 1.1, 75.3, 75.3]; 402 | let before: Vec = vec![1.0_f64, 1.1, 0.9, 75.3, 1.0, 75.3]; 403 | let after: Vec = before.iter().sorted_partial_first().cloned().collect(); 404 | 405 | assert_eq!(expected, after); 406 | } 407 | 408 | #[test] 409 | fn sorted_by_test() { 410 | let expected: Vec = vec![4, 1, 3, 2]; 411 | let before: Vec<(f64, u64)> = vec![(0.2, 1), (0.9, 2), (0.4, 3), (0.1, 4)]; 412 | 413 | let after: Vec = before 414 | .iter() 415 | .sorted_by(|&a, &b| { 416 | let (ax, _) = *a; 417 | let (bx, _) = *b; 418 | ax.partial_cmp(&bx).unwrap() 419 | }) 420 | .map(|&(_, y)| y) 421 | .collect(); 422 | 423 | assert_eq!(expected, after); 424 | } 425 | } 426 | 427 | #[cfg(feature = "nightly")] 428 | #[cfg(test)] 429 | mod benches { 430 | extern crate rand; 431 | 432 | extern crate test; 433 | 434 | use self::test::{black_box, Bencher}; 435 | 436 | use self::rand::distributions::{IndependentSample, Range}; 437 | 438 | use super::Sorted; 439 | 440 | use std::cmp::Ordering; 441 | use std::collections::BinaryHeap; 442 | use std::iter::FromIterator; 443 | 444 | static RANGE: u64 = 1000000; 445 | static VEC_SIZE: u64 = 50000; 446 | static PICK_SIZE_A: usize = 1000; 447 | static PICK_SIZE_B: usize = 10000; 448 | static PICK_SIZE_C: usize = 50000; 449 | 450 | fn data() -> Vec { 451 | let mut rng = rand::thread_rng(); 452 | let between = Range::new(0u64, RANGE); 453 | (0u64..VEC_SIZE) 454 | .map(|_| between.ind_sample(&mut rng)) 455 | .collect() 456 | } 457 | 458 | #[bench] 459 | fn a_standard_bench(b: &mut Bencher) { 460 | let input = data(); 461 | 462 | b.iter(|| { 463 | let mut numbers = black_box(&input).clone(); 464 | numbers.sort(); 465 | let pick: Vec = numbers.into_iter().take(PICK_SIZE_A).collect(); 466 | black_box(pick) 467 | }); 468 | } 469 | 470 | #[bench] 471 | fn a_lazy_bench(b: &mut Bencher) { 472 | let input = data(); 473 | 474 | b.iter(|| { 475 | let numbers = black_box(&input).clone(); 476 | 477 | let pick: Vec = numbers.into_iter().sorted().take(PICK_SIZE_A).collect(); 478 | black_box(pick) 479 | }); 480 | } 481 | 482 | #[bench] 483 | fn a_heap_bench(b: &mut Bencher) { 484 | let input = data(); 485 | 486 | b.iter(|| { 487 | let mut heap = BinaryHeap::from_iter(black_box(&input).iter().cloned().map(RevOrd)); 488 | 489 | let mut pick: Vec = Vec::with_capacity(PICK_SIZE_A); 490 | for _ in 0..PICK_SIZE_A { 491 | pick.push(heap.pop().unwrap().0); 492 | } 493 | black_box(pick) 494 | }); 495 | } 496 | 497 | #[bench] 498 | fn b_standard_bench(b: &mut Bencher) { 499 | let input = data(); 500 | 501 | b.iter(|| { 502 | let mut numbers = black_box(&input).clone(); 503 | numbers.sort(); 504 | let pick: Vec = numbers.into_iter().take(PICK_SIZE_B).collect(); 505 | black_box(pick) 506 | }); 507 | } 508 | 509 | #[bench] 510 | fn b_lazy_bench(b: &mut Bencher) { 511 | let input = data(); 512 | 513 | b.iter(|| { 514 | let numbers = black_box(&input).clone(); 515 | 516 | let pick: Vec = numbers.into_iter().sorted().take(PICK_SIZE_B).collect(); 517 | black_box(pick) 518 | }); 519 | } 520 | 521 | #[bench] 522 | fn b_heap_bench(b: &mut Bencher) { 523 | let input = data(); 524 | 525 | b.iter(|| { 526 | let mut heap = BinaryHeap::from_iter(black_box(&input).iter().cloned().map(RevOrd)); 527 | 528 | let mut pick: Vec = Vec::with_capacity(PICK_SIZE_B); 529 | for _ in 0..PICK_SIZE_B { 530 | pick.push(heap.pop().unwrap().0); 531 | } 532 | black_box(pick) 533 | }); 534 | } 535 | 536 | #[bench] 537 | fn c_standard_bench(b: &mut Bencher) { 538 | let input = data(); 539 | 540 | b.iter(|| { 541 | let mut numbers = black_box(&input).clone(); 542 | numbers.sort(); 543 | let pick: Vec = numbers.into_iter().take(PICK_SIZE_C).collect(); 544 | black_box(pick) 545 | }); 546 | } 547 | 548 | #[bench] 549 | fn c_lazy_bench(b: &mut Bencher) { 550 | let input = data(); 551 | 552 | b.iter(|| { 553 | let numbers = black_box(&input).clone(); 554 | 555 | let pick: Vec = numbers.into_iter().sorted().take(PICK_SIZE_C).collect(); 556 | black_box(pick) 557 | }); 558 | } 559 | 560 | #[bench] 561 | fn c_heap_bench(b: &mut Bencher) { 562 | let input = data(); 563 | 564 | b.iter(|| { 565 | let mut heap = BinaryHeap::from_iter(black_box(&input).iter().cloned().map(RevOrd)); 566 | 567 | let mut pick: Vec = Vec::with_capacity(PICK_SIZE_C); 568 | for _ in 0..PICK_SIZE_C { 569 | pick.push(heap.pop().unwrap().0); 570 | } 571 | black_box(pick) 572 | }); 573 | } 574 | 575 | // BinaryHeap is a max heap. We want to extract the minimum values so 576 | // reverse the ordering. 577 | struct RevOrd(V); 578 | 579 | impl PartialOrd for RevOrd 580 | where 581 | V: PartialOrd, 582 | { 583 | fn partial_cmp(&self, other: &RevOrd) -> Option { 584 | other.0.partial_cmp(&self.0) 585 | } 586 | } 587 | 588 | impl Ord for RevOrd 589 | where 590 | V: Ord, 591 | { 592 | fn cmp(&self, other: &RevOrd) -> Ordering { 593 | other.0.cmp(&self.0) 594 | } 595 | } 596 | 597 | impl PartialEq for RevOrd 598 | where 599 | V: PartialEq, 600 | { 601 | fn eq(&self, other: &RevOrd) -> bool { 602 | other.0.eq(&self.0) 603 | } 604 | } 605 | 606 | impl Eq for RevOrd where V: Eq {} 607 | } 608 | --------------------------------------------------------------------------------