mdarray 0.5.0

Multidimensional array for Rust
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
#[cfg(feature = "nightly")]
use std::alloc::{Allocator, Global};
use std::collections::TryReserveError;
use std::iter::FromIterator;
use std::mem::{self, MaybeUninit};
use std::ops::RangeBounds;
use std::result::Result;
use std::{ptr, slice};

#[cfg(not(feature = "nightly"))]
use crate::alloc::Allocator;
use crate::array::GridArray;
use crate::buffer::GridBuffer;
use crate::dim::{Const, Dim, Shape};
use crate::expr::{self, Drain, IntoExpr};
use crate::expression::Expression;
use crate::mapping::{DenseMapping, Mapping};
use crate::traits::{Apply, IntoCloned, IntoExpression};

#[cfg(not(feature = "nightly"))]
macro_rules! vec_t {
    ($type:ty, $alloc:ty) => {
        Vec<$type>
    };
}

#[cfg(feature = "nightly")]
macro_rules! vec_t {
    ($type:ty, $alloc:ty) => {
        Vec<$type, $alloc>
    };
}

impl<T, D: Dim, A: Allocator> GridArray<T, D, A> {
    /// Returns a reference to the underlying allocator.
    #[cfg(feature = "nightly")]
    pub fn allocator(&self) -> &A {
        self.buffer.allocator()
    }

    /// Moves all elements from another array into the array along the outermost dimension.
    ///
    /// # Panics
    ///
    /// Panics if the inner dimensions do not match.
    pub fn append(&mut self, other: &mut Self) {
        self.expand(other.drain(..));
    }

    /// Returns the number of elements the array can hold without reallocating.
    pub fn capacity(&self) -> usize {
        self.buffer.capacity()
    }

    /// Clears the array, removing all values.
    ///
    /// Note that this method has no effect on the allocated capacity of the array.
    pub fn clear(&mut self) {
        unsafe {
            self.buffer.with_mut_vec(|vec| vec.clear());
            self.buffer.set_mapping(DenseMapping::default());
        }
    }

    /// Removes the specified range from the array along the outermost dimension,
    /// and returns the removed range as an expression.
    pub fn drain<R: RangeBounds<usize>>(&mut self, range: R) -> Expression<Drain<T, D, A>> {
        #[cfg(not(feature = "nightly"))]
        let range = crate::index::range(range, ..self.size(D::RANK - 1));
        #[cfg(feature = "nightly")]
        let range = slice::range(range, ..self.size(D::RANK - 1));

        Expression::new(Drain::new(self, range.start, range.end))
    }

    /// Appends an expression to the array along the outermost dimension with broadcasting,
    /// cloning elements if needed.
    ///
    /// If the rank of the expression equals one less than the rank of the array,
    /// the expression is assumed to have outermost dimension of size 1.
    ///
    /// # Panics
    ///
    /// Panics if the inner dimensions do not match, or if the rank of the expression
    /// is not valid.
    pub fn expand<I: IntoExpression>(&mut self, expr: I)
    where
        I::Item: IntoCloned<T>,
    {
        assert!(I::Dim::RANK == D::RANK - 1 || I::Dim::RANK == D::RANK, "invalid rank");

        let expr = expr.into_expr();
        let len = expr.len();

        if len > 0 {
            let inner_shape = &expr.shape()[..D::RANK - 1];
            let mut shape = self.shape();

            if self.is_empty() {
                shape[..D::RANK - 1].copy_from_slice(inner_shape);
            } else {
                assert!(inner_shape == &shape[..D::RANK - 1], "inner dimensions mismatch");
            };

            shape[D::RANK - 1] += if I::Dim::RANK < D::RANK { 1 } else { expr.size(D::RANK - 1) };

            unsafe {
                self.buffer.with_mut_vec(|vec| {
                    vec.reserve(len);
                    expr.clone_into_vec(vec);
                });

                self.set_shape(shape);
            }
        }
    }

