this crate needs to be rewritten
This commit is contained in:
59
src/data.rs
59
src/data.rs
@@ -1,4 +1,5 @@
|
|||||||
use core::cmp::Ordering;
|
use core::cmp::Ordering;
|
||||||
|
use core::fmt::{Debug, Formatter};
|
||||||
use crate::upcast::IntoUsize;
|
use crate::upcast::IntoUsize;
|
||||||
|
|
||||||
//noinspection SpellCheckingInspection
|
//noinspection SpellCheckingInspection
|
||||||
@@ -88,3 +89,61 @@ pub fn iterate<T: pointer_iterator::Pointer>(pointer: T) -> T::IterType {
|
|||||||
pub fn fill_with<T, F: FnMut(usize) -> T>(slice: &mut [T], mut func: F) {
|
pub fn fill_with<T, F: FnMut(usize) -> T>(slice: &mut [T], mut func: F) {
|
||||||
slice.iter_mut().enumerate().for_each(|(i,v)|*v = func(i))
|
slice.iter_mut().enumerate().for_each(|(i,v)|*v = func(i))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct FixedVec<const MAX_LEN: usize, T> {
|
||||||
|
length: usize,
|
||||||
|
buffer: [core::mem::MaybeUninit<T>;MAX_LEN],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const MAX_LEN: usize, T: Sized> FixedVec<MAX_LEN, T> {
|
||||||
|
pub fn push(&mut self, value: T) -> Option<()> {
|
||||||
|
if self.length >= MAX_LEN { return None }
|
||||||
|
unsafe { *self.buffer[self.length].as_mut_ptr() = value };
|
||||||
|
self.length += 1;
|
||||||
|
Some(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pop(&mut self) -> Option<T> {
|
||||||
|
if self.length == 0 { return None }
|
||||||
|
self.length -= 1;
|
||||||
|
Some(unsafe { self.buffer[self.length].assume_init_read() })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn extend<Iter: Iterator<Item=T>>(&mut self, mut iter: Iter) -> Result<usize, usize> {
|
||||||
|
let mut count = 0;
|
||||||
|
loop {
|
||||||
|
if self.length == MAX_LEN {
|
||||||
|
return Err(count);
|
||||||
|
}
|
||||||
|
match iter.next() {
|
||||||
|
None => return Ok(count),
|
||||||
|
Some(value) => {
|
||||||
|
unsafe { *self.buffer[self.length].as_mut_ptr() = value };
|
||||||
|
self.length += 1;
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_slice(&self) -> &[T] {
|
||||||
|
// length can't be larger than MaxLen, use unsafe to dodge the panic
|
||||||
|
unsafe { core::slice::from_raw_parts(self.buffer.as_ptr() as *const T, self.length) }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const MAX_LEN: usize, T: Debug> Debug for FixedVec<MAX_LEN, T> {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
|
||||||
|
Debug::fmt(self.as_slice(), f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const MAX_LEN: usize, T> Drop for FixedVec<MAX_LEN, T> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
for e in &mut self.buffer[..self.length] {
|
||||||
|
unsafe { e.assume_init_drop() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
18
src/iter_tools/mod.rs
Normal file
18
src/iter_tools/mod.rs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
|
||||||
|
mod take_until;
|
||||||
|
pub use take_until::*;
|
||||||
|
|
||||||
|
pub trait IterTools: Iterator {
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn take_until<P>(self, predicate: P) -> TakeUntil<Self, P>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
P: FnMut(&Self::Item) -> bool,
|
||||||
|
{
|
||||||
|
TakeUntil::new(self, predicate)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Iterator> IterTools for T {}
|
||||||
50
src/iter_tools/take_until.rs
Normal file
50
src/iter_tools/take_until.rs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
|
||||||
|
use core::fmt;
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct TakeUntil<I, P> {
|
||||||
|
iter: I,
|
||||||
|
flag: bool,
|
||||||
|
predicate: P,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<I, P> TakeUntil<I, P> {
|
||||||
|
pub(in super) fn new(iter: I, predicate: P) -> TakeUntil<I, P> {
|
||||||
|
TakeUntil { iter, flag: false, predicate }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<I: Iterator, P> Iterator for TakeUntil<I, P>
|
||||||
|
where
|
||||||
|
P: FnMut(&I::Item) -> bool,
|
||||||
|
{
|
||||||
|
type Item = I::Item;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn next(&mut self) -> Option<I::Item> {
|
||||||
|
if self.flag {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
let x = self.iter.next()?;
|
||||||
|
if (self.predicate)(&x) {
|
||||||
|
Some(x)
|
||||||
|
} else {
|
||||||
|
self.flag = true;
|
||||||
|
Some(x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||||
|
if self.flag {
|
||||||
|
(0, Some(0))
|
||||||
|
} else {
|
||||||
|
let (_, upper) = self.iter.size_hint();
|
||||||
|
(0, upper) // can't know a lower bound, due to the predicate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -43,3 +43,6 @@ pub use hash::*;
|
|||||||
|
|
||||||
mod strings;
|
mod strings;
|
||||||
pub use strings::*;
|
pub use strings::*;
|
||||||
|
|
||||||
|
mod iter_tools;
|
||||||
|
pub use iter_tools::*;
|
||||||
Reference in New Issue
Block a user