use core::fmt;
use core::iter::FusedIterator;
use core::ops::Index;
mod raw;
#[macro_export]
macro_rules! vec {
() => {
$crate::Vec::new()
};
($elem:expr; $n:expr) => {{
let n = $n;
let mut vec = $crate::Vec::with_capacity(n);
let iter = ::core::iter::Iterator::take(::core::iter::repeat($elem), n);
::core::iter::Extend::extend(&mut vec, iter);
vec
}};
($($x:expr),+ $(,)?) => (
<$crate::Vec<_> as ::core::iter::FromIterator<_>>::from_iter([$($x),+])
);
}
pub struct Vec<T> {
raw: raw::Vec<T>,
}
impl<T> Default for Vec<T> {
fn default() -> Vec<T> {
Vec::new()
}
}
impl<T> Vec<T> {
#[inline]
#[cfg(not(loom))]
pub const fn new() -> Vec<T> {
Vec {
raw: raw::Vec::new(),
}
}
#[cfg(loom)]
pub fn new() -> Vec<T> {
Vec {
raw: raw::Vec::new(),
}
}
#[inline]
pub fn with_capacity(capacity: usize) -> Vec<T> {
Vec {
raw: raw::Vec::with_capacity(capacity),
}
}
pub fn reserve(&self, additional: usize) {
self.raw.reserve(additional)
}
#[inline]
pub fn push(&self, value: T) -> usize {
self.raw.push(value)
}
#[inline]
pub fn push_with<F>(&self, f: F) -> usize
where
F: FnOnce(usize) -> T,
{
self.raw.push_with(f)
}
#[inline]
pub fn count(&self) -> usize {
self.raw.count()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.count() == 0
}
#[inline]
pub fn get(&self, index: usize) -> Option<&T> {
self.raw.get(index)
}
#[inline]
pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
self.raw.get_mut(index)
}
#[inline]
pub unsafe fn get_unchecked(&self, index: usize) -> &T {
unsafe { self.raw.get_unchecked(index) }
}
#[inline]
pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T {
unsafe { self.raw.get_unchecked_mut(index) }
}
#[inline]
pub fn iter(&self) -> Iter<'_, T> {
Iter {
raw: self.raw.iter(),
min_remaining: self.count(),
}
}
#[inline]
pub fn clear(&mut self) {
self.raw.clear();
}
}
impl<T> Index<usize> for Vec<T> {
type Output = T;
#[inline]
fn index(&self, index: usize) -> &Self::Output {
&self.raw[index]
}
}
impl<T> IntoIterator for Vec<T> {
type Item = T;
type IntoIter = IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
IntoIter {
raw: self.raw.into_iter(),
}
}
}
impl<'a, T> IntoIterator for &'a Vec<T> {
type Item = (usize, &'a T);
type IntoIter = Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
pub struct IntoIter<T> {
raw: raw::IntoIter<T>,
}
impl<T> Iterator for IntoIter<T> {
type Item = T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.raw.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.raw.size_hint()
}
}
impl<T> ExactSizeIterator for IntoIter<T> {
fn len(&self) -> usize {
self.raw.len()
}
}
impl<T> FusedIterator for IntoIter<T> {}
pub struct Iter<'a, T> {
raw: raw::Iter<'a, T>,
min_remaining: usize,
}
impl<T> FusedIterator for Iter<'_, T> {}
impl<'a, T> Clone for Iter<'a, T> {
fn clone(&self) -> Iter<'a, T> {
Iter {
raw: self.raw.clone(),
min_remaining: self.min_remaining,
}
}
}
impl<'a, T> Iterator for Iter<'a, T> {
type Item = (usize, &'a T);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let item = self.raw.next()?;
self.min_remaining = self.min_remaining.saturating_sub(1);
Some(item)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(self.min_remaining, None)
}
}
impl<T: fmt::Debug> fmt::Debug for Iter<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
struct Contents<'a, T>(&'a T);
impl<T> fmt::Debug for Contents<'_, T>
where
T: Iterator + Clone,
T::Item: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.0.clone()).finish()
}
}
f.debug_tuple("Iter").field(&Contents(self)).finish()
}
}
impl<T> FromIterator<T> for Vec<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let iter = iter.into_iter();
let (lower, _) = iter.size_hint();
let vec = Vec::with_capacity(lower);
for value in iter {
vec.push(value);
}
vec
}
}
impl<T> Extend<T> for Vec<T> {
#[inline]
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
let iter = iter.into_iter();
let (lower, _) = iter.size_hint();
self.reserve(lower);
for value in iter {
self.push(value);
}
}
}
impl<T: Clone> Clone for Vec<T> {
fn clone(&self) -> Vec<T> {
self.iter().map(|(_, x)| x).cloned().collect()
}
}
impl<T: fmt::Debug> fmt::Debug for Vec<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iter().map(|(_, v)| v)).finish()
}
}
impl<T: PartialEq> PartialEq for Vec<T> {
fn eq(&self, other: &Self) -> bool {
if self.count() != other.count() {
return false;
}
for (index, value) in self.iter() {
if other.get(index) != Some(value) {
return false;
}
}
true
}
}
impl<A, T> PartialEq<A> for Vec<T>
where
A: AsRef<[T]>,
T: PartialEq,
{
fn eq(&self, other: &A) -> bool {
let other = other.as_ref();
if self.count() != other.len() {
return false;
}
for (index, value) in self.iter() {
if other.get(index) != Some(value) {
return false;
}
}
true
}
}
impl<T: Eq> Eq for Vec<T> {}