    /// Creates an array from the given element with the specified allocator.
    #[cfg(feature = "nightly")]
    pub fn from_elem_in(shape: D::Shape, elem: T, alloc: A) -> Self
    where
        T: Clone,
    {
        expr::from_elem(shape, elem).eval_in(alloc)
    }

    /// Creates an array from the given expression with the specified allocator.
    #[cfg(feature = "nightly")]
    pub fn from_expr_in<I: IntoExpression<Item = T, Dim = D>>(expr: I, alloc: A) -> Self {
        let expr = expr.into_expr();
        let shape = expr.shape();

        let mut vec = Vec::with_capacity_in(expr.len(), alloc);

        expr.clone_into_vec(&mut vec);

        unsafe { Self::from_parts(vec, DenseMapping::new(shape)) }
    }

    /// Creates an array with the results from the given function with the specified allocator.
    #[cfg(feature = "nightly")]
    pub fn from_fn_in<F: FnMut(D::Shape) -> T>(shape: D::Shape, f: F, alloc: A) -> Self {
        expr::from_fn(shape, f).eval_in(alloc)
    }

    /// Creates an array from raw components of another array with the specified allocator.
    ///
    /// # Safety
    ///
    /// The pointer must be a valid allocation given the shape, capacity and allocator.
    #[cfg(feature = "nightly")]
    pub unsafe fn from_raw_parts_in(
        ptr: *mut T,
        shape: D::Shape,
        capacity: usize,
        alloc: A,
    ) -> Self {
        let mapping = DenseMapping::new(shape);

        Self::from_parts(Vec::from_raw_parts_in(ptr, mapping.len(), capacity, alloc), mapping)
    }

    /// Converts the array into a one-dimensional array.
    pub fn into_flattened(self) -> GridArray<T, Const<1>, A> {
        self.into_vec().into()
    }

    /// Decomposes an array into its raw components including the allocator.
    #[cfg(feature = "nightly")]
    pub fn into_raw_parts_with_alloc(self) -> (*mut T, D::Shape, usize, A) {
        let (vec, mapping) = self.buffer.into_parts();
        let (ptr, _, capacity, alloc) = vec.into_raw_parts_with_alloc();

        (ptr, mapping.shape(), capacity, alloc)
    }

    /// Converts an array with a single element into the contained value.
    ///
    /// # Panics
    ///
    /// Panics if the array length is not equal to one.
    pub fn into_scalar(self) -> T {
        assert!(self.len() == 1, "invalid length");

        self.into_vec().pop().unwrap()
    }

    /// Converts the array into a reshaped array, which must have the same length.
    ///
    /// # Panics
    ///
    /// Panics if the array length is changed.
    pub fn into_shape<S: Shape>(self, shape: S) -> GridArray<T, S::Dim, A> {
        let (vec, mapping) = self.buffer.into_parts();

        unsafe { GridArray::from_parts(vec, Mapping::reshape(mapping, shape)) }
    }

    /// Converts the array into a vector.
    pub fn into_vec(self) -> vec_t!(T, A) {
        let (vec, _) = self.buffer.into_parts();

        vec
    }

    /// Creates a new, empty array with the specified allocator.
    #[cfg(feature = "nightly")]
    pub fn new_in(alloc: A) -> Self {
        unsafe { Self::from_parts(Vec::new_in(alloc), DenseMapping::default()) }
    }

    /// Reserves capacity for at least the additional number of elements in the array.
    pub fn reserve(&mut self, additional: usize) {
        unsafe {
            self.buffer.with_mut_vec(|vec| vec.reserve(additional));
        }
    }

    /// Reserves the minimum capacity for the additional number of elements in the array.
    pub fn reserve_exact(&mut self, additional: usize) {
        unsafe {
            self.buffer.with_mut_vec(|vec| vec.reserve_exact(additional));
        }
    }

    /// Resizes the array to the new shape, creating new elements with the given value.
    pub fn resize(&mut self, new_shape: D::Shape, value: T)
    where
        T: Clone,
        A: Clone,
    {
        self.buffer.resize_with(new_shape, || value.clone());
    }

