lk_math 0.5.0

Collection of reusable mathematical tools.
Documentation
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
use std::{
    fmt::Display,
    io::{BufRead, BufReader},
    iter,
    ops::{Index, IndexMut},
    str::FromStr,
};

use super::{
    geometric_traits::{IterateNeighbours, IterateNeighboursContext},
    line::Line,
    line_iterator::LineIterator,
    linear_index::LinearIndex,
    vector::Vector,
};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ArrayNd<const N: usize, T> {
    pub data: Vec<T>,
    #[cfg_attr(feature = "serde", serde(with = "serde_arrays"))]
    pub dims: [usize; N],
    // #[serde(with = "serde_arrays")]
    #[cfg_attr(feature = "serde", serde(with = "serde_arrays"))]
    pub dim_strides: [usize; N],
}

impl<const C: usize, T: Copy> IntoIterator for ArrayNd<C, T> {
    type Item = T;
    type IntoIter = std::vec::IntoIter<T>;

    fn into_iter(self) -> Self::IntoIter {
        self.data.into_iter()
    }
}

impl<'a, const C: usize, T: Copy> IntoIterator for &'a ArrayNd<C, T> {
    type Item = &'a T;
    type IntoIter = std::slice::Iter<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.data.iter()
    }
}

impl<'a, const C: usize, T: Copy> ArrayNd<C, T> {
    pub fn iter(&'a self) -> std::slice::Iter<'a, T> {
        self.into_iter()
    }
}

impl<'a, const C: usize, T: Copy> IntoIterator for &'a mut ArrayNd<C, T> {
    type Item = &'a mut T;
    type IntoIter = std::slice::IterMut<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.data.iter_mut()
    }
}

impl<'a, const C: usize, T: Copy> ArrayNd<C, T> {
    pub fn iter_mut(&'a mut self) -> std::slice::IterMut<'a, T> {
        self.into_iter()
    }
}

impl<const C: usize, T: Copy> Index<usize> for ArrayNd<C, T> {
    type Output = T;

    fn index(&self, index: usize) -> &Self::Output {
        &self.data[index]
    }
}

impl<const C: usize, T: Copy> IndexMut<usize> for ArrayNd<C, T> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.data[index]
    }
}

impl<const C: usize, T: Copy> Index<Vector<C, i32>> for ArrayNd<C, T> {
    type Output = T;

    fn index(&self, index: Vector<C, i32>) -> &Self::Output {
        &self.data[self.index_unchecked(index).unwrap()]
    }
}

impl<const C: usize, T: Copy> ArrayNd<C, T> {
    pub fn new<U: Copy + TryInto<usize>>(dims: [U; C], default: T) -> Self {
        let mut d = [0; C];
        let mut current_stride = 1;
        let mut dim_strides = [0; C];
        for i in 0..C {
            d[i] = dims[i].try_into().ok().unwrap();
            dim_strides[i] = current_stride;
            current_stride *= d[i];
            assert_ne!(d[i], 0);
        }

        Self {
            data: iter::repeat(default).take(d.iter().product()).collect(),
            dims: d,
            dim_strides,
        }
    }

    pub fn from_slice<U: Copy + TryInto<usize>>(dims: [U; C], slice: &[T]) -> Self {
        let mut d = [0; C];
        let mut current_stride = 1;
        let mut dim_strides = [0; C];
        for i in 0..C {
            d[i] = dims[i].try_into().ok().unwrap();
            dim_strides[i] = current_stride;
            current_stride *= d[i];
            assert_ne!(d[i], 0);
        }
        Self {
            data: slice.to_owned(),
            dims: d,
            dim_strides,
        }
    }

    pub fn resized(&self, new_dims: [usize; C], default: T, offset: Vector<C, i32>) -> Self {
        let mut new = Self::new(new_dims, default);
        new.data
            .iter_mut()
            .enumerate()
            .for_each(|(linear_index, val)| {
                let i: Vector<C, i32> = Vector::new(new_dims)
                    .unindex(linear_index)
                    .unwrap()
                    .try_into()
                    .unwrap();
                if let Some(src) = self.get(i - offset) {
                    *val = *src;
                }
            });

        new
    }
    pub fn padded(&self, padding: i32, default: T) -> Self {
        let mut new_dims = self.dims;
        new_dims.iter_mut().for_each(|x| *x += 2 * padding as usize);

        self.resized(new_dims, default, Vector::all(padding))
    }
}

