use std::{fmt::Debug, iter::FusedIterator, ops::Index};
use num_traits::NumCast;
use crate::Integer;
#[derive(PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct VecMap<const LOWER: usize, const UPPER: usize, K: Integer, V> {
data: Vec<Option<V>>,
len: usize,
lower_cast: K,
upper_cast: K,
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V: Debug> Debug
for VecMap<LOWER, UPPER, K, V>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_set().entries(self.iter()).finish()
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V: Clone> Clone
for VecMap<LOWER, UPPER, K, V>
{
fn clone(&self) -> Self {
Self {
data: self.data.clone(),
len: self.len,
lower_cast: self.lower_cast,
upper_cast: self.upper_cast,
}
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V: Clone> Default
for VecMap<LOWER, UPPER, K, V>
{
fn default() -> Self {
Self {
data: vec![None; (UPPER - LOWER) + 1],
len: 0,
lower_cast: if let Some(lower_cast) = NumCast::from(LOWER) {
lower_cast
} else {
panic!(
"Unable to cast LOWER bound of BitSet<{}, {}> into \
associated type",
LOWER, UPPER,
)
},
upper_cast: if let Some(upper_cast) = NumCast::from(UPPER) {
upper_cast
} else {
panic!(
"Unable to cast UPPER bound of BitSet<{}, {}> into \
associated type",
LOWER, UPPER,
)
},
}
}
}
macro_rules! bounds_check {
($k:expr, $self:expr) => {
assert!(
$k >= $self.lower_cast && $k <= $self.upper_cast,
"Out of bounds: VecMap<{}, {}> can never contain {:?}",
LOWER,
UPPER,
$k
);
};
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V: Clone> VecMap<LOWER, UPPER, K, V> {
pub fn new() -> Self {
Self::default()
}
pub fn clear(&mut self) {
self.data.fill(None);
self.len = 0;
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> VecMap<LOWER, UPPER, K, V> {
pub fn len(&self) -> usize {
debug_assert_eq!(self.data.iter().filter(|x| x.is_some()).count(), self.len);
self.len
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
fn position(x: K) -> usize {
<usize as NumCast>::from(x).unwrap() - LOWER
}
pub fn contains_key(&self, x: &K) -> bool {
let x = *x;
unsafe {
x >= NumCast::from(LOWER).unwrap()
&& x <= NumCast::from(UPPER).unwrap()
&& self.contains_key_unsafe(x)
}
}
unsafe fn contains_key_unsafe(&self, x: K) -> bool {
self.get_unchecked(x).is_some()
}
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
bounds_check!(k, self);
unsafe {
if let old_v @ Some(_) = self.get_unchecked_mut(k).replace(v) {
old_v
} else {
self.len += 1;
None
}
}
}
pub fn remove(&mut self, k: &K) -> Option<V> {
let k = *k;
bounds_check!(k, self);
unsafe {
if let old_v @ Some(_) = self.get_unchecked_mut(k).take() {
self.len -= 1;
old_v
} else {
None
}
}
}
pub fn remove_entry(&mut self, k: &K) -> Option<(K, V)> {
let k = *k;
bounds_check!(k, self);
unsafe {
if let Some(old_v) = self.get_unchecked_mut(k).take() {
self.len -= 1;
Some((k, old_v))
} else {
None
}
}
}
pub fn keys(&self) -> Keys<'_, LOWER, UPPER, K, V> {
Keys::new(self)
}
pub fn into_keys(self) -> IntoKeys<LOWER, UPPER, K, V> {
IntoKeys::new(self)
}
pub fn values(&self) -> Values<'_, LOWER, UPPER, K, V> {
Values::new(self)
}
pub fn values_mut(&mut self) -> ValuesMut<'_, LOWER, UPPER, K, V> {
ValuesMut::new(self)
}
pub fn into_values(self) -> IntoValues<LOWER, UPPER, K, V> {
IntoValues::new(self)
}
pub fn iter(&self) -> Iter<'_, LOWER, UPPER, K, V> {
Iter::new(self)
}
pub fn iter_mut(&mut self) -> IterMut<'_, LOWER, UPPER, K, V> {
IterMut::new(self)
}
pub fn drain(&mut self) -> Drain<'_, LOWER, UPPER, K, V> {
Drain::new(self)
}
pub fn retain<F>(&mut self, f: F)
where
F: Fn(&K, &mut V) -> bool,
{
for i in 0..self.data.len() {
unsafe {
if let Some(v) = self.data.get_unchecked_mut(i) {
if !f(&NumCast::from(i + LOWER).unwrap(), v) {
self.data.get_unchecked_mut(i).take();
self.len -= 1;
}
}
}
}
}
pub fn get(&self, k: &K) -> Option<&V> {
let k = *k;
bounds_check!(k, self);
unsafe { self.get_unchecked(k) }
}
unsafe fn get_unchecked(&self, x: K) -> Option<&V> {
self.data.get_unchecked(Self::position(x)).as_ref()
}
unsafe fn get_unchecked_mut(&mut self, x: K) -> &mut Option<V> {
self.data.get_unchecked_mut(Self::position(x))
}
pub fn get_key_value(&self, k: &K) -> Option<(K, &V)> {
self.get(k).map(|value| (*k, value))
}
pub fn get_mut(&mut self, k: &K) -> Option<&mut V> {
let k = *k;
bounds_check!(k, self);
unsafe { self.get_unchecked_mut(k).as_mut() }
}
}
#[derive(Debug, Clone, Copy)]
pub struct Keys<'a, const LOWER: usize, const UPPER: usize, K: Integer, V> {
inner: Iter<'a, LOWER, UPPER, K, V>,
}
impl<'a, const LOWER: usize, const UPPER: usize, K: Integer, V> Keys<'a, LOWER, UPPER, K, V> {
fn new(collection: &'a VecMap<LOWER, UPPER, K, V>) -> Self {
Self {
inner: Iter::new(collection),
}
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> Iterator
for Keys<'_, LOWER, UPPER, K, V>
{
type Item = K;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(k, _)| k)
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.inner.collection.len, Some(UPPER - LOWER))
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> ExactSizeIterator
for Keys<'_, LOWER, UPPER, K, V>
{
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> FusedIterator
for Keys<'_, LOWER, UPPER, K, V>
{
}
#[derive(Debug, Clone)]
pub struct IntoKeys<const LOWER: usize, const UPPER: usize, K: Integer, V> {
inner: IntoIter<LOWER, UPPER, K, V>,
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> IntoKeys<LOWER, UPPER, K, V> {
fn new(collection: VecMap<LOWER, UPPER, K, V>) -> Self {
Self {
inner: IntoIter::new(collection),
}
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> Iterator
for IntoKeys<LOWER, UPPER, K, V>
{
type Item = K;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(k, _)| k)
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.inner.collection.len, Some(UPPER - LOWER))
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> ExactSizeIterator
for IntoKeys<LOWER, UPPER, K, V>
{
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> FusedIterator
for IntoKeys<LOWER, UPPER, K, V>
{
}
#[derive(Debug, Clone, Copy)]
pub struct Values<'a, const LOWER: usize, const UPPER: usize, K: Integer, V> {
inner: Iter<'a, LOWER, UPPER, K, V>,
}
impl<'a, const LOWER: usize, const UPPER: usize, K: Integer, V> Values<'a, LOWER, UPPER, K, V> {
fn new(collection: &'a VecMap<LOWER, UPPER, K, V>) -> Self {
Self {
inner: Iter::new(collection),
}
}
}
impl<'a, const LOWER: usize, const UPPER: usize, K: Integer, V> Iterator
for Values<'a, LOWER, UPPER, K, V>
{
type Item = &'a V;
fn next(&mut self) -> Option<Self::Item> {
match self.inner.next() {
Some((_, v)) => Some(v),
None => None,
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.inner.collection.len, Some(UPPER - LOWER))
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> ExactSizeIterator
for Values<'_, LOWER, UPPER, K, V>
{
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> FusedIterator
for Values<'_, LOWER, UPPER, K, V>
{
}
#[derive(Debug)]
pub struct ValuesMut<'a, const LOWER: usize, const UPPER: usize, K: Integer, V> {
inner: IterMut<'a, LOWER, UPPER, K, V>,
}
impl<'a, const LOWER: usize, const UPPER: usize, K: Integer, V> ValuesMut<'a, LOWER, UPPER, K, V> {
fn new(collection: &'a mut VecMap<LOWER, UPPER, K, V>) -> Self {
Self {
inner: IterMut::new(collection),
}
}
}
impl<'a, const LOWER: usize, const UPPER: usize, K: Integer, V> Iterator
for ValuesMut<'a, LOWER, UPPER, K, V>
{
type Item = &'a mut V;
fn next(&mut self) -> Option<Self::Item> {
if let Some((_, v)) = self.inner.next() {
Some(v)
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.inner.collection.len, Some(UPPER - LOWER))
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> ExactSizeIterator
for ValuesMut<'_, LOWER, UPPER, K, V>
{
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> FusedIterator
for ValuesMut<'_, LOWER, UPPER, K, V>
{
}
#[derive(Debug, Clone)]
pub struct IntoValues<const LOWER: usize, const UPPER: usize, K: Integer, V> {
inner: IntoIter<LOWER, UPPER, K, V>,
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> IntoValues<LOWER, UPPER, K, V> {
fn new(collection: VecMap<LOWER, UPPER, K, V>) -> Self {
Self {
inner: IntoIter::new(collection),
}
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> Iterator
for IntoValues<LOWER, UPPER, K, V>
{
type Item = V;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(_, v)| v)
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.inner.collection.len, Some(UPPER - LOWER))
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> ExactSizeIterator
for IntoValues<LOWER, UPPER, K, V>
{
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> FusedIterator
for IntoValues<LOWER, UPPER, K, V>
{
}
#[derive(Debug)]
pub struct Drain<'a, const LOWER: usize, const UPPER: usize, K: Integer, V> {
index: usize,
collection: &'a mut VecMap<LOWER, UPPER, K, V>,
}
impl<'a, const LOWER: usize, const UPPER: usize, K: Integer, V> Drain<'a, LOWER, UPPER, K, V> {
fn new(collection: &'a mut VecMap<LOWER, UPPER, K, V>) -> Self {
Self {
index: 0,
collection,
}
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> Iterator
for Drain<'_, LOWER, UPPER, K, V>
{
type Item = (K, V);
fn next(&mut self) -> Option<Self::Item> {
while self.index < self.collection.data.len() {
let idx = self.index;
self.index += 1;
unsafe {
let x = self.collection.data.get_unchecked_mut(idx);
if x.is_some() {
self.collection.len -= 1;
return Some((
NumCast::from(idx + LOWER).unwrap(),
x.take().unwrap_unchecked(),
));
}
}
}
None
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.collection.len, Some(UPPER - LOWER))
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> ExactSizeIterator
for Drain<'_, LOWER, UPPER, K, V>
{
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> FusedIterator
for Drain<'_, LOWER, UPPER, K, V>
{
}
#[derive(Debug, Clone, Copy)]
pub struct Iter<'a, const LOWER: usize, const UPPER: usize, K: Integer, V> {
index: usize,
collection: &'a VecMap<LOWER, UPPER, K, V>,
}
impl<'a, const LOWER: usize, const UPPER: usize, K: Integer, V> Iter<'a, LOWER, UPPER, K, V> {
fn new(collection: &'a VecMap<LOWER, UPPER, K, V>) -> Self {
Self {
index: 0,
collection,
}
}
}
impl<'a, const LOWER: usize, const UPPER: usize, K: Integer, V> Iterator
for Iter<'a, LOWER, UPPER, K, V>
{
type Item = (K, &'a V);
fn next(&mut self) -> Option<Self::Item> {
while self.index < self.collection.data.len() {
let idx = self.index;
self.index += 1;
unsafe {
if let Some(v) = &self.collection.data.get_unchecked(idx) {
return Some((NumCast::from(idx + LOWER).unwrap(), v));
}
}
}
None
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.collection.len, Some(UPPER - LOWER))
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> ExactSizeIterator
for Iter<'_, LOWER, UPPER, K, V>
{
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> FusedIterator
for Iter<'_, LOWER, UPPER, K, V>
{
}
#[derive(Debug)]
pub struct IterMut<'a, const LOWER: usize, const UPPER: usize, K: Integer, V> {
index: usize,
collection: &'a mut VecMap<LOWER, UPPER, K, V>,
}
impl<'a, const LOWER: usize, const UPPER: usize, K: Integer, V> IterMut<'a, LOWER, UPPER, K, V> {
fn new(collection: &'a mut VecMap<LOWER, UPPER, K, V>) -> Self {
Self {
index: 0,
collection,
}
}
}
impl<'a, const LOWER: usize, const UPPER: usize, K: Integer, V> Iterator
for IterMut<'a, LOWER, UPPER, K, V>
{
type Item = (K, &'a mut V);
fn next(&mut self) -> Option<Self::Item> {
while self.index < self.collection.data.len() {
let idx = self.index;
self.index += 1;
unsafe {
if self.collection.data.get_unchecked(idx).is_some() {
let ptr = self.collection.data.as_mut_ptr();
let v = (*ptr.add(idx)).as_mut().unwrap_unchecked();
return Some((NumCast::from(idx + LOWER).unwrap(), v));
}
}
}
None
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.collection.len, Some(UPPER - LOWER))
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> ExactSizeIterator
for IterMut<'_, LOWER, UPPER, K, V>
{
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> FusedIterator
for IterMut<'_, LOWER, UPPER, K, V>
{
}
#[derive(Debug, Clone)]
pub struct IntoIter<const LOWER: usize, const UPPER: usize, K: Integer, V> {
index: usize,
collection: VecMap<LOWER, UPPER, K, V>,
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> IntoIter<LOWER, UPPER, K, V> {
fn new(collection: VecMap<LOWER, UPPER, K, V>) -> Self {
Self {
index: 0,
collection,
}
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> Iterator
for IntoIter<LOWER, UPPER, K, V>
{
type Item = (K, V);
fn next(&mut self) -> Option<Self::Item> {
while self.index < self.collection.data.len() {
let idx = self.index;
self.index += 1;
unsafe {
if self.collection.data.get_unchecked(idx).is_some() {
let v = self.collection.data[idx].take().unwrap_unchecked();
return Some((NumCast::from(idx + LOWER).unwrap(), v));
}
}
}
None
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.collection.len, Some(UPPER - LOWER))
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> ExactSizeIterator
for IntoIter<LOWER, UPPER, K, V>
{
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> FusedIterator
for IntoIter<LOWER, UPPER, K, V>
{
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V> IntoIterator
for VecMap<LOWER, UPPER, K, V>
{
type Item = (K, V);
type IntoIter = IntoIter<LOWER, UPPER, K, V>;
fn into_iter(self) -> Self::IntoIter {
IntoIter::new(self)
}
}
impl<'a, const LOWER: usize, const UPPER: usize, K: Integer, V> IntoIterator
for &'a VecMap<LOWER, UPPER, K, V>
{
type Item = (K, &'a V);
type IntoIter = Iter<'a, LOWER, UPPER, K, V>;
fn into_iter(self) -> Self::IntoIter {
Iter::new(self)
}
}
impl<'a, const LOWER: usize, const UPPER: usize, K: Integer, V> IntoIterator
for &'a mut VecMap<LOWER, UPPER, K, V>
{
type Item = (K, &'a mut V);
type IntoIter = IterMut<'a, LOWER, UPPER, K, V>;
fn into_iter(self) -> Self::IntoIter {
IterMut::new(self)
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V: Clone, const N: usize> From<[(K, V); N]>
for VecMap<LOWER, UPPER, K, V>
{
fn from(value: [(K, V); N]) -> Self {
let mut map = Self::new();
for (k, v) in value {
map.insert(k, v);
}
map
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V: Clone> FromIterator<(K, V)>
for VecMap<LOWER, UPPER, K, V>
{
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
let mut s = Self::new();
s.extend(iter);
s
}
}
impl<'a, const LOWER: usize, const UPPER: usize, K: Integer, V: Clone> FromIterator<&'a (K, V)>
for VecMap<LOWER, UPPER, K, V>
{
fn from_iter<I: IntoIterator<Item = &'a (K, V)>>(iter: I) -> Self {
let mut s = Self::new();
s.extend(iter);
s
}
}
impl<'a, const LOWER: usize, const UPPER: usize, K: Integer, V: Clone> FromIterator<(&'a K, &'a V)>
for VecMap<LOWER, UPPER, K, V>
{
fn from_iter<I: IntoIterator<Item = (&'a K, &'a V)>>(iter: I) -> Self {
let mut s = Self::new();
s.extend(iter);
s
}
}
impl<'a, const LOWER: usize, const UPPER: usize, K: Integer, V: Clone> FromIterator<(&'a K, V)>
for VecMap<LOWER, UPPER, K, V>
{
fn from_iter<I: IntoIterator<Item = (&'a K, V)>>(iter: I) -> Self {
let mut s = Self::new();
s.extend(iter);
s
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V: Clone> Extend<(K, V)>
for VecMap<LOWER, UPPER, K, V>
{
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
for (k, v) in iter {
self.insert(k, v);
}
}
}
impl<'a, const LOWER: usize, const UPPER: usize, K: Integer, V: Clone> Extend<&'a (K, V)>
for VecMap<LOWER, UPPER, K, V>
{
fn extend<T: IntoIterator<Item = &'a (K, V)>>(&mut self, iter: T) {
for (k, v) in iter {
self.insert(*k, v.clone());
}
}
}
impl<'a, const LOWER: usize, const UPPER: usize, K: Integer, V: Clone> Extend<(&'a K, &'a V)>
for VecMap<LOWER, UPPER, K, V>
{
fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T) {
for (k, v) in iter {
self.insert(*k, v.clone());
}
}
}
impl<'a, const LOWER: usize, const UPPER: usize, K: Integer, V: Clone> Extend<(&'a K, V)>
for VecMap<LOWER, UPPER, K, V>
{
fn extend<T: IntoIterator<Item = (&'a K, V)>>(&mut self, iter: T) {
for (k, v) in iter {
self.insert(*k, v.clone());
}
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V: Clone> Index<K>
for VecMap<LOWER, UPPER, K, V>
{
type Output = V;
fn index(&self, index: K) -> &Self::Output {
if self.contains_key(&index) {
unsafe { self.get_unchecked(index).unwrap_unchecked() }
} else {
panic!(
"Out of bounds: Tried indexing into firims::VecMap with index \
{:?}, but it is not a valid key in this map.",
index
);
}
}
}
impl<const LOWER: usize, const UPPER: usize, K: Integer, V: Clone> Index<&K>
for VecMap<LOWER, UPPER, K, V>
{
type Output = V;
fn index(&self, index: &K) -> &Self::Output {
if self.contains_key(index) {
unsafe { self.get_unchecked(*index).unwrap_unchecked() }
} else {
panic!(
"Out of bounds: Tried indexing into firims::VecMap with index \
{:?}, but it is not a valid key in this map.",
*index
);
}
}
}