use super::*;
use core::{
cmp,
fmt::{
self,
Debug,
Formatter,
},
iter::FusedIterator,
mem,
};
impl<'a, O, T> IntoIterator for &'a BitSlice<O, T>
where O: BitOrder, T: 'a + BitStore {
type Item = &'a bool;
type IntoIter = Iter<'a, O, T>;
fn into_iter(self) -> Self::IntoIter {
Iter {
inner: self
}
}
}
#[derive(Clone, Debug)]
pub struct Iter<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
pub(super) inner: &'a BitSlice<O, T>,
}
impl<'a, O, T> Iter<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
#[inline]
pub fn as_bitslice(&self) -> &'a BitSlice<O, T> {
self.inner
}
#[inline]
pub fn as_slice(&self) -> &'a [T] {
self.inner.as_slice()
}
#[allow(dead_code)]
pub(crate) fn bitptr(&self) -> BitPtr<T> {
self.inner.bitptr()
}
}
impl<'a, O, T> Iterator for Iter<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
type Item = &'a bool;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.split_first().map(|(b, r)| {
self.inner = r;
b
})
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.inner.len();
(len, Some(len))
}
#[inline]
fn count(self) -> usize {
self.len()
}
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
mem::replace(&mut self.inner, BitSlice::empty())
.get(n ..)
.and_then(|s| {
self.inner = s;
self.next()
})
}
#[inline]
fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}
impl<'a, O, T> DoubleEndedIterator for Iter<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.inner.split_last().map(|(b, r)| {
self.inner = r;
b
})
}
}
impl<O, T> ExactSizeIterator for Iter<'_, O, T>
where O: BitOrder, T: BitStore {}
impl<O, T> FusedIterator for Iter<'_, O, T>
where O: BitOrder, T: BitStore {}
impl<O, T> AsRef<BitSlice<O, T>> for Iter<'_, O, T>
where O: BitOrder, T: BitStore {
fn as_ref(&self) -> &BitSlice<O, T> {
self.inner
}
}
impl<O, T> AsRef<[T]> for Iter<'_, O, T>
where O: BitOrder, T: BitStore {
fn as_ref(&self) -> &[T] {
self.inner.as_slice()
}
}
impl<'a, O, T> IntoIterator for &'a mut BitSlice<O, T>
where O: BitOrder, T: 'a + BitStore {
type Item = BitMut<'a, O, T>;
type IntoIter = IterMut<'a, O, T>;
fn into_iter(self) -> Self::IntoIter {
IterMut {
inner: self
}
}
}
#[derive(Debug)]
pub struct IterMut<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
pub(super) inner: &'a mut BitSlice<O, T>,
}
impl<'a, O, T> IterMut<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
pub fn into_bitslice(self) -> &'a mut BitSlice<O, T> {
self.inner
}
pub fn into_slice(self) -> &'a mut [T] {
self.inner.as_mut_slice()
}
#[allow(dead_code)]
pub(crate) fn bitptr(&self) -> BitPtr<T> {
self.inner.bitptr()
}
}
impl<'a, O, T> Iterator for IterMut<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
type Item = BitMut<'a, O, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
mem::replace(&mut self.inner, BitSlice::empty_mut())
.split_first_mut()
.map(|(b, r)| {
self.inner = r;
b
})
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.inner.len();
(len, Some(len))
}
#[inline]
fn count(self) -> usize {
self.inner.len()
}
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
mem::replace(&mut self.inner, BitSlice::empty_mut())
.get_mut(n ..)
.and_then(|s| {
self.inner = s;
self.next()
})
}
#[inline]
fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}
impl<'a, O, T> DoubleEndedIterator for IterMut<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
mem::replace(&mut self.inner, BitSlice::empty_mut())
.split_last_mut()
.map(|(b, r)| {
self.inner = r;
b
})
}
}
impl<O, T> ExactSizeIterator for IterMut<'_, O, T>
where O: BitOrder, T: BitStore {}
impl<O, T> FusedIterator for IterMut<'_, O, T>
where O: BitOrder, T: BitStore {}
#[derive(Clone, Debug)]
pub struct Chunks<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
pub(super) inner: &'a BitSlice<O, T>,
pub(super) width: usize,
}
impl<'a, O, T> Iterator for Chunks<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
type Item = &'a BitSlice<O, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
match self.inner.len() {
0 => None,
n if n <= self.width =>
Some(mem::replace(&mut self.inner, BitSlice::empty())),
_ => {
let (head, rest) = self.inner.split_at(self.width);
self.inner = rest;
Some(head)
},
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match self.inner.len() {
0 => (0, Some(0)),
len => {
let (n, r) = (len / self.width, len % self.width);
let out = n + (r > 0) as usize;
(out, Some(out))
},
}
}
#[inline]
fn count(self) -> usize {
self.len()
}
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
let (start, ovf) = self.width.overflowing_mul(n);
let len = self.inner.len();
if start >= len || ovf {
self.inner = BitSlice::empty();
return None;
}
self.inner = unsafe { self.inner.get_unchecked(start ..) };
self.next()
}
#[inline]
fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}
impl<'a, O, T> DoubleEndedIterator for Chunks<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
match self.inner.len() {
0 => None,
len => {
let rem = len % self.width;
let size = if rem == 0 { self.width } else { rem };
let (rest, tail) = self.inner.split_at(len - size);
self.inner = rest;
Some(tail)
},
}
}
}
impl<O, T> ExactSizeIterator for Chunks<'_, O, T>
where O: BitOrder, T: BitStore {}
impl<O, T> FusedIterator for Chunks<'_, O, T>
where O: BitOrder, T: BitStore {}
#[derive(Clone, Debug)]
pub struct ChunksExact<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
pub(super) inner: &'a BitSlice<O, T>,
pub(super) extra: &'a BitSlice<O, T>,
pub(super) width: usize,
}
impl<'a, O, T> ChunksExact<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
pub fn remainder(&self) -> &'a BitSlice<O, T> {
self.extra
}
}
impl<'a, O, T> Iterator for ChunksExact<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
type Item = &'a BitSlice<O, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
match self.inner.len() {
n if n < self.width => {
self.inner = BitSlice::empty();
None
},
_ => {
let (head, rest) = self.inner.split_at(self.width);
self.inner = rest;
Some(head)
},
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.inner.len() / self.width;
(len, Some(len))
}
#[inline]
fn count(self) -> usize {
self.len()
}
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
let (start, ovf) = self.width.overflowing_mul(n);
if start >= self.inner.len() || ovf {
self.inner = BitSlice::empty();
return None;
}
self.inner = unsafe { self.inner.get_unchecked(start ..) };
self.next()
}
#[inline]
fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}
impl<'a, O, T> DoubleEndedIterator for ChunksExact<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
match self.inner.len() {
len if len < self.width => {
self.inner = BitSlice::empty();
None
},
len => {
let (rest, tail) = self.inner.split_at(len - self.width);
self.inner = rest;
Some(tail)
},
}
}
}
impl<O, T> ExactSizeIterator for ChunksExact<'_, O, T>
where O: BitOrder, T: BitStore {}
impl<O, T> FusedIterator for ChunksExact<'_, O, T>
where O: BitOrder, T: BitStore {}
#[derive(Debug)]
pub struct ChunksExactMut<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
pub(super) inner: &'a mut BitSlice<O, T>,
pub(super) extra: &'a mut BitSlice<O, T>,
pub(super) width: usize,
}
impl<'a, O, T> ChunksExactMut<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
pub fn into_remainder(self) -> &'a mut BitSlice<O, T> {
self.extra
}
}
impl<'a, O, T> Iterator for ChunksExactMut<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
type Item = &'a mut BitSlice<O, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let slice = mem::replace(&mut self.inner, BitSlice::empty_mut());
match slice.len() {
n if n < self.width => None,
_ => {
let (head, rest) = slice.split_at_mut(self.width);
self.inner = rest;
Some(head)
},
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.inner.len() / self.width;
(len, Some(len))
}
#[inline]
fn count(self) -> usize {
self.len()
}
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
let slice = mem::replace(&mut self.inner, BitSlice::empty_mut());
let (start, ovf) = self.width.overflowing_mul(n);
if start >= slice.len() || ovf {
return None;
}
self.inner = unsafe { slice.get_unchecked_mut(start ..) };
self.next()
}
#[inline]
fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}
impl<'a, O, T> DoubleEndedIterator for ChunksExactMut<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
let slice = mem::replace(&mut self.inner, BitSlice::empty_mut());
match slice.len() {
len if len < self.width => None,
len => {
let (rest, tail) = slice.split_at_mut(len - self.width);
self.inner = rest;
Some(tail)
},
}
}
}
impl<O, T> ExactSizeIterator for ChunksExactMut<'_, O, T>
where O: BitOrder, T: BitStore {}
impl<O, T> FusedIterator for ChunksExactMut<'_, O, T>
where O: BitOrder, T: BitStore {}
#[derive(Debug)]
pub struct ChunksMut<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
pub(super) inner: &'a mut BitSlice<O, T>,
pub(super) width: usize,
}
impl<'a, O, T> Iterator for ChunksMut<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
type Item = &'a mut BitSlice<O, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let slice = mem::replace(&mut self.inner, BitSlice::empty_mut());
match slice.len() {
0 => None,
n if n <= self.width => Some(slice),
_ => {
let (head, rest) = slice.split_at_mut(self.width);
self.inner = rest;
Some(head)
},
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match self.inner.len() {
0 => (0, Some(0)),
len => {
let (n, r) = (len / self.width, len % self.width);
let out = n + (r > 0) as usize;
(out, Some(out))
},
}
}
#[inline]
fn count(self) -> usize {
self.len()
}
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
let slice = mem::replace(&mut self.inner, BitSlice::empty_mut());
let (start, ovf) = self.width.overflowing_mul(n);
if start >= slice.len() || ovf {
return None;
}
self.inner = unsafe { slice.get_unchecked_mut(start ..) };
self.next()
}
#[inline]
fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}
impl<'a, O, T> DoubleEndedIterator for ChunksMut<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
let slice = mem::replace(&mut self.inner, BitSlice::empty_mut());
match slice.len() {
0 => None,
len => {
let rem = len % self.width;
let size = if rem == 0 { self.width } else { rem };
let (rest, tail) = slice.split_at_mut(len - size);
self.inner = rest;
Some(tail)
},
}
}
}
impl<O, T> ExactSizeIterator for ChunksMut<'_, O, T>
where O: BitOrder, T: BitStore {}
impl<O, T> FusedIterator for ChunksMut<'_, O, T>
where O: BitOrder, T: BitStore {}
#[derive(Clone, Debug)]
pub struct RChunks<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
pub(super) inner: &'a BitSlice<O, T>,
pub(super) width: usize,
}
impl<'a, O, T> Iterator for RChunks<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
type Item = &'a BitSlice<O, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
match self.inner.len() {
0 => None,
n => {
let len = cmp::min(n, self.width);
let (rest, tail) = self.inner.split_at(n - len);
self.inner = rest;
Some(tail)
},
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match self.inner.len() {
0 => (0, Some(0)),
n => {
let (len, rem) = (n / self.width, n % self.width);
let len = len + (rem > 0) as usize;
(len, Some(len))
},
}
}
#[inline]
fn count(self) -> usize {
self.len()
}
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
let len = self.inner.len();
let (end, ovf) = self.width.overflowing_mul(n);
if end >= len || ovf {
self.inner = BitSlice::empty();
return None;
}
let end = len - end;
self.inner = unsafe { self.inner.get_unchecked(.. end) };
self.next()
}
#[inline]
fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}
impl<'a, O, T> DoubleEndedIterator for RChunks<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
match self.inner.len() {
0 => None,
n => {
let rem = n % self.width;
let len = if rem == 0 { self.width } else { rem };
let (head, rest) = self.inner.split_at(len);
self.inner = rest;
Some(head)
},
}
}
}
impl<O, T> ExactSizeIterator for RChunks<'_, O, T>
where O: BitOrder, T: BitStore {}
impl<O, T> FusedIterator for RChunks<'_, O, T>
where O: BitOrder, T: BitStore {}
#[derive(Clone, Debug)]
pub struct RChunksExact<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
pub(super) inner: &'a BitSlice<O, T>,
pub(super) extra: &'a BitSlice<O, T>,
pub(super) width: usize,
}
impl<'a, O, T> RChunksExact<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
pub fn remainder(&self) -> &'a BitSlice<O, T> {
self.extra
}
}
impl<'a, O, T> Iterator for RChunksExact<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
type Item = &'a BitSlice<O, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
match self.inner.len() {
n if n < self.width => {
self.inner = BitSlice::empty();
None
},
n => {
let (rest, tail) = self.inner.split_at(n - self.width);
self.inner = rest;
Some(tail)
},
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.inner.len() / self.width;
(len, Some(len))
}
#[inline]
fn count(self) -> usize {
self.len()
}
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
let len = self.inner.len();
let (end, ovf) = self.width.overflowing_mul(n);
if end >= len || ovf {
self.inner = BitSlice::empty();
return None;
}
self.inner = unsafe { self.inner.get_unchecked(len - end ..) };
self.next()
}
#[inline]
fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}
impl<'a, O, T> DoubleEndedIterator for RChunksExact<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
match self.inner.len() {
n if n < self.width => {
self.inner = BitSlice::empty();
None
},
_ => {
let (head, rest) = self.inner.split_at(self.width);
self.inner = rest;
Some(head)
},
}
}
}
impl<O, T> ExactSizeIterator for RChunksExact<'_, O, T>
where O: BitOrder, T: BitStore {}
impl<O, T> FusedIterator for RChunksExact<'_, O, T>
where O: BitOrder, T: BitStore {}
#[derive(Debug)]
pub struct RChunksExactMut<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
pub(super) inner: &'a mut BitSlice<O, T>,
pub(super) extra: &'a mut BitSlice<O, T>,
pub(super) width: usize,
}
impl<'a, O, T> RChunksExactMut<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
pub fn into_remainder(self) -> &'a mut BitSlice<O, T> {
self.extra
}
}
impl<'a, O, T> Iterator for RChunksExactMut<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
type Item = &'a mut BitSlice<O, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let slice = mem::replace(&mut self.inner, BitSlice::empty_mut());
match slice.len() {
n if n < self.width => None,
n => {
let (rest, tail) = slice.split_at_mut(n - self.width);
self.inner = rest;
Some(tail)
},
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.inner.len() / self.width;
(len, Some(len))
}
#[inline]
fn count(self) -> usize {
self.len()
}
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
let slice = mem::replace(&mut self.inner, BitSlice::empty_mut());
let len = slice.len();
let (end, ovf) = self.width.overflowing_mul(n);
if end >= len || ovf {
return None;
}
self.inner = unsafe { slice.get_unchecked_mut(.. len - end) };
self.next()
}
#[inline]
fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}
impl<'a, O, T> DoubleEndedIterator for RChunksExactMut<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
let slice = mem::replace(&mut self.inner, BitSlice::empty_mut());
match slice.len() {
n if n < self.width => None,
_ => {
let (head, rest) = slice.split_at_mut(self.width);
self.inner = rest;
Some(head)
},
}
}
}
impl<O, T> ExactSizeIterator for RChunksExactMut<'_, O, T>
where O: BitOrder, T: BitStore {}
impl<O, T> FusedIterator for RChunksExactMut<'_, O, T>
where O: BitOrder, T: BitStore {}
#[derive(Debug)]
pub struct RChunksMut<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
pub(super) inner: &'a mut BitSlice<O, T>,
pub(super) width: usize,
}
impl<'a, O, T> Iterator for RChunksMut<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
type Item = &'a mut BitSlice<O, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let slice = mem::replace(&mut self.inner, BitSlice::empty_mut());
match slice.len() {
0 => None,
len => {
let mid = cmp::min(len, self.width);
let (rest, tail) = slice.split_at_mut(len - mid);
self.inner = rest;
Some(tail)
},
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match self.inner.len() {
0 => (0, Some(0)),
n => {
let (len, rem) = (n / self.width, n % self.width);
let len = len + (rem > 0) as usize;
(len, Some(len))
},
}
}
#[inline]
fn count(self) -> usize {
self.len()
}
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
let slice = mem::replace(&mut self.inner, BitSlice::empty_mut());
let len = slice.len();
let (end, ovf) = self.width.overflowing_mul(n);
if end >= len || ovf {
return None;
}
self.inner = unsafe { slice.get_unchecked_mut(.. len - end) };
self.next()
}
#[inline]
fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}
impl<'a, O, T> DoubleEndedIterator for RChunksMut<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
let slice = mem::replace(&mut self.inner, BitSlice::empty_mut());
match slice.len() {
0 => None,
n => {
let rem = n % self.width;
let len = if rem == 0 { self.width } else { rem };
let (head, rest) = slice.split_at_mut(len);
self.inner = rest;
Some(head)
},
}
}
}
impl<O, T> ExactSizeIterator for RChunksMut<'_, O, T>
where O: BitOrder, T: BitStore {}
impl<O, T> FusedIterator for RChunksMut<'_, O, T>
where O: BitOrder, T: BitStore {}
#[doc(hidden)]
pub(super) trait SplitIter: DoubleEndedIterator {
fn finish(&mut self) -> Option<Self::Item>;
}
#[derive(Clone)]
pub struct Split<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
pub(super) inner: &'a BitSlice<O, T>,
pub(super) place: Option<usize>,
pub(super) func: F,
}
impl<'a, O, T, F> Debug for Split<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("Split")
.field("inner", &self.inner)
.field("place", &self.place)
.finish()
}
}
impl<'a, O, T, F> SplitIter for Split<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
#[inline]
fn finish(&mut self) -> Option<&'a BitSlice<O, T>> {
self.place.take().map(|_| self.inner)
}
}
impl<'a, O, T, F> Iterator for Split<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
type Item = &'a BitSlice<O, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let place = self.place?;
match self.inner
.iter()
.zip(place ..)
.position(|(bit, idx)| (self.func)(place + idx, bit))
{
None => self.finish(),
Some(idx) => unsafe {
let out = self.inner.get_unchecked(.. idx);
self.inner = self.inner.get_unchecked(idx + 1 ..);
self.place = Some(place + idx + 1);
Some(out)
},
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match self.place {
None => (0, Some(0)),
Some(_) => (1, Some(self.inner.len() + 1)),
}
}
}
impl<'a, O, T, F> DoubleEndedIterator for Split<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
let place = self.place?;
match self.inner
.iter()
.zip(place .. place + self.inner.len())
.rposition(|(bit, idx)| (self.func)(place + idx, bit))
{
None => self.finish(),
Some(idx) => unsafe {
let out = self.inner.get_unchecked(idx + 1 ..);
self.inner = self.inner.get_unchecked(.. idx);
Some(out)
},
}
}
}
impl<'a, O, T, F> FusedIterator for Split<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {}
pub struct SplitMut<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
pub(super) inner: &'a mut BitSlice<O, T>,
pub(super) place: Option<usize>,
pub(super) func: F,
}
impl<'a, O, T, F> Debug for SplitMut<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
fmt.debug_struct("SplitMut")
.field("inner", &self.inner)
.field("place", &self.place)
.finish()
}
}
impl<'a, O, T, F> SplitIter for SplitMut<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
#[inline]
fn finish(&mut self) -> Option<&'a mut BitSlice<O, T>> {
self.place.take().map(|_| mem::replace(&mut self.inner, BitSlice::empty_mut()))
}
}
impl<'a, O, T, F> Iterator for SplitMut<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
type Item = &'a mut BitSlice<O, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let place = self.place?;
match {
let func = &mut self.func;
self.inner
.iter()
.zip(place ..)
.position(|(bit, idx)| (*func)(place + idx, bit))
} {
None => self.finish(),
Some(idx) => unsafe {
let (out, rest) = mem::replace(
&mut self.inner,
BitSlice::empty_mut(),
).split_at_mut(idx);
self.inner = rest.get_unchecked_mut(1 ..);
self.place = Some(place + idx + 1);
Some(out)
},
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match self.place {
None => (0, Some(0)),
Some(_) => (1, Some(self.inner.len() + 1)),
}
}
}
impl<'a, O, T, F> DoubleEndedIterator for SplitMut<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
let place = self.place?;
match {
let func = &mut self.func;
self.inner
.iter()
.zip(place .. place + self.inner.len())
.rposition(|(bit, idx)| (*func)(place + idx, bit))
} {
None => self.finish(),
Some(idx) => unsafe {
let (rest, out) = mem::replace(
&mut self.inner,
BitSlice::empty_mut(),
).split_at_mut(idx);
self.inner = rest;
self.place = Some(place + idx + 1);
Some(out.get_unchecked_mut(1 ..))
},
}
}
}
impl<'a, O, T, F> FusedIterator for SplitMut<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {}
#[derive(Clone)]
pub struct RSplit<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
pub(super) inner: Split<'a, O, T, F>,
}
impl<'a, O, T, F> Debug for RSplit<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("RSplit")
.field("inner", &self.inner.inner)
.field("place", &self.inner.place)
.finish()
}
}
impl<'a, O, T, F> SplitIter for RSplit<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
fn finish(&mut self) -> Option<Self::Item> {
self.inner.finish()
}
}
impl<'a, O, T, F> Iterator for RSplit<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
type Item = &'a BitSlice<O, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next_back()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl<'a, O, T, F> DoubleEndedIterator for RSplit<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
impl<'a, O, T, F> FusedIterator for RSplit<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {}
pub struct RSplitMut<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
pub(super) inner: SplitMut<'a, O, T, F>,
}
impl<'a, O, T, F> Debug for RSplitMut<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("RSplitMut")
.field("inner", &self.inner.inner)
.field("place", &self.inner.place)
.finish()
}
}
impl<'a, O, T, F> SplitIter for RSplitMut<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
fn finish(&mut self) -> Option<Self::Item> {
self.inner.finish()
}
}
impl<'a, O, T, F> Iterator for RSplitMut<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
type Item = &'a mut BitSlice<O, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next_back()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl<'a, O, T, F> DoubleEndedIterator for RSplitMut<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
impl<'a, O, T, F> FusedIterator for RSplitMut<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {}
pub(super) struct GenericSplitN<I>
where I: SplitIter {
pub(super) inner: I,
pub(super) count: usize,
}
impl<I> Iterator for GenericSplitN<I>
where I: SplitIter {
type Item = I::Item;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
match self.count {
0 => None,
1 => { self.count = 0; self.inner.finish() },
_ => { self.count -= 1; self.inner.next() },
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (floor, ceil) = self.inner.size_hint();
(floor, ceil.map(|c| cmp::min(self.count, c)))
}
}
pub struct SplitN<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
pub(super) inner: GenericSplitN<Split<'a, O, T, F>>,
}
impl<'a, O, T, F> Debug for SplitN<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
fmt.debug_struct("SplitN")
.field("inner", &self.inner.inner)
.field("count", &self.inner.count)
.finish()
}
}
pub struct SplitNMut<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
pub(super) inner: GenericSplitN<SplitMut<'a, O, T, F>>,
}
impl<'a, O, T, F> Debug for SplitNMut<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
fmt.debug_struct("SplitNMut")
.field("inner", &self.inner.inner)
.field("count", &self.inner.count)
.finish()
}
}
pub struct RSplitN<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
pub(super) inner: GenericSplitN<RSplit<'a, O, T, F>>,
}
impl<'a, O, T, F> Debug for RSplitN<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
fmt.debug_struct("RSplitN")
.field("inner", &self.inner.inner)
.field("count", &self.inner.count)
.finish()
}
}
pub struct RSplitNMut<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
pub(super) inner: GenericSplitN<RSplitMut<'a, O, T, F>>,
}
impl<'a, O, T, F> Debug for RSplitNMut<'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
fmt.debug_struct("RSplitNMut")
.field("inner", &self.inner.inner)
.field("count", &self.inner.count)
.finish()
}
}
macro_rules! forward_iterator {
( $name:ident ) => {
impl<'a, O, T, F> Iterator for $name <'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
type Item = &'a BitSlice<O, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl<'a, O, T, F> FusedIterator for $name <'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {}
};
( $name:ident mut ) => {
impl<'a, O, T, F> Iterator for $name <'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {
type Item = &'a mut BitSlice<O, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl<'a, O, T, F> FusedIterator for $name <'a, O, T, F>
where O: BitOrder, T: 'a + BitStore, F: FnMut(usize, &bool) -> bool {}
};
}
forward_iterator!(SplitN);
forward_iterator!(RSplitN);
forward_iterator!(SplitNMut mut);
forward_iterator!(RSplitNMut mut);
#[derive(Clone, Debug)]
pub struct Windows<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
pub(super) inner: &'a BitSlice<O, T>,
pub(super) width: usize,
}
impl<'a, O, T> Iterator for Windows<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
type Item = &'a BitSlice<O, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.width > self.inner.len() {
self.inner = BitSlice::empty();
None
}
else {
unsafe {
let out = self.inner.get_unchecked(.. self.width);
self.inner = self.inner.get_unchecked(1 ..);
Some(out)
}
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.inner.len();
if self.width > len {
(0, Some(0))
}
else {
let count = len - self.width + 1;
(count, Some(count))
}
}
#[inline]
fn count(self) -> usize {
self.len()
}
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
let (end, ovf) = self.width.overflowing_add(n);
if end > self.inner.len() || ovf {
self.inner = BitSlice::empty();
return None;
}
unsafe {
let out = self.inner.get_unchecked(n .. end);
self.inner = self.inner.get_unchecked(n + 1 ..);
Some(out)
}
}
#[inline]
fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}
impl<'a, O, T> DoubleEndedIterator for Windows<'a, O, T>
where O: BitOrder, T: 'a + BitStore {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
let len = self.inner.len();
if !(1 .. self.width).contains(&len) {
self.inner = BitSlice::empty();
return None;
}
unsafe {
let out = self.inner.get_unchecked(len - self.width ..);
self.inner = self.inner.get_unchecked(.. len - 1);
Some(out)
}
}
}
impl<'a, O, T> ExactSizeIterator for Windows<'a, O, T>
where O: BitOrder, T: 'a + BitStore {}
impl<'a, O, T> FusedIterator for Windows<'a, O, T>
where O: BitOrder, T: 'a + BitStore {}