impl<const N: usize, T> LinearIndex<usize> for ArrayNd<N, T> {
    fn index_unchecked(&self, i: usize) -> Option<usize> {
        if i < self.data.len() {
            Some(i)
        } else {
            None
        }
    }
    fn unindex(&self, i: usize) -> Option<usize> {
        if i < self.data.len() {
            Some(i)
        } else {
            None
        }
    }
    unsafe fn cardinality(&self) -> Option<usize> {
        Some(self.data.len())
    }
    fn is_in_bounds(&self, i: &usize) -> bool {
        *i < self.data.len()
    }
}

macro_rules! array_vector_linear_index {
    ($($t:ty),*) => {
        $(
impl<const N: usize, T> LinearIndex<Vector<N, $t>> for ArrayNd<N, T> {
    fn index_unchecked(&self, i: Vector<N, $t>) -> Option<usize> {
        Vector::new(self.dims).index_unchecked(i.try_into().unwrap())
    }
    fn unindex(&self, i: usize) -> Option<Vector<N, $t>> {
        if let Some(a) = Vector::new(self.dims).unindex(i) {
            match a.try_into() {
                Ok(a) => Some(a),
                Err(_) => None,
            }
        } else {
            None
        }
    }
    unsafe fn cardinality(&self) -> Option<usize> {
        Some(self.dims.iter().product())
    }
    fn is_in_bounds(&self, i: &Vector<N, $t>) -> bool {
        if let Ok(a) = (*i).try_into() {
            Vector::new(self.dims).is_in_bounds(&a)
        } else {
            false
        }
    }
}
        )*
    };
}

array_vector_linear_index!(i32, usize);

impl<const N: usize, T: Copy + PartialEq> ArrayNd<N, T> {
    pub fn replace_all(&mut self, from: &T, to: &T) {
        self.data.iter_mut().for_each(|x| {
            if x == from {
                *x = *to;
            }
        })
    }
}

impl<const N: usize, T> ArrayNd<N, T> {
    pub fn get_linear(&self, index: usize) -> &T {
        &self.data[index]
    }
    pub fn get_mut_linear(&mut self, index: usize) -> &mut T {
        &mut self.data[index]
    }
    pub fn set_linear(&mut self, index: usize, v: T) {
        *self.get_mut_linear(index) = v;
    }

    pub fn get<I>(&self, p: I) -> Option<&T>
    where
        Self: LinearIndex<I>,
    {
        match self.index(p) {
            Some(index) => Some(self.get_linear(index)),
            None => None,
        }
    }
    pub fn get_mut<I>(&mut self, p: I) -> Option<&mut T>
    where
        Self: LinearIndex<I>,
    {
        match self.index(p) {
            Some(index) => Some(self.get_mut_linear(index)),
            None => None,
        }
    }
    pub fn set<I>(&mut self, p: I, v: T) -> bool
    where
        Self: LinearIndex<I>,
    {
        match self.get_mut(p) {
            Some(a) => {
                *a = v;
                true
            }
            None => false,
        }
    }
}

impl<const N: usize, T> ArrayNd<N, T> {
    fn find_internal<F, I>(&self, predicate: F) -> Option<I>
    where
        F: Fn(&T) -> bool,
        Self: LinearIndex<I>,
    {
        match self.data.iter().position(predicate) {
            Some(index) => self.unindex(index),
            None => None,
        }
    }

    fn rfind_internal<F, I>(&self, predicate: F) -> Option<I>
    where
        F: Fn(&T) -> bool,
        Self: LinearIndex<I>,
    {
        match self.data.iter().rposition(predicate) {
            Some(index) => self.unindex(index),
            None => None,
        }
    }

    fn find_item_internal<I>(&self, item: &T) -> Option<I>
    where
        T: PartialEq,
        Self: LinearIndex<I>,
    {
        self.find_internal(|x| x == item)
    }

    fn rfind_item_internal<I>(&self, item: &T) -> Option<I>
    where
        T: PartialEq,
        Self: LinearIndex<I>,
    {
        self.rfind_internal(|x| x == item)
    }

    pub fn find_item(&self, item: &T) -> Option<Vector<N, i32>>
    where
        T: PartialEq,
    {
        self.find_item_internal(item)
    }

    pub fn find_last_item(&self, item: &T) -> Option<Vector<N, i32>>
    where
        T: PartialEq,
    {
        self.rfind_item_internal(item)
    }

    pub fn find_all<F, I>(&self, predicate: F) -> Vec<I>
    where
        F: Fn(&T) -> bool,
        T: PartialEq,
        Self: LinearIndex<I>,
    {
        self.data
            .iter()
            .enumerate()
            .filter(|(_, x)| predicate(x))
            .map(|(i, _)| self.unindex(i).unwrap())
            .collect()
    }

