1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
use super::*;
use super::vec_fn::VectorFn;
use std::ops::{RangeBounds, Bound, Index, IndexMut};
use std::marker::PhantomData;
///
/// Trait for all vector views that "shrink" the section of the vector
/// that is represented, without changing the type. This is mainly important
/// in combination with recursive algorithms.
///
/// # Example
///
/// Assuming we wanted to implement a (very stupid) recursive variant of summing
/// all values in a vector. We would like to do it as
/// ```rust,ignore
/// # use feanor_math::vector::*;
/// # use feanor_math::vector::subvector::*;
///
/// // Compiler error: overflow evaluating the requirement `&Vec<i64>: Sized
/// fn sum<V: VectorView<i64> + Copy>(vector: V) -> i64 {
/// if vector.len() == 0 { 0 } else { sum(Subvector::new(vector).subvector(1..)) + *vector.at(0) }
/// }
///
/// assert_eq!(7, sum(&[1, 1, 1, 1, 1, 1, 1][..]));
/// ```
/// but this clearly cannot work - this can never be monomorphized.
/// Instead, use
/// ```
/// # use feanor_math::vector::*;
/// # use feanor_math::vector::subvector::*;
///
/// // This works!
/// fn sum<V: SelfSubvectorView<i64> + Copy>(vector: V) -> i64 {
/// if vector.len() == 0 { 0 } else { sum(vector.subvector(1..)) + *vector.at(0) }
/// }
///
/// assert_eq!(7, sum(&[1, 1, 1, 1, 1, 1, 1][..]));
/// ```
///
/// ## The mutable case
///
/// In the mutable case, this is much more difficult, as we cannot have vectors that are `Copy`.
/// Simple examples still work:
/// ```
/// # use feanor_math::vector::*;
/// # use feanor_math::vector::subvector::*;
///
/// fn inc<V: VectorViewMut<i64> + SelfSubvectorView<i64>>(mut vector: V) {
/// if vector.len() > 0 {
/// *vector.at_mut(0) += 1;
/// inc(vector.subvector(1..));
/// }
/// }
///
/// let mut data = [1, 1, 1, 0, 0, 0];
/// inc(Subvector::new(&mut data));
/// assert_eq!([2, 2, 2, 1, 1, 1], data);
/// ```
/// But it is a problem that [`SelfSubvectorView::subvector()`] moves the current object.
/// In particular, the following does not work:
/// ```rust,ignore
/// # use feanor_math::vector::*;
/// # use feanor_math::vector::subvector::*;
///
/// fn inc<V: VectorViewMut<i64> + SelfSubvectorView<i64>>(mut vector: V) {
/// if vector.len() > 0 {
/// inc(vector.subvector(1..));
/// *vector.at_mut(0) += 1;
/// }
/// }
///
/// let mut data = [1, 1, 1, 0, 0, 0];
/// inc(Subvector::new(&mut data));
/// assert_eq!([2, 2, 2, 1, 1, 1], data);
/// ```
/// Currently, there is no solution implemented for this, as in most cases mutable
/// slices do the job. However, it might be solved with a `BorrowableMut`-trait in
/// the future.
///
pub trait SelfSubvectorView<T: ?Sized>: VectorView<T> {
fn subvector<R: RangeBounds<usize>>(self, range: R) -> Self;
}
impl<'a, T> SelfSubvectorView<T> for &'a [T] {
fn subvector<R: RangeBounds<usize>>(self, range: R) -> Self {
self.index((range.start_bound().cloned(), range.end_bound().cloned()))
}
}
impl<'a, T> SelfSubvectorView<T> for &'a mut [T] {
fn subvector<R: RangeBounds<usize>>(self, range: R) -> Self {
self.index_mut((range.start_bound().cloned(), range.end_bound().cloned()))
}
}
///
/// A view on a part of another vector view.
///
pub struct Subvector<T: ?Sized, V>
where V: VectorView<T>
{
from: usize,
to: usize,
base: V,
element: PhantomData<T>
}
impl<T: ?Sized, V> Subvector<T, V>
where V: VectorView<T>
{
pub fn new(base: V) -> Self {
Subvector {
from: 0,
to: base.len(),
base: base,
element: PhantomData
}
}
}
impl<T: ?Sized, V> VectorView<T> for Subvector<T, V>
where V: VectorView<T>
{
fn len(&self) -> usize {
self.to - self.from
}
fn at(&self, i: usize) -> &T {
debug_assert!(i < self.len());
self.base.at(i + self.from)
}
}
impl<T: ?Sized, V> VectorViewMut<T> for Subvector<T, V>
where V: VectorViewMut<T>
{
fn at_mut(&mut self, i: usize) -> &mut T {
debug_assert!(i < self.len());
self.base.at_mut(i + self.from)
}
}
impl<T, V> SwappableVectorViewMut<T> for Subvector<T, V>
where V: SwappableVectorViewMut<T>
{
fn swap(&mut self, i: usize, j: usize) {
debug_assert!(i < self.len());
debug_assert!(j < self.len());
self.base.swap(i + self.from, j + self.from);
}
}
impl<T: ?Sized, V> SelfSubvectorView<T> for Subvector<T, V>
where V: VectorView<T>
{
fn subvector<R: RangeBounds<usize>>(mut self, range: R) -> Self {
let from = match range.start_bound() {
Bound::Included(x) => *x,
Bound::Excluded(x) => *x + 1,
Bound::Unbounded => 0
};
assert!(from <= self.len());
let to = match range.end_bound() {
Bound::Included(x) => *x + 1,
Bound::Excluded(x) => *x,
Bound::Unbounded => self.len()
};
assert!(to <= self.len());
assert!(to >= from);
self.to = to + self.from;
self.from = from + self.from;
return self;
}
}
impl<T: ?Sized, V> Copy for Subvector<T, V>
where V: VectorView<T> + Copy
{}
impl<T: ?Sized, V> Clone for Subvector<T, V>
where V: VectorView<T> + Clone
{
fn clone(&self) -> Self {
Self {
base: self.base.clone(),
from: self.from,
to: self.to,
element: PhantomData
}
}
}
pub trait SelfSubvectorFn<T>: VectorFn<T> {
fn subvector<R: RangeBounds<usize>>(self, range: R) -> Self;
}
pub struct SubvectorFn<T, V>
where V: VectorFn<T>
{
from: usize,
to: usize,
base: V,
element: PhantomData<T>
}
impl<T, V> SubvectorFn<T, V>
where V: VectorFn<T>
{
pub fn new(base: V) -> Self {
SubvectorFn {
from: 0,
to: base.len(),
base: base,
element: PhantomData
}
}
}
impl<T, V> VectorFn<T> for SubvectorFn<T, V>
where V: VectorFn<T>
{
fn len(&self) -> usize {
self.to - self.from
}
fn at(&self, i: usize) -> T {
debug_assert!(i < self.len());
self.base.at(i + self.from)
}
}
impl<T, V> SelfSubvectorFn<T> for SubvectorFn<T, V>
where V: VectorFn<T>
{
fn subvector<R: RangeBounds<usize>>(mut self, range: R) -> Self {
let from = match range.start_bound() {
Bound::Included(x) => *x,
Bound::Excluded(x) => *x + 1,
Bound::Unbounded => 0
};
assert!(from <= self.len());
let to = match range.end_bound() {
Bound::Included(x) => *x + 1,
Bound::Excluded(x) => *x,
Bound::Unbounded => self.len()
};
assert!(to <= self.len());
assert!(to >= from);
self.to = to + self.from;
self.from = from + self.from;
return self;
}
}
impl<T, V> Copy for SubvectorFn<T, V>
where V: VectorFn<T> + Copy
{}
impl<T, V> Clone for SubvectorFn<T, V>
where V: VectorFn<T> + Clone
{
fn clone(&self) -> Self {
Self {
base: self.base.clone(),
from: self.from,
to: self.to,
element: PhantomData
}
}
}
#[cfg(test)]
use super::vec_fn::IntoVectorFn;
#[test]
fn test_subvector_ranges() {
let a = Subvector::new([0, 1, 2, 3, 4]);
assert_eq!(3, a.subvector(0..3).len());
assert_eq!(3, a.subvector(0..=2).len());
assert_eq!(5, a.subvector(0..).len());
assert_eq!(5, a.subvector(..).len());
assert_eq!(2, a.subvector(3..).len());
}
#[test]
fn test_subvector_fn_ranges() {
let a = SubvectorFn::new([0, 1, 2, 3, 4].into_fn());
assert_eq!(3, a.subvector(0..3).len());
assert_eq!(3, a.subvector(0..=2).len());
assert_eq!(5, a.subvector(0..).len());
assert_eq!(5, a.subvector(..).len());
assert_eq!(2, a.subvector(3..).len());
}