    /// Resizes the array to the new shape, creating new elements from the given closure.
    pub fn resize_with<F: FnMut() -> T>(&mut self, new_shape: D::Shape, f: F)
    where
        A: Clone,
    {
        self.buffer.resize_with(new_shape, f);
    }

    /// Forces the array shape to the new shape.
    ///
    /// # Safety
    ///
    /// All elements within the array length must be initialized.
    pub unsafe fn set_shape(&mut self, new_shape: D::Shape) {
        self.buffer.set_mapping(DenseMapping::new(new_shape));
    }

    /// Shrinks the capacity of the array with a lower bound.
    pub fn shrink_to(&mut self, min_capacity: usize) {
        unsafe {
            self.buffer.with_mut_vec(|vec| vec.shrink_to(min_capacity));
        }
    }

    /// Shrinks the capacity of the array as much as possible.
    pub fn shrink_to_fit(&mut self) {
        unsafe {
            self.buffer.with_mut_vec(|vec| vec.shrink_to_fit());
        }
    }

    /// Returns the remaining spare capacity of the array as a slice of `MaybeUninit<T>`.
    ///
    /// The returned slice can be used to fill the array with data, before marking
    /// the data as initialized using the `set_shape` method.
    pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
        let ptr = self.as_mut_ptr();
        let len = self.capacity() - self.len();

        unsafe { slice::from_raw_parts_mut(ptr.add(self.len()) as *mut MaybeUninit<T>, len) }
    }

    /// Shortens the array along the outermost dimension, keeping the first `size` elements.
    pub fn truncate(&mut self, size: usize) {
        if size < self.size(D::RANK - 1) {
            let new_mapping = self.mapping().resize_dim(D::RANK - 1, size);

            unsafe {
                self.buffer.with_mut_vec(|vec| vec.truncate(new_mapping.len()));
                self.buffer.set_mapping(new_mapping);
            }
        }
    }

    /// Tries to reserve capacity for at least the additional number of elements in the array.
    ///
    /// # Errors
    ///
    /// If the capacity overflows, or the allocator reports a failure, then an error is returned.
    pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
        unsafe { self.buffer.with_mut_vec(|vec| vec.try_reserve(additional)) }
    }

    /// Tries to reserve the minimum capacity for the additional number of elements in the array.
    ///
    /// # Errors
    ///
    /// If the capacity overflows, or the allocator reports a failure, then an error is returned.
    pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
        unsafe { self.buffer.with_mut_vec(|vec| vec.try_reserve_exact(additional)) }
    }

    /// Creates a new, empty array with the specified capacity and allocator.
    #[cfg(feature = "nightly")]
    pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
        unsafe { Self::from_parts(Vec::with_capacity_in(capacity, alloc), DenseMapping::default()) }
    }

    pub(crate) unsafe fn from_parts(vec: vec_t!(T, A), mapping: DenseMapping<D>) -> Self {
        Self { buffer: GridBuffer::from_parts(vec, mapping) }
    }

    pub(crate) fn zip_with<I: IntoExpression, F>(mut self, expr: I, mut f: F) -> Self
    where
        F: FnMut(T, I::Item) -> T,
    {
        struct DropGuard<'a, T, D: Dim, A: Allocator> {
            grid: &'a mut GridArray<T, D, A>,
            index: usize,
        }

        impl<'a, T, D: Dim, A: Allocator> Drop for DropGuard<'a, T, D, A> {
            fn drop(&mut self) {
                let ptr = self.grid.as_mut_ptr();
                let tail = self.grid.len() - self.index;

                // Drop all elements except the current one, which is read but not written back.
                unsafe {
                    self.grid.buffer.set_mapping(DenseMapping::default());

                    if self.index > 1 {
                        ptr::drop_in_place(ptr::slice_from_raw_parts_mut(ptr, self.index - 1));
                    }

                    ptr::drop_in_place(ptr::slice_from_raw_parts_mut(ptr.add(self.index), tail));
                }
            }
        }

        let mut guard = DropGuard { grid: &mut self, index: 0 };
        let expr = guard.grid.expr_mut().zip(expr);

        expr.for_each(|(x, y)| unsafe {
            guard.index += 1;
            ptr::write(x, f(ptr::read(x), y));
        });

        mem::forget(guard);

        self
    }
}

