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
use crate::{index::IntoIndex, iter_over_val::IterOverValues};

/// Trait to provide abstraction over `DIM`-dimensional vectors allowing access using indices.
///
/// Such an abstraction is particularly important in performance-critical algorithms both requiring flexibility through abstraction
/// over inputs and performance through monomorphization.
///
/// This trait for a given or generic `DIM` can be extended by implementing `fn at<Idx: IntoIndex<DIM>>(&self, index: Idx) -> Option<T>`.
///
/// # Examples
///
/// ## Dimension 1 Example
///
/// ```rust
/// use orx_funvec::*;
/// use orx_closure::Capture;
/// use std::collections::HashMap;
///
/// fn moving_average<V: FunVec<1, i32>>(observations: &V, period: usize) -> Option<i32> {
///     let last = if period == 0 { None } else { observations.at(period - 1) };
///     let current = observations.at(period);
///
///     match (last, current) {
///         (None, None) => None,
///         (None, Some(y)) => Some(y),
///         (Some(x), None) => Some(x),
///         (Some(x), Some(y)) => Some((x + y) / 2)
///     }
/// }
///
/// let period = 2;
///
/// let stdvec = vec![10, 11, 12, 13];
/// assert_eq!(Some(11), moving_average(&stdvec, period));
///
/// let map = HashMap::from_iter([(1, 10), (2, 20), (3, 30)].into_iter());
/// assert_eq!(Some(15), moving_average(&map, period));
///
/// let closure = Capture(()).fun(|_, i: usize| Some(if i == 2 { 20 } else { 30 }));
/// assert_eq!(Some(25), moving_average(&closure, period));
///
/// let uniform = ScalarAsVec(42);
/// assert_eq!(Some(42), moving_average(&uniform, period));
///
/// let no_data = EmptyVec::new();
/// assert_eq!(None, moving_average(&no_data, period));
/// ```
///
/// ## Dimension 2 Example
///
/// ```rust
/// use orx_funvec::*;
/// use orx_closure::Capture;
/// use std::collections::{BTreeMap, HashMap};
///
/// fn distance<V: FunVec<2, u32>>(distances: &V, a: usize, b: usize) -> Option<u32> {
///     distances.at([a, b])
/// }
///
/// // complete matrix
/// let jagged_vecs = vec![
///     vec![0, 1, 2, 3],
///     vec![10, 11, 12, 13],
///     vec![20, 21, 22, 23],
///     vec![30, 31, 32, 33],
/// ];
/// assert_eq!(Some(23), distance(&jagged_vecs, 2, 3));
/// assert_eq!(None, distance(&jagged_vecs, 2, 4));
/// assert_eq!(None, distance(&jagged_vecs, 4, 0));
///
/// // some sparsity in the first or second dimensions
/// let vec_of_maps = vec![
///     BTreeMap::from_iter([(1, 1), (14, 2)].into_iter()),
///     BTreeMap::from_iter([(0, 10), (7, 20)].into_iter()),
///     BTreeMap::from_iter([(9, 100), (16, 200)].into_iter()),
/// ];
/// assert_eq!(Some(20), distance(&vec_of_maps, 1, 7));
/// assert_eq!(None, distance(&vec_of_maps, 0, 0));
/// assert_eq!(None, distance(&vec_of_maps, 3, 0));
///
/// let map_of_vecs = HashMap::from_iter([
///     (1, vec![3, 4, 5]),
///     (7, vec![30, 40, 50]),
/// ].into_iter());
/// assert_eq!(Some(5), distance(&map_of_vecs, 1, 2));
/// assert_eq!(Some(40), distance(&map_of_vecs, 7, 1));
/// assert_eq!(None, distance(&map_of_vecs, 0, 0));
///
/// // complete sparsity
/// let map_of_indices = HashMap::from_iter([
///     ((0, 1), 14),
///     ((3, 6), 42),
/// ].into_iter());
/// assert_eq!(Some(14), distance(&map_of_indices, 0, 1));
/// assert_eq!(Some(42), distance(&map_of_indices, 3, 6));
/// assert_eq!(None, distance(&map_of_indices, 0, 0));
///
/// // closure to compute distances on the fly rather than to store them
/// fn get_euclidean_distance(location1: (f64, f64), location2: (f64, f64)) -> u32 {
///     let (x1, y1) = location1;
///     let (x2, y2) = location2;
///     (f64::powf(x1 - x2, 2.0) + f64::powf(y1 - y2, 2.0)).sqrt() as u32
/// }
/// let locations = vec![(0.0, 1.0), (3.0, 5.0), (7.0, 2.0), (1.0, 1.0)];
/// let closure = Capture(&locations).fun(|loc, (i, j): (usize, usize)| {
///     loc.get(i)
///         .and_then(|l1| loc.get(j).map(|l2| (l1, l2)))
///         .map(|(l1, l2)| get_euclidean_distance(*l1, *l2))
/// });
/// assert_eq!(Some(0), distance(&closure, 1, 1));
/// assert_eq!(Some(5), distance(&closure, 0, 1));
/// assert_eq!(None, distance(&closure, 0, 4));
///
/// // uniform distance for all pairs
/// let uniform = ScalarAsVec(42);
/// assert_eq!(Some(42), distance(&uniform, 7, 42));
///
/// // all disconnected pairs
/// let disconnected = EmptyVec::new();
/// assert_eq!(None, distance(&disconnected, 7, 42));
/// ```
///
/// ## Extension
///
/// Implementing the trait requires implementation of only the `fn at<Idx: IntoIndex<DIM>>(&self, index: Idx) -> Option<T>` method.
///
/// Assume we are working with distance matrices.
/// In certaion scenarios, we observe that we access only a limited number of pairs.
/// Assuming the distance computation is expensive, we do not want to populate and store the entire matrix.
/// Instead, we implement a distance provider with caching capabilities.
/// The goal is to be able to use this provider as a generic distance matrix, and hence, we implement `FunVec<2, _>`.
///
/// ```rust
/// use orx_funvec::*;
/// use std::{cell::RefCell, collections::HashMap};
///
/// type Dist = u32;
///
/// struct DistanceProvider {
///     locations: Vec<(i32, i32)>,
///     cached: RefCell<HashMap<(usize, usize), Dist>>,
/// }
/// impl DistanceProvider {
///     fn distance(&self, from: usize, to: usize) -> Option<Dist> {
///         if let Some(cached) = self.cached.borrow().get(&(from, to)) {
///             return Some(*cached);
///         }
///         let locations = self
///             .locations
///             .get(from)
///             .and_then(|l1| self.locations.get(to).map(|l2| (l1, l2)));
///
///         if let Some((l1, l2)) = locations {
///             let (x1, y1) = l1;
///             let (x2, y2) = l2;
///             let distance =
///                 ((i32::pow(x1 - x2, 2) + i32::pow(y1 - y2, 2)) as f32).sqrt() as Dist;
///
///             // cache computed distance
///             self.cached.borrow_mut().insert((from, to), distance);
///
///             Some(distance)
///         } else {
///             None
///         }
///     }
/// }
///
/// impl FunVec<2, Dist> for DistanceProvider {
///     fn at<Idx: IntoIndex<2>>(&self, index: Idx) -> Option<Dist> {
///         let [from, to] = index.into_index();
///         self.distance(from, to)
///     }
/// }
///
/// let locations = vec![(0, 1), (3, 5), (7, 2), (1, 2)];
/// let distances = DistanceProvider {
///     locations,
///     cached: RefCell::new(HashMap::new()),
/// };
///
/// assert_eq!(Some(5), distances.at([0, 1]));
/// assert_eq!(Some(5), distances.at([0, 1])); // from cache
/// assert_eq!(None, distances.at([0, 4]));
///
/// let pairs = vec![(0, 1), (2, 3)];
/// assert_eq!(
///     11u32,
///     distances.iter_over(pairs.iter().copied()).flatten().sum()
/// );
/// ```
pub trait FunVec<const DIM: usize, T>
where
    T: Clone + Copy,
{
    /// Returns the value at the given `index` or `None` if the position is empty.
    ///
    /// This allows to access elements of all funvec implementations in a unified way. Thanks to monomorphization, this abstraction does not have a performance penalty.
    ///
    /// Note that funvec's are different than, generalization of, traditional vectors since the elements are not necessarily contagious or dense.
    /// Instead they can be sparse to desired degrees.
    ///
    /// Therefore, `at` always returns an optional.
    ///
    /// # Examples - Dimension 1
    ///
    /// ```rust
    /// use orx_funvec::*;
    /// use orx_closure::Capture;
    /// use std::collections::HashMap;
    ///
    /// let stdvec = vec![10, 11, 12, 13];
    /// assert_eq!(Some(13), stdvec.at(3));
    /// assert_eq!(None, stdvec.at(4));
    ///
    /// let map = HashMap::from_iter([(1, 10), (2, 20)].into_iter());
    /// assert_eq!(Some(10), map.at(1));
    /// assert_eq!(None, map.at(0));
    ///
    /// let (s, t) = (0, 42);
    /// let closure = Capture((s, t))
    ///     .fun(|(s, t), i: usize| if i == *s { Some(1) } else if i == *t { Some(-1) } else { None });
    /// assert_eq!(Some(1), closure.at(0));
    /// assert_eq!(Some(-1), closure.at(42));
    /// assert_eq!(None, closure.at(3));
    ///
    /// let scalar = ScalarAsVec(42);
    /// assert_eq!(Some(42), scalar.at(7));
    /// assert_eq!(Some(42), scalar.at(12));
    ///
    /// let empty_vec: EmptyVec<i32> = EmptyVec::new();
    /// assert_eq!(None, empty_vec.at([7]));
    /// assert_eq!(None, empty_vec.at([12]));
    /// ```
    ///
    /// # Examples - Dimension 2
    ///
    /// ```rust
    /// use orx_funvec::*;
    /// use orx_closure::Capture;
    /// use std::collections::{BTreeMap, HashMap};
    ///
    /// fn distance<V: FunVec<2, u32>>(distances: &V, a: usize, b: usize) -> Option<u32> {
    ///     distances.at([a, b])
    /// }
    ///
    /// // complete matrix
    /// let jagged_vecs = vec![
    ///     vec![0, 1, 2, 3],
    ///     vec![10, 11, 12, 13],
    ///     vec![20, 21, 22, 23],
    ///     vec![30, 31, 32, 33],
    /// ];
    /// assert_eq!(Some(23), distance(&jagged_vecs, 2, 3));
    /// assert_eq!(None, distance(&jagged_vecs, 2, 4));
    /// assert_eq!(None, distance(&jagged_vecs, 4, 0));
    ///
    /// // some sparsity in the first or second dimensions
    /// let vec_of_maps = vec![
    ///     BTreeMap::from_iter([(1, 1), (14, 2)].into_iter()),
    ///     BTreeMap::from_iter([(0, 10), (7, 20)].into_iter()),
    ///     BTreeMap::from_iter([(9, 100), (16, 200)].into_iter()),
    /// ];
    /// assert_eq!(Some(20), distance(&vec_of_maps, 1, 7));
    /// assert_eq!(None, distance(&vec_of_maps, 0, 0));
    /// assert_eq!(None, distance(&vec_of_maps, 3, 0));
    ///
    /// let map_of_vecs = HashMap::from_iter([
    ///     (1, vec![3, 4, 5]),
    ///     (7, vec![30, 40, 50]),
    /// ].into_iter());
    /// assert_eq!(Some(5), distance(&map_of_vecs, 1, 2));
    /// assert_eq!(Some(40), distance(&map_of_vecs, 7, 1));
    /// assert_eq!(None, distance(&map_of_vecs, 0, 0));
    ///
    /// // complete sparsity
    /// let map_of_indices = HashMap::from_iter([
    ///     ((0, 1), 14),
    ///     ((3, 6), 42),
    /// ].into_iter());
    /// assert_eq!(Some(14), distance(&map_of_indices, 0, 1));
    /// assert_eq!(Some(42), distance(&map_of_indices, 3, 6));
    /// assert_eq!(None, distance(&map_of_indices, 0, 0));
    ///
    /// // closure to compute distances on the fly rather than to store them
    /// fn get_euclidean_distance(location1: (f64, f64), location2: (f64, f64)) -> u32 {
    ///     let (x1, y1) = location1;
    ///     let (x2, y2) = location2;
    ///     (f64::powf(x1 - x2, 2.0) + f64::powf(y1 - y2, 2.0)).sqrt() as u32
    /// }
    /// let locations = vec![(0.0, 1.0), (3.0, 5.0), (7.0, 2.0), (1.0, 1.0)];
    /// let closure = Capture(&locations).fun(|loc, (i, j): (usize, usize)| {
    ///     loc.get(i)
    ///         .and_then(|l1| loc.get(j).map(|l2| (l1, l2)))
    ///         .map(|(l1, l2)| get_euclidean_distance(*l1, *l2))
    /// });
    /// assert_eq!(Some(0), distance(&closure, 1, 1));
    /// assert_eq!(Some(5), distance(&closure, 0, 1));
    /// assert_eq!(None, distance(&closure, 0, 4));
    ///
    /// // uniform distance for all pairs
    /// let uniform = ScalarAsVec(42);
    /// assert_eq!(Some(42), distance(&uniform, 7, 42));
    ///
    /// // all disconnected pairs
    /// let disconnected = EmptyVec::new();
    /// assert_eq!(None, distance(&disconnected, 7, 42));
    /// ```
    fn at<Idx: IntoIndex<DIM>>(&self, index: Idx) -> Option<T>;

    /// Returns an iterator of elements of the vector for the given `indices`.
    ///
    /// `indices` can be any `Iterator` yielding `Idx` indices, where `Idx` can be any usize-primitive that can be converted into `[usize; DIM]`.
    /// For instance:
    /// * `usize` or `(usize)` can be converted into `[usize; 1]`,
    /// * `(usize, usize)` can be converted into `[usize; 2]`.
    ///
    /// This allows to iterate over all funvec implementations in a unified way. Thanks to monomorphization, this abstraction does not have a performance penalty.
    ///
    /// # Examples - Dimension 1
    ///
    /// ```rust
    /// use orx_funvec::*;
    /// use orx_closure::Capture;
    /// use std::collections::HashMap;
    ///
    /// fn sum_values<V: FunVec<1, i32>, I: Iterator<Item = usize>>(vec: &V, indices: I) -> i32 {
    ///     vec.iter_over(indices).flatten().sum()
    /// }
    ///
    /// let stdvec = vec![10, 11, 12, 13];
    /// assert_eq!(23, sum_values(&stdvec, 1..3));
    ///
    /// let map = HashMap::from_iter([(1, 10), (8, 20), (12, 200)].into_iter());
    /// assert_eq!(20, sum_values(&map, (1..10).filter(|x| x % 2 == 0)));
    ///
    /// let (s, t) = (0, 42);
    /// let closure = Capture((s, t))
    ///     .fun(|(s, t), i: usize| if i == *s { Some(10) } else if i == *t { Some(-1) } else { None });
    /// assert_eq!(9, sum_values(&closure, [0, 21, 42].iter().copied()));
    ///
    /// let scalar = ScalarAsVec(21);
    /// assert_eq!(42, sum_values(&scalar, 1..3));
    ///
    /// let empty_vec: EmptyVec<i32> = EmptyVec::new();
    /// assert_eq!(0, sum_values(&empty_vec, 1..3));
    /// ```
    ///
    /// # Examples - Dimension 2
    ///
    /// ```rust
    /// use orx_funvec::*;
    /// use orx_closure::Capture;
    /// use std::collections::{BTreeMap, HashMap};
    ///
    /// fn total_distance<V, I>(distances: &V, pairs: I) -> u32
    /// where
    ///     V: FunVec<2, u32>,
    ///     I: Iterator<Item = (usize, usize)>
    /// {
    ///     distances.iter_over(pairs).flatten().sum()
    /// }
    ///
    /// // complete matrix
    /// let jagged_vecs = vec![
    ///     vec![0, 1, 2, 3],
    ///     vec![10, 11, 12, 13],
    ///     vec![20, 21, 22, 23],
    ///     vec![30, 31, 32, 33],
    /// ];
    /// assert_eq!(0 + 1 + 10 + 11,
    ///     total_distance(&jagged_vecs, (0..2).flat_map(|i| (0..2).map(move |j| (i, j)))));
    /// assert_eq!(12 + 33, total_distance(&jagged_vecs, [(1, 2), (3, 3), (10, 10)].iter().copied()));
    ///
    /// // some sparsity in the first or second dimensions
    /// let vec_of_maps = vec![
    ///     BTreeMap::from_iter([(1, 1), (14, 2)].into_iter()),
    ///     BTreeMap::from_iter([(0, 10), (7, 20)].into_iter()),
    ///     BTreeMap::from_iter([(9, 100), (16, 200)].into_iter()),
    /// ];
    /// assert_eq!(1 + 10,
    ///     total_distance(&vec_of_maps, (0..2).flat_map(|i| (0..2).map(move |j| (i, j)))));
    /// assert_eq!(20 + 100,
    ///     total_distance(&vec_of_maps, [(1, 7), (2, 9)].iter().copied()));
    ///
    /// let map_of_vecs = HashMap::from_iter([
    ///     (1, vec![3, 4, 5]),
    ///     (7, vec![30, 40, 50]),
    /// ].into_iter());
    /// assert_eq!(3 + 4,
    ///     total_distance(&map_of_vecs, (0..2).flat_map(|i| (0..2).map(move |j| (i, j)))));
    /// assert_eq!(5 + 40,
    ///     total_distance(&map_of_vecs, [(1, 2), (7, 1)].iter().copied()));
    ///
    /// // complete sparsity
    /// let map_of_indices = HashMap::from_iter([
    ///     ((0, 1), 14),
    ///     ((3, 6), 42),
    /// ].into_iter());
    /// assert_eq!(14,
    ///     total_distance(&map_of_indices, (0..2).flat_map(|i| (0..2).map(move |j| (i, j)))));
    /// assert_eq!(14 + 42,
    ///     total_distance(&map_of_indices, [(0, 1), (3, 6), (100, 100)].iter().copied()));
    ///
    /// // closure to compute distances on the fly rather than to store them
    /// fn get_euclidean_distance(location1: (f64, f64), location2: (f64, f64)) -> u32 {
    ///     let (x1, y1) = location1;
    ///     let (x2, y2) = location2;
    ///     (f64::powf(x1 - x2, 2.0) + f64::powf(y1 - y2, 2.0)).sqrt() as u32
    /// }
    /// let locations = vec![(0.0, 1.0), (3.0, 5.0), (7.0, 2.0), (1.0, 1.0)];
    /// let closure = Capture(&locations).fun(|loc, (i, j): (usize, usize)| {
    ///     loc.get(i)
    ///         .and_then(|l1| loc.get(j).map(|l2| (l1, l2)))
    ///         .map(|(l1, l2)| get_euclidean_distance(*l1, *l2))
    /// });
    /// assert_eq!(2 * 5,
    ///     total_distance(&closure, (0..2).flat_map(|i| (0..2).map(move |j| (i, j)))));
    /// assert_eq!(5 + 1,
    ///     total_distance(&closure, [(0, 1), (3, 0), (100, 100)].iter().copied()));
    ///
    /// // uniform distance for all pairs
    /// let uniform = ScalarAsVec(42);
    /// assert_eq!(4 * 42,
    ///     total_distance(&uniform, (0..2).flat_map(|i| (0..2).map(move |j| (i, j)))));
    /// assert_eq!(42 * 3,
    ///     total_distance(&uniform, [(0, 1), (3, 0), (100, 100)].iter().copied()));
    ///
    /// // all disconnected pairs
    /// let disconnected = EmptyVec::new();
    /// assert_eq!(0,
    ///     total_distance(&disconnected, (0..2).flat_map(|i| (0..2).map(move |j| (i, j)))));
    /// assert_eq!(0,
    ///     total_distance(&disconnected, [(0, 1), (3, 0), (100, 100)].iter().copied()));
    /// ```
    fn iter_over<'a, Idx, IdxIter>(
        &self,
        indices: IdxIter,
    ) -> IterOverValues<DIM, T, Idx, IdxIter, Self>
    where
        Idx: IntoIndex<DIM>,
        IdxIter: Iterator<Item = Idx> + 'a,
    {
        IterOverValues::new(self, indices)
    }
}