use crate::iter::{Iter, IterMut};
use crate::keys::Keys;
use crate::EntityRef;
use alloc::vec::Vec;
use core::cmp::min;
use core::marker::PhantomData;
use core::ops::{Index, IndexMut};
use core::slice;
#[cfg(feature = "enable-serde")]
use serde::{
de::{Deserializer, SeqAccess, Visitor},
ser::{SerializeSeq, Serializer},
Deserialize, Serialize,
};
#[derive(Debug, Clone, Hash)]
pub struct SecondaryMap<K, V>
where
K: EntityRef,
V: Clone,
{
elems: Vec<V>,
default: V,
unused: PhantomData<K>,
}
impl<K, V> SecondaryMap<K, V>
where
K: EntityRef,
V: Clone,
{
pub fn new() -> Self
where
V: Default,
{
Self {
elems: Vec::new(),
default: Default::default(),
unused: PhantomData,
}
}
pub fn with_capacity(capacity: usize) -> Self
where
V: Default,
{
Self {
elems: Vec::with_capacity(capacity),
default: Default::default(),
unused: PhantomData,
}
}
pub fn with_default(default: V) -> Self {
Self {
elems: Vec::new(),
default,
unused: PhantomData,
}
}
pub fn capacity(&self) -> usize {
self.elems.capacity()
}
#[inline(always)]
pub fn get(&self, k: K) -> Option<&V> {
self.elems.get(k.index())
}
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.elems.is_empty()
}
#[inline(always)]
pub fn clear(&mut self) {
self.elems.clear()
}
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 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 resize(&mut self, n: usize) {
self.elems.resize(n, self.default.clone());
}
#[cold]
fn resize_for_index_mut(&mut self, i: usize) -> &mut V {
self.elems.resize(i + 1, self.default.clone());
&mut self.elems[i]
}
}
impl<K, V> Default for SecondaryMap<K, V>
where
K: EntityRef,
V: Clone + Default,
{
fn default() -> SecondaryMap<K, V> {
SecondaryMap::new()
}
}
impl<K, V> Index<K> for SecondaryMap<K, V>
where
K: EntityRef,
V: Clone,
{
type Output = V;
#[inline(always)]
fn index(&self, k: K) -> &V {
self.elems.get(k.index()).unwrap_or(&self.default)
}
}
impl<K, V> IndexMut<K> for SecondaryMap<K, V>
where
K: EntityRef,
V: Clone,
{
#[inline(always)]
fn index_mut(&mut self, k: K) -> &mut V {
let i = k.index();
if i >= self.elems.len() {
return self.resize_for_index_mut(i);
}
&mut self.elems[i]
}
}
impl<K, V> PartialEq for SecondaryMap<K, V>
where
K: EntityRef,
V: Clone + PartialEq,
{
fn eq(&self, other: &Self) -> bool {
let min_size = min(self.elems.len(), other.elems.len());
self.default == other.default
&& self.elems[..min_size] == other.elems[..min_size]
&& self.elems[min_size..].iter().all(|e| *e == self.default)
&& other.elems[min_size..].iter().all(|e| *e == other.default)
}
}
impl<K, V> Eq for SecondaryMap<K, V>
where
K: EntityRef,
V: Clone + PartialEq + Eq,
{
}
#[cfg(feature = "enable-serde")]
impl<K, V> Serialize for SecondaryMap<K, V>
where
K: EntityRef,
V: Clone + PartialEq + Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut elems_cnt = self.elems.len();
while elems_cnt > 0 && self.elems[elems_cnt - 1] == self.default {
elems_cnt -= 1;
}
let mut seq = serializer.serialize_seq(Some(1 + elems_cnt))?;
seq.serialize_element(&Some(self.default.clone()))?;
for e in self.elems.iter().take(elems_cnt) {
let some_e = Some(e);
seq.serialize_element(if *e == self.default { &None } else { &some_e })?;
}
seq.end()
}
}
#[cfg(feature = "enable-serde")]
impl<'de, K, V> Deserialize<'de> for SecondaryMap<K, V>
where
K: EntityRef,
V: Clone + Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
use alloc::fmt;
struct SecondaryMapVisitor<K, V> {
unused: PhantomData<fn(K) -> V>,
}
impl<'de, K, V> Visitor<'de> for SecondaryMapVisitor<K, V>
where
K: EntityRef,
V: Clone + Deserialize<'de>,
{
type Value = SecondaryMap<K, V>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("struct SecondaryMap")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
match seq.next_element()? {
Some(Some(default_val)) => {
let default_val: V = default_val; let mut m = SecondaryMap::with_default(default_val.clone());
let mut idx = 0;
while let Some(val) = seq.next_element()? {
let val: Option<_> = val; m[K::new(idx)] = val.unwrap_or_else(|| default_val.clone());
idx += 1;
}
Ok(m)
}
_ => Err(serde::de::Error::custom("Default value required")),
}
}
}
deserializer.deserialize_seq(SecondaryMapVisitor {
unused: PhantomData {},
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct E(u32);
impl EntityRef for E {
fn new(i: usize) -> Self {
E(i as u32)
}
fn index(self) -> usize {
self.0 as usize
}
}
#[test]
fn basic() {
let r0 = E(0);
let r1 = E(1);
let r2 = E(2);
let mut m = SecondaryMap::new();
let v: Vec<E> = m.keys().collect();
assert_eq!(v, []);
m[r2] = 3;
m[r1] = 5;
assert_eq!(m[r1], 5);
assert_eq!(m[r2], 3);
let v: Vec<E> = m.keys().collect();
assert_eq!(v, [r0, r1, r2]);
let shared = &m;
assert_eq!(shared[r0], 0);
assert_eq!(shared[r1], 5);
assert_eq!(shared[r2], 3);
}
}