| 1 | //! The galloping search algorithm. |
| 2 | |
| 3 | #[cfg(test)] |
| 4 | mod tests; |
| 5 | |
| 6 | use crate::Comparator; |
| 7 | use core::cmp::Ordering; |
| 8 | |
| 9 | #[derive(Copy, Clone)] |
| 10 | pub(crate) enum Mode { |
| 11 | Forward, |
| 12 | Reverse, |
| 13 | } |
| 14 | |
| 15 | /// Returns the index where key should be inserted, assuming it shoul be placed |
| 16 | /// at the beginning of any cluster of equal items. |
| 17 | pub(crate) fn gallop_left<T, C: Comparator<T>>( |
| 18 | key: &T, |
| 19 | list: &[T], |
| 20 | mode: Mode, |
| 21 | cmp: &C, |
| 22 | ) -> Result<usize, C::Error> { |
| 23 | let (mut base, mut lim) = gallop(key, list, mode, cmp)?; |
| 24 | while lim != 0 { |
| 25 | let ix = base + (lim / 2); |
| 26 | match cmp.ordering(&list[ix], key)? { |
| 27 | Ordering::Less => { |
| 28 | base = ix + 1; |
| 29 | lim -= 1; |
| 30 | } |
| 31 | Ordering::Greater => (), |
| 32 | Ordering::Equal => { |
| 33 | if ix == 0 || cmp.is_gt(key, &list[ix - 1])? { |
| 34 | base = ix; |
| 35 | break; |
| 36 | } |
| 37 | } |
| 38 | }; |
| 39 | lim /= 2; |
| 40 | } |
| 41 | Ok(base) |
| 42 | } |
| 43 | |
| 44 | /// Returns the index where key should be inserted, assuming it shoul be placed |
| 45 | /// at the end of any cluster of equal items. |
| 46 | pub(crate) fn gallop_right<T, C: Comparator<T>>( |
| 47 | key: &T, |
| 48 | list: &[T], |
| 49 | mode: Mode, |
| 50 | cmp: &C, |
| 51 | ) -> Result<usize, C::Error> { |
| 52 | let list_len = list.len(); |
| 53 | let (mut base, mut lim) = gallop(key, list, mode, cmp)?; |
| 54 | while lim != 0 { |
| 55 | let ix = base + (lim / 2); |
| 56 | match cmp.ordering(&list[ix], key)? { |
| 57 | Ordering::Less => { |
| 58 | base = ix + 1; |
| 59 | lim -= 1; |
| 60 | } |
| 61 | Ordering::Greater => (), |
| 62 | Ordering::Equal => { |
| 63 | base = ix + 1; |
| 64 | if ix == list_len - 1 || cmp.is_gt(&list[ix + 1], key)? { |
| 65 | break; |
| 66 | } else { |
| 67 | lim -= 1; |
| 68 | } |
| 69 | } |
| 70 | }; |
| 71 | lim /= 2; |
| 72 | } |
| 73 | Ok(base) |
| 74 | } |
| 75 | |
| 76 | fn gallop<T, C: Comparator<T>>( |
| 77 | key: &T, |
| 78 | list: &[T], |
| 79 | mode: Mode, |
| 80 | cmp: &C, |
| 81 | ) -> Result<(usize, usize), C::Error> { |
| 82 | let list_len = list.len(); |
| 83 | if list_len == 0 { |
| 84 | return Ok((0, 0)); |
| 85 | } |
| 86 | let ret = match mode { |
| 87 | Mode::Forward => { |
| 88 | let mut prev_val = 0; |
| 89 | let mut next_val = 1; |
| 90 | while next_val < list_len { |
| 91 | match cmp.ordering(&list[next_val], key)? { |
| 92 | Ordering::Less => { |
| 93 | prev_val = next_val; |
| 94 | next_val = ((next_val + 1) * 2) - 1; |
| 95 | } |
| 96 | Ordering::Greater => { |
| 97 | break; |
| 98 | } |
| 99 | Ordering::Equal => { |
| 100 | next_val += 1; |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | if next_val > list_len { |
| 106 | next_val = list_len; |
| 107 | } |
| 108 | (prev_val, next_val - prev_val) |
| 109 | } |
| 110 | Mode::Reverse => { |
| 111 | let mut prev_val = list_len; |
| 112 | let mut next_val = ((prev_val + 1) / 2) - 1; |
| 113 | while cmp.is_gt(&list[next_val], key)? { |
| 114 | prev_val = next_val + 1; |
| 115 | next_val = (next_val + 1) / 2; |
| 116 | if next_val != 0 { |
| 117 | next_val -= 1; |
| 118 | } else { |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | (next_val, prev_val - next_val) |
| 123 | } |
| 124 | }; |
| 125 | Ok(ret) |
| 126 | } |