use std::mem::size_of;
use std::marker::PhantomData;
use std::ops::Index;
use std::slice;
use std::slice::{Iter as CoreSliceIter};
use rawpointer::PointerExt;
use rawpointer::ptrdistance;
#[derive(Debug)]
pub struct SliceIter<'a, T: 'a> {
ptr: *const T,
end: *const T,
ty: PhantomData<&'a T>,
}
impl<'a, T> Copy for SliceIter<'a, T> { }
impl<'a, T> Clone for SliceIter<'a, T> {
fn clone(&self) -> Self { *self }
}
impl<'a, T> SliceIter<'a, T> {
#[inline]
pub unsafe fn new(start: *const T, end: *const T) -> Self {
assert!(size_of::<T>() != 0);
SliceIter {
ptr: start,
end: end,
ty: PhantomData,
}
}
pub fn start(&self) -> *const T {
self.ptr
}
pub fn end(&self) -> *const T {
self.end
}
pub unsafe fn start_mut(&mut self) -> &mut *const T {
&mut self.ptr
}
pub unsafe fn end_mut(&mut self) -> &mut *const T {
&mut self.end
}
pub fn peek_next(&self) -> Option<<Self as Iterator>::Item> {
if self.ptr != self.end {
unsafe {
Some(&*self.ptr)
}
} else {
None
}
}
pub fn as_slice(&self) -> &'a [T] {
unsafe {
slice::from_raw_parts(self.ptr, self.len())
}
}
#[inline]
pub unsafe fn next_unchecked(&mut self) -> <Self as Iterator>::Item {
&*self.ptr.post_inc()
}
pub unsafe fn get_unchecked(&self, i: usize) -> &T {
&*self.ptr.offset(i as isize)
}
}
impl<'a, T> Iterator for SliceIter<'a, T> {
type Item = &'a T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.ptr != self.end {
unsafe {
Some(&*self.ptr.post_inc())
}
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
fn count(self) -> usize {
self.len()
}
fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
fn all<F>(&mut self, mut predicate: F) -> bool
where F: FnMut(Self::Item) -> bool,
{
self.fold_while(true, move |_, elt| {
if predicate(elt) {
FoldWhile::Continue(true)
} else {
FoldWhile::Done(false)
}
})
}
fn any<F>(&mut self, mut predicate: F) -> bool
where F: FnMut(Self::Item) -> bool,
{
!self.all(move |x| !predicate(x))
}
fn find<F>(&mut self, mut predicate: F) -> Option<Self::Item>
where F: FnMut(&Self::Item) -> bool,
{
self.fold_while(None, move |_, elt| {
if predicate(&elt) {
FoldWhile::Done(Some(elt))
} else {
FoldWhile::Continue(None)
}
})
}
fn position<F>(&mut self, mut predicate: F) -> Option<usize>
where F: FnMut(Self::Item) -> bool,
{
let mut index = 0;
self.fold_while(None, move |_, elt| {
if predicate(elt) {
FoldWhile::Done(Some(index))
} else {
index += 1;
FoldWhile::Continue(None)
}
})
}
fn rposition<F>(&mut self, mut predicate: F) -> Option<usize>
where F: FnMut(Self::Item) -> bool,
{
let mut index = self.len();
self.rfold_while(None, move |_, elt| {
index -= 1;
if predicate(elt) {
FoldWhile::Done(Some(index))
} else {
FoldWhile::Continue(None)
}
})
}
}
impl<'a, T> DoubleEndedIterator for SliceIter<'a, T> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
if self.ptr != self.end {
unsafe {
Some(&*self.end.pre_dec())
}
} else {
None
}
}
}
impl<'a, T> ExactSizeIterator for SliceIter<'a, T> {
fn len(&self) -> usize {
ptrdistance(self.ptr, self.end)
}
}
impl<'a, T> From<&'a [T]> for SliceIter<'a, T> {
fn from(slice: &'a [T]) -> Self {
unsafe {
let ptr = slice.as_ptr();
let end = ptr.offset(slice.len() as isize);
SliceIter::new(ptr, end)
}
}
}
impl<'a, T> From<CoreSliceIter<'a, T>> for SliceIter<'a, T> {
fn from(slice: CoreSliceIter<'a, T>) -> Self {
SliceIter::from(slice.as_slice())
}
}
impl<'a, T> Default for SliceIter<'a, T> {
fn default() -> Self {
unsafe {
SliceIter::new(0x1 as *const T, 0x1 as *const T)
}
}
}
impl<'a, T> Index<usize> for SliceIter<'a, T> {
type Output = T;
fn index(&self, i: usize) -> &T {
assert!(i < self.len());
unsafe {
&*self.ptr.offset(i as isize)
}
}
}
#[derive(Copy, Clone, Debug)]
enum FoldWhile<T> {
Continue(T),
Done(T),
}
trait FoldWhileExt : Iterator {
fn fold_while<Acc, G>(&mut self, init: Acc, g: G) -> Acc
where Self: Sized,
G: FnMut(Acc, Self::Item) -> FoldWhile<Acc>;
fn rfold_while<Acc, G>(&mut self, accum: Acc, g: G) -> Acc
where Self: Sized,
G: FnMut(Acc, Self::Item) -> FoldWhile<Acc>;
}
macro_rules! fold_while {
($e:expr) => {
match $e {
FoldWhile::Continue(t) => t,
FoldWhile::Done(done) => return done,
}
}
}
impl<'a, T> FoldWhileExt for SliceIter<'a, T> {
fn fold_while<Acc, G>(&mut self, init: Acc, mut g: G) -> Acc
where Self: Sized,
G: FnMut(Acc, Self::Item) -> FoldWhile<Acc>
{
let mut accum = init;
unsafe {
while ptrdistance(self.ptr, self.end) >= 4 {
accum = fold_while!(g(accum, &*self.ptr.post_inc()));
accum = fold_while!(g(accum, &*self.ptr.post_inc()));
accum = fold_while!(g(accum, &*self.ptr.post_inc()));
accum = fold_while!(g(accum, &*self.ptr.post_inc()));
}
while self.ptr != self.end {
accum = fold_while!(g(accum, &*self.ptr.post_inc()));
}
}
accum
}
fn rfold_while<Acc, G>(&mut self, mut accum: Acc, mut g: G) -> Acc
where Self: Sized,
G: FnMut(Acc, Self::Item) -> FoldWhile<Acc>
{
unsafe {
while ptrdistance(self.ptr, self.end) >= 4 {
accum = fold_while!(g(accum, &*self.end.pre_dec()));
accum = fold_while!(g(accum, &*self.end.pre_dec()));
accum = fold_while!(g(accum, &*self.end.pre_dec()));
accum = fold_while!(g(accum, &*self.end.pre_dec()));
}
while self.ptr != self.end {
accum = fold_while!(g(accum, &*self.end.pre_dec()));
}
}
accum
}
}