|
| 1 | +# Recursion |
| 2 | + |
| 3 | +```rust |
| 4 | +use std::cmp::Ordering; |
| 5 | + |
| 6 | +fn find_rec<U: AsRef<[T]>, T: Ord>(array: U, key: T, offset: usize) -> Option<usize> { |
| 7 | + let array = array.as_ref(); |
| 8 | + if array.len() == 0 { |
| 9 | + return None; |
| 10 | + } |
| 11 | + let mid = array.len() / 2; |
| 12 | + |
| 13 | + match array[mid].cmp(&key) { |
| 14 | + Ordering::Equal => Some(offset + mid), |
| 15 | + Ordering::Less => find_rec(&array[mid + 1..], key, offset + mid + 1), |
| 16 | + Ordering::Greater => find_rec(&array[..mid], key, offset), |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +pub fn find<U: AsRef<[T]>, T: Ord>(array: U, key: T) -> Option<usize> { |
| 21 | + find_rec(array, key, 0) |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +This approach starts by using the [`Ordering`][ordering-enum] enum. |
| 26 | + |
| 27 | +The `find_rec()` function has a signature to support the optional generic tests. |
| 28 | +To support slices, arrays and Vecs, which can be of varying lengths and sizes at runtime, |
| 29 | +the compiler needs to be given informaton it can know at compile time. |
| 30 | +A reference to any of those containers will always be of the same size (essentially the size of a pointer), |
| 31 | +so [`AsRef`][asref] is used to constrain the generic type to be anything that is a reference or can be coerced to a reference. |
| 32 | + |
| 33 | +The `<[T]>` is used to constrain the reference type to an indexable type `T`. |
| 34 | +The `T` is constrained to be anything which implements the [`Ord`][ord] trait, which essentially means the values must be able to be ordered. |
| 35 | + |
| 36 | +So, the `key` is of type `T` (orderable), and the `array` is of type `U` (a reference to an indexable container of orderable values |
| 37 | +of the same type as the `key`.) |
| 38 | + |
| 39 | +Since slices of the `array` will keep getting shorter with each recursive call to itself, `find_rec()` has an `offset` parameter |
| 40 | +to keep track of the actual midpoint as it relates to the original `array`. |
| 41 | + |
| 42 | +Although `array` is defined as generic type `U`, which is constrained to be of type `AsRef`, |
| 43 | +the [`as_ref()`][asref] method is used to get the reference to the actual type. |
| 44 | +Without it, the compiler would complain that "no method named `len` found for type parameter `U` in the current scope" and |
| 45 | +"cannot index into a value of type `U`". |
| 46 | + |
| 47 | +If the `array` is empty, then [`None`][none] is returned. |
| 48 | + |
| 49 | +The midpoint of the `array` is used to get the element of the `array` at the index of the midpoint value. |
| 50 | +The [`cmp()`][cmp] method of the `Ord` trait is used to compare that element value with the key value. |
| 51 | +Since the element is a reference, the `key` must also be referenced. |
| 52 | + |
| 53 | +The [`match`][match] arms each use a value from the `Ordering` enum. |
| 54 | +- If the midpoint element value equals the `key`, then the midpoint plus the offset is returned from the function wrapped in a [`Some`][some]. |
| 55 | +- If the midpoint element value is less than the `key`, then `find_rec()` calls itself, |
| 56 | +passing a slice of the `array` from the element to the right of the midpoint through the end of the `array`. |
| 57 | +The offset is adjusted to be itself plus the midpoint plus `1`. |
| 58 | +- If the midpoint element value is greater than the `key`, then `find_rec()` calls itself, |
| 59 | +passing a slice of the `array` from the beginning up to but not including the midpoint element. |
| 60 | +The offset remains as is. |
| 61 | + |
| 62 | +While the element value is not equal to the `key`, `find_rec()` keeps calling itself while halving the number of elements being searched, |
| 63 | +until either the `key` is found, or, if it is not in the `array`, the `array` is whittled down to empty. |
| 64 | + |
| 65 | +The `find()` method returns the final result from calling the `find_rec()` method, passing in the `array`, `key`, and `0` for the initial |
| 66 | +offset value. |
| 67 | + |
| 68 | +[ordering-enum]: https://doc.rust-lang.org/std/cmp/enum.Ordering.html |
| 69 | +[asref]: https://doc.rust-lang.org/std/convert/trait.AsRef.html |
| 70 | +[ord]: https://doc.rust-lang.org/std/cmp/trait.Ord.html |
| 71 | +[asref]: https://doc.rust-lang.org/std/convert/trait.AsRef.html#tymethod.as_ref |
| 72 | +[usize]: https://doc.rust-lang.org/std/primitive.usize.html |
| 73 | +[match]: https://doc.rust-lang.org/rust-by-example/flow_control/match.html |
| 74 | +[cmp]: https://doc.rust-lang.org/std/cmp/trait.Ord.html#tymethod.cmp |
| 75 | +[none]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None |
| 76 | +[some]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.Some |
0 commit comments