    pub fn find_all_items<I>(&self, item: &T) -> Vec<I>
    where
        T: PartialEq,
        Self: LinearIndex<I>,
    {
        self.find_all(|x| x == item)
    }
}

impl<const N: usize, T> ArrayNd<N, T> {
    pub fn map<F, U>(&self, f: F) -> ArrayNd<N, U>
    where
        F: Fn(&T) -> U,
    {
        let data = self.data.iter().map(f).collect();

        ArrayNd::<N, U> {
            data,
            dims: self.dims,
            dim_strides: self.dim_strides,
        }
    }
}

impl<const N: usize, T> ArrayNd<N, T> {
    pub fn line_iter<const B: bool>(
        &self,
        p0: Vector<N, i32>,
        p1: Vector<N, i32>,
    ) -> LineIterator<B, N> {
        LineIterator::new(p0, p1)
    }
}

impl<const N: usize, T> ArrayNd<N, T> {
    pub fn iter_values_in_line<const B: bool>(
        &'_ self,
        p0: Vector<N, i32>,
        p1: Vector<N, i32>,
    ) -> impl Iterator<Item = &'_ T> {
        // self.line_iter(p0, p1).filter_map(|p| self.get(p))
        self.line_iter::<B>(p0, p1).map(|p| self.get(p).unwrap())
    }
}

impl<const N: usize, T: Copy> ArrayNd<N, T> {
    pub fn draw_line<const B: bool>(&mut self, line: Line<Vector<N, i32>>, v: T) {
        for p in self.line_iter::<B>(line.start, line.end) {
            self.set(p, v);
        }
    }
    // pub fn draw_line_from_points<const B: bool>(
    //     &mut self,
    //     p0: Vector<N, i32>,
    //     p1: Vector<N, i32>,
    //     v: T,
    // ) {
    //     for p in self.line_iter::<B>(p0, p1) {
    //         self.set(p, v);
    //     }
    // }
}

// NOTE(lubo): Choose which slice (index) to paint in each dimension, or pass None to paint all tiles in that dimension.
// Example:
//   Draw a plane at Y = 3 in a 3D array
//   a (: Array3D) .draw_block(&[None, Some(3), None])
impl<const N: usize, T: Copy> ArrayNd<N, T> {
    // TODO(lubo): Block iterator!!
    pub fn iter_block(&mut self, mut matching: [Option<usize>; N]) -> impl Iterator<Item = &T> {
        todo!();
        [].into_iter()
        // let mut index = 0;
        // for i in (0..N).rev() {
        //     match matching[i] {
        //         Some(value) => index += value * self.dim_strides[i],
        //         None => {
        //             for a in 0..self.dims[i] {
        //                 matching[i] = Some(a);
        //                 // self.draw_block(matching, v);
        //             }
        //             // return;
        //         }
        //     }
        // }
        // // self.set_linear(index, v)
    }

    pub fn draw_block(&mut self, mut matching: [Option<usize>; N], v: T) {
        let mut index = 0;
        for i in (0..N).rev() {
            match matching[i] {
                Some(value) => index += value * self.dim_strides[i],
                None => {
                    for a in 0..self.dims[i] {
                        matching[i] = Some(a);
                        self.draw_block(matching, v);
                    }
                    return;
                }
            }
        }
        self.set_linear(index, v)
    }
}

impl<const C: usize, T> IterateNeighboursContext for ArrayNd<C, T> {}

impl<const C: usize, T: IterateNeighbours<()> + Copy, U> IterateNeighbours<ArrayNd<C, U>> for T
where
    ArrayNd<C, U>: LinearIndex<T>,
{
    fn neighbours(&self, _context: &ArrayNd<C, U>) -> Vec<Self> {
        self.neighbours(&())
            .into_iter()
            .filter(|x| _context.is_in_bounds(x))
            .collect()
    }
}

// TODO(lubo): Slices?
// impl<const C: usize, T: Copy> Display for ArrayNd<C, T> {
//     pub fn get_slice(&self, ) {

//     }
// }

impl<const C: usize, T: Display> Display for ArrayNd<C, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let total_size = self.data.len(); // 0..self.dims.iter().product()
        if C > 1 {
            let mut index = 0;
            while index < total_size {
                if C > 2 {
                    let slice: Vector<C, usize> = self.unindex(index).unwrap();
                    writeln!(f, "Slice = {}", slice)?;
                }

                for _y in 0..self.dims[1] {
                    for _x in 0..self.dims[0] {
                        write!(f, "{}", self.get_linear(index))?;
                        index += 1;
                    }
                    writeln!(f)?;
                }
            }
        } else {
            for x in 0..total_size {
                write!(f, "{}", self.get_linear(x))?;
            }
        }

        write!(f, "")
    }
}

