use std::fmt;
use std::iter::FusedIterator;
pub(crate) mod with_metadata;
#[derive(Clone, PartialEq, Eq)]
pub struct Vec2d<T>(with_metadata::Vec2d<T, 0>);
impl<T> Default for Vec2d<T> {
#[inline(always)]
fn default() -> Self {
Self(Default::default())
}
}
impl<T> Vec2d<T> {
#[inline(always)]
pub fn new() -> Self {
Self::default()
}
#[inline(always)]
pub fn with_capacity(vectors: usize, elements_sum: usize) -> Self {
Self(with_metadata::Vec2d::with_capacity(vectors, elements_sum))
}
#[inline(always)]
pub fn reserve_vectors(&mut self, additional: usize) {
self.0.reserve_vectors(additional);
}
#[inline(always)]
pub fn reserve_elements(&mut self, additional: usize) {
self.0.reserve_elements(additional);
}
#[inline(always)]
pub fn len(&self) -> usize {
self.0.len()
}
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[inline]
pub fn get(&self, index: usize) -> Option<&[T]> {
Some(self.0.get(index)?.1)
}
#[inline(always)]
pub fn get_mut(&mut self, index: usize) -> Option<&mut [T]> {
self.0.get_mut(index)
}
#[inline]
pub fn first(&self) -> Option<&[T]> {
Some(self.0.first()?.1)
}
#[inline(always)]
pub fn first_mut(&mut self) -> Option<&mut [T]> {
self.0.first_mut()
}
#[inline]
pub fn last(&self) -> Option<&[T]> {
Some(self.0.last()?.1)
}
#[inline(always)]
pub fn last_mut(&mut self) -> Option<&mut [T]> {
self.0.last_mut()
}
#[inline(always)]
pub fn iter(&self) -> Vec2dIter<'_, T> {
Vec2dIter(self.0.iter())
}
#[inline(always)]
pub fn push_vec(&mut self) {
self.0.push_vec(0);
}
#[inline(always)]
pub fn pop_vec(&mut self) -> bool {
self.0.pop_vec()
}
#[inline(always)]
pub fn truncate(&mut self, len: usize) {
self.0.truncate(len);
}
#[track_caller]
#[inline(always)]
pub fn push_element(&mut self, element: T) {
self.0.push_element(element);
}
#[track_caller]
#[inline(always)]
pub fn push_elements(&mut self, iter: impl IntoIterator<Item = T>) {
self.0.push_elements(iter);
}
#[inline(always)]
pub fn all_elements(&self) -> &[T] {
self.0.all_elements()
}
#[inline(always)]
pub fn all_elements_mut(&mut self) -> &mut [T] {
self.0.all_elements_mut()
}
}
impl<T: Clone> Vec2d<T> {
#[track_caller]
#[inline(always)]
pub fn resize_last(&mut self, new_len: usize, value: T) {
self.0.resize_last(new_len, value);
}
}
#[cold]
#[inline(never)]
fn panic_bounds_check(len: usize, index: usize) -> ! {
panic!("index out of bounds: the len is {len} but the index is {index}");
}
impl<T> std::ops::Index<usize> for Vec2d<T> {
type Output = [T];
#[track_caller]
#[inline(always)]
fn index(&self, index: usize) -> &[T] {
if let Some(v) = self.get(index) {
return v;
}
panic_bounds_check(self.len(), index)
}
}
impl<T> std::ops::IndexMut<usize> for Vec2d<T> {
#[track_caller]
#[inline(always)]
fn index_mut(&mut self, index: usize) -> &mut [T] {
let len = self.len(); if let Some(v) = self.get_mut(index) {
return v;
}
panic_bounds_check(len, index)
}
}
impl<'a, T: Copy> Extend<&'a [T]> for Vec2d<T> {
fn extend<I: IntoIterator<Item = &'a [T]>>(&mut self, iter: I) {
self.0.extend(iter.into_iter().map(|i| (0, i)));
}
}
impl<'a, T: Copy> FromIterator<&'a [T]> for Vec2d<T> {
#[inline(always)]
fn from_iter<I: IntoIterator<Item = &'a [T]>>(iter: I) -> Self {
let mut vec = Vec2d::new();
vec.extend(iter);
vec
}
}
impl<T: fmt::Debug> fmt::Debug for Vec2d<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}
#[derive(Clone)]
pub struct Vec2dIter<'a, T>(with_metadata::Vec2dIter<'a, T, 0>);
impl<'a, T> Iterator for Vec2dIter<'a, T> {
type Item = &'a [T];
#[inline]
fn next(&mut self) -> Option<&'a [T]> {
Some(self.0.next()?.1)
}
#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
#[inline(always)]
fn count(self) -> usize {
self.0.count()
}
#[inline(always)]
fn last(self) -> Option<&'a [T]> {
Some(self.0.last()?.1)
}
#[inline]
fn nth(&mut self, n: usize) -> Option<&'a [T]> {
Some(self.0.nth(n)?.1)
}
}
impl<T> FusedIterator for Vec2dIter<'_, T> {}
impl<T> ExactSizeIterator for Vec2dIter<'_, T> {
#[inline(always)]
fn len(&self) -> usize {
self.0.len()
}
}
impl<'a, T> DoubleEndedIterator for Vec2dIter<'a, T> {
#[inline]
fn next_back(&mut self) -> Option<&'a [T]> {
Some(self.0.next_back()?.1)
}
#[inline]
fn nth_back(&mut self, n: usize) -> Option<&'a [T]> {
Some(self.0.nth_back(n)?.1)
}
}