| 1 | pub use craterust-analyzer cargo bisection 0.1.0 crate/::bisect_rightrust-analyzer cargo bisection 0.1.0 bisect_right(). as bisectrust-analyzer cargo bisection 0.1.0 bisect_right().; |
| 2 | pub use craterust-analyzer cargo bisection 0.1.0 crate/::insort_rightrust-analyzer cargo bisection 0.1.0 insort_right(). as insortrust-analyzer cargo bisection 0.1.0 insort_right().; |
| 3 | |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#; |
| 5 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::{Boundrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Bound#::*, RangeBoundsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeBounds#}; |
| 6 | |
| 7 | // TODO: Doctest examples |
| 8 | |
| 9 | /// Insert `x` in `a[within]`, keeping it sorted assuming `a` is sorted. |
| 10 | /// |
| 11 | /// If `a` contains `x`, insert it just *after* the *rightmost* occurence of `x`. |
| 12 | /// |
| 13 | /// # Panics |
| 14 | /// |
| 15 | /// Panics if `within` is out of bounds of `a`. |
| 16 | pub fn insort_right_slicerust-analyzer cargo bisection 0.1.0 insort_right_slice().<T, I>(a: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>, x: T, within: I) |
| 17 | where |
| 18 | I: RangeBoundsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeBounds#<usize>, |
| 19 | T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, |
| 20 | { |
| 21 | insort_right_slice_byrust-analyzer cargo bisection 0.1.0 insort_right_slice_by().(a, x, within, T::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#cmp().); |
| 22 | } |
| 23 | |
| 24 | /// Insert `x` in `a`, keeping it sorted assuming `a` is sorted. |
| 25 | /// If `a` contains `x`, insert it just *after* the *rightmost* occurence of `x`. |
| 26 | pub fn insort_rightrust-analyzer cargo bisection 0.1.0 insort_right().<T>(a: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>, x: T) |
| 27 | where |
| 28 | T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, |
| 29 | { |
| 30 | insort_right_byrust-analyzer cargo bisection 0.1.0 insort_right_by().(a, x, T::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#cmp().); |
| 31 | } |
| 32 | |
| 33 | /// Insert `x` in `a`, keeping it sorted, assuming `a` is sorted, according to a comparator |
| 34 | /// function. |
| 35 | /// |
| 36 | /// The comparator function should implement an order consistent with the sort order of the |
| 37 | /// underlying slice. |
| 38 | /// |
| 39 | /// If `a` contains `x`, insert it just *after* the *rightmost* occurence of `x`. |
| 40 | pub fn insort_right_byrust-analyzer cargo bisection 0.1.0 insort_right_by().<T, F>(a: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>, x: T, f: F) |
| 41 | where |
| 42 | T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, |
| 43 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&T, &T) -> Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#, |
| 44 | { |
| 45 | insort_right_slice_byrust-analyzer cargo bisection 0.1.0 insort_right_slice_by().(a, x, ..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#, f); |
| 46 | } |
| 47 | |
| 48 | /// Insert x in `a[within]`, keeping it sorted, assuming `a` is sorted, according to a comparator |
| 49 | /// function. |
| 50 | /// |
| 51 | /// The comparator function should implement an order consistent with the sort order of the |
| 52 | /// underlying slice. |
| 53 | /// |
| 54 | /// If `a` contains `x`, insert it just *after* the *rightmost* occurence of `x`. |
| 55 | /// |
| 56 | /// # Panics |
| 57 | /// |
| 58 | /// Panics if `within` is out of bounds of `a`. |
| 59 | pub fn insort_right_slice_byrust-analyzer cargo bisection 0.1.0 insort_right_slice_by().<T, I, F>(a: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>, x: T, within: I, mut f: F) |
| 60 | where |
| 61 | I: RangeBoundsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeBounds#<usize>, |
| 62 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&T, &T) -> Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#, |
| 63 | { |
| 64 | let lo = bisect_right_slice_byrust-analyzer cargo bisection 0.1.0 bisect_right_slice_by().(a, within, |p| f(&x, p)); |
| 65 | a.insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]insert().(lo, x); |
| 66 | } |
| 67 | |
| 68 | /// Return the index where `x` should be inserted in `a[within]`, assuming `a` |
| 69 | /// is sorted. |
| 70 | /// |
| 71 | /// The return value `i` is such that all `e` in `a[..i]` have `e <= x`, and |
| 72 | /// all `e` in `a[i..]` have `e > x`. |
| 73 | /// - If `a` contains `x`, `a.insert(i, x)` will insert just *after* the |
| 74 | /// *rightmost* `x`. |
| 75 | /// |
| 76 | /// # Panics |
| 77 | /// |
| 78 | /// Panics if `within` is out of bounds of `a`. |
| 79 | pub fn bisect_right_slicerust-analyzer cargo bisection 0.1.0 bisect_right_slice().<T, I>(a: &[T], x: &T, within: I) -> usize |
| 80 | where |
| 81 | I: RangeBoundsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeBounds#<usize>, |
| 82 | T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, |
| 83 | { |
| 84 | bisect_right_slice_byrust-analyzer cargo bisection 0.1.0 bisect_right_slice_by().(a, within, |p| x.cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#cmp().(p)) |
| 85 | } |
| 86 | |
| 87 | /// Return the index where `x` should be inserted in `a`, assuming `a` is sorted. |
| 88 | /// |
| 89 | /// The return value `i` is such that all `e` in `a[..i]` have `e <= x`, and |
| 90 | /// all `e` in `a[i..]` have `e > x`. |
| 91 | /// - If `a` contains `x`, `a.insert(i, x)` will insert just *after* the |
| 92 | /// *rightmost* occurence of `x`. |
| 93 | pub fn bisect_rightrust-analyzer cargo bisection 0.1.0 bisect_right().<T>(a: &[T], x: &T) -> usize |
| 94 | where |
| 95 | T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, |
| 96 | { |
| 97 | bisect_right_slicerust-analyzer cargo bisection 0.1.0 bisect_right_slice().(a, x, ..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#) |
| 98 | } |
| 99 | |
| 100 | /// Return the index where `x` should be inserted in `a`, assuming `a` is sorted, according to |
| 101 | /// a comparator function. |
| 102 | /// |
| 103 | /// The comparator function should implement an order consistent with the sort order of the |
| 104 | /// underlying slice, returning an order code that indicates whethers its argument is `Less`, |
| 105 | /// `Equal` or `Greater` that the **desired target**. |
| 106 | /// |
| 107 | /// The return value `i` is such that all `e` in `a[..i]` have `f(e) == Less | f(e) == Equal`, and |
| 108 | /// all `e` in `a[i..]` have `f(e) == Greater`. |
| 109 | /// - If `a` contains `x`, `a.insert(i, x)` will insert just *after* the |
| 110 | /// *rightmost* occurence of `x`. |
| 111 | pub fn bisect_right_byrust-analyzer cargo bisection 0.1.0 bisect_right_by().<T, F>(a: &[T], f: F) -> usize |
| 112 | where |
| 113 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&T) -> Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#, |
| 114 | { |
| 115 | bisect_right_slice_byrust-analyzer cargo bisection 0.1.0 bisect_right_slice_by().(a, ..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#, f) |
| 116 | } |
| 117 | |
| 118 | /// Return the index where a value should be inserted in `a[within]`, assuming it sorted, |
| 119 | /// according to a comparator function. |
| 120 | /// |
| 121 | /// The comparator function should implement an order consistent with the sort order of the |
| 122 | /// underlying slice, returning an order code that indicates whethers its argument is `Less`, |
| 123 | /// `Equal` or `Greater` that the **desired target**. |
| 124 | /// |
| 125 | /// The return value `i` is such that all `e` in `a[..i]` have `f(e) == Less | f(e) == Equal`, and |
| 126 | /// all `e` in `a[i..]` have `f(e) == Greater`. |
| 127 | /// - If `a` contains `x`, `a.insert(i, x)` will insert just *after* the |
| 128 | /// *rightmost* occurence of `x`. |
| 129 | /// |
| 130 | /// # Panics |
| 131 | /// |
| 132 | /// Panics if `within` is out of bounds of `a`. |
| 133 | pub fn bisect_right_slice_byrust-analyzer cargo bisection 0.1.0 bisect_right_slice_by().<T, I, F>(a: &[T], within: I, mut f: F) -> usize |
| 134 | where |
| 135 | I: RangeBoundsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeBounds#<usize>, |
| 136 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&T) -> Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#, |
| 137 | { |
| 138 | let (mut lo, mut hi) = bounds_to_indicesrust-analyzer cargo bisection 0.1.0 bounds_to_indices().(a, within); |
| 139 | while lo rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().hi { |
| 140 | let mid = (lo rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().hi) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().2; |
| 141 | if f(&a[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().mid]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impl#[Ordering][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impl#[Ordering][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impl#[Ordering][`PartialEq<Self>`]eq().Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Lessrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Less# { |
| 142 | hi = mid; |
| 143 | } else { |
| 144 | lo = mid rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1; |
| 145 | } |
| 146 | } |
| 147 | lo |
| 148 | } |
| 149 | |
| 150 | /// Insert `x` in `a[within]`, keeping it sorted assuming `a` is sorted. |
| 151 | /// |
| 152 | /// If `a` contains `x`, insert it just *before* the *leftmost* occurence of `x`. |
| 153 | /// |
| 154 | /// # Panics |
| 155 | /// |
| 156 | /// Panics if `within` is out of bounds of `a`. |
| 157 | pub fn insort_left_slicerust-analyzer cargo bisection 0.1.0 insort_left_slice().<T, I>(a: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>, x: T, within: I) |
| 158 | where |
| 159 | I: RangeBoundsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeBounds#<usize>, |
| 160 | T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, |
| 161 | { |
| 162 | insort_left_slice_byrust-analyzer cargo bisection 0.1.0 insort_left_slice_by().(a, x, within, T::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#cmp().); |
| 163 | } |
| 164 | |
| 165 | /// Insert `x` in `a`, keeping it sorted assuming `a` is sorted. |
| 166 | /// |
| 167 | /// If `a` contains `x`, insert it just *before* the *leftmost* occurence of `x`. |
| 168 | pub fn insort_leftrust-analyzer cargo bisection 0.1.0 insort_left().<T>(a: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>, x: T) |
| 169 | where |
| 170 | T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, |
| 171 | { |
| 172 | insort_left_byrust-analyzer cargo bisection 0.1.0 insort_left_by().(a, x, T::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#cmp().); |
| 173 | } |
| 174 | |
| 175 | /// Insert `x` in `a`, keeping it sorted, assuming `a` is sorted, according to a comparator |
| 176 | /// function. |
| 177 | /// |
| 178 | /// The comparator function should implement an order consistent with the sort order of the |
| 179 | /// underlying slice. |
| 180 | /// |
| 181 | /// If `a` contains `x`, insert it just *before* the *leftmost* occurence of `x`. |
| 182 | pub fn insort_left_byrust-analyzer cargo bisection 0.1.0 insort_left_by().<T, F>(a: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>, x: T, f: F) |
| 183 | where |
| 184 | T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, |
| 185 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&T, &T) -> Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#, |
| 186 | { |
| 187 | insort_left_slice_byrust-analyzer cargo bisection 0.1.0 insort_left_slice_by().(a, x, ..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#, f); |
| 188 | } |
| 189 | |
| 190 | /// Insert x in `a[within]`, keeping it sorted, assuming `a` is sorted, according to a comparator |
| 191 | /// function. |
| 192 | /// |
| 193 | /// The comparator function should implement an order consistent with the sort order of the |
| 194 | /// underlying slice. |
| 195 | /// |
| 196 | /// If `a` contains `x`, insert it just *before* the *leftmost* occurence of `x`. |
| 197 | /// |
| 198 | /// # Panics |
| 199 | /// |
| 200 | /// Panics if `within` is out of bounds of `a`. |
| 201 | pub fn insort_left_slice_byrust-analyzer cargo bisection 0.1.0 insort_left_slice_by().<T, I, F>(a: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>, x: T, within: I, mut f: F) |
| 202 | where |
| 203 | I: RangeBoundsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeBounds#<usize>, |
| 204 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&T, &T) -> Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#, |
| 205 | { |
| 206 | let lo = bisect_right_slice_byrust-analyzer cargo bisection 0.1.0 bisect_right_slice_by().(a, within, |p| f(&x, p)); |
| 207 | a.insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]insert().(lo, x); |
| 208 | } |
| 209 | |
| 210 | /// Return the index where `x` should be inserted in `a[within]`, assuming `a` |
| 211 | /// is sorted. |
| 212 | /// |
| 213 | /// The return value `i` is such that all `e` in `a[..i]` have `e < x`, and |
| 214 | /// all `e` in `a[i..]` have `e >= x`. |
| 215 | /// - If `a` contains `x`, `a.insert(i, x)` will insert just *before* the |
| 216 | /// *leftmost* `x`. |
| 217 | /// |
| 218 | /// # Panics |
| 219 | /// |
| 220 | /// Panics if `within` is out of bounds of `a`. |
| 221 | pub fn bisect_left_slicerust-analyzer cargo bisection 0.1.0 bisect_left_slice().<T, I>(a: &[T], x: &T, within: I) -> usize |
| 222 | where |
| 223 | I: RangeBoundsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeBounds#<usize>, |
| 224 | T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, |
| 225 | { |
| 226 | bisect_left_slice_byrust-analyzer cargo bisection 0.1.0 bisect_left_slice_by().(a, within, |p| p.cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#cmp().(x)) |
| 227 | } |
| 228 | |
| 229 | /// Return the index where `x` should be inserted in `a`, assuming `a` is sorted. |
| 230 | /// |
| 231 | /// The return value `i` is such that all `e` in `a[..i]` have `e < x`, and |
| 232 | /// all `e` in `a[i..]` have `e >= x`. |
| 233 | /// - If `a` contains `x`, `a.insert(i, x)` will insert just *before* the |
| 234 | /// *leftmost* `x`. |
| 235 | pub fn bisect_leftrust-analyzer cargo bisection 0.1.0 bisect_left().<T>(a: &[T], x: &T) -> usize |
| 236 | where |
| 237 | T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, |
| 238 | { |
| 239 | bisect_left_slicerust-analyzer cargo bisection 0.1.0 bisect_left_slice().(a, x, ..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#) |
| 240 | } |
| 241 | |
| 242 | /// Return the index where a value should be inserted in `a`, assuming `a` is sorted, according to |
| 243 | /// a comparator function. |
| 244 | /// |
| 245 | /// The comparator function should implement an order consistent with the sort order of the |
| 246 | /// underlying slice, returning an order code that indicates whethers its argument is `Less`, |
| 247 | /// `Equal` or `Greater` that the **desired target**. |
| 248 | /// |
| 249 | /// The return value `i` is such that all `e` in `a[..i]` have `f(e) == Less`, and |
| 250 | /// all `e` in `a[i..]` have `f(e) == Greater | f(e) == Equal` |
| 251 | /// - If `a` contains `x`, `a.insert(i, x)` will insert just *before* the |
| 252 | /// *leftmost* `x`. |
| 253 | pub fn bisect_left_byrust-analyzer cargo bisection 0.1.0 bisect_left_by().<T, F>(a: &[T], f: F) -> usize |
| 254 | where |
| 255 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&T) -> Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#, |
| 256 | { |
| 257 | bisect_left_slice_byrust-analyzer cargo bisection 0.1.0 bisect_left_slice_by().(a, ..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#, f) |
| 258 | } |
| 259 | |
| 260 | /// Return the index where a value should be inserted in `a[within]`, assuming it sorted, |
| 261 | /// according to a comparator function. |
| 262 | /// |
| 263 | /// The comparator function should implement an order consistent with the sort order of the |
| 264 | /// underlying slice, returning an order code that indicates whethers its argument is `Less`, |
| 265 | /// `Equal` or `Greater` that the **desired target**. |
| 266 | /// |
| 267 | /// The return value `i` is such that all `e` in `a[..i]` have `f(e) == Less`, and |
| 268 | /// all `e` in `a[i..]` have `f(e) == Greater | f(e) == Equal` |
| 269 | /// - If `a` contains `x`, `a.insert(i, x)` will insert just *before* the |
| 270 | /// *leftmost* `x`. |
| 271 | /// # Panics |
| 272 | /// |
| 273 | /// Panics if `within` is out of bounds of `a`. |
| 274 | pub fn bisect_left_slice_byrust-analyzer cargo bisection 0.1.0 bisect_left_slice_by().<T, I, F>(a: &[T], within: I, mut f: F) -> usize |
| 275 | where |
| 276 | I: RangeBoundsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeBounds#<usize>, |
| 277 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&T) -> Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#, |
| 278 | { |
| 279 | let (mut lo, mut hi) = bounds_to_indicesrust-analyzer cargo bisection 0.1.0 bounds_to_indices().(a, within); |
| 280 | while lo rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().hi { |
| 281 | let mid = (lo rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().hi) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().2; |
| 282 | let cmp = f(&a[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().mid]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 283 | if cmp rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impl#[Ordering][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impl#[Ordering][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impl#[Ordering][`PartialEq<Self>`]eq().Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Lessrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Less# { |
| 284 | lo = mid rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1; |
| 285 | } else { |
| 286 | hi = mid; |
| 287 | } |
| 288 | } |
| 289 | lo |
| 290 | } |
| 291 | |
| 292 | /// Convert bounds to a `(lo, hi)` pair for indexing into a slice of `a`. |
| 293 | /// |
| 294 | /// # Panics |
| 295 | /// |
| 296 | /// Panics if `within` is out of bounds of `a`. |
| 297 | fn bounds_to_indicesrust-analyzer cargo bisection 0.1.0 bounds_to_indices().<T, I>(a: &[T], within: I) -> (usize, usize) |
| 298 | where |
| 299 | I: RangeBoundsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeBounds#<usize>, |
| 300 | { |
| 301 | // TODO: Panics |
| 302 | let lo = match within.start_boundrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeBounds#start_bound().() { |
| 303 | Unboundedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Bound#Unbounded# => 0, |
| 304 | Includedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Bound#Included#(i) => *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().i, |
| 305 | Excludedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Bound#Excluded#(i) => i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[`&usize`][`Add<usize>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[`&usize`][`Add<usize>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[`&usize`][`Add<usize>`]add().1, |
| 306 | }; |
| 307 | |
| 308 | let hi = match within.end_boundrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeBounds#end_bound().() { |
| 309 | Unboundedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Bound#Unbounded# => a.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), |
| 310 | Includedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Bound#Included#(i) => i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[`&usize`][`Add<usize>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[`&usize`][`Add<usize>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[`&usize`][`Add<usize>`]add().1, |
| 311 | Excludedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Bound#Excluded#(i) => *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().i, |
| 312 | }; |
| 313 | |
| 314 | if hi rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().a.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() { |
| 315 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("index out of bounds") |
| 316 | } |
| 317 | |
| 318 | (lo, hi) |
| 319 | } |
| 320 | |
| 321 | #[cfg(test)] |
| 322 | mod testsrust-analyzer cargo bisection 0.1.0 tests/ { |
| 323 | |
| 324 | use superrust-analyzer cargo bisection 0.1.0 crate/::*; |
| 325 | use proptestrust-analyzer cargo proptest 1.11.0 crate/::preluderust-analyzer cargo proptest 1.11.0 prelude/::*; |
| 326 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#; |
| 327 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::FromIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/FromIterator#; |
| 328 | |
| 329 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 330 | struct BisectTestrust-analyzer cargo bisection 0.1.0 tests/BisectTest#<T: 'static> |
| 331 | where |
| 332 | T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, |
| 333 | { |
| 334 | namerust-analyzer cargo bisection 0.1.0 tests/BisectTest#name.: &'static str, |
| 335 | arust-analyzer cargo bisection 0.1.0 tests/BisectTest#a.: &'static [T], |
| 336 | xrust-analyzer cargo bisection 0.1.0 tests/BisectTest#x.: T, |
| 337 | expected_indexrust-analyzer cargo bisection 0.1.0 tests/BisectTest#expected_index.: usize, |
| 338 | } |
| 339 | |
| 340 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 341 | enum TestDirectionrust-analyzer cargo bisection 0.1.0 tests/TestDirection# { |
| 342 | Leftrust-analyzer cargo bisection 0.1.0 tests/TestDirection#Left#, |
| 343 | Rightrust-analyzer cargo bisection 0.1.0 tests/TestDirection#Right#, |
| 344 | } |
| 345 | |
| 346 | type TestCollectionrust-analyzer cargo bisection 0.1.0 tests/TestCollection#<T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#> = &'static [BisectTestrust-analyzer cargo bisection 0.1.0 tests/BisectTest#<T>]; |
| 347 | |
| 348 | macro_rules! trust-analyzer cargo bisection 0.1.0 tests/t! { |
| 349 | ($name:ident, $a:expr, $x:expr, $expected_index:expr) => { |
| 350 | BisectTest { |
| 351 | name: stringify!($name), |
| 352 | a: $a, |
| 353 | x: $x, |
| 354 | expected_index: $expected_index, |
| 355 | } |
| 356 | }; |
| 357 | } |
| 358 | |
| 359 | const RIGHT_INT_CASESrust-analyzer cargo bisection 0.1.0 tests/RIGHT_INT_CASES.: TestCollectionrust-analyzer cargo bisection 0.1.0 tests/TestCollection#<i32> = &[ |
| 360 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_0, &[], 1, 0), |
| 361 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_1, &[1], 0, 0), |
| 362 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_2, &[1], 1, 1), |
| 363 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_3, &[1], 2, 1), |
| 364 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_4, &[1, 1], 0, 0), |
| 365 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_5, &[1, 1], 1, 2), |
| 366 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_6, &[1, 1], 2, 2), |
| 367 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_7, &[1, 1, 1], 0, 0), |
| 368 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_8, &[1, 1, 1], 1, 3), |
| 369 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_9, &[1, 1, 1], 2, 3), |
| 370 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_10, &[1, 1, 1, 1], 0, 0), |
| 371 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_11, &[1, 1, 1, 1], 1, 4), |
| 372 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_12, &[1, 1, 1, 1], 2, 4), |
| 373 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_13, &[1, 2], 0, 0), |
| 374 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_14, &[1, 2], 1, 1), |
| 375 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_15, &[1, 2], 2, 2), |
| 376 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_16, &[1, 2], 3, 2), |
| 377 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_17, &[1, 1, 2, 2], 0, 0), |
| 378 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_18, &[1, 1, 2, 2], 1, 2), |
| 379 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_19, &[1, 1, 2, 2], 2, 4), |
| 380 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_20, &[1, 1, 2, 2], 3, 4), |
| 381 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_21, &[1, 2, 3], 0, 0), |
| 382 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_22, &[1, 2, 3], 1, 1), |
| 383 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_23, &[1, 2, 3], 2, 2), |
| 384 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_24, &[1, 2, 3], 3, 3), |
| 385 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_25, &[1, 2, 3], 4, 3), |
| 386 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_26, &[1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 0, 0), |
| 387 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_27, &[1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 1, 1), |
| 388 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_28, &[1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 2, 3), |
| 389 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_29, &[1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 3, 6), |
| 390 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_30, &[1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 4, 10), |
| 391 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_right_31, &[1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 5, 10), |
| 392 | ]; |
| 393 | |
| 394 | const LEFT_INT_CASESrust-analyzer cargo bisection 0.1.0 tests/LEFT_INT_CASES.: TestCollectionrust-analyzer cargo bisection 0.1.0 tests/TestCollection#<i32> = &[ |
| 395 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_0, &[], 1, 0), |
| 396 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_1, &[1], 0, 0), |
| 397 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_2, &[1], 1, 0), |
| 398 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_3, &[1], 2, 1), |
| 399 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_4, &[1, 1], 0, 0), |
| 400 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_5, &[1, 1], 1, 0), |
| 401 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_6, &[1, 1], 2, 2), |
| 402 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_7, &[1, 1, 1], 0, 0), |
| 403 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_8, &[1, 1, 1], 1, 0), |
| 404 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_9, &[1, 1, 1], 2, 3), |
| 405 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_10, &[1, 1, 1, 1], 0, 0), |
| 406 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_11, &[1, 1, 1, 1], 1, 0), |
| 407 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_12, &[1, 1, 1, 1], 2, 4), |
| 408 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_13, &[1, 2], 0, 0), |
| 409 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_14, &[1, 2], 1, 0), |
| 410 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_15, &[1, 2], 2, 1), |
| 411 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_16, &[1, 2], 3, 2), |
| 412 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_17, &[1, 1, 2, 2], 0, 0), |
| 413 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_18, &[1, 1, 2, 2], 1, 0), |
| 414 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_19, &[1, 1, 2, 2], 2, 2), |
| 415 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_20, &[1, 1, 2, 2], 3, 4), |
| 416 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_21, &[1, 2, 3], 0, 0), |
| 417 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_22, &[1, 2, 3], 1, 0), |
| 418 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_23, &[1, 2, 3], 2, 1), |
| 419 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_24, &[1, 2, 3], 3, 2), |
| 420 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_25, &[1, 2, 3], 4, 3), |
| 421 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_26, &[1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 0, 0), |
| 422 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_27, &[1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 1, 0), |
| 423 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_28, &[1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 2, 1), |
| 424 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_29, &[1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 3, 3), |
| 425 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_30, &[1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 4, 6), |
| 426 | trust-analyzer cargo bisection 0.1.0 tests/t!!(ints_left_31, &[1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 5, 10), |
| 427 | ]; |
| 428 | |
| 429 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 430 | fn bisect_right_precomputedrust-analyzer cargo bisection 0.1.0 tests/bisect_right_precomputed().() { |
| 431 | run_bisect_testsrust-analyzer cargo bisection 0.1.0 tests/run_bisect_tests().(TestDirectionrust-analyzer cargo bisection 0.1.0 tests/TestDirection#::Rightrust-analyzer cargo bisection 0.1.0 tests/TestDirection#Right#, RIGHT_INT_CASESrust-analyzer cargo bisection 0.1.0 tests/RIGHT_INT_CASES.); |
| 432 | } |
| 433 | |
| 434 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 435 | fn bisect_left_precomputedrust-analyzer cargo bisection 0.1.0 tests/bisect_left_precomputed().() { |
| 436 | run_bisect_testsrust-analyzer cargo bisection 0.1.0 tests/run_bisect_tests().(TestDirectionrust-analyzer cargo bisection 0.1.0 tests/TestDirection#::Leftrust-analyzer cargo bisection 0.1.0 tests/TestDirection#Left#, LEFT_INT_CASESrust-analyzer cargo bisection 0.1.0 tests/LEFT_INT_CASES.); |
| 437 | } |
| 438 | |
| 439 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 440 | fn bisect_right_slice_precomputedrust-analyzer cargo bisection 0.1.0 tests/bisect_right_slice_precomputed().() { |
| 441 | run_bisect_slice_testsrust-analyzer cargo bisection 0.1.0 tests/run_bisect_slice_tests().(TestDirectionrust-analyzer cargo bisection 0.1.0 tests/TestDirection#::Rightrust-analyzer cargo bisection 0.1.0 tests/TestDirection#Right#, RIGHT_INT_CASESrust-analyzer cargo bisection 0.1.0 tests/RIGHT_INT_CASES.); |
| 442 | } |
| 443 | |
| 444 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 445 | fn bisect_left_slice_precomputedrust-analyzer cargo bisection 0.1.0 tests/bisect_left_slice_precomputed().() { |
| 446 | run_bisect_slice_testsrust-analyzer cargo bisection 0.1.0 tests/run_bisect_slice_tests().(TestDirectionrust-analyzer cargo bisection 0.1.0 tests/TestDirection#::Leftrust-analyzer cargo bisection 0.1.0 tests/TestDirection#Left#, LEFT_INT_CASESrust-analyzer cargo bisection 0.1.0 tests/LEFT_INT_CASES.); |
| 447 | } |
| 448 | |
| 449 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 450 | #[should_panic] |
| 451 | fn right_slice_index_out_of_boundsrust-analyzer cargo bisection 0.1.0 tests/right_slice_index_out_of_bounds().() { |
| 452 | let a: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u32> = (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#10).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 453 | // 10 does not fit within 5..10 so the search goes into the out of bounds range |
| 454 | bisect_right_slicerust-analyzer cargo bisection 0.1.0 bisect_right_slice().(&a, &10, 5..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#15); |
| 455 | } |
| 456 | |
| 457 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 458 | #[should_panic] |
| 459 | fn left_slice_index_out_of_boundsrust-analyzer cargo bisection 0.1.0 tests/left_slice_index_out_of_bounds().() { |
| 460 | let a: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u32> = (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#10).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 461 | // 10 does not fit within 5..10 so the search goes into the out of bounds range |
| 462 | bisect_left_slicerust-analyzer cargo bisection 0.1.0 bisect_left_slice().(&a, &10, 5..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#15); |
| 463 | } |
| 464 | |
| 465 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 466 | #[should_panic] |
| 467 | fn right_slice_index_out_of_bounds_when_not_searchedrust-analyzer cargo bisection 0.1.0 tests/right_slice_index_out_of_bounds_when_not_searched().() { |
| 468 | let a: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u32> = (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#10).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 469 | // 5 fits within 0..10 bounds so the search *doesnt* actually index into the out of bounds |
| 470 | bisect_right_slicerust-analyzer cargo bisection 0.1.0 bisect_right_slice().(&a, &5, ..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#15); |
| 471 | } |
| 472 | |
| 473 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 474 | #[should_panic] |
| 475 | fn left_slice_index_out_of_bounds_when_not_searchedrust-analyzer cargo bisection 0.1.0 tests/left_slice_index_out_of_bounds_when_not_searched().() { |
| 476 | let a: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u32> = (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#10).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 477 | // 5 fits within 0..10 bounds so the search *doesnt* actually index into the out of bounds |
| 478 | bisect_left_slicerust-analyzer cargo bisection 0.1.0 bisect_left_slice().(&a, &5, ..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#15); |
| 479 | } |
| 480 | |
| 481 | fn run_bisect_testsrust-analyzer cargo bisection 0.1.0 tests/run_bisect_tests().<T: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# + Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#>(direction: TestDirectionrust-analyzer cargo bisection 0.1.0 tests/TestDirection#, test_cases: TestCollectionrust-analyzer cargo bisection 0.1.0 tests/TestCollection#<T>) { |
| 482 | let bisect_func = match direction { |
| 483 | TestDirectionrust-analyzer cargo bisection 0.1.0 tests/TestDirection#::Leftrust-analyzer cargo bisection 0.1.0 tests/TestDirection#Left# => bisect_leftrust-analyzer cargo bisection 0.1.0 bisect_left()., |
| 484 | TestDirectionrust-analyzer cargo bisection 0.1.0 tests/TestDirection#::Rightrust-analyzer cargo bisection 0.1.0 tests/TestDirection#Right# => bisect_rightrust-analyzer cargo bisection 0.1.0 bisect_right()., |
| 485 | }; |
| 486 | |
| 487 | for test_case in test_cases { |
| 488 | let data = test_case.arust-analyzer cargo bisection 0.1.0 tests/BisectTest#a..to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().(); |
| 489 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(test_case.expected_indexrust-analyzer cargo bisection 0.1.0 tests/BisectTest#expected_index., bisect_func(&data, &test_case.xrust-analyzer cargo bisection 0.1.0 tests/BisectTest#x.)); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | fn run_bisect_slice_testsrust-analyzer cargo bisection 0.1.0 tests/run_bisect_slice_tests().<T: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# + Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#>( |
| 494 | direction: TestDirectionrust-analyzer cargo bisection 0.1.0 tests/TestDirection#, |
| 495 | test_cases: TestCollectionrust-analyzer cargo bisection 0.1.0 tests/TestCollection#<T>, |
| 496 | ) { |
| 497 | let bisect_func = match direction { |
| 498 | TestDirectionrust-analyzer cargo bisection 0.1.0 tests/TestDirection#::Leftrust-analyzer cargo bisection 0.1.0 tests/TestDirection#Left# => bisect_left_slicerust-analyzer cargo bisection 0.1.0 bisect_left_slice()., |
| 499 | TestDirectionrust-analyzer cargo bisection 0.1.0 tests/TestDirection#::Rightrust-analyzer cargo bisection 0.1.0 tests/TestDirection#Right# => bisect_right_slicerust-analyzer cargo bisection 0.1.0 bisect_right_slice()., |
| 500 | }; |
| 501 | |
| 502 | for test_case in test_cases { |
| 503 | let data = test_case.arust-analyzer cargo bisection 0.1.0 tests/BisectTest#a..to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().(); |
| 504 | for lo in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#4 { |
| 505 | for hi in 3..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#8 { |
| 506 | let hi = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(data.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), hi); |
| 507 | let ip = bisect_func(&data, &test_case.xrust-analyzer cargo bisection 0.1.0 tests/BisectTest#x., lo..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#hi); |
| 508 | |
| 509 | match direction { |
| 510 | TestDirectionrust-analyzer cargo bisection 0.1.0 tests/TestDirection#::Leftrust-analyzer cargo bisection 0.1.0 tests/TestDirection#Left# => { |
| 511 | if ip rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().hi { |
| 512 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(test_case.xrust-analyzer cargo bisection 0.1.0 tests/BisectTest#x. <= data[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().ip]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().); |
| 513 | } |
| 514 | |
| 515 | if ip rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().lo { |
| 516 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(data[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().ip -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). 1]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index(). <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt(). test_case.xrust-analyzer cargo bisection 0.1.0 tests/BisectTest#x.) |
| 517 | } |
| 518 | } |
| 519 | TestDirectionrust-analyzer cargo bisection 0.1.0 tests/TestDirection#::Rightrust-analyzer cargo bisection 0.1.0 tests/TestDirection#Right# => { |
| 520 | if ip rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().hi { |
| 521 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(test_case.xrust-analyzer cargo bisection 0.1.0 tests/BisectTest#x. <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt(). data[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().ip]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().); |
| 522 | } |
| 523 | |
| 524 | if ip rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().lo { |
| 525 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(data[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().ip -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). 1]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index(). <= test_case.xrust-analyzer cargo bisection 0.1.0 tests/BisectTest#x.) |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 531 | ip, |
| 532 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().(lo, stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(hi, test_case.expected_indexrust-analyzer cargo bisection 0.1.0 tests/BisectTest#expected_index.)) |
| 533 | ); |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:)] |
| 540 | struct Personrust-analyzer cargo bisection 0.1.0 tests/Person# { |
| 541 | namerust-analyzer cargo bisection 0.1.0 tests/Person#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 542 | agerust-analyzer cargo bisection 0.1.0 tests/Person#age.: u32, |
| 543 | } |
| 544 | |
| 545 | fn arb_personrust-analyzer cargo bisection 0.1.0 tests/arb_person().() -> impl Strategyrust-analyzer cargo proptest 1.11.0 strategy/traits/Strategy#<Valuerust-analyzer cargo proptest 1.11.0 strategy/traits/Strategy#Value# = Personrust-analyzer cargo bisection 0.1.0 tests/Person#> { |
| 546 | ("[a-z]*", 1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#100_u32).prop_maprust-analyzer cargo proptest 1.11.0 strategy/traits/Strategy#prop_map().(|(name, age)| Personrust-analyzer cargo bisection 0.1.0 tests/Person# { namerust-analyzer cargo bisection 0.1.0 tests/Person#name., agerust-analyzer cargo bisection 0.1.0 tests/Person#age. }) |
| 547 | } |
| 548 | |
| 549 | fn check_index_right_invariantrust-analyzer cargo bisection 0.1.0 tests/check_index_right_invariant().<T, F>(a: &[T], target: &T, index: usize, mut f: F) |
| 550 | where |
| 551 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&T, &T) -> Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#, |
| 552 | { |
| 553 | // See `bisect_right_by` docs |
| 554 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(a[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...index]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().allrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]all().(|x| match f(x, &target) { |
| 555 | Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Lessrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Less# | Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Equalrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Equal# => true, |
| 556 | _ => false, |
| 557 | })); |
| 558 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(a[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().index..]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). |
| 559 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 560 | .allrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]all().(|x| f(x, &target) == Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Greaterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Greater#)); |
| 561 | } |
| 562 | |
| 563 | fn check_index_left_invariantrust-analyzer cargo bisection 0.1.0 tests/check_index_left_invariant().<T, F>(a: &[T], target: &T, index: usize, mut f: F) |
| 564 | where |
| 565 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&T, &T) -> Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#, |
| 566 | { |
| 567 | // See `bisect_left_by` docs |
| 568 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(a[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...index]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().allrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]all().(|x| f(x, &target) == Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Lessrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Less#)); |
| 569 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(a[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().index..]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().allrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]all().(|x| match f(x, &target) { |
| 570 | Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Greaterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Greater# | Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Equalrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Equal# => true, |
| 571 | _ => false, |
| 572 | })); |
| 573 | } |
| 574 | |
| 575 | proptestrust-analyzer cargo proptest 1.11.0 sugar/proptest!! { |
| 576 | |
| 577 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 578 | fn test_bisect_left_index_invariantrust-analyzer cargo bisection 0.1.0 tests/test_bisect_left_index_invariant().( |
| 579 | mut nums in proprust-analyzer cargo proptest 1.11.0 prelude/prop/::collectionrust-analyzer cargo proptest 1.11.0 collection/::vecrust-analyzer cargo proptest 1.11.0 collection/vec().(anyrust-analyzer cargo proptest 1.11.0 arbitrary/traits/any().::<u32>(), 0..500), |
| 580 | num in anyrust-analyzer cargo proptest 1.11.0 arbitrary/traits/any().::<u32>() |
| 581 | ) { |
| 582 | nums.sortrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort().(); |
| 583 | |
| 584 | let i = bisect_leftrust-analyzer cargo bisection 0.1.0 bisect_left().(&nums, &num); |
| 585 | |
| 586 | check_index_left_invariantrust-analyzer cargo bisection 0.1.0 tests/check_index_left_invariant().(&nums, &num, i, u32::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][Ord]cmp().); |
| 587 | } |
| 588 | |
| 589 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 590 | fn test_bisect_left_by_index_invariantrust-analyzer cargo bisection 0.1.0 tests/test_bisect_left_by_index_invariant().( |
| 591 | mut people in proprust-analyzer cargo proptest 1.11.0 prelude/prop/::collectionrust-analyzer cargo proptest 1.11.0 collection/::vecrust-analyzer cargo proptest 1.11.0 collection/vec().(arb_personrust-analyzer cargo bisection 0.1.0 tests/arb_person().(), 0..500), |
| 592 | new_person in arb_personrust-analyzer cargo bisection 0.1.0 tests/arb_person().() |
| 593 | ) { |
| 594 | // Sort by age only |
| 595 | let f = |a: &Personrust-analyzer cargo bisection 0.1.0 tests/Person#, b: &Personrust-analyzer cargo bisection 0.1.0 tests/Person#| a.agerust-analyzer cargo bisection 0.1.0 tests/Person#age..cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][Ord]cmp().(&b.agerust-analyzer cargo bisection 0.1.0 tests/Person#age.); |
| 596 | |
| 597 | people.sort_byrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort_by().(f); |
| 598 | |
| 599 | let i = bisect_left_byrust-analyzer cargo bisection 0.1.0 bisect_left_by().(&people, |p| f(p, &new_person)); |
| 600 | |
| 601 | check_index_left_invariantrust-analyzer cargo bisection 0.1.0 tests/check_index_left_invariant().(&people, &new_person, i, f); |
| 602 | |
| 603 | // Sort by name only |
| 604 | let f = |a: &Personrust-analyzer cargo bisection 0.1.0 tests/Person#, b: &Personrust-analyzer cargo bisection 0.1.0 tests/Person#| a.namerust-analyzer cargo bisection 0.1.0 tests/Person#name..cmprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Ord]cmp().(&b.namerust-analyzer cargo bisection 0.1.0 tests/Person#name.); |
| 605 | |
| 606 | people.sort_byrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort_by().(f); |
| 607 | |
| 608 | let i = bisect_left_byrust-analyzer cargo bisection 0.1.0 bisect_left_by().(&people, |p| f(p, &new_person)); |
| 609 | |
| 610 | check_index_left_invariantrust-analyzer cargo bisection 0.1.0 tests/check_index_left_invariant().(&people, &new_person, i, f); |
| 611 | } |
| 612 | |
| 613 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 614 | fn test_bisect_right_index_invariantrust-analyzer cargo bisection 0.1.0 tests/test_bisect_right_index_invariant().( |
| 615 | mut nums in proprust-analyzer cargo proptest 1.11.0 prelude/prop/::collectionrust-analyzer cargo proptest 1.11.0 collection/::vecrust-analyzer cargo proptest 1.11.0 collection/vec().(anyrust-analyzer cargo proptest 1.11.0 arbitrary/traits/any().::<u32>(), 0..500), |
| 616 | num in anyrust-analyzer cargo proptest 1.11.0 arbitrary/traits/any().::<u32>() |
| 617 | ) { |
| 618 | nums.sortrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort().(); |
| 619 | |
| 620 | let i = bisect_rightrust-analyzer cargo bisection 0.1.0 bisect_right().(&nums, &num); |
| 621 | |
| 622 | check_index_right_invariantrust-analyzer cargo bisection 0.1.0 tests/check_index_right_invariant().(&nums, &num, i, u32::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][Ord]cmp().) |
| 623 | } |
| 624 | |
| 625 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 626 | fn test_bisect_right_by_index_invariantrust-analyzer cargo bisection 0.1.0 tests/test_bisect_right_by_index_invariant().( |
| 627 | mut people in proprust-analyzer cargo proptest 1.11.0 prelude/prop/::collectionrust-analyzer cargo proptest 1.11.0 collection/::vecrust-analyzer cargo proptest 1.11.0 collection/vec().(arb_personrust-analyzer cargo bisection 0.1.0 tests/arb_person().(), 0..500), |
| 628 | new_person in arb_personrust-analyzer cargo bisection 0.1.0 tests/arb_person().() |
| 629 | ) { |
| 630 | // Sort by age only |
| 631 | let f = |a: &Personrust-analyzer cargo bisection 0.1.0 tests/Person#, b: &Personrust-analyzer cargo bisection 0.1.0 tests/Person#| a.agerust-analyzer cargo bisection 0.1.0 tests/Person#age..cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][Ord]cmp().(&b.agerust-analyzer cargo bisection 0.1.0 tests/Person#age.); |
| 632 | |
| 633 | people.sort_byrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort_by().(f); |
| 634 | |
| 635 | let i = bisect_right_byrust-analyzer cargo bisection 0.1.0 bisect_right_by().(&people, |p| f(&new_person, p)); |
| 636 | |
| 637 | check_index_right_invariantrust-analyzer cargo bisection 0.1.0 tests/check_index_right_invariant().(&people, &new_person, i, f); |
| 638 | |
| 639 | |
| 640 | // Sort by name only |
| 641 | let f = |a: &Personrust-analyzer cargo bisection 0.1.0 tests/Person#, b: &Personrust-analyzer cargo bisection 0.1.0 tests/Person#| a.namerust-analyzer cargo bisection 0.1.0 tests/Person#name..cmprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Ord]cmp().(&b.namerust-analyzer cargo bisection 0.1.0 tests/Person#name.); |
| 642 | |
| 643 | people.sort_byrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort_by().(f); |
| 644 | |
| 645 | let i = bisect_right_byrust-analyzer cargo bisection 0.1.0 bisect_right_by().(&people, |p| f(&new_person, p)); |
| 646 | |
| 647 | check_index_right_invariantrust-analyzer cargo bisection 0.1.0 tests/check_index_right_invariant().(&people, &new_person, i, f); |
| 648 | } |
| 649 | |
| 650 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 651 | fn test_insort_vs_vec_sortrust-analyzer cargo bisection 0.1.0 tests/test_insort_vs_vec_sort().( |
| 652 | digits in proprust-analyzer cargo proptest 1.11.0 prelude/prop/::collectionrust-analyzer cargo proptest 1.11.0 collection/::vecrust-analyzer cargo proptest 1.11.0 collection/vec().(0..10, 0..500) |
| 653 | ) { |
| 654 | let left_digits = HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#::<i32>::from_iterrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S>`][`FromIterator<T>`]from_iter().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 2, 4, 6, 8]); |
| 655 | let mut insorted = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 656 | |
| 657 | for digit in digits { |
| 658 | let f = if left_digits.containsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`]contains().(&digit) { |
| 659 | insort_leftrust-analyzer cargo bisection 0.1.0 insort_left(). |
| 660 | } else { |
| 661 | insort_rightrust-analyzer cargo bisection 0.1.0 insort_right(). |
| 662 | }; |
| 663 | |
| 664 | f(&mut insorted, digit); |
| 665 | } |
| 666 | |
| 667 | let vec_sorted = { |
| 668 | let mut v = insorted.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().(); |
| 669 | v.sortrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort().(); |
| 670 | v |
| 671 | }; |
| 672 | |
| 673 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(vec_sorted, insorted); |
| 674 | } |
| 675 | } |
| 676 | } |