#![allow(clippy::pedantic, clippy::all)]
extern crate alloc;
use alloc::vec::Vec;
use core::fmt;
use core::marker::PhantomData;
use core::ops::{Index, IndexMut};
use core::slice;
use alloc::vec;
use core::iter::Enumerate;
pub trait EntityRef: Copy + Eq {
fn new(_: usize) -> Self;
fn index(self) -> usize;
}
#[macro_export]
macro_rules! entity_impl {
($entity:ident) => {
impl $crate::EntityRef for $entity {
#[inline]
fn new(index: usize) -> Self {
debug_assert!(index < (u32::MAX as usize));
$entity(index as u32)
}
#[inline]
fn index(self) -> usize {
self.0 as usize
}
}
impl $entity {
#[allow(dead_code, reason = "macro-generated code")]
#[inline]
pub fn from_u32(x: u32) -> Self {
debug_assert!(x < u32::MAX);
$entity(x)
}
#[allow(dead_code, reason = "macro-generated code")]
#[inline]
pub fn as_u32(self) -> u32 {
self.0
}
#[allow(dead_code, reason = "macro-generated code")]
#[inline]
pub fn as_bits(self) -> u32 {
self.0
}
#[allow(dead_code, reason = "macro-generated code")]
#[inline]
pub fn from_bits(x: u32) -> Self {
$entity(x)
}
}
};
($entity:ident, $display_prefix:expr) => {
$crate::entity_impl!($entity);
impl core::fmt::Display for $entity {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, concat!($display_prefix, "{}"), self.0)
}
}
impl core::fmt::Debug for $entity {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
(self as &dyn core::fmt::Display).fmt(f)
}
}
};
($entity:ident, $display_prefix:expr, $arg:ident, $to_expr:expr, $from_expr:expr) => {
impl $crate::EntityRef for $entity {
#[inline]
fn new(index: usize) -> Self {
debug_assert!(index < (u32::MAX as usize));
let $arg = index as u32;
$to_expr
}
#[inline]
fn index(self) -> usize {
let $arg = self;
$from_expr as usize
}
}
impl $crate::packed_option::ReservedValue for $entity {
#[inline]
fn reserved_value() -> $entity {
$entity::from_u32(u32::MAX)
}
#[inline]
fn is_reserved_value(&self) -> bool {
self.as_u32() == u32::MAX
}
}
impl $entity {
#[allow(dead_code, reason = "macro-generated code")]
#[inline]
pub fn from_u32(x: u32) -> Self {
debug_assert!(x < u32::MAX);
let $arg = x;
$to_expr
}
#[allow(dead_code, reason = "macro-generated code")]
#[inline]
pub fn as_u32(self) -> u32 {
let $arg = self;
$from_expr
}
}
impl core::fmt::Display for $entity {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, concat!($display_prefix, "{}"), self.as_u32())
}
}
impl core::fmt::Debug for $entity {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
(self as &dyn core::fmt::Display).fmt(f)
}
}
};
}
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct PrimaryMap<K, V>
where
K: EntityRef,
{
elems: Vec<V>,
unused: PhantomData<K>,
}
impl<K, V> PrimaryMap<K, V>
where
K: EntityRef,
{
pub fn new() -> Self {
Self {
elems: Vec::new(),
unused: PhantomData,
}
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
elems: Vec::with_capacity(capacity),
unused: PhantomData,
}
}
pub fn is_valid(&self, k: K) -> bool {
k.index() < self.elems.len()
}
pub fn get(&self, k: K) -> Option<&V> {
self.elems.get(k.index())
}
pub fn get_range(&self, range: core::ops::Range<K>) -> Option<&[V]> {
self.elems.get(range.start.index()..range.end.index())
}
pub fn get_mut(&mut self, k: K) -> Option<&mut V> {
self.elems.get_mut(k.index())
}
pub fn is_empty(&self) -> bool {
self.elems.is_empty()
}
pub fn len(&self) -> usize {
self.elems.len()
}
pub fn keys(&self) -> Keys<K> {
Keys::with_len(self.elems.len())
}
pub fn values(&self) -> slice::Iter<'_, V> {
self.elems.iter()
}
pub fn values_mut(&mut self) -> slice::IterMut<'_, V> {
self.elems.iter_mut()
}
pub fn as_values_slice(&self) -> &[V] {
&self.elems
}
pub fn iter(&self) -> Iter<'_, K, V> {
Iter::new(self.elems.iter())
}
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
IterMut::new(self.elems.iter_mut())
}
pub fn clear(&mut self) {
self.elems.clear()
}
pub fn next_key(&self) -> K {
K::new(self.elems.len())
}
pub fn push(&mut self, v: V) -> K {
let k = self.next_key();
self.elems.push(v);
k
}
pub fn last(&self) -> Option<(K, &V)> {
let len = self.elems.len();
let last = self.elems.last()?;
Some((K::new(len - 1), last))
}
pub fn last_mut(&mut self) -> Option<(K, &mut V)> {
let len = self.elems.len();
let last = self.elems.last_mut()?;
Some((K::new(len - 1), last))
}
pub fn reserve(&mut self, additional: usize) {
self.elems.reserve(additional)
}
pub fn reserve_exact(&mut self, additional: usize) {
self.elems.reserve_exact(additional)
}
pub fn shrink_to_fit(&mut self) {
self.elems.shrink_to_fit()
}
pub fn get_disjoint_mut<const N: usize>(
&mut self,
indices: [K; N],
) -> Result<[&mut V; N], slice::GetDisjointMutError> {
self.elems.get_disjoint_mut(indices.map(|k| k.index()))
}
pub fn binary_search_values_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<K, K>
where
F: FnMut(&'a V) -> B,
B: Ord,
{
self.elems
.binary_search_by_key(b, f)
.map(|i| K::new(i))
.map_err(|i| K::new(i))
}
pub fn get_raw_mut(&mut self, k: K) -> Option<*mut V> {
if k.index() < self.elems.len() {
unsafe { Some(self.elems.as_mut_ptr().add(k.index())) }
} else {
None
}
}
}
impl<K, V> Default for PrimaryMap<K, V>
where
K: EntityRef,
{
fn default() -> PrimaryMap<K, V> {
PrimaryMap::new()
}
}
impl<K, V> Index<K> for PrimaryMap<K, V>
where
K: EntityRef,
{
type Output = V;
fn index(&self, k: K) -> &V {
&self.elems[k.index()]
}
}
impl<K, V> IndexMut<K> for PrimaryMap<K, V>
where
K: EntityRef,
{
fn index_mut(&mut self, k: K) -> &mut V {
&mut self.elems[k.index()]
}
}
impl<K, V> IntoIterator for PrimaryMap<K, V>
where
K: EntityRef,
{
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
fn into_iter(self) -> Self::IntoIter {
IntoIter::new(self.elems.into_iter())
}
}
impl<'a, K, V> IntoIterator for &'a PrimaryMap<K, V>
where
K: EntityRef,
{
type Item = (K, &'a V);
type IntoIter = Iter<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
Iter::new(self.elems.iter())
}
}
impl<'a, K, V> IntoIterator for &'a mut PrimaryMap<K, V>
where
K: EntityRef,
{
type Item = (K, &'a mut V);
type IntoIter = IterMut<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
IterMut::new(self.elems.iter_mut())
}
}
impl<K, V> FromIterator<V> for PrimaryMap<K, V>
where
K: EntityRef,
{
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = V>,
{
Self {
elems: Vec::from_iter(iter),
unused: PhantomData,
}
}
}
impl<K, V> Extend<V> for PrimaryMap<K, V>
where
K: EntityRef,
{
fn extend<T>(&mut self, iter: T)
where
T: IntoIterator<Item = V>,
{
self.elems.extend(iter);
}
}
impl<K, V> From<Vec<V>> for PrimaryMap<K, V>
where
K: EntityRef,
{
fn from(elems: Vec<V>) -> Self {
Self {
elems,
unused: PhantomData,
}
}
}
impl<K, V> From<PrimaryMap<K, V>> for Vec<V>
where
K: EntityRef,
{
fn from(map: PrimaryMap<K, V>) -> Self {
map.elems
}
}
impl<K: EntityRef + fmt::Debug, V: fmt::Debug> fmt::Debug for PrimaryMap<K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut struct_ = f.debug_struct("PrimaryMap");
for (k, v) in self {
struct_.field(&alloc::format!("{k:?}"), v);
}
struct_.finish()
}
}
pub struct Keys<K: EntityRef> {
pos: usize,
rev_pos: usize,
unused: PhantomData<K>,
}
impl<K: EntityRef> Keys<K> {
pub fn with_len(len: usize) -> Self {
Self {
pos: 0,
rev_pos: len,
unused: PhantomData,
}
}
}
impl<K: EntityRef> Iterator for Keys<K> {
type Item = K;
fn next(&mut self) -> Option<Self::Item> {
if self.pos < self.rev_pos {
let k = K::new(self.pos);
self.pos += 1;
Some(k)
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let size = self.rev_pos - self.pos;
(size, Some(size))
}
}
impl<K: EntityRef> DoubleEndedIterator for Keys<K> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.rev_pos > self.pos {
let k = K::new(self.rev_pos - 1);
self.rev_pos -= 1;
Some(k)
} else {
None
}
}
}
impl<K: EntityRef> ExactSizeIterator for Keys<K> {}
pub struct Iter<'a, K: EntityRef, V>
where
V: 'a,
{
enumerate: Enumerate<slice::Iter<'a, V>>,
unused: PhantomData<K>,
}
impl<'a, K: EntityRef, V> Iter<'a, K, V> {
pub fn new(iter: slice::Iter<'a, V>) -> Self {
Self {
enumerate: iter.enumerate(),
unused: PhantomData,
}
}
}
impl<'a, K: EntityRef, V> Iterator for Iter<'a, K, V> {
type Item = (K, &'a V);
fn next(&mut self) -> Option<Self::Item> {
self.enumerate.next().map(|(i, v)| (K::new(i), v))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.enumerate.size_hint()
}
}
impl<'a, K: EntityRef, V> DoubleEndedIterator for Iter<'a, K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
self.enumerate.next_back().map(|(i, v)| (K::new(i), v))
}
}
impl<'a, K: EntityRef, V> ExactSizeIterator for Iter<'a, K, V> {}
pub struct IterMut<'a, K: EntityRef, V>
where
V: 'a,
{
enumerate: Enumerate<slice::IterMut<'a, V>>,
unused: PhantomData<K>,
}
impl<'a, K: EntityRef, V> IterMut<'a, K, V> {
pub fn new(iter: slice::IterMut<'a, V>) -> Self {
Self {
enumerate: iter.enumerate(),
unused: PhantomData,
}
}
}
impl<'a, K: EntityRef, V> Iterator for IterMut<'a, K, V> {
type Item = (K, &'a mut V);
fn next(&mut self) -> Option<Self::Item> {
self.enumerate.next().map(|(i, v)| (K::new(i), v))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.enumerate.size_hint()
}
}
impl<'a, K: EntityRef, V> DoubleEndedIterator for IterMut<'a, K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
self.enumerate.next_back().map(|(i, v)| (K::new(i), v))
}
}
impl<'a, K: EntityRef, V> ExactSizeIterator for IterMut<'a, K, V> {}
pub struct IntoIter<K: EntityRef, V> {
enumerate: Enumerate<vec::IntoIter<V>>,
unused: PhantomData<K>,
}
impl<K: EntityRef, V> IntoIter<K, V> {
pub fn new(iter: vec::IntoIter<V>) -> Self {
Self {
enumerate: iter.enumerate(),
unused: PhantomData,
}
}
}
impl<K: EntityRef, V> Iterator for IntoIter<K, V> {
type Item = (K, V);
fn next(&mut self) -> Option<Self::Item> {
self.enumerate.next().map(|(i, v)| (K::new(i), v))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.enumerate.size_hint()
}
}
impl<K: EntityRef, V> DoubleEndedIterator for IntoIter<K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
self.enumerate.next_back().map(|(i, v)| (K::new(i), v))
}
}
impl<K: EntityRef, V> ExactSizeIterator for IntoIter<K, V> {}