// NOTE(lubo): Specific lower dimensional arrays

pub type Array2d<T> = ArrayNd<2, T>;
pub type Array3d<T> = ArrayNd<3, T>;
impl<T> Array2d<T> {
    pub fn width(&self) -> usize {
        self.dims[0]
    }
    pub fn height(&self) -> usize {
        self.dims[1]
    }
}
impl<T> Array3d<T> {
    pub fn width(&self) -> usize {
        self.dims[0]
    }
    pub fn height(&self) -> usize {
        self.dims[1]
    }
    pub fn depth(&self) -> usize {
        self.dims[2]
    }
}

pub type CharArray2d = Array2d<char>;

impl FromStr for CharArray2d {
    type Err = CharArrayParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let cursor = std::io::Cursor::new(s);
        Self::from_read(cursor)
    }
}

#[derive(Debug)]
pub enum CharArrayParseError {
    InconsistentLineWidth(usize, usize, usize, usize),
    Io(std::io::Error),
}

impl Display for CharArrayParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CharArrayParseError::InconsistentLineWidth(l1, w1, l2, w2) => write!(f, "Inconsistent line width. On line {l1} the width is {w1}, while on line {l2} the width is {w2}."),
            CharArrayParseError::Io(io) => write!(f, "IO error: {io}"),
        }
    }
}

impl CharArray2d {
    pub fn from_buffer<R: std::io::Read>(
        reader: BufReader<R>,
    ) -> Result<Self, CharArrayParseError> {
        let mut array2d_width_line_number = 0;
        let mut array2d_width = 0;

        let mut data = vec![];
        let mut height = 0;

        for (line_number, line) in reader.lines().enumerate() {
            let line = match line {
                Ok(l) => l,
                Err(e) => return Err(CharArrayParseError::Io(e)),
            };
            let line_width = line.len();
            if array2d_width == 0 {
                array2d_width = line_width;
                array2d_width_line_number = line_number;
            } else if array2d_width != line_width && line_width != 0 {
                return Err(CharArrayParseError::InconsistentLineWidth(
                    array2d_width_line_number,
                    array2d_width,
                    line_number,
                    line_width,
                ));
            }
            if line_width > 0 {
                height += 1;
                data.extend(line.chars());
            }
        }

        Ok(Self {
            data,
            dims: [array2d_width, height],
            dim_strides: [1, array2d_width],
        })
    }

    pub fn from_read<R: std::io::Read>(reader: R) -> Result<Self, CharArrayParseError> {
        Self::from_buffer(BufReader::new(reader))
    }
}

impl<T: Copy> Array2d<T> {
    pub fn with_dimensions(width: usize, height: usize, default: T) -> Self {
        Self {
            data: iter::repeat(default).take(width * height).collect(),
            dims: [width, height],
            dim_strides: [1, width],
        }
    }

    pub fn shift_n_rows_down(&mut self, n: usize, default: T) {
        self.data.drain(..self.width() * n);
        self.data.extend(
            iter::repeat(default)
                .take(self.width() * n)
                .collect::<Vec<T>>(),
        );
    }
}

impl<T: Copy> Array3d<T> {
    pub fn with_dimensions(width: usize, height: usize, depth: usize, default: T) -> Self {
        Self {
            data: iter::repeat(default).take(width * height * depth).collect(),
            dims: [width, height, depth],
            dim_strides: [1, width, width * height],
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::vector::V2i32;

    use super::*;

    const EXAMPLE: &str = r#"
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
"#;

    #[test]
    fn parse_chararray2d() {
        let map: CharArray2d = EXAMPLE.parse().unwrap();
        assert_eq!(10, map.width());
        assert_eq!(10, map.height());
        assert_eq!(Some(&'4'), map.get(V2i32::from_xy(0, 0)));
    }

    #[test]
    fn fail_parse_chararray2d_inconsistent_line_width() {
        let bad = r#"
line 1
line 2
line 3+
"#;
        if let Err(CharArrayParseError::InconsistentLineWidth(l1, w1, l2, w2)) =
            bad.parse::<CharArray2d>()
        {
            assert!((1..=2).contains(&l1));
            assert_eq!(w1, 6);
            assert_eq!(l2, 3);
            assert_eq!(w2, 7);
        } else {
            panic!();
        }
    }
}