1use super::{Isomorphic, Size, Index, impl_ops_for_view, View, ViewRef, ViewMut, impl_ops_for_memoryview};
2
3#[derive(Debug, Clone)]
5pub struct Array<I: Index, T> {
6 size: I::Size,
7 items: Box<[T]>,
8}
9
10impl<I: Index, T> Array<I, T> {
11 fn new_inner(size: I::Size, items: Box<[T]>) -> Self {
12 assert_eq!(I::length(size), items.len());
13 Self {size, items}
14 }
15
16 pub fn new(size: impl Isomorphic<I::Size>, items: impl Into<Box<[T]>>) -> Self {
29 Self::new_inner(size.to_iso(), items.into())
30 }
31
32 pub fn from_fn(
44 size: impl Isomorphic<I::Size>,
45 mut f: impl FnMut(I) -> T,
46 ) -> Self {
47 let size = size.to_iso();
48 let mut items = Vec::with_capacity(I::length(size));
49 size.each(|i| items.push(f(i)));
50 Self::new_inner(size, items.into())
51 }
52
53 pub fn to_raw(self) -> Box<[T]> { self.items }
55
56 pub fn iso<J: Index>(self) -> Array<J, T> where
58 J: Isomorphic<I>,
59 J::Size: Isomorphic<<I as Index>::Size>,
60 {
61 Array {size: J::Size::from_iso(self.size), items: self.items}
62 }
63}
64
65impl<I: Index, T> std::convert::AsRef<[T]> for Array<I, T> {
66 fn as_ref(&self) -> &[T] { &self.items }
67}
68
69impl<I: Index, T> std::convert::AsMut<[T]> for Array<I, T> {
70 fn as_mut(&mut self) -> &mut [T] { &mut self.items }
71}
72
73impl<I: Index, T: Clone> View for Array<I, T> {
74 type I = I;
75 type T = T;
76 #[inline(always)]
77 fn size(&self) -> I::Size { self.size }
78 #[inline(always)]
79 fn len(&self) -> usize { self.as_ref().len() }
80 #[inline(always)]
81 fn at(&self, index: I) -> T { self[index].clone() }
82}
83
84impl<I: Index, T: Clone> ViewRef for Array<I, T> {
85 #[inline(always)]
86 fn at_ref(&self, index: Self::I) -> &Self::T { &self.items[index.to_usize(self.size)] }
87}
88
89impl<I: Index, T: Clone> ViewMut for Array<I, T> {
90 #[inline(always)]
91 fn at_mut(&mut self, index: Self::I) -> &mut Self::T { &mut self.items[index.to_usize(self.size)] }
92}
93
94impl_ops_for_view!(Array<I: Index, T>);
95impl_ops_for_memoryview!(Array<I: Index, T>);
96
97impl<T> super::Push<T> for Vec<T> {
100 fn push(&mut self, t: T) { Vec::push(self, t); }
101}
102
103impl<I: Index, T: Clone> super::NewView for Array<I, T> {
104 type Buffer = Vec<T>;
105
106 fn new_view(
107 size: I::Size,
108 callback: impl FnOnce(&mut Self::Buffer),
109 ) -> Self {
110 let mut buffer = Vec::with_capacity(I::length(size));
111 callback(&mut buffer);
112 Self::new_inner(size, buffer.into())
113 }
114}