feanor_math::seq

Trait SelfSubvectorFn

Source
pub trait SelfSubvectorFn<T>: Sized + VectorFn<T> {
    // Required method
    fn restrict_full(self, range: Range<usize>) -> Self;

    // Provided method
    fn restrict<R: RangeBounds<usize>>(self, range: R) -> Self { ... }
}
Expand description

Trait for VectorFns that support shrinking, i.e. transforming the vector into a subvector of itself.

Note that you can easily get a subvector of a vector by using subvector::SubvectorFn, but this will wrap the original type. This makes subvector::SubvectorFn unsuitable for some applications, like recursive algorithms.

Note also that SelfSubvectorFn::restrict() consumes the current object, thus it is most useful for vectors that implement Clone/Copy, in particular for references to vectors.

This is the VectorFn-counterpart to SelfSubvectorView.

§Default impls

As opposed to VectorView, there are no implementations of VectorFn for standard containers like Vec<T>, &[T] etc. This is because it is not directly clear whether elements should be cloned on access, or whether a VectorFn<&T> is desired. Instead, use the appropriate functions VectorView::as_fn() or VectorView::clone_els() to create a VectorFn. An exception is made for Range<usize>, which directly implements VectorFn. This allows for yet another way of creating arbitrary VectorFns by using (0..len).map_fn(|i| ...).

§Example

fn compute_sum_recursive<V: SelfSubvectorFn<usize>>(vec: V) -> usize {
    if vec.len() == 0 {
        0
    } else {
        vec.at(0) + compute_sum_recursive(vec.restrict(1..))
    }
}
assert_eq!(10, compute_sum_recursive(SubvectorFn::new([1, 2, 3, 4].copy_els())));
assert_eq!(10, compute_sum_recursive(SubvectorFn::new(vec![1, 2, 3, 4].copy_els())));
assert_eq!(10, compute_sum_recursive(SubvectorFn::new((&[1, 2, 3, 4, 5][..4]).copy_els())));

Required Methods§

Source

fn restrict_full(self, range: Range<usize>) -> Self

Returns a SelfSubvectorFn that represents the elements within the given range of this vector.

Provided Methods§

Source

fn restrict<R: RangeBounds<usize>>(self, range: R) -> Self

Returns a SelfSubvectorFn that represents the elements within the given range of this vector.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<V: SelfSubvectorView<T>, T, F: Fn(&T) -> T> SelfSubvectorFn<T> for CloneElFn<V, T, F>

Source§

impl<V: VectorFn<T>, T> SelfSubvectorFn<T> for SubvectorFn<V, T>