1use indexmap::IndexMap;
2
3use crate::prelude_internal::*;
4
5#[derive(Debug, Clone, Plural)]
6#[plural(len, is_empty, iter, into_iter, into_iter_ref, new)]
7pub struct Map<K, V>(IndexMap<K, V>);
8
9impl<K: Eq + std::hash::Hash, V: Eq> Eq for Map<K, V> {}
10impl<K: Eq + std::hash::Hash, V: PartialEq> PartialEq for Map<K, V> {
11 fn eq(&self, other: &Self) -> bool {
12 self.0 == other.0
13 }
14}
15
16impl<K: Eq + std::hash::Hash, V> FromIterator<(K, V)> for Map<K, V> {
17 fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
18 Self(IndexMap::from_iter(iter))
19 }
20}
21
22impl<K, V> Default for Map<K, V> {
23 fn default() -> Self {
24 Self(IndexMap::new())
25 }
26}
27
28impl<K: std::hash::Hash + Eq, V> Map<K, V> {
29 pub fn get(&self, key: &K) -> Option<&V> {
30 self.0.get(key)
31 }
32
33 pub fn insert(&mut self, key: K, value: V) -> Option<V> {
34 self.0.insert(key, value)
35 }
36
37 pub fn remove_fast(&mut self, key: &K) -> Option<V> {
40 self.0.swap_remove(key)
41 }
42
43 pub fn remove_ordered(&mut self, key: &K) -> Option<V> {
46 self.0.shift_remove(key)
47 }
48
49 pub fn contains_key(&self, key: &K) -> bool {
50 self.0.contains_key(key)
51 }
52}
53
54impl Map<ObjectKey, NodeId> {
55 pub fn add(&mut self, key: ObjectKey, value: NodeId) -> Result<(), InsertErrorKind> {
56 match self.0.entry(key) {
57 indexmap::map::Entry::Occupied(e) => Err(InsertErrorKind::AlreadyAssigned {
58 key: e.key().clone(),
59 }),
60 indexmap::map::Entry::Vacant(e) => {
61 e.insert(value);
62 Ok(())
63 }
64 }
65 }
66
67 pub fn get_node_id(&self, key: &ObjectKey) -> Option<NodeId> {
68 self.0.get(key).copied()
69 }
70}