1use crate::prelude::*;
4use alloc::slice::Iter;
5use core::hash::Hash;
6use core::ops::{Bound, RangeBounds};
7
8#[derive(Clone, Debug, Eq)]
26pub struct IndexedMap<K: Hash + Ord, V> {
27 map: HashMap<K, V>,
28 keys: Vec<K>,
29}
30
31impl<K: Clone + Hash + Ord, V> IndexedMap<K, V> {
32 pub fn new() -> Self {
34 Self { map: new_hash_map(), keys: Vec::new() }
35 }
36
37 pub fn with_capacity(capacity: usize) -> Self {
39 Self { map: hash_map_with_capacity(capacity), keys: Vec::with_capacity(capacity) }
40 }
41
42 #[inline(always)]
43 pub fn get(&self, key: &K) -> Option<&V> {
45 self.map.get(key)
46 }
47
48 pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
50 self.map.get_mut(key)
51 }
52
53 pub fn get_key_value(&self, key: &K) -> Option<(&K, &V)> {
55 self.map.get_key_value(key)
56 }
57
58 #[inline]
59 pub fn contains_key(&self, key: &K) -> bool {
61 self.map.contains_key(key)
62 }
63
64 pub fn remove(&mut self, key: &K) -> Option<V> {
66 let ret = self.map.remove(key);
67 if let Some(_) = ret {
68 let idx =
69 self.keys.iter().position(|k| k == key).expect("map and keys must be consistent");
70 self.keys.remove(idx);
71 }
72 ret
73 }
74
75 pub fn insert(&mut self, key: K, value: V) -> Option<V> {
78 let ret = self.map.insert(key.clone(), value);
79 if ret.is_none() {
80 self.keys.push(key);
81 }
82 ret
83 }
84
85 pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
87 match self.map.entry(key.clone()) {
88 hash_map::Entry::Vacant(entry) => {
89 Entry::Vacant(VacantEntry { underlying_entry: entry, key, keys: &mut self.keys })
90 },
91 hash_map::Entry::Occupied(entry) => {
92 Entry::Occupied(OccupiedEntry { underlying_entry: entry, keys: &mut self.keys })
93 },
94 }
95 }
96
97 pub fn unordered_keys(&self) -> impl Iterator<Item = &K> {
99 self.map.keys()
100 }
101
102 pub fn unordered_iter(&self) -> impl Iterator<Item = (&K, &V)> {
104 self.map.iter()
105 }
106
107 pub fn unordered_iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)> {
110 self.map.iter_mut()
111 }
112
113 pub fn range<R: RangeBounds<K>>(&mut self, range: R) -> Range<K, V> {
115 self.keys.sort_unstable();
116 let start = match range.start_bound() {
117 Bound::Unbounded => 0,
118 Bound::Included(key) => self.keys.binary_search(key).unwrap_or_else(|index| index),
119 Bound::Excluded(key) => {
120 self.keys.binary_search(key).map(|index| index + 1).unwrap_or_else(|index| index)
121 },
122 };
123 let end = match range.end_bound() {
124 Bound::Unbounded => self.keys.len(),
125 Bound::Included(key) => {
126 self.keys.binary_search(key).map(|index| index + 1).unwrap_or_else(|index| index)
127 },
128 Bound::Excluded(key) => self.keys.binary_search(key).unwrap_or_else(|index| index),
129 };
130
131 Range { inner_range: self.keys[start..end].iter(), map: &self.map }
132 }
133
134 pub fn len(&self) -> usize {
136 self.map.len()
137 }
138
139 pub fn is_empty(&self) -> bool {
141 self.map.is_empty()
142 }
143}
144
145impl<K: Hash + Ord + PartialEq, V: PartialEq> PartialEq for IndexedMap<K, V> {
146 fn eq(&self, other: &Self) -> bool {
147 self.map == other.map
148 }
149}
150
151pub struct Range<'a, K: Hash + Ord, V> {
155 inner_range: Iter<'a, K>,
156 map: &'a HashMap<K, V>,
157}
158impl<'a, K: Hash + Ord, V: 'a> Iterator for Range<'a, K, V> {
159 type Item = (&'a K, &'a V);
160 fn next(&mut self) -> Option<(&'a K, &'a V)> {
161 self.inner_range
162 .next()
163 .map(|k| (k, self.map.get(k).expect("map and keys must be consistent")))
164 }
165}
166
167pub struct VacantEntry<'a, K: Hash + Ord, V> {
171 underlying_entry: VacantHashMapEntry<'a, K, V>,
172 key: K,
173 keys: &'a mut Vec<K>,
174}
175
176pub struct OccupiedEntry<'a, K: Hash + Ord, V> {
180 underlying_entry: OccupiedHashMapEntry<'a, K, V>,
181 keys: &'a mut Vec<K>,
182}
183
184pub enum Entry<'a, K: Hash + Ord, V> {
189 Vacant(VacantEntry<'a, K, V>),
191 Occupied(OccupiedEntry<'a, K, V>),
193}
194
195impl<'a, K: Hash + Ord, V> VacantEntry<'a, K, V> {
196 pub fn insert(self, value: V) -> &'a mut V {
198 self.keys.push(self.key);
199 self.underlying_entry.insert(value)
200 }
201}
202
203impl<'a, K: Hash + Ord, V> OccupiedEntry<'a, K, V> {
204 pub fn remove_entry(self) -> (K, V) {
206 let res = self.underlying_entry.remove_entry();
207 let idx =
208 self.keys.iter().position(|k| k == &res.0).expect("map and keys must be consistent");
209 self.keys.remove(idx);
210 res
211 }
212
213 pub fn get(&self) -> &V {
215 self.underlying_entry.get()
216 }
217
218 pub fn get_mut(&mut self) -> &mut V {
220 self.underlying_entry.get_mut()
221 }
222
223 pub fn into_mut(self) -> &'a mut V {
226 self.underlying_entry.into_mut()
227 }
228}