1use std::{
8 fmt,
9 iter::{self, FusedIterator},
10 marker::PhantomData,
11 ops::{Index, IndexMut},
12 slice,
13};
14
15use serde::{Deserialize, Serialize};
16use smol_bitmap::SmolBitmap;
17
18use crate::{sparse_index::TrySparseIndex, sparse_set::SparseSet};
19
20pub struct SparseMap<K, V> {
81 bits: SmolBitmap,
83 values: Vec<V>,
85 _phantom: PhantomData<K>,
87}
88
89impl<K, V: Clone> Clone for SparseMap<K, V> {
90 fn clone(&self) -> Self {
91 Self { bits: self.bits.clone(), values: self.values.clone(), _phantom: PhantomData }
92 }
93}
94
95impl<K: TrySparseIndex + fmt::Debug, V: fmt::Debug> fmt::Debug for SparseMap<K, V> {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97 let mut s = f.debug_map();
98 for (index, value) in self {
99 s.entry(&index, &value);
100 }
101 s.finish()
102 }
103}
104impl<K: TrySparseIndex + Serialize, V: Serialize> Serialize for SparseMap<K, V> {
105 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
106 where
107 S: serde::Serializer,
108 {
109 if serializer.is_human_readable() {
110 use serde::ser::SerializeSeq;
111 let mut seq = serializer.serialize_seq(Some(self.len()))?;
112 for (index, value) in self {
113 seq.serialize_element(&(index, value))?;
114 }
115 seq.end()
116 } else {
117 use serde::ser::SerializeTuple;
118 let mut tuple = serializer.serialize_tuple(2)?;
119 tuple.serialize_element(&self.bits)?;
120 tuple.serialize_element(&self.values)?;
121 tuple.end()
122 }
123 }
124}
125
126impl<'de, K: TrySparseIndex + Deserialize<'de>, V: Deserialize<'de>> Deserialize<'de>
127 for SparseMap<K, V>
128{
129 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
130 where
131 D: serde::Deserializer<'de>,
132 {
133 if deserializer.is_human_readable() {
134 use std::fmt;
135
136 use serde::de::{SeqAccess, Visitor};
137
138 struct SparseMapVisitor<K, V> {
139 _phantom: PhantomData<(K, V)>,
140 }
141
142 impl<'de, K: TrySparseIndex + Deserialize<'de>, V: Deserialize<'de>> Visitor<'de>
143 for SparseMapVisitor<K, V>
144 {
145 type Value = SparseMap<K, V>;
146
147 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
148 formatter.write_str("a sequence of (index, value) pairs")
149 }
150
151 fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
152 where
153 A: SeqAccess<'de>,
154 {
155 let mut map = SparseMap::new();
156 if let Some(len) = seq.size_hint() {
157 map.reserve(len);
158 }
159 let mut prev_index = None;
160 while let Some((index, value)) = seq.next_element::<(K, V)>()? {
161 let index = index.index();
162 if prev_index.is_some_and(|prev| prev >= index) {
163 return Err(serde::de::Error::invalid_value(
164 serde::de::Unexpected::Unsigned(index as u64),
165 &"indices must be in ascending order",
166 ));
167 }
168 prev_index = Some(index);
169 map.bits.insert(index);
170 map.values.push(value);
171 }
172 Ok(map)
173 }
174 }
175
176 deserializer.deserialize_seq(SparseMapVisitor { _phantom: PhantomData })
177 } else {
178 let (bits, values): (SmolBitmap, Vec<V>) = Deserialize::deserialize(deserializer)?;
179 if bits.count_ones() != values.len() {
183 return Err(serde::de::Error::invalid_length(
184 values.len(),
185 &"as many values as set bits in the occupancy bitmap",
186 ));
187 }
188 K::validate_sorted(bits.iter()).map_err(serde::de::Error::custom)?;
189 Ok(Self { bits, values, _phantom: PhantomData })
190 }
191 }
192}
193
194impl<K, V> Default for SparseMap<K, V> {
195 fn default() -> Self {
196 Self::new()
197 }
198}
199
200impl<K, V: PartialEq> PartialEq for SparseMap<K, V> {
201 fn eq(&self, other: &Self) -> bool {
202 self.bits == other.bits && self.values == other.values
203 }
204}
205
206impl<K, V: Eq> Eq for SparseMap<K, V> {}
207
208impl<K, V> SparseMap<K, V> {
209 pub fn from_sequence(values: Vec<V>) -> Self {
218 let len = values.len();
219 let mut bits = SmolBitmap::with_capacity(len);
220
221 for i in 0..len {
223 bits.insert(i);
224 }
225
226 Self { bits, values, _phantom: PhantomData }
227 }
228
229 pub const fn new() -> Self {
231 Self { bits: SmolBitmap::new(), values: Vec::new(), _phantom: PhantomData }
232 }
233
234 pub fn with_capacity(capacity: usize) -> Self {
240 Self {
241 bits: SmolBitmap::with_capacity(capacity),
242 values: Vec::with_capacity(capacity),
243 _phantom: PhantomData,
244 }
245 }
246
247 #[inline]
249 pub const fn len(&self) -> usize {
250 self.values.len()
251 }
252
253 #[inline]
255 pub const fn is_empty(&self) -> bool {
256 self.values.is_empty()
257 }
258
259 #[inline]
261 pub const fn capacity(&self) -> usize {
262 self.bits.capacity()
263 }
264
265 pub fn clear(&mut self) {
267 self.bits.clear();
268 self.values.clear();
269 }
270
271 pub fn shrink_to_fit(&mut self) {
273 self.bits.shrink_to_fit();
274 self.values.shrink_to_fit();
275 }
276
277 pub fn reserve(&mut self, additional: usize) {
280 self.bits.reserve(additional);
281 self.values.reserve(additional);
282 }
283
284 pub const fn key_set(&self) -> &SparseSet<K> {
286 unsafe { &*(&raw const self.bits).cast::<SparseSet<K>>() }
289 }
290
291 #[inline]
297 pub fn into_parts(self) -> (SmolBitmap, Vec<V>) {
298 (self.bits, self.values)
299 }
300
301 #[inline]
316 pub fn from_parts(bits: SmolBitmap, values: Vec<V>) -> Self {
317 assert_eq!(bits.count_ones(), values.len(), "bitmap and values length mismatch");
318 Self { bits, values, _phantom: PhantomData }
319 }
320}
321
322impl<K: TrySparseIndex, V> SparseMap<K, V> {
323 #[inline]
333 pub fn get(&self, key: K) -> Option<&V> {
334 let index = key.index();
335 if self.bits.get(index) {
336 self.values.get(self.bits.rank(index))
337 } else {
338 None
339 }
340 }
341
342 #[inline]
353 pub fn get_mut(&mut self, key: K) -> Option<&mut V> {
354 let index = key.index();
355 if self.bits.get(index) {
356 let pos = self.bits.rank(index);
357 self.values.get_mut(pos)
358 } else {
359 None
360 }
361 }
362
363 #[inline]
379 pub fn get_or_insert(&mut self, key: K, value: V) -> &mut V {
380 let index = key.index();
381 let pos = if self.bits.insert(index) {
382 let pos = self.bits.rank(index);
384 self.values.insert(pos, value);
385 pos
386 } else {
387 self.bits.rank(index)
389 };
390 &mut self.values[pos]
391 }
392
393 #[inline]
410 pub fn get_or_insert_with<F>(&mut self, key: K, f: F) -> &mut V
411 where
412 F: FnOnce() -> V,
413 {
414 let index = key.index();
415 let pos = if self.bits.insert(index) {
416 let pos = self.bits.rank(index);
418 self.values.insert(pos, f());
419 pos
420 } else {
421 self.bits.rank(index)
423 };
424 &mut self.values[pos]
425 }
426
427 #[inline]
433 pub fn contains_key(&self, key: K) -> bool {
434 self.bits.get(key.index())
435 }
436
437 pub fn insert(&mut self, key: K, value: V) -> Option<V> {
452 let index = key.index();
453 let pos = self.bits.rank(index);
454 if self.bits.insert(index) {
455 self.values.insert(pos, value);
457 None
458 } else {
459 Some(std::mem::replace(&mut self.values[pos], value))
461 }
462 }
463
464 pub fn remove(&mut self, key: K) -> Option<V> {
475 let index = key.index();
476 if self.bits.remove(index) {
477 let pos = self.bits.rank(index);
478 Some(self.values.remove(pos))
479 } else {
480 None
481 }
482 }
483
484 pub fn retain<F>(&mut self, mut f: F)
489 where
490 F: FnMut(K, &mut V) -> bool,
491 {
492 let mut write_idx = 0;
493 let mut read_idx = 0;
494 self.bits.retain(|idx| {
495 let key = K::from_index(idx);
496 let should_retain = f(key, &mut self.values[read_idx]);
497 if should_retain {
498 if write_idx != read_idx {
499 self.values.swap(write_idx, read_idx);
500 }
501 write_idx += 1;
502 }
503 read_idx += 1;
504 should_retain
505 });
506 self.values.truncate(write_idx);
507 }
508
509 #[define_opaque(Iter)]
513 pub fn iter(&self) -> Iter<'_, K, V> {
514 iter::zip(self.bits.iter().map(K::from_index), self.values.iter())
515 }
516
517 #[define_opaque(IterMut)]
519 pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
520 iter::zip(self.bits.iter().map(K::from_index), self.values.iter_mut())
521 }
522
523 #[define_opaque(KeyIter)]
525 pub fn keys(&self) -> KeyIter<'_, K> {
526 self.bits.iter().map(K::from_index)
527 }
528
529 #[inline]
531 pub fn values(&self) -> slice::Iter<'_, V> {
532 self.values.iter()
533 }
534
535 #[inline]
537 pub fn values_mut(&mut self) -> slice::IterMut<'_, V> {
538 self.values.iter_mut()
539 }
540
541 #[inline]
546 pub fn rank(&self, key: K) -> usize {
547 self.bits.rank(key.index())
548 }
549
550 pub fn first(&self) -> Option<(K, &V)> {
553 self
554 .bits
555 .first()
556 .and_then(|idx| self.values.first().map(|v| (K::from_index(idx), v)))
557 }
558
559 pub fn last(&self) -> Option<(K, &V)> {
562 self
563 .bits
564 .last()
565 .and_then(|idx| self.values.last().map(|v| (K::from_index(idx), v)))
566 }
567
568 pub fn is_sparse(&self) -> bool {
574 match (self.bits.first(), self.bits.last()) {
575 (Some(first), Some(last)) => {
576 self.len() < (last - first + 1)
579 },
580 _ => false, }
582 }
583}
584
585pub type Iter<'a, K: TrySparseIndex, V: 'a> =
587 impl DoubleEndedIterator<Item = (K, &'a V)> + ExactSizeIterator + FusedIterator + Clone;
588pub type IterMut<'a, K: TrySparseIndex, V: 'a> =
591 impl DoubleEndedIterator<Item = (K, &'a mut V)> + ExactSizeIterator + FusedIterator;
592pub type KeyIter<'a, K: TrySparseIndex> =
594 impl DoubleEndedIterator<Item = K> + ExactSizeIterator + FusedIterator + Clone;
595pub type IntoIter<K: TrySparseIndex, V> =
597 impl DoubleEndedIterator<Item = (K, V)> + ExactSizeIterator + FusedIterator;
598
599impl<K: TrySparseIndex, V> Index<K> for SparseMap<K, V> {
601 type Output = V;
602
603 fn index(&self, key: K) -> &Self::Output {
604 self.get(key).expect("key not found in SparseMap")
605 }
606}
607
608impl<K: TrySparseIndex, V> IndexMut<K> for SparseMap<K, V> {
609 fn index_mut(&mut self, key: K) -> &mut Self::Output {
610 self.get_mut(key).expect("key not found in SparseMap")
611 }
612}
613
614impl<'a, K: TrySparseIndex, V> IntoIterator for &'a SparseMap<K, V> {
615 type IntoIter = Iter<'a, K, V>;
616 type Item = (K, &'a V);
617
618 fn into_iter(self) -> Self::IntoIter {
619 self.iter()
620 }
621}
622
623impl<'a, K: TrySparseIndex, V> IntoIterator for &'a mut SparseMap<K, V> {
624 type IntoIter = IterMut<'a, K, V>;
625 type Item = (K, &'a mut V);
626
627 fn into_iter(self) -> Self::IntoIter {
628 self.iter_mut()
629 }
630}
631
632impl<K: TrySparseIndex, V> IntoIterator for SparseMap<K, V> {
633 type IntoIter = IntoIter<K, V>;
634 type Item = (K, V);
635
636 #[define_opaque(IntoIter)]
637 fn into_iter(self) -> Self::IntoIter {
638 let indices = self.bits.into_iter().map(K::from_index);
639 let values = self.values.into_iter();
640 indices.zip(values)
641 }
642}
643
644impl<K: TrySparseIndex, V> FromIterator<(K, V)> for SparseMap<K, V> {
645 fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
646 let iter = iter.into_iter();
647 let mut map = Self::with_capacity(iter.size_hint().0);
648 let mut hi = None;
649
650 for (key, value) in iter {
651 let ix = key.index();
652 match hi {
653 Some(hi) if ix == hi => {
654 *map.values.last_mut().expect("can't have key without value") = value;
655 },
656 Some(hi) if ix < hi => {
657 map.insert(K::from_index(ix), value);
658 },
659 _ => {
660 map.bits.insert(ix);
661 map.values.push(value);
662 hi = Some(ix);
663 },
664 }
665 }
666
667 map
668 }
669}
670
671impl<K: TrySparseIndex, V> Extend<(K, V)> for SparseMap<K, V> {
672 fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
673 let mut hi = self.bits.last();
674 for (key, value) in iter {
675 let ix = key.index();
676 match hi {
677 Some(hi) if ix == hi => {
678 *self
679 .values
680 .last_mut()
681 .expect("can't have key without value") = value;
682 },
683 Some(hi) if ix < hi => {
684 self.insert(K::from_index(ix), value);
685 },
686 _ => {
687 self.bits.insert(ix);
688 self.values.push(value);
689 hi = Some(ix);
690 },
691 }
692 }
693 }
694}