polylane 0.13.0

Portable and versatile SIMD.
Documentation
// Copyright 2025 Gabriel Bjørnager Jensen.
//
// This Source Code Form is subject to the terms of
// the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you
// can obtain one at:
// <https://mozilla.org/MPL/2.0/>.

use crate::mask::Mask;
use crate::simd::{Layout, ValidLayout};

use core::iter::FusedIterator;

/// An owning iterator of a SIMD mask.
#[must_use]
#[derive(Clone, Copy, Debug)]
pub struct IntoIter<const N: usize>
where
	Layout<(), N>: ValidLayout,
{
	data:  Mask<N>,
	len:   usize,
	index: usize,
}

impl<const N: usize> IntoIter<N>
where
	Layout<(), N>:  ValidLayout,
{
	/// Constructs a new, owning iterator of a SIMD mask.
	#[inline]
	pub(crate) const fn new(mask: Mask<N>) -> Self {
		Self {
			data:  mask,
			len:   N,
			index: 0,
		}
	}
}

impl<const N: usize> DoubleEndedIterator for IntoIter<N>
where
	Layout<(), N>:  ValidLayout,
{
	#[inline]
	fn next_back(&mut self) -> Option<Self::Item> {
		if self.len == 0 {
			return None;
		}

		let index = self.index + self.len - 1;
		self.len -= 1;

		let value = unsafe { self.data.test_unchecked(index) };
		Some(value)
	}
}

impl<const N: usize> ExactSizeIterator for IntoIter<N>
where
	Layout<(), N>:  ValidLayout,
{}

impl<const N: usize> FusedIterator for IntoIter<N>
where
	Layout<(), N>:  ValidLayout,
{}

impl<const N: usize> Iterator for IntoIter<N>
where
	Layout<(), N>:  ValidLayout,
{
	type Item = bool;

	#[inline]
	fn next(&mut self) -> Option<Self::Item> {
		if self.len == 0 {
			return None;
		}

		let index = self.index;

		self.len   -= 1;
		self.index += 1;

		let value = unsafe { self.data.test_unchecked(index) };
		Some(value)
	}

	#[inline]
	fn size_hint(&self) -> (usize, Option<usize>) {
		(self.len, Some(self.len))
	}
}