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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
use std::marker::{PhantomData};
use std::ops::{Deref};

use super::{Index, Isomorphic, Flatten, Broadcast, impl_ops_for_view, Binary};

/// Implemented by types that behave like an array of `Self::T`s indexed by
/// `Self::I`, but whose array elements are computed on demand.
///
/// ### Arithmetic
///
/// All implementations of `View` defined in this crate define the standard
/// Rust arithmetic operators to mean pointwise arithmetic. For example,
/// `(v + w).at(i)` gives the same answer as `v.at(i) + w.at(i)`.
///
/// More generally, `View`s with different index types can be added if they are
/// compatible according to the [`Broadcast`] trait. For example,
/// `(v + Scalar(x)).at(i)` gives the same answer as `v.at(i) + x`.
///
/// You are encouraged to define your `View`s similarly, e.g. using the macro
/// [`impl_ops_for_view`] which is provided for this purpose.
///
/// ### Ownership
///
/// If `V` implements `View`, then so do `&V`, `Box<V>`, `Rc<V>` and all other
/// types that [`Deref`] to `V`. This means that the `View` structures you can
/// build are agnostic about the ownership of the data they access.
///
/// ```
/// use multidimension::{Index, View, Array};
/// let a: Array<_, _> = std::rc::Rc::new(usize::all(5)).collect();
/// assert_eq!(a.as_ref(), [0, 1, 2, 3, 4]);
/// ```
///
/// If you implement a `View` that draws data from another `View`, it should
/// own the other view, as this is the most general design.
///
/// ```
/// use multidimension::{Index, View};
/// /// `MyView` owns a `V`.
/// struct MyView<V>(V);
/// impl<V: View> View for MyView<V> {
///     type I = V::I;
///     type T = V::T;
///     fn size(&self) -> <Self::I as Index>::Size { self.0.size() }
///     fn at(&self, index: Self::I) -> Self::T { self.0.at(index) }
/// }
/// /// MyView can nonetheless borrow a `V`.
/// fn my_borrow<V: View>(v: &V) -> MyView<&V> { MyView(v) }
/// ```
pub trait View: Sized {
    /// The index type.
    type I: Index;

    /// The element type.
    type T;

    /// The size of the array.
    fn size(&self) -> <Self::I as Index>::Size;

    /// The number of elements in `Self`.
    fn len(&self) -> usize { <Self::I as Index>::length(self.size()) }

    /// Compute the element at `index`.
    fn at(&self, index: Self::I) -> Self::T;

    /// Materialises this `View` into a collection of type `A`, e.g. an
    /// [`Array`].
    ///
    /// ```
    /// use multidimension::{Index, View, Array};
    /// let a: Array<_, _> = usize::all(5).collect();
    /// assert_eq!(a.as_ref(), [0, 1, 2, 3, 4]);
    /// ```
    ///
    /// [`Array`]: super::Array
    fn collect<A: FromView<Self::I, Self::T>>(&self) -> A {
        A::from_view(self)
    }

    /// Creates a `View` that clones its elements.
    ///
    /// This is useful when you have a `View` that computes `&T`, but you need
    /// a `View` that computes `T`.
    fn cloned<'u, U>(self) -> Cloned<Self> where
        U: 'u + Clone,
        Self: View<T=&'u U>,
    {
        Cloned(self)
    }

    /// Returns a `View` that maps `(i, i)` to `t` when `Self` maps `i` to `t`.
    /// It maps `(i, j)` to `T::default()` if `i != t`.
    ///
    /// ```
    /// use multidimension::{Index, View, Array};
    /// let a: Array<_, _> = usize::all(3).map(|x| x + 10).diagonal().collect();
    /// assert_eq!(a.as_ref(), [
    ///     10, 0, 0,
    ///     0, 11, 0,
    ///     0, 0, 12,
    /// ]);
    /// ```
    fn diagonal(self) -> Diagonal<Self> where
        Self::I: PartialEq,
        Self::T: Default,
    {
        Diagonal(self)
    }

    /// Creates a `View` that applies `f` to the elements of `Self`.
    ///
    /// There is no guarantee that the elements will be passed to `f` in a
    /// particular order, only once, or at all.
    ///
    /// ```
    /// use multidimension::{Index, View, Array};
    /// let a: Array<_, _> = usize::all(5).map(|x| x*x).collect();
    /// assert_eq!(a.as_ref(), [0, 1, 4, 9, 16]);
    /// ```
    fn map<U, F>(self, f: F) -> Map<Self, F> where
        F: Fn(Self::T) -> U,
    {
        Map(self, f)
    }

