use crate::traits::data_container::{
ContainerType, DataContainer, ModifiableDataContainer, MutableRawAccessDataContainer,
RawAccessDataContainer, RefDataContainer, RefDataContainerMut, ResizeableDataContainer,
ValueDataContainer,
};
use crate::base_types::{Stack, Unknown};
use aligned_vec::{AVec, CACHELINE_ALIGN, ConstAlign, avec};
pub struct VectorContainer<Item> {
data: Vec<Item>,
}
pub struct AlignedVectorContainer<Item, const ALIGNMENT: usize = CACHELINE_ALIGN> {
data: AVec<Item, ConstAlign<ALIGNMENT>>,
}
impl<Item: Default + Copy> VectorContainer<Item> {
#[inline(always)]
pub fn new(nelems: usize) -> VectorContainer<Item> {
VectorContainer::<Item> {
data: vec![<Item as Default>::default(); nelems],
}
}
#[inline(always)]
pub fn from_vec(data: Vec<Item>) -> VectorContainer<Item> {
VectorContainer::<Item> { data }
}
}
impl<Item: Default + Copy, const ALIGNMENT: usize> AlignedVectorContainer<Item, ALIGNMENT> {
#[inline(always)]
pub fn new(nelems: usize) -> AlignedVectorContainer<Item, ALIGNMENT> {
AlignedVectorContainer::<Item, ALIGNMENT> {
data: avec![[ALIGNMENT]| <Item as Default>::default(); nelems],
}
}
#[inline(always)]
pub fn from_avec(
data: AVec<Item, ConstAlign<ALIGNMENT>>,
) -> AlignedVectorContainer<Item, ALIGNMENT> {
AlignedVectorContainer::<Item, ALIGNMENT> { data }
}
}
impl<Item> ContainerType for VectorContainer<Item> {
type Type = Unknown;
}
impl<Item, const ALIGNMENT: usize> ContainerType for AlignedVectorContainer<Item, ALIGNMENT> {
type Type = Unknown;
}
impl<Item: Copy + Default> DataContainer for VectorContainer<Item> {
type Item = Item;
#[inline(always)]
fn number_of_elements(&self) -> usize {
self.data.len()
}
}
impl<Item: Copy + Default, const ALIGNMENT: usize> DataContainer
for AlignedVectorContainer<Item, ALIGNMENT>
{
type Item = Item;
#[inline(always)]
fn number_of_elements(&self) -> usize {
self.data.len()
}
}
impl<Item: Copy + Default> ValueDataContainer for VectorContainer<Item> {
#[inline(always)]
unsafe fn get_unchecked_value(&self, index: usize) -> Self::Item {
debug_assert!(index < self.number_of_elements());
unsafe { *self.data.get_unchecked(index) }
}
}
impl<Item: Copy + Default, const ALIGNMENT: usize> ValueDataContainer
for AlignedVectorContainer<Item, ALIGNMENT>
{
#[inline(always)]
unsafe fn get_unchecked_value(&self, index: usize) -> Self::Item {
debug_assert!(index < self.number_of_elements());
unsafe { *self.data.get_unchecked(index) }
}
}
impl<Item: Copy + Default> RefDataContainer for VectorContainer<Item> {
#[inline(always)]
unsafe fn get_unchecked(&self, index: usize) -> &Self::Item {
debug_assert!(index < self.number_of_elements());
unsafe { self.data.get_unchecked(index) }
}
}
impl<Item: Copy + Default, const ALIGNMENT: usize> RefDataContainer
for AlignedVectorContainer<Item, ALIGNMENT>
{
#[inline(always)]
unsafe fn get_unchecked(&self, index: usize) -> &Self::Item {
debug_assert!(index < self.number_of_elements());
unsafe { self.data.get_unchecked(index) }
}
}
unsafe impl<Item: Copy + Default> crate::traits::data_container::DisjointRefDataContainerMut
for VectorContainer<Item>
{
}
impl<Item: Copy + Default> RefDataContainerMut for VectorContainer<Item> {
#[inline(always)]
unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut Self::Item {
debug_assert!(index < self.number_of_elements());
unsafe { self.data.get_unchecked_mut(index) }
}
}
unsafe impl<Item: Copy + Default, const ALIGNMENT: usize>
crate::traits::data_container::DisjointRefDataContainerMut
for AlignedVectorContainer<Item, ALIGNMENT>
{
}
impl<Item: Copy + Default, const ALIGNMENT: usize> RefDataContainerMut
for AlignedVectorContainer<Item, ALIGNMENT>
{
#[inline(always)]
unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut Self::Item {
debug_assert!(index < self.number_of_elements());
unsafe { self.data.get_unchecked_mut(index) }
}
}
impl<Item: Copy + Default> ModifiableDataContainer for VectorContainer<Item> {
#[inline(always)]
unsafe fn set_unchecked_value(&mut self, index: usize, value: Self::Item) {
debug_assert!(index < self.number_of_elements());
unsafe {
*self.data.get_unchecked_mut(index) = value;
}
}
}
impl<Item: Copy + Default, const ALIGNMENT: usize> ModifiableDataContainer
for AlignedVectorContainer<Item, ALIGNMENT>
{
#[inline(always)]
unsafe fn set_unchecked_value(&mut self, index: usize, value: Self::Item) {
debug_assert!(index < self.number_of_elements());
unsafe {
*self.data.get_unchecked_mut(index) = value;
}
}
}
impl<Item: Copy + Default> ResizeableDataContainer for VectorContainer<Item> {
#[inline(always)]
fn resize(&mut self, new_len: usize) {
self.data.resize_with(new_len, Default::default);
}
}
impl<Item: Copy + Default, const ALIGNMENT: usize> ResizeableDataContainer
for AlignedVectorContainer<Item, ALIGNMENT>
{
#[inline(always)]
fn resize(&mut self, new_len: usize) {
self.data.resize(new_len, Default::default());
}
}
impl<Item: Copy + Default> RawAccessDataContainer for VectorContainer<Item> {
#[inline(always)]
fn data(&self) -> Option<&[Self::Item]> {
Some(self.data.as_slice())
}
}
impl<Item: Copy + Default, const ALIGNMENT: usize> RawAccessDataContainer
for AlignedVectorContainer<Item, ALIGNMENT>
{
#[inline(always)]
fn data(&self) -> Option<&[Self::Item]> {
Some(self.data.as_slice())
}
}
unsafe impl<Item: Copy + Default> crate::traits::data_container::StableRawAccessDataContainer
for VectorContainer<Item>
{
}
impl<Item: Copy + Default> MutableRawAccessDataContainer for VectorContainer<Item> {
#[inline(always)]
fn data_mut(&mut self) -> Option<&mut [Self::Item]> {
Some(self.data.as_mut_slice())
}
}
unsafe impl<Item: Copy + Default, const ALIGNMENT: usize>
crate::traits::data_container::StableRawAccessDataContainer
for AlignedVectorContainer<Item, ALIGNMENT>
{
}
impl<Item: Copy + Default, const ALIGNMENT: usize> MutableRawAccessDataContainer
for AlignedVectorContainer<Item, ALIGNMENT>
{
#[inline(always)]
fn data_mut(&mut self) -> Option<&mut [Self::Item]> {
Some(self.data.as_mut_slice())
}
}
pub struct ArrayContainer<Item, const N: usize> {
data: [Item; N],
}
impl<Item, const N: usize> ContainerType for ArrayContainer<Item, N> {
type Type = Stack<N>;
}
impl<Item: Default + Copy, const N: usize> ArrayContainer<Item, N> {
#[inline(always)]
pub fn new() -> ArrayContainer<Item, N> {
ArrayContainer::<Item, N> {
data: [<Item as Default>::default(); N],
}
}
}
impl<Item: Default + Copy, const N: usize> From<[Item; N]> for ArrayContainer<Item, N> {
fn from(value: [Item; N]) -> Self {
ArrayContainer { data: value }
}
}
impl<Item: Default + Copy, const N: usize> Default for ArrayContainer<Item, N> {
#[inline(always)]
fn default() -> Self {
Self::new()
}
}
impl<Item: Copy + Default, const N: usize> DataContainer for ArrayContainer<Item, N> {
type Item = Item;
#[inline(always)]
fn number_of_elements(&self) -> usize {
self.data.len()
}
}
impl<Item: Copy + Default, const N: usize> ValueDataContainer for ArrayContainer<Item, N> {
#[inline(always)]
unsafe fn get_unchecked_value(&self, index: usize) -> Self::Item {
debug_assert!(index < N);
unsafe { *self.data.get_unchecked(index) }
}
}
impl<Item: Copy + Default, const N: usize> RefDataContainer for ArrayContainer<Item, N> {
#[inline(always)]
unsafe fn get_unchecked(&self, index: usize) -> &Self::Item {
debug_assert!(index < N);
unsafe { self.data.get_unchecked(index) }
}
}
unsafe impl<Item: Copy + Default, const N: usize>
crate::traits::data_container::DisjointRefDataContainerMut for ArrayContainer<Item, N>
{
}
impl<Item: Copy + Default, const N: usize> RefDataContainerMut for ArrayContainer<Item, N> {
#[inline(always)]
unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut Self::Item {
debug_assert!(index < N);
unsafe { self.data.get_unchecked_mut(index) }
}
}
impl<Item: Copy + Default, const N: usize> ModifiableDataContainer for ArrayContainer<Item, N> {
#[inline(always)]
unsafe fn set_unchecked_value(&mut self, index: usize, value: Self::Item) {
debug_assert!(index < N);
unsafe {
*self.get_unchecked_mut(index) = value;
}
}
}
impl<Item: Copy + Default, const N: usize> RawAccessDataContainer for ArrayContainer<Item, N> {
#[inline(always)]
fn data(&self) -> Option<&[Self::Item]> {
Some(&self.data)
}
}
unsafe impl<Item: Copy + Default, const N: usize>
crate::traits::data_container::StableRawAccessDataContainer for ArrayContainer<Item, N>
{
}
impl<Item: Copy + Default, const N: usize> MutableRawAccessDataContainer
for ArrayContainer<Item, N>
{
#[inline(always)]
fn data_mut(&mut self) -> Option<&mut [Self::Item]> {
Some(&mut self.data)
}
}
pub struct SliceContainer<'a, Item> {
data: &'a [Item],
}
impl<'a, Item> ContainerType for SliceContainer<'a, Item> {
type Type = Unknown; }
impl<'a, Item> SliceContainer<'a, Item> {
#[inline(always)]
pub fn new(slice: &'a [Item]) -> SliceContainer<'a, Item> {
SliceContainer::<Item> { data: slice }
}
}
impl<Item: Copy + Default> DataContainer for SliceContainer<'_, Item> {
type Item = Item;
#[inline(always)]
fn number_of_elements(&self) -> usize {
self.data.len()
}
}
impl<Item: Copy + Default> ValueDataContainer for SliceContainer<'_, Item> {
#[inline(always)]
unsafe fn get_unchecked_value(&self, index: usize) -> Self::Item {
debug_assert!(index < self.number_of_elements());
unsafe { *self.data.get_unchecked(index) }
}
}
impl<Item: Copy + Default> RefDataContainer for SliceContainer<'_, Item> {
#[inline(always)]
unsafe fn get_unchecked(&self, index: usize) -> &Self::Item {
debug_assert!(index < self.number_of_elements());
unsafe { self.data.get_unchecked(index) }
}
}
impl<Item: Copy + Default> RawAccessDataContainer for SliceContainer<'_, Item> {
#[inline(always)]
fn data(&self) -> Option<&[Self::Item]> {
Some(self.data)
}
}
pub struct SliceContainerMut<'a, Item> {
data: &'a mut [Item],
}
impl<'a, Item> ContainerType for SliceContainerMut<'a, Item> {
type Type = Unknown; }
impl<'a, Item> SliceContainerMut<'a, Item> {
pub fn new(slice: &'a mut [Item]) -> SliceContainerMut<'a, Item> {
SliceContainerMut::<Item> { data: slice }
}
}
impl<Item: Copy + Default> DataContainer for SliceContainerMut<'_, Item> {
type Item = Item;
#[inline(always)]
fn number_of_elements(&self) -> usize {
self.data.len()
}
}
impl<Item: Copy + Default> ValueDataContainer for SliceContainerMut<'_, Item> {
#[inline(always)]
unsafe fn get_unchecked_value(&self, index: usize) -> Self::Item {
debug_assert!(index < self.number_of_elements());
unsafe { *self.data.get_unchecked(index) }
}
}
impl<Item: Copy + Default> RefDataContainer for SliceContainerMut<'_, Item> {
#[inline(always)]
unsafe fn get_unchecked(&self, index: usize) -> &Self::Item {
debug_assert!(index < self.number_of_elements());
unsafe { self.data.get_unchecked(index) }
}
}
unsafe impl<Item: Copy + Default> crate::traits::data_container::DisjointRefDataContainerMut
for SliceContainerMut<'_, Item>
{
}
impl<Item: Copy + Default> RefDataContainerMut for SliceContainerMut<'_, Item> {
#[inline(always)]
unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut Self::Item {
debug_assert!(index < self.number_of_elements());
unsafe { self.data.get_unchecked_mut(index) }
}
}
impl<Item: Copy + Default> ModifiableDataContainer for SliceContainerMut<'_, Item> {
#[inline(always)]
unsafe fn set_unchecked_value(&mut self, index: usize, value: Self::Item) {
debug_assert!(index < self.number_of_elements());
unsafe {
*self.data.get_unchecked_mut(index) = value;
}
}
}
impl<Item: Copy + Default> RawAccessDataContainer for SliceContainerMut<'_, Item> {
#[inline(always)]
fn data(&self) -> Option<&[Self::Item]> {
Some(self.data)
}
}
unsafe impl<Item: Copy + Default> crate::traits::data_container::StableRawAccessDataContainer
for SliceContainerMut<'_, Item>
{
}
impl<Item: Copy + Default> MutableRawAccessDataContainer for SliceContainerMut<'_, Item> {
#[inline(always)]
fn data_mut(&mut self) -> Option<&mut [Self::Item]> {
Some(self.data)
}
}