#[cfg(not(feature = "nightly"))]
impl<T, D: Dim> GridArray<T, D> {
    /// Creates an array from the given element.
    pub fn from_elem(shape: D::Shape, elem: T) -> Self
    where
        T: Clone,
    {
        expr::from_elem(shape, elem).eval()
    }

    /// Creates an array from the given expression.
    pub fn from_expr<I: IntoExpression<Item = T, Dim = D>>(expr: I) -> Self {
        let expr = expr.into_expr();
        let shape = expr.shape();

        let mut vec = Vec::with_capacity(expr.len());

        expr.clone_into_vec(&mut vec);

        unsafe { Self::from_parts(vec, DenseMapping::new(shape)) }
    }

    /// Creates an array with the results from the given function.
    pub fn from_fn<F: FnMut(D::Shape) -> T>(shape: D::Shape, f: F) -> Self {
        expr::from_fn(shape, f).eval()
    }

    /// Creates an array from raw components of another array.
    ///
    /// # Safety
    ///
    /// The pointer must be a valid allocation given the shape and capacity.
    pub unsafe fn from_raw_parts(ptr: *mut T, shape: D::Shape, capacity: usize) -> Self {
        let mapping = DenseMapping::new(shape);
        let vec = Vec::from_raw_parts(ptr, mapping.len(), capacity);

        Self::from_parts(vec, mapping)
    }

    /// Decomposes an array into its raw components.
    pub fn into_raw_parts(self) -> (*mut T, D::Shape, usize) {
        let (vec, mapping) = self.buffer.into_parts();
        let mut vec = mem::ManuallyDrop::new(vec);

        (vec.as_mut_ptr(), mapping.shape(), vec.capacity())
    }

    /// Creates a new, empty array.
    pub fn new() -> Self {
        unsafe { Self::from_parts(Vec::new(), DenseMapping::default()) }
    }

    /// Creates a new, empty array with the specified capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        unsafe { Self::from_parts(Vec::with_capacity(capacity), DenseMapping::default()) }
    }
}

#[cfg(feature = "nightly")]
impl<T, D: Dim> GridArray<T, D> {
    /// Creates an array from the given element.
    pub fn from_elem(shape: D::Shape, elem: T) -> Self
    where
        T: Clone,
    {
        Self::from_elem_in(shape, elem, Global)
    }

    /// Creates an array from the given expression.
    pub fn from_expr<I: IntoExpression<Item = T, Dim = D>>(expr: I) -> Self {
        Self::from_expr_in(expr, Global)
    }

    /// Creates an array with the results from the given function.
    pub fn from_fn<F: FnMut(D::Shape) -> T>(shape: D::Shape, f: F) -> Self {
        Self::from_fn_in(shape, f, Global)
    }

    /// Creates an array from raw components of another array.
    ///
    /// # Safety
    ///
    /// The pointer must be a valid allocation given the shape and capacity.
    pub unsafe fn from_raw_parts(ptr: *mut T, shape: D::Shape, capacity: usize) -> Self {
        Self::from_raw_parts_in(ptr, shape, capacity, Global)
    }

    /// Decomposes an array into its raw components.
    pub fn into_raw_parts(self) -> (*mut T, D::Shape, usize) {
        let (ptr, shape, capacity, _) = self.into_raw_parts_with_alloc();

        (ptr, shape, capacity)
    }

    /// Creates a new, empty array.
    pub fn new() -> Self {
        Self::new_in(Global)
    }

    /// Creates a new, empty array with the specified capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        Self::with_capacity_in(capacity, Global)
    }
}

impl<T, D: Dim, A: Allocator> Apply<T> for GridArray<T, D, A> {
    type Output = GridArray<T, D, A>;
    type ZippedWith<I: IntoExpression> = GridArray<T, D, A>;

    fn apply<F: FnMut(T) -> T>(self, mut f: F) -> GridArray<T, D, A> {
        self.zip_with(expr::fill(()), |x, ()| f(x))
    }