    /// Creates a `View` that uses `self` to select elements of `other`.
    ///
    /// ```
    /// use multidimension::{Index, View, Array};
    /// let a: Array<bool, usize> = Array::new((), [2, 1]);
    /// let b: Array<usize, &str> = Array::new(3, ["apple", "body", "crane"]);
    /// let ab: Array<bool, &str> = a.compose(b).collect();
    /// assert_eq!(ab.as_ref(), ["crane", "body"])
    /// ```
    fn compose<V>(self, other: V) -> Compose<Self, V> where
        V: View<I=Self::T>,
    {
        Compose(self, other)
    }

    /// Creates a `View` that pairs of an element of `self` and an element of
    /// `other`.
    ///
    /// Two `View`s can be zipped only if they have compatible indices. See
    /// trait [`Broadcast`] for more details.
    ///
    /// Here's an example of zipping two 1D [`Array`]s of the same size:
    /// ```
    /// use multidimension::{Index, View, Array};
    /// let a: Array<usize, usize> = usize::all(3).collect();
    /// let b: Array<usize, &str> = Array::new(3, ["apple", "body", "crane"]);
    /// let ab: Array<usize, (usize, &str)> = a.zip(b).collect();
    /// assert_eq!(ab.as_ref(), [
    ///     (0, "apple"),
    ///     (1, "body"),
    ///     (2, "crane"),
    /// ]);
    /// ```
    ///
    /// Here's an example of zipping a 1D [`Array`] with a [`Scalar`]:
    /// ```
    /// use multidimension::{Index, View, Array, Scalar};
    /// let a: Array<usize, usize> = usize::all(3).collect();
    /// let b = Scalar("repeated");
    /// let ab: Array<usize, (usize, &str)> = a.zip(b).collect();
    /// assert_eq!(ab.as_ref(), [
    ///     (0, "repeated"),
    ///     (1, "repeated"),
    ///     (2, "repeated"),
    /// ]);
    /// ```
    /// 
    /// Here's an example of zipping two 2D [`Array`]s of different shape:
    /// ```
    /// use multidimension::{Index, View, Array};
    /// let a: Array<(usize, ()), usize> = usize::all(3).iso().collect();
    /// let b: Array<((), bool), bool> = bool::all(()).iso().collect();
    /// let ab: Array<(usize, bool), (usize, bool)> = a.zip(b).collect();
    /// assert_eq!(ab.as_ref(), [
    ///     (0, false), (0, true),
    ///     (1, false), (1, true),
    ///     (2, false), (2, true),
    /// ]);
    /// ```
    ///
    /// [`Array`]: super::Array
    fn zip<V>(self, other: V) -> Zip<Self, V, super::ops::Pair> where
        V: View,
        Self::I: Broadcast<V::I>,
    {
        Zip(self, other, PhantomData)
    }

    /// Creates a `View` that pairs of an element of `self` and an element of
    /// `other`, then applies binary operator `B`.
    ///
    /// ```
    /// use multidimension::{Index, View, Array, ops::Add};
    /// let a: Array<usize, usize> = Array::new(3, [9, 8, 7]);
    /// let b: Array<usize, usize> = Array::new(3, [10, 20, 30]);
    /// // Equivalent to `(a + b).collect()`.
    /// let ab: Array<usize, usize> = a.binary::<_, Add>(b).collect();
    /// assert_eq!(ab.as_ref(), [19, 28, 37]);
    /// ```
    fn binary<V, B>(self, other: V) -> Zip<Self, V, B> where
        V: View,
        Self::I: Broadcast<V::I>,
        B: Binary<Self::T, V::T>,
    {
        Zip(self, other, PhantomData)
    }

    /// Change the index type of this `View` to an [`Isomorphic`] type.
    fn iso<J: Index>(self) -> Iso<Self, J> where
        J: Isomorphic<Self::I>,
        J::Size: Isomorphic<<Self::I as Index>::Size>,
    {
        Iso(self, PhantomData)
    }

    /// Reorder the indices of this `View` up to [`Isomorphic`].
    ///
    /// This method makes a `View` indexed by `(I, (X, Y), J)` if `Self` is
    /// indexed by `(I, (Y, X), J)`, i.e. it swaps the `X` and `Y` indices.
    /// Note that `I` and/or `J` can be `()` if they are unwanted.
    ///
    /// ```
    /// use multidimension::{Index, View, Array};
    /// let a: Array<_, _> = <(usize, usize)>::all((3, 2)).collect();
    /// assert_eq!(a.as_ref(), [
    ///     (0, 0), (0, 1),
    ///     (1, 0), (1, 1),
    ///     (2, 0), (2, 1),
    /// ]);
    /// let b: Array<_, _> = a.transpose::<(), usize, usize, ()>().collect();
    /// assert_eq!(b.as_ref(), [
    ///     (0, 0), (1, 0), (2, 0),
    ///     (0, 1), (1, 1), (2, 1),
    /// ]);
    /// ```
    fn transpose<I: Index, X: Index, Y: Index, J: Index>(self)
    -> Transpose<Self, I, X, Y, J> where
        (I, (Y, X), J): Isomorphic<Self::I>,
        (I::Size, (Y::Size, X::Size), J::Size): Isomorphic<<Self::I as Index>::Size>,
    {
        Transpose(self, PhantomData)
    }

