1use std::any::Any;
4
5use crate::base::constraint::{SameNumberOfColumns, SameNumberOfRows, ShapeConstraint};
6use crate::base::dimension::{Dim, U1};
7use crate::base::{DefaultAllocator, Scalar};
8use crate::storage::{IsContiguous, RawStorageMut};
9use crate::StorageMut;
10use std::fmt::Debug;
11use std::mem::MaybeUninit;
12
13pub trait Allocator<R: Dim, C: Dim = U1>: Any + Sized {
23 type Buffer<T: Scalar>: StorageMut<T, R, C> + IsContiguous + Clone + Debug;
25 type BufferUninit<T: Scalar>: RawStorageMut<MaybeUninit<T>, R, C> + IsContiguous;
27
28 fn allocate_uninit<T: Scalar>(nrows: R, ncols: C) -> Self::BufferUninit<T>;
30
31 unsafe fn assume_init<T: Scalar>(uninit: Self::BufferUninit<T>) -> Self::Buffer<T>;
37
38 fn allocate_from_iterator<T: Scalar, I: IntoIterator<Item = T>>(
40 nrows: R,
41 ncols: C,
42 iter: I,
43 ) -> Self::Buffer<T>;
44
45 #[inline]
46 fn allocate_from_row_iterator<T: Scalar, I: IntoIterator<Item = T>>(
48 nrows: R,
49 ncols: C,
50 iter: I,
51 ) -> Self::Buffer<T> {
52 let mut res = Self::allocate_uninit(nrows, ncols);
53 let mut count = 0;
54
55 unsafe {
56 let res_ptr = res.as_mut_slice_unchecked();
58
59 for (k, e) in iter
60 .into_iter()
61 .take(ncols.value() * nrows.value())
62 .enumerate()
63 {
64 let i = k / ncols.value();
65 let j = k % ncols.value();
66 *res_ptr.get_unchecked_mut(i + j * nrows.value()) = MaybeUninit::new(e);
68 count += 1;
69 }
70
71 assert!(
72 count == nrows.value() * ncols.value(),
73 "Matrix init. from row iterator: iterator not long enough."
74 );
75
76 <Self as Allocator<R, C>>::assume_init(res)
77 }
78 }
79}
80
81pub trait Reallocator<T: Scalar, RFrom: Dim, CFrom: Dim, RTo: Dim, CTo: Dim>:
84 Allocator<RFrom, CFrom> + Allocator<RTo, CTo>
85{
86 unsafe fn reallocate_copy(
95 nrows: RTo,
96 ncols: CTo,
97 buf: <Self as Allocator<RFrom, CFrom>>::Buffer<T>,
98 ) -> <Self as Allocator<RTo, CTo>>::BufferUninit<T>;
99}
100
101pub type SameShapeR<R1, R2> = <ShapeConstraint as SameNumberOfRows<R1, R2>>::Representative;
103
104pub type SameShapeC<C1, C2> = <ShapeConstraint as SameNumberOfColumns<C1, C2>>::Representative;
106
107pub trait SameShapeAllocator<R1, C1, R2, C2>:
110 Allocator<R1, C1> + Allocator<SameShapeR<R1, R2>, SameShapeC<C1, C2>>
111where
112 R1: Dim,
113 R2: Dim,
114 C1: Dim,
115 C2: Dim,
116 ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2>,
117{
118}
119
120impl<R1, R2, C1, C2> SameShapeAllocator<R1, C1, R2, C2> for DefaultAllocator
121where
122 R1: Dim,
123 R2: Dim,
124 C1: Dim,
125 C2: Dim,
126 DefaultAllocator: Allocator<R1, C1> + Allocator<SameShapeR<R1, R2>, SameShapeC<C1, C2>>,
127 ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2>,
128{
129}
130
131pub trait SameShapeVectorAllocator<R1, R2>:
134 Allocator<R1> + Allocator<SameShapeR<R1, R2>> + SameShapeAllocator<R1, U1, R2, U1>
135where
136 R1: Dim,
137 R2: Dim,
138 ShapeConstraint: SameNumberOfRows<R1, R2>,
139{
140}
141
142impl<R1, R2> SameShapeVectorAllocator<R1, R2> for DefaultAllocator
143where
144 R1: Dim,
145 R2: Dim,
146 DefaultAllocator: Allocator<R1, U1> + Allocator<SameShapeR<R1, R2>>,
147 ShapeConstraint: SameNumberOfRows<R1, R2>,
148{
149}