use core::{
cmp::Ordering,
fmt::{self, Debug, Formatter},
hash::BuildHasher,
hash::{Hash, Hasher},
iter::FusedIterator,
marker::PhantomData,
ops::{
BitAnd, BitOr, BitXor, Bound, Deref, DerefMut, Index, Range, RangeBounds, RangeFrom,
RangeFull, RangeInclusive, RangeTo, RangeToInclusive, Sub,
},
ptr,
};
use indexmap::set::{self, IndexSet};
use super::{Entity, EntityHash, EntitySetIterator};
use bevy_platform::prelude::Box;
#[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))]
#[derive(Debug, Clone, Default)]
pub struct EntityIndexSet(pub(crate) IndexSet<Entity, EntityHash>);
impl EntityIndexSet {
pub const fn new() -> Self {
Self(IndexSet::with_hasher(EntityHash))
}
pub fn with_capacity(n: usize) -> Self {
Self(IndexSet::with_capacity_and_hasher(n, EntityHash))
}
pub fn into_inner(self) -> IndexSet<Entity, EntityHash> {
self.0
}
pub fn as_slice(&self) -> &Slice {
unsafe { Slice::from_slice_unchecked(self.0.as_slice()) }
}
pub fn drain<R: RangeBounds<usize>>(&mut self, range: R) -> Drain<'_> {
Drain(self.0.drain(range), PhantomData)
}
pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Slice> {
self.0.get_range(range).map(|slice|
unsafe { Slice::from_slice_unchecked(slice) })
}
pub fn iter(&self) -> Iter<'_> {
Iter(self.0.iter(), PhantomData)
}
pub fn into_boxed_slice(self) -> Box<Slice> {
unsafe { Slice::from_boxed_slice_unchecked(self.0.into_boxed_slice()) }
}
}
impl Deref for EntityIndexSet {
type Target = IndexSet<Entity, EntityHash>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for EntityIndexSet {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<'a> IntoIterator for &'a EntityIndexSet {
type Item = &'a Entity;
type IntoIter = Iter<'a>;
fn into_iter(self) -> Self::IntoIter {
Iter((&self.0).into_iter(), PhantomData)
}
}
impl IntoIterator for EntityIndexSet {
type Item = Entity;
type IntoIter = IntoIter;
fn into_iter(self) -> Self::IntoIter {
IntoIter(self.0.into_iter(), PhantomData)
}
}
impl BitAnd for &EntityIndexSet {
type Output = EntityIndexSet;
fn bitand(self, rhs: Self) -> Self::Output {
EntityIndexSet(self.0.bitand(&rhs.0))
}
}
impl BitOr for &EntityIndexSet {
type Output = EntityIndexSet;
fn bitor(self, rhs: Self) -> Self::Output {
EntityIndexSet(self.0.bitor(&rhs.0))
}
}
impl BitXor for &EntityIndexSet {
type Output = EntityIndexSet;
fn bitxor(self, rhs: Self) -> Self::Output {
EntityIndexSet(self.0.bitxor(&rhs.0))
}
}
impl Sub for &EntityIndexSet {
type Output = EntityIndexSet;
fn sub(self, rhs: Self) -> Self::Output {
EntityIndexSet(self.0.sub(&rhs.0))
}
}
impl<'a> Extend<&'a Entity> for EntityIndexSet {
fn extend<T: IntoIterator<Item = &'a Entity>>(&mut self, iter: T) {
self.0.extend(iter);
}
}
impl Extend<Entity> for EntityIndexSet {
fn extend<T: IntoIterator<Item = Entity>>(&mut self, iter: T) {
self.0.extend(iter);
}
}
impl<const N: usize> From<[Entity; N]> for EntityIndexSet {
fn from(value: [Entity; N]) -> Self {
Self(IndexSet::from_iter(value))
}
}
impl FromIterator<Entity> for EntityIndexSet {
fn from_iter<I: IntoIterator<Item = Entity>>(iterable: I) -> Self {
Self(IndexSet::from_iter(iterable))
}
}
impl<S2> PartialEq<IndexSet<Entity, S2>> for EntityIndexSet
where
S2: BuildHasher,
{
fn eq(&self, other: &IndexSet<Entity, S2>) -> bool {
self.0.eq(other)
}
}
impl PartialEq for EntityIndexSet {
fn eq(&self, other: &EntityIndexSet) -> bool {
self.0.eq(other)
}
}
impl Eq for EntityIndexSet {}
impl Index<(Bound<usize>, Bound<usize>)> for EntityIndexSet {
type Output = Slice;
fn index(&self, key: (Bound<usize>, Bound<usize>)) -> &Self::Output {
unsafe { Slice::from_slice_unchecked(self.0.index(key)) }
}
}
impl Index<Range<usize>> for EntityIndexSet {
type Output = Slice;
fn index(&self, key: Range<usize>) -> &Self::Output {
unsafe { Slice::from_slice_unchecked(self.0.index(key)) }
}
}
impl Index<RangeFrom<usize>> for EntityIndexSet {
type Output = Slice;
fn index(&self, key: RangeFrom<usize>) -> &Self::Output {
unsafe { Slice::from_slice_unchecked(self.0.index(key)) }
}
}
impl Index<RangeFull> for EntityIndexSet {
type Output = Slice;
fn index(&self, key: RangeFull) -> &Self::Output {
unsafe { Slice::from_slice_unchecked(self.0.index(key)) }
}
}
impl Index<RangeInclusive<usize>> for EntityIndexSet {
type Output = Slice;
fn index(&self, key: RangeInclusive<usize>) -> &Self::Output {
unsafe { Slice::from_slice_unchecked(self.0.index(key)) }
}
}
impl Index<RangeTo<usize>> for EntityIndexSet {
type Output = Slice;
fn index(&self, key: RangeTo<usize>) -> &Self::Output {
unsafe { Slice::from_slice_unchecked(self.0.index(key)) }
}
}
impl Index<RangeToInclusive<usize>> for EntityIndexSet {
type Output = Slice;
fn index(&self, key: RangeToInclusive<usize>) -> &Self::Output {
unsafe { Slice::from_slice_unchecked(self.0.index(key)) }
}
}
impl Index<usize> for EntityIndexSet {
type Output = Entity;
fn index(&self, key: usize) -> &Entity {
self.0.index(key)
}
}
#[repr(transparent)]
pub struct Slice<S = EntityHash>(PhantomData<S>, set::Slice<Entity>);
impl Slice {
pub const fn new<'a>() -> &'a Self {
unsafe { Self::from_slice_unchecked(set::Slice::new()) }
}
pub const unsafe fn from_slice_unchecked(slice: &set::Slice<Entity>) -> &Self {
unsafe { &*(ptr::from_ref(slice) as *const Self) }
}
pub const unsafe fn from_slice_unchecked_mut(slice: &mut set::Slice<Entity>) -> &mut Self {
unsafe { &mut *(ptr::from_mut(slice) as *mut Self) }
}
pub const fn as_inner(&self) -> &set::Slice<Entity> {
&self.1
}
pub unsafe fn from_boxed_slice_unchecked(slice: Box<set::Slice<Entity>>) -> Box<Self> {
unsafe { Box::from_raw(Box::into_raw(slice) as *mut Self) }
}
#[expect(
clippy::borrowed_box,
reason = "We wish to access the Box API of the inner type, without consuming it."
)]
pub fn as_boxed_inner(self: &Box<Self>) -> &Box<set::Slice<Entity>> {
unsafe { &*(ptr::from_ref(self).cast::<Box<set::Slice<Entity>>>()) }
}
pub fn into_boxed_inner(self: Box<Self>) -> Box<set::Slice<Entity>> {
unsafe { Box::from_raw(Box::into_raw(self) as *mut set::Slice<Entity>) }
}
pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Self> {
self.1.get_range(range).map(|slice|
unsafe { Self::from_slice_unchecked(slice) })
}
pub fn split_at(&self, index: usize) -> (&Self, &Self) {
let (slice_1, slice_2) = self.1.split_at(index);
unsafe {
(
Self::from_slice_unchecked(slice_1),
Self::from_slice_unchecked(slice_2),
)
}
}
pub fn split_first(&self) -> Option<(&Entity, &Self)> {
self.1.split_first().map(|(first, rest)| {
(
first,
unsafe { Self::from_slice_unchecked(rest) },
)
})
}
pub fn split_last(&self) -> Option<(&Entity, &Self)> {
self.1.split_last().map(|(last, rest)| {
(
last,
unsafe { Self::from_slice_unchecked(rest) },
)
})
}
pub fn iter(&self) -> Iter<'_> {
Iter(self.1.iter(), PhantomData)
}
}
impl Deref for Slice {
type Target = set::Slice<Entity>;
fn deref(&self) -> &Self::Target {
&self.1
}
}
impl<'a> IntoIterator for &'a Slice {
type IntoIter = Iter<'a>;
type Item = &'a Entity;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl IntoIterator for Box<Slice> {
type IntoIter = IntoIter;
type Item = Entity;
fn into_iter(self) -> Self::IntoIter {
IntoIter(self.into_boxed_inner().into_iter(), PhantomData)
}
}
impl Clone for Box<Slice> {
fn clone(&self) -> Self {
unsafe { Slice::from_boxed_slice_unchecked(self.as_boxed_inner().clone()) }
}
}
impl Default for &Slice {
fn default() -> Self {
unsafe { Slice::from_slice_unchecked(<&set::Slice<Entity>>::default()) }
}
}
impl Default for Box<Slice> {
fn default() -> Self {
unsafe { Slice::from_boxed_slice_unchecked(<Box<set::Slice<Entity>>>::default()) }
}
}
impl<V: Debug> Debug for Slice<V> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_tuple("Slice")
.field(&self.0)
.field(&&self.1)
.finish()
}
}
impl From<&Slice> for Box<Slice> {
fn from(value: &Slice) -> Self {
unsafe { Slice::from_boxed_slice_unchecked(value.1.into()) }
}
}
impl Hash for Slice {
fn hash<H: Hasher>(&self, state: &mut H) {
self.1.hash(state);
}
}
impl PartialOrd for Slice {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Slice {
fn cmp(&self, other: &Self) -> Ordering {
self.1.cmp(other)
}
}
impl PartialEq for Slice {
fn eq(&self, other: &Self) -> bool {
self.1 == other.1
}
}
impl Eq for Slice {}
impl Index<(Bound<usize>, Bound<usize>)> for Slice {
type Output = Self;
fn index(&self, key: (Bound<usize>, Bound<usize>)) -> &Self {
unsafe { Self::from_slice_unchecked(self.1.index(key)) }
}
}
impl Index<Range<usize>> for Slice {
type Output = Self;
fn index(&self, key: Range<usize>) -> &Self {
unsafe { Self::from_slice_unchecked(self.1.index(key)) }
}
}
impl Index<RangeFrom<usize>> for Slice {
type Output = Slice;
fn index(&self, key: RangeFrom<usize>) -> &Self {
unsafe { Self::from_slice_unchecked(self.1.index(key)) }
}
}
impl Index<RangeFull> for Slice {
type Output = Self;
fn index(&self, key: RangeFull) -> &Self {
unsafe { Self::from_slice_unchecked(self.1.index(key)) }
}
}
impl Index<RangeInclusive<usize>> for Slice {
type Output = Self;
fn index(&self, key: RangeInclusive<usize>) -> &Self {
unsafe { Self::from_slice_unchecked(self.1.index(key)) }
}
}
impl Index<RangeTo<usize>> for Slice {
type Output = Self;
fn index(&self, key: RangeTo<usize>) -> &Self {
unsafe { Self::from_slice_unchecked(self.1.index(key)) }
}
}
impl Index<RangeToInclusive<usize>> for Slice {
type Output = Self;
fn index(&self, key: RangeToInclusive<usize>) -> &Self {
unsafe { Self::from_slice_unchecked(self.1.index(key)) }
}
}
impl Index<usize> for Slice {
type Output = Entity;
fn index(&self, key: usize) -> &Entity {
self.1.index(key)
}
}
pub struct Iter<'a, S = EntityHash>(set::Iter<'a, Entity>, PhantomData<S>);
impl<'a> Iter<'a> {
pub fn into_inner(self) -> set::Iter<'a, Entity> {
self.0
}
pub fn as_slice(&self) -> &Slice {
unsafe { Slice::from_slice_unchecked(self.0.as_slice()) }
}
}
impl<'a> Deref for Iter<'a> {
type Target = set::Iter<'a, Entity>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a> Iterator for Iter<'a> {
type Item = &'a Entity;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
impl DoubleEndedIterator for Iter<'_> {
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back()
}
}
impl ExactSizeIterator for Iter<'_> {}
impl FusedIterator for Iter<'_> {}
impl Clone for Iter<'_> {
fn clone(&self) -> Self {
Self(self.0.clone(), PhantomData)
}
}
impl Debug for Iter<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_tuple("Iter").field(&self.0).field(&self.1).finish()
}
}
impl Default for Iter<'_> {
fn default() -> Self {
Self(Default::default(), PhantomData)
}
}
unsafe impl EntitySetIterator for Iter<'_> {}
pub struct IntoIter<S = EntityHash>(set::IntoIter<Entity>, PhantomData<S>);
impl IntoIter {
pub fn into_inner(self) -> set::IntoIter<Entity> {
self.0
}
pub fn as_slice(&self) -> &Slice {
unsafe { Slice::from_slice_unchecked(self.0.as_slice()) }
}
}
impl Deref for IntoIter {
type Target = set::IntoIter<Entity>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Iterator for IntoIter {
type Item = Entity;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
impl DoubleEndedIterator for IntoIter {
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back()
}
}
impl ExactSizeIterator for IntoIter {}
impl FusedIterator for IntoIter {}
impl Clone for IntoIter {
fn clone(&self) -> Self {
Self(self.0.clone(), PhantomData)
}
}
impl Debug for IntoIter {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_tuple("IntoIter")
.field(&self.0)
.field(&self.1)
.finish()
}
}
impl Default for IntoIter {
fn default() -> Self {
Self(Default::default(), PhantomData)
}
}
unsafe impl EntitySetIterator for IntoIter {}
pub struct Drain<'a, S = EntityHash>(set::Drain<'a, Entity>, PhantomData<S>);
impl<'a> Drain<'a> {
pub fn into_inner(self) -> set::Drain<'a, Entity> {
self.0
}
pub fn as_slice(&self) -> &Slice {
unsafe { Slice::from_slice_unchecked(self.0.as_slice()) }
}
}
impl<'a> Deref for Drain<'a> {
type Target = set::Drain<'a, Entity>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a> Iterator for Drain<'a> {
type Item = Entity;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
impl DoubleEndedIterator for Drain<'_> {
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back()
}
}
impl ExactSizeIterator for Drain<'_> {}
impl FusedIterator for Drain<'_> {}
impl Debug for Drain<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_tuple("Drain")
.field(&self.0)
.field(&self.1)
.finish()
}
}
unsafe impl EntitySetIterator for Drain<'_> {}
unsafe impl EntitySetIterator for set::Difference<'_, Entity, EntityHash> {}
unsafe impl EntitySetIterator for set::Intersection<'_, Entity, EntityHash> {}
unsafe impl EntitySetIterator for set::SymmetricDifference<'_, Entity, EntityHash, EntityHash> {}
unsafe impl EntitySetIterator for set::Union<'_, Entity, EntityHash> {}
unsafe impl<I: Iterator<Item = Entity>> EntitySetIterator
for set::Splice<'_, I, Entity, EntityHash>
{
}