    /// Returns a `View` whose `at(j)` returns `self.at(i, j)`.
    ///
    /// ```
    /// use multidimension::{Index, View, Array};
    /// let a: Array<_, _> = <(usize, usize)>::all((3, 2)).collect();
    /// assert_eq!(a.as_ref(), [
    ///     (0, 0), (0, 1),
    ///     (1, 0), (1, 1),
    ///     (2, 0), (2, 1),
    /// ]);
    /// let a_row: Array<_, _> = a.row::<usize, usize>(1).collect();
    /// assert_eq!(a_row.as_ref(), [
    ///     (1, 0), (1, 1),
    /// ]);
    /// ```
    fn row<I: Index, J: Index>(self, i: I) -> Row<Self, I, J> where
        (I, J): Isomorphic<Self::I>,
        (I::Size, J::Size): Isomorphic<<Self::I as Index>::Size>,
    {
        Row(self, i, PhantomData)
    }

    /// Returns a `View` whose `at(i)` returns `self.at(i, j)`.
    ///
    /// ```
    /// use multidimension::{Index, View, Array};
    /// let a: Array<_, _> = <(usize, usize)>::all((3, 2)).collect();
    /// assert_eq!(a.as_ref(), [
    ///     (0, 0), (0, 1),
    ///     (1, 0), (1, 1),
    ///     (2, 0), (2, 1),
    /// ]);
    /// let a_column: Array<_, _> = a.column::<usize, usize>(1).collect();
    /// assert_eq!(a_column.as_ref(), [
    ///     (0, 1), (1, 1), (2, 1),
    /// ]);
    /// ```
    fn column<I: Index, J: Index>(self, j: J) -> Column<Self, I, J> where
        (I, J): Isomorphic<Self::I>,
        (I::Size, J::Size): Isomorphic<<Self::I as Index>::Size>,
        (J, I): Flatten,
        (J::Size, I::Size): Flatten,
    {
        self.transpose::<(), J, I, ()>().row(j)
    }
}

impl<V: View, T: Deref<Target=V>> View for T {
    type I = V::I;
    type T = V::T;
    fn size(&self) -> <Self::I as Index>::Size { V::size(self) }
    fn at(&self, index: Self::I) -> Self::T { V::at(self, index) }
}

// ----------------------------------------------------------------------------

/// The return type of [`View::cloned()`].
#[derive(Debug, Copy, Clone)]
pub struct Cloned<V>(V);

impl<'t, T: 't + Clone, V: View<T=&'t T>> View for Cloned<V> {
    type I = V::I;
    type T = T;
    fn size(&self) -> <Self::I as Index>::Size { self.0.size() }
    fn at(&self, index: Self::I) -> Self::T { self.0.at(index).clone() }
}

impl_ops_for_view!(Cloned<V>);

// ----------------------------------------------------------------------------

/// The return type of [`View::diagonal()`].
#[derive(Debug, Copy, Clone)]
pub struct Diagonal<V>(V);

impl<V: View> View for Diagonal<V> where
    V::I: PartialEq,
    V::T: Default,
    (V::I, V::I): Flatten,
    (<V::I as Index>::Size, <V::I as Index>::Size): Flatten,
{
    type I = (V::I, V::I);
    type T = V::T;
    fn size(&self) -> <Self::I as Index>::Size { (self.0.size(), self.0.size()) }
    fn at(&self, index: Self::I) -> Self::T {
        if index.0 == index.1 { self.0.at(index.0) } else { Default::default() }
    }
}

impl_ops_for_view!(Diagonal<V>);

// ----------------------------------------------------------------------------

/// The return type of [`View::map()`].
#[derive(Debug, Copy, Clone)]
pub struct Map<V, F>(V, F);

impl<V: View, U, F: Fn(V::T) -> U> View for Map<V, F> {
    type I = V::I;
    type T = U;
    fn size(&self) -> <Self::I as Index>::Size { self.0.size() }
    fn at(&self, index: Self::I) -> Self::T { self.1(self.0.at(index)) }
}

impl_ops_for_view!(Map<V, F>);

// ----------------------------------------------------------------------------

/// The return type of [`View::compose()`].
#[derive(Debug, Copy, Clone)]
pub struct Compose<V, W>(V, W);

