#![cfg_attr(feature = "std", doc = "[Vec]: std::vec::Vec")]
#![doc = include_str!("../README.md")]
#![no_std]
extern crate alloc;
use alloc::vec::Vec;
use alloc::string::String;
use alloc::collections::*;
mod queue;
pub use queue::ReusingQueue;
#[derive(Clone, Default)]
pub struct ReusingVec<T> {
logical_len: usize,
contents: Vec<T>
}
impl<T> core::fmt::Debug for ReusingVec<T> where T: core::fmt::Debug {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
write!(f, "{:?}", self as &[_])
}
}
impl<T> ReusingVec<T> {
#[inline]
pub const fn new() -> Self {
Self {
logical_len: 0,
contents: Vec::new()
}
}
#[inline]
pub fn with_capacity(capacity: usize) -> Self {
Self {
logical_len: 0,
contents: Vec::with_capacity(capacity)
}
}
#[inline]
pub fn clear(&mut self) {
self.logical_len = 0;
}
#[inline]
pub fn truncate(&mut self, len: usize) {
if len < self.logical_len {
self.logical_len = len;
}
}
#[inline]
pub fn len(&self) -> usize {
self.logical_len
}
#[inline]
pub fn is_empty(&self) -> bool {
self.logical_len == 0
}
#[inline]
pub fn push_val(&mut self, val: T) {
if self.logical_len < self.contents.len() {
*self.contents.get_mut(self.logical_len).unwrap() = val;
} else {
self.contents.push(val);
}
self.logical_len += 1;
}
#[inline]
pub fn push_with<NewF, ResetF>(&mut self, new_f: NewF, reset_f: ResetF)
where
NewF: FnOnce() -> T,
ResetF: FnOnce(&mut T)
{
if self.logical_len < self.contents.len() {
reset_f(self.contents.get_mut(self.logical_len).unwrap());
} else {
self.contents.push(new_f());
}
self.logical_len += 1;
}
#[inline]
pub fn pop(&mut self) -> Option<&mut T> {
if self.logical_len > 0 {
self.logical_len -= 1;
self.contents.get_mut(self.logical_len)
} else {
None
}
}
}
impl<T> AsMut<[T]> for ReusingVec<T> {
fn as_mut(&mut self) -> &mut [T] {
&mut self.contents[0..self.logical_len]
}
}
impl<T> AsRef<[T]> for ReusingVec<T> {
fn as_ref(&self) -> &[T] {
&self.contents[0..self.logical_len]
}
}
impl<T> core::borrow::Borrow<[T]> for ReusingVec<T> {
fn borrow(&self) -> &[T] {
&self.contents[0..self.logical_len]
}
}
impl<T> core::borrow::BorrowMut<[T]> for ReusingVec<T> {
fn borrow_mut(&mut self) -> &mut [T] {
&mut self.contents[0..self.logical_len]
}
}
impl<T> core::ops::Deref for ReusingVec<T> {
type Target = [T];
fn deref(&self) -> &[T] {
&self.contents[0..self.logical_len]
}
}
impl<T> core::ops::DerefMut for ReusingVec<T> {
fn deref_mut(&mut self) -> &mut [T] {
&mut self.contents[0..self.logical_len]
}
}
impl<T> From<Vec<T>> for ReusingVec<T> {
fn from(vec: Vec<T>) -> Self {
Self {
logical_len: vec.len(),
contents: vec
}
}
}
impl<T> From<ReusingVec<T>> for Vec<T> {
fn from(mut vec: ReusingVec<T>) -> Self {
vec.contents.truncate(vec.logical_len);
vec.contents
}
}
impl<T, U> FromIterator<U> for ReusingVec<T> where T: From<U> {
fn from_iter<I: IntoIterator<Item=U>>(iter: I) -> Self {
let contents: Vec<T> = iter.into_iter().map(|element| element.into()).collect();
Self {
logical_len: contents.len(),
contents,
}
}
}
impl<T> IntoIterator for ReusingVec<T> {
type Item = T;
type IntoIter = ReusingVecIter<T>;
fn into_iter(self) -> Self::IntoIter {
self.contents.into_iter().take(self.logical_len)
}
}
pub type ReusingVecIter<T> = core::iter::Take<alloc::vec::IntoIter<T>>;
impl<T> PartialEq<Self> for ReusingVec<T> where T: PartialEq {
fn eq(&self, other: &Self) -> bool {
(self as &[T]).eq(other as &[T])
}
}
impl<T> Eq for ReusingVec<T> where T: Eq {}
impl<T> PartialEq<[T]> for ReusingVec<T> where T: PartialEq {
fn eq(&self, other: &[T]) -> bool {
(self as &[T]).eq(other)
}
}
impl<T> PartialEq<Vec<T>> for ReusingVec<T> where T: PartialEq {
fn eq(&self, other: &Vec<T>) -> bool {
(self as &[T]).eq(other)
}
}
impl<T: ReusableElement> ReusingVec<T> {
#[inline]
pub fn push_mut(&mut self) -> &mut T {
if self.logical_len < self.contents.len() {
self.contents.get_mut(self.logical_len).unwrap().reset();
} else {
self.contents.push(T::new());
}
let element = self.contents.get_mut(self.logical_len).unwrap();
self.logical_len += 1;
element
}
}
pub trait ReusableElement {
fn reset(&mut self);
fn new() -> Self;
}
impl<T> ReusableElement for Option<T> {
fn reset(&mut self) {
*self = None
}
fn new() -> Self {
None
}
}
impl<T> ReusableElement for Vec<T> {
fn reset(&mut self) {
self.clear()
}
fn new() -> Self {
Self::new()
}
}
impl ReusableElement for String {
fn reset(&mut self) {
self.clear()
}
fn new() -> Self {
Self::new()
}
}
impl<T: Ord> ReusableElement for BinaryHeap<T> {
fn reset(&mut self) {
self.clear()
}
fn new() -> Self {
Self::new()
}
}
impl<K, V> ReusableElement for BTreeMap<K, V> {
fn reset(&mut self) {
self.clear()
}
fn new() -> Self {
Self::new()
}
}
impl<T> ReusableElement for BTreeSet<T> {
fn reset(&mut self) {
self.clear()
}
fn new() -> Self {
Self::new()
}
}
impl<T> ReusableElement for LinkedList<T> {
fn reset(&mut self) {
self.clear()
}
fn new() -> Self {
Self::new()
}
}
impl<T> ReusableElement for VecDeque<T> {
fn reset(&mut self) {
self.clear()
}
fn new() -> Self {
Self::new()
}
}
#[cfg(feature = "std")]
extern crate std;
#[cfg(feature = "std")]
impl<K, V> ReusableElement for std::collections::HashMap<K, V> {
fn reset(&mut self) {
self.clear()
}
fn new() -> Self {
Self::new()
}
}
#[cfg(feature = "std")]
impl<T> ReusableElement for std::collections::HashSet<T> {
fn reset(&mut self) {
self.clear()
}
fn new() -> Self {
Self::new()
}
}
#[cfg(feature = "smallvec")]
impl<A: smallvec::Array> ReusableElement for smallvec::SmallVec<A> {
fn reset(&mut self) {
self.clear()
}
fn new() -> Self {
Self::new()
}
}