    fn zip_with<I: IntoExpression, F>(self, expr: I, f: F) -> GridArray<T, D, A>
    where
        F: FnMut(T, I::Item) -> T,
    {
        self.zip_with(expr, f)
    }
}

impl<T, D: Dim> Default for GridArray<T, D> {
    fn default() -> Self {
        Self::new()
    }
}

impl<'a, T: 'a + Copy, A: Allocator> Extend<&'a T> for GridArray<T, Const<1>, A> {
    fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
        self.extend(iter.into_iter().copied());
    }
}

impl<T, A: Allocator> Extend<T> for GridArray<T, Const<1>, A> {
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
        unsafe {
            let len = self.buffer.with_mut_vec(|vec| {
                vec.extend(iter);
                vec.len()
            });

            self.set_shape([len]);
        }
    }
}

impl<T: Clone> From<&[T]> for GridArray<T, Const<1>> {
    fn from(slice: &[T]) -> Self {
        Self::from(slice.to_vec())
    }
}

macro_rules! impl_from_array {
    ($n:tt, ($($size:tt),+), $array:tt) => {
        impl<T, $(const $size: usize),+> From<$array> for GridArray<T, Const<$n,>> {
            #[cfg(not(feature = "nightly"))]
            fn from(array: $array) -> Self {
                if [$($size),+].contains(&0) {
                    Self::new()
                } else {
                    let mut vec = std::mem::ManuallyDrop::new(Vec::from(array));
                    let (ptr, capacity) = (vec.as_mut_ptr(), vec.capacity());

                    unsafe {
                        let capacity = capacity * (mem::size_of_val(&*ptr) / mem::size_of::<T>());

                        Self::from_raw_parts(ptr.cast(), [$($size),+], capacity)
                    }
                }
            }

            #[cfg(feature = "nightly")]
            fn from(array: $array) -> Self {
                if [$($size),+].contains(&0) {
                    Self::new()
                } else {
                    let (ptr, _, capacity, alloc) = Vec::from(array).into_raw_parts_with_alloc();

                    unsafe {
                        let capacity = capacity * (mem::size_of_val(&*ptr) / mem::size_of::<T>());

                        Self::from_raw_parts_in(ptr.cast(), [$($size),+], capacity, alloc)
                    }
                }
            }
        }
    };
}

impl_from_array!(1, (X), [T; X]);
impl_from_array!(2, (X, Y), [[T; X]; Y]);
impl_from_array!(3, (X, Y, Z), [[[T; X]; Y]; Z]);
impl_from_array!(4, (X, Y, Z, W), [[[[T; X]; Y]; Z]; W]);
impl_from_array!(5, (X, Y, Z, W, U), [[[[[T; X]; Y]; Z]; W]; U]);
impl_from_array!(6, (X, Y, Z, W, U, V), [[[[[[T; X]; Y]; Z]; W]; U]; V]);

impl<T, D: Dim, A: Allocator> From<GridArray<T, D, A>> for vec_t!(T, A) {
    fn from(grid: GridArray<T, D, A>) -> Self {
        grid.into_vec()
    }
}

impl<T, A: Allocator> From<vec_t!(T, A)> for GridArray<T, Const<1>, A> {
    fn from(vec: vec_t!(T, A)) -> Self {
        let mapping = DenseMapping::new([vec.len()]);

        unsafe { Self::from_parts(vec, mapping) }
    }
}

impl<T> FromIterator<T> for GridArray<T, Const<1>> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        Self::from(Vec::from_iter(iter))
    }
}

impl<T, D: Dim, A: Allocator> IntoExpression for GridArray<T, D, A> {
    type Item = T;
    type Dim = D;
    type Producer = IntoExpr<T, D, A>;

    fn into_expr(self) -> Expression<Self::Producer> {
        Expression::new(IntoExpr::new(self))
    }
}

impl<T, D: Dim, A: Allocator> IntoIterator for GridArray<T, D, A> {
    type Item = T;
    type IntoIter = <vec_t!(T, A) as IntoIterator>::IntoIter;

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