impl<V: View, W: View<I=V::T>> View for Compose<V, W> {
    type I = V::I;
    type T = W::T;
    fn size(&self) -> <Self::I as Index>::Size { self.0.size() }
    fn at(&self, index: Self::I) -> Self::T { self.1.at(self.0.at(index)) }
}

impl_ops_for_view!(Compose<V, W>);

// ----------------------------------------------------------------------------

/// The return type of [`View::zip()`] and [`View::binary()`].
#[derive(Debug, Copy, Clone)]
pub struct Zip<V, W, B>(V, W, PhantomData<B>);

impl<V: View, W: View, B> View for Zip<V, W, B> where
    V::I: Broadcast<W::I>,
    B: Binary<V::T, W::T>,
{
    type I = <V::I as Broadcast<W::I>>::Result;
    type T = <B as Binary<V::T, W::T>>::Output;

    fn size(&self) -> <Self::I as Index>::Size {
        <V::I as Broadcast<W::I>>::size(self.0.size(), self.1.size())
    }

    fn at(&self, index: Self::I) -> Self::T {
        let (v_index, w_index) = <V::I as Broadcast<W::I>>::index(index);
        B::call(self.0.at(v_index), self.1.at(w_index))
    }
}

impl_ops_for_view!(Zip<V, W, B>);

// ----------------------------------------------------------------------------

/// The return type of [`View::iso()`].
#[derive(Debug, Copy, Clone)]
pub struct Iso<V, J: Index>(V, PhantomData<J>);

impl<V: View, J: Index> View for Iso<V, J> where
    J: Isomorphic<V::I>,
    J::Size: Isomorphic<<V::I as Index>::Size>,
{
    type I = J;
    type T = V::T;
    fn size(&self) -> J::Size { Isomorphic::from_iso(self.0.size()) }
    fn at(&self, index: J) -> Self::T { self.0.at(index.to_iso()) }
}

impl_ops_for_view!(Iso<V, J: Index>);

// ----------------------------------------------------------------------------

/// The return type of [`View::transpose()`].
#[derive(Debug, Copy, Clone)]
pub struct Transpose<V, I, X, Y, J>(V, PhantomData<(I, (X, Y), J)>);

impl<V: View, I: Index, X: Index, Y: Index, J: Index> View for Transpose<V, I, X, Y, J> where
    (I, (Y, X), J): Isomorphic<V::I>,
    (I::Size, (Y::Size, X::Size), J::Size): Isomorphic<<V::I as Index>::Size>,
    (X, Y): Flatten,
    (I, (X, Y), J): Flatten,
    (X::Size, Y::Size): Flatten,
    (I::Size, (X::Size, Y::Size), J::Size): Flatten,
{
    type I = (I, (X, Y), J);
    type T = V::T;

    fn size(&self) -> (I::Size, (X::Size, Y::Size), J::Size) {
        let (i, (y, x), j) = Isomorphic::from_iso(self.0.size());
        (i, (x, y), j)
    }

    fn at(&self, index: (I, (X, Y), J)) -> Self::T {
        let (i, (x, y), j) = index;
        self.0.at((i, (y, x), j).to_iso())
    }
}

impl_ops_for_view!(Transpose<V, I, X, Y, J>);

// ----------------------------------------------------------------------------

/// The return type of [`View::row()`].
#[derive(Debug, Copy, Clone)]
pub struct Row<V, I, J>(V, I, PhantomData<J>);

impl<V: View, I: Index, J: Index> View for Row<V, I, J> where
    (I, J): Isomorphic<V::I>,
    (I::Size, J::Size): Isomorphic<<V::I as Index>::Size>,
{
    type I = J;
    type T = V::T;
    fn size(&self) -> J::Size { <(I::Size, J::Size)>::from_iso(self.0.size()).1 }
    fn at(&self, index: J) -> Self::T { self.0.at((self.1, index).to_iso()) }
}

impl_ops_for_view!(Row<V, I, J>);

// ----------------------------------------------------------------------------

/// The return type of [`View::column()`]
type Column<V, I, J> = Row<Transpose<V, (), J, I, ()>, J, I>;

// ----------------------------------------------------------------------------

/// A 0-dimensional [`View`].
#[derive(Debug, Copy, Clone)]
pub struct Scalar<T: Clone>(pub T);

impl<T: Clone> View for Scalar<T> {
    type I = ();
    type T = T;
    fn size(&self) -> () { () }
    fn at(&self, _: ()) -> T { self.0.clone() }
}

impl_ops_for_view!(Scalar<T: Clone>);

// ----------------------------------------------------------------------------

/// Construct a collection from a `View`.
pub trait FromView<I: Index, T> {
    fn from_view<V: View<I=I, T=T>>(v: &V) -> Self;
}