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
use core::{borrow::{Borrow, BorrowMut}, ops::{Deref, DerefMut}, marker::Destruct, mem::{MaybeUninit, ManuallyDrop}};
use array_trait::{ArrayNd, Array};
use super::*;
/// A trait for N-dimensional arrays
#[const_trait]
pub trait ArrayNdOps<const D: usize, T, const L: usize>: Array + ArrayNd<D, ItemNd = T, /*FLAT_LENGTH = {L}*/>
{
type Mapped<M>: /*~const*/ ArrayNdOps<D, M, L>;
fn as_ptr_nd(&self) -> *const T;
fn as_mut_ptr_nd(&mut self) -> *mut T;
/// Fills an N-dimensional array. Indices passed to fill-function are sorted from outermost to innermost.
///
/// # Example
///
/// ```rust
/// #![feature(generic_const_exprs)]
/// #![feature(const_trait_impl)]
/// #![feature(const_closures)]
/// #![feature(const_mut_refs)]
///
/// use array_trait::ArrayNdOps;
///
/// type T = u8;
///
/// let nd: [[T; 3]; 3] = ArrayNdOps::fill_nd(|[i, j]| 1 + 3*i as T + j as T);
///
/// assert_eq!(nd, [
/// [1, 2, 3],
/// [4, 5, 6],
/// [7, 8, 9]
/// ]);
/// ```
fn fill_nd<F>(fill: F) -> Self
where
F: /*~const*/ FnMut([usize; D]) -> T + /*~const*/ Destruct;
/// Maps each element in the N-dimensional array.
///
/// # Example
///
/// ```rust
/// #![feature(generic_const_exprs)]
/// #![feature(const_trait_impl)]
/// #![feature(const_closures)]
/// #![feature(const_mut_refs)]
///
/// use array_trait::ArrayNdOps;
///
/// const ND: [[u8; 3]; 3] = [
/// [1, 2, 3],
/// [4, 5, 6],
/// [7, 8, 9]
/// ];
///
/// let nd_mapped: [[i8; 3]; 3] = ND.map_nd(const |x: u8| -(x as i8));
///
/// assert_eq!(nd_mapped, [
/// [-1, -2, -3],
/// [-4, -5, -6],
/// [-7, -8, -9]
/// ]);
/// ```
fn map_nd<M>(self, map: M) -> Self::Mapped<<M as FnOnce<(T,)>>::Output>
where
M: /*~const*/ FnMut<(T,)> + /*~const*/ Destruct;
/// Enumerates each element of an N-dimensional array. Indices are sorted from outermost to innermost.
///
/// # Example
///
/// ```rust
/// #![feature(generic_const_exprs)]
/// #![feature(const_trait_impl)]
/// #![feature(const_closures)]
/// #![feature(const_mut_refs)]
/// #![feature(generic_arg_infer)]
///
/// use array_trait::ArrayNdOps;
///
/// type T = u8;
///
/// const ND: [[T; 3]; 3] = [
/// [1, 2, 3],
/// [4, 5, 6],
/// [7, 8, 9]
/// ];
///
/// // For now, the compiler cannot infer the type, so type annotations are needed.
/// let nd_enum: [[([usize; 2], T); 3]; 3] = <[[T; 3]; 3] as ArrayNdOps<2, _, _>>::enumerate_nd(ND);
///
/// assert_eq!(nd_enum, [
/// [([0, 0], 1), ([0, 1], 2), ([0, 2], 3)],
/// [([1, 0], 4), ([1, 1], 5), ([1, 2], 6)],
/// [([2, 0], 7), ([2, 1], 8), ([2, 2], 9)]
/// ]);
/// ```
fn enumerate_nd(self) -> Self::Mapped<([usize; D], T)>;
/// Flattens one or multiple dimensions of an N-dimensional array.
///
/// # Example
///
/// ```rust
/// #![feature(generic_const_exprs)]
/// #![feature(const_trait_impl)]
///
/// use array_trait::ArrayNdOps;
///
/// type T = u8;
///
/// const ND: [[T; 3]; 3] = [
/// [1, 2, 3],
/// [4, 5, 6],
/// [7, 8, 9]
/// ];
/// let flat: [T; 9] = ND.flatten_nd_array();
/// assert_eq!(flat, [1, 2, 3, 4, 5, 6, 7, 8, 9]);
/// ```
fn flatten_nd_array(self) -> [T; L]
where
[(); L]:;
/// Flattens one or multiple dimensions of an N-dimensional array-slice.
///
/// # Example
///
/// ```rust
/// #![feature(generic_const_exprs)]
/// #![feature(const_trait_impl)]
///
/// use array_trait::ArrayNdOps;
///
/// type T = u8;
///
/// const ND: [[T; 3]; 3] = [
/// [1, 2, 3],
/// [4, 5, 6],
/// [7, 8, 9]
/// ];
/// let flat: &[T; 9] = ND.flatten_nd_array_ref();
/// assert_eq!(flat, &[1, 2, 3, 4, 5, 6, 7, 8, 9]);
/// ```
fn flatten_nd_array_ref(&self) -> &[T; L]
where
[(); L]:;
/// Flattens one or multiple dimensions of an N-dimensional array-slice
///
/// # Example
///
/// ```rust
/// #![feature(generic_const_exprs)]
///
/// use array_trait::ArrayNdOps;
///
/// type T = u8;
///
/// let mut nd: [[T; 3]; 3] = [
/// [1, 2, 3],
/// [4, 5, 6],
/// [7, 8, 9]
/// ];
/// let flat: &mut [T; 9] = nd.flatten_nd_array_mut();
///
/// for x in flat.into_iter()
/// {
/// *x = 10 - *x;
/// }
///
/// assert_eq!(nd, [
/// [9, 8, 7],
/// [6, 5, 4],
/// [3, 2, 1]
/// ]);
/// ```
fn flatten_nd_array_mut(&mut self) -> &mut [T; L]
where
[(); L]:;
fn each_ref_nd(&self) -> Self::Mapped<&T>;
fn each_mut_nd(&mut self) -> Self::Mapped<&mut T>;
/// Reduces inner elements in N-dimensional array into one element, using a given operand
///
/// # Example
///
/// ```rust
/// #![feature(generic_const_exprs)]
///
/// use array_trait::ArrayNdOps;
///
/// const A: [[(u8, u8); 3]; 2] = [
/// [(0, 0), (0, 1), (0, 2)],
/// [(1, 0), (1, 1), (1, 2)]
/// ];
///
/// let r: (u8, u8) = A.reduce_nd(|(a1, a2), (b1, b2)| (a1 + b1, a2 + b2)).unwrap();
///
/// assert_eq!(r, (3, 6));
/// ```
fn reduce_nd<R>(self, reduce: R) -> Option<T>
where
R: /*~const*/ FnMut(T, T) -> T + /*~const*/ Destruct,
T: /*~const*/ Destruct;
/// Retrieves the inner item using an array of indices, sorted from outermost to innermost, as a reference
///
/// # Example
///
/// ```rust
/// #![feature(const_closures)]
/// #![feature(const_option)]
/// #![feature(const_trait_impl)]
/// #![feature(generic_const_exprs)]
/// #![feature(const_mut_refs)]
///
/// use array_trait::ArrayNdOps;
///
/// const A: [[u8; 2]; 3] = [
/// [1, 2],
/// [3, 4],
/// [5, 6]
/// ];
/// let b: [[u8; 2]; 3] = ArrayNdOps::fill_nd(|[i, j]| {
/// let item = *A.get_nd([i, j]).unwrap();
/// assert_eq!(item, A[i][j]);
/// item
/// });
///
/// assert_eq!(A, b);
/// ```
fn get_nd(&self, i: [usize; D]) -> Option<&T>;
/// Retrieves the inner item using an array of indices, sorted from outermost to innermost, as a mutable reference
///
/// # Example
///
/// ```rust
/// #![feature(const_closures)]
/// #![feature(const_option)]
/// #![feature(const_trait_impl)]
/// #![feature(generic_const_exprs)]
/// #![feature(const_mut_refs)]
///
/// use array_trait::ArrayNdOps;
///
/// const A: [[u8; 2]; 3] = [
/// [1, 2],
/// [3, 4],
/// [5, 6]
/// ];
/// let mut b: [[u8; 2]; 3] = [[0; 2]; 3];
///
/// let mut i = 0;
/// while i < 3
/// {
/// let mut j = 0;
/// while j < 2
/// {
/// let item = *A.get_nd([i, j]).unwrap();
/// assert_eq!(item, A[i][j]);
/// *b.get_nd_mut([i, j]).unwrap() = item;
/// j += 1;
/// }
/// i += 1;
/// }
///
/// assert_eq!(A, b);
/// ```
fn get_nd_mut(&mut self, i: [usize; D]) -> Option<&mut T>;
}
macro_rules! count {
() => {0};
($a:ident) => {1};
($a:ident $($b:ident)+) => {1 $(+ count!($b))+};
}
macro_rules! flat_len {
() => {0};
($a:ident $($b:ident)*) => {$a $(* $b)*}
}
macro_rules! nd {
($t:ty;) => {
$t
};
($t:ty; $a:ident) => {
[$t; $a]
};
($t:ty; $a:ident $($b:ident)+) => {
[nd!{$t; $($b)+}; $a]
};
}
macro_rules! fill_nd {
(($fill:ident, $dims:ident, $i:ident, $array:ident); $($c:ident)*) => {
core::mem::swap($array, &mut MaybeUninit::new($fill($i)));
};
(($fill:ident, $dims:ident, $i:ident, $array:ident) $a:ident $($b:ident)*; $($c:ident)*) => {
const J: usize = count!($($c)*);
$i[J] = 0;
while $i[J] < $dims[J]
{
let array = &mut $array[$i[J]];
fill_nd!(($fill, $dims, $i, array) $($b)*; $a $($c)*);
$i[J] += 1;
}
};
}
macro_rules! index_nd {
(($this:tt.$fn:ident($i:ident)) $a:ident; $($c:ident)*) => {
$this.$fn($i[count!{$($c)*}])
};
(($this:tt.$fn:ident($i:ident)) $a:ident $($b:ident)+; $($c:ident)*) => {
$this.$fn($i[count!{$($c)*}])
.and_then(|item| index_nd!{(item.$fn($i)) $($b)+; $a $($c)*})
};
}
macro_rules! impl_nd_array {
($a:ident $($($b:ident)+)?) => {
impl<T, const $a: usize $($(, const $b: usize)+)?> /*const*/ ArrayNdOps<{count!{$a $($($b)+)?}}, T, {flat_len!{$a $($($b)+)?}}> for nd!{T; $a $($($b)+)?}
{
type Mapped<M> = nd!{M; $a $($($b)+)?};
fn as_ptr_nd(&self) -> *const T
{
self.as_ptr().cast()
}
fn as_mut_ptr_nd(&mut self) -> *mut T
{
self.as_mut_ptr().cast()
}
fn fill_nd<F>(mut fill: F) -> Self
where
F: /*~const*/ FnMut([usize; count!{$a $($($b)+)?}]) -> T + /*~const*/ Destruct
{
let dims: [usize; {count!{$a $($($b)+)?}}] = Self::DIMENSIONS;
let mut i = [0; {count!{$a $($($b)+)?}}];
let mut array: nd!{MaybeUninit<T>; $a $($($b)+)?} =
unsafe {private::transmute_unchecked_size(MaybeUninit::<nd!{MaybeUninit<T>; $($($b)+)?}>::uninit_array::<$a>())};
while i[0] < dims[0]
{
let array = &mut array[i[0]];
fill_nd!((fill, dims, i, array) $($($b)+)?; $a);
i[0] += 1;
}
unsafe {private::transmute_unchecked_size(array)}
}
fn map_nd<M>(self, mut map: M) -> Self::Mapped<<M as FnOnce<(T,)>>::Output>
where
M: /*~const*/ FnMut<(T,)> + /*~const*/ Destruct
{
let mut iter = ManuallyDrop::new(self.flatten_nd_array().into_iter());
ArrayNdOps::fill_nd(|_| map(iter.deref_mut().next().unwrap()))
}
fn enumerate_nd(self) -> Self::Mapped<([usize; {count!{$a $($($b)+)?}}], T)>
{
let mut iter = ManuallyDrop::new(self.flatten_nd_array().into_iter());
ArrayNdOps::fill_nd(|i| (i, iter.deref_mut().next().unwrap()))
}
fn flatten_nd_array(self) -> [T; {flat_len!{$a $($($b)+)?}}]
where
[(); {flat_len!{$a $($($b)+)?}}]:
{
unsafe {private::transmute_unchecked_size(self)}
}
fn flatten_nd_array_ref(&self) -> &[T; {flat_len!{$a $($($b)+)?}}]
where
[(); {flat_len!{$a $($($b)+)?}}]:
{
unsafe {core::mem::transmute(self)}
}
fn flatten_nd_array_mut(&mut self) -> &mut [T; {flat_len!{$a $($($b)+)?}}]
where
[(); {flat_len!{$a $($($b)+)?}}]:
{
unsafe {core::mem::transmute(self)}
}
fn each_ref_nd(&self) -> Self::Mapped<&T>
{
let mut ptr = unsafe {core::mem::transmute::<_, *const T>(self)};
ArrayNdOps::fill_nd(|_| {
let y = unsafe {core::mem::transmute::<_, &T>(ptr)};
ptr = unsafe {ptr.add(1)};
y
})
}
fn each_mut_nd(&mut self) -> Self::Mapped<&mut T>
{
let mut ptr = unsafe {core::mem::transmute::<_, *mut T>(self)};
ArrayNdOps::fill_nd(|_| {
let y = unsafe {core::mem::transmute::<_, &mut T>(ptr)};
ptr = unsafe {ptr.add(1)};
y
})
}
fn reduce_nd<R>(self, mut reduce: R) -> Option<T>
where
R: /*~const*/ FnMut(T, T) -> T + /*~const*/ Destruct,
T: /*~const*/ Destruct
{
let this = ManuallyDrop::new(self);
if flat_len!{$a $($($b)+)?} == 0
{
return None
}
let mut i = 1;
unsafe {
let mut ptr: *const T = core::mem::transmute(this.deref());
let mut reduction = core::ptr::read(ptr);
while i < flat_len!{$a $($($b)+)?}
{
ptr = ptr.add(1);
reduction = reduce(reduction, core::ptr::read(ptr));
i += 1;
}
Some(reduction)
}
}
fn get_nd(&self, i: [usize; count!{$a $($($b)+)?}]) -> Option<&T>
{
index_nd!{(self.get(i)) $a $($($b)+)?;}
}
fn get_nd_mut(&mut self, i: [usize; count!{$a $($($b)+)?}]) -> Option<&mut T>
{
index_nd!{(self.get_mut(i)) $a $($($b)+)?;}
}
}
$(impl_nd_array!($($b)+);)?
};
}
mod r#impl
{
use super::*;
impl_nd_array!(
_0 _1 _2 _3 _4 _5 _6 _7 _8 _9 _10 _11 _12 _13 _14 _15 _16
);
}