microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/index_map/src/lib.rs
341lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use std::{ |
| 5 | fmt::{self, Debug, Formatter}, |
| 6 | iter::Enumerate, |
| 7 | marker::PhantomData, |
| 8 | ops::{Index, IndexMut}, |
| 9 | option::Option, |
| 10 | slice, vec, |
| 11 | }; |
| 12 | |
| 13 | pub struct IndexMap<K, V> { |
| 14 | _keys: PhantomData<K>, |
| 15 | values: Vec<Option<V>>, |
| 16 | } |
| 17 | |
| 18 | impl<K, V> IndexMap<K, V> |
| 19 | where |
| 20 | K: Into<usize>, |
| 21 | V: Default, |
| 22 | { |
| 23 | pub fn get_mut_or_default(&mut self, key: K) -> &mut V { |
| 24 | let index: usize = key.into(); |
| 25 | if index >= self.values.len() { |
| 26 | self.values.resize_with(index + 1, Option::default); |
| 27 | } |
| 28 | self.values |
| 29 | .get_mut(index) |
| 30 | .expect("IndexMap::get_mut_or_default: index out of bounds") |
| 31 | .get_or_insert_with(Default::default) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl<K, V> IndexMap<K, V> { |
| 36 | #[must_use] |
| 37 | pub fn new() -> Self { |
| 38 | Self::default() |
| 39 | } |
| 40 | |
| 41 | #[must_use] |
| 42 | pub fn with_capacity(capacity: usize) -> Self { |
| 43 | Self { |
| 44 | _keys: PhantomData, |
| 45 | values: Vec::with_capacity(capacity), |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | #[must_use] |
| 50 | pub fn is_empty(&self) -> bool { |
| 51 | self.values.iter().all(Option::is_none) |
| 52 | } |
| 53 | |
| 54 | // `Iter` does implement `Iterator`, but it has an additional bound on `K`. |
| 55 | #[allow(clippy::iter_not_returning_iterator)] |
| 56 | #[must_use] |
| 57 | pub fn iter(&self) -> Iter<'_, K, V> { |
| 58 | Iter { |
| 59 | _keys: PhantomData, |
| 60 | base: self.values.iter().enumerate(), |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // `Iter` does implement `Iterator`, but it has an additional bound on `K`. |
| 65 | #[allow(clippy::iter_not_returning_iterator)] |
| 66 | pub fn iter_mut(&mut self) -> IterMut<'_, K, V> { |
| 67 | IterMut { |
| 68 | _keys: PhantomData, |
| 69 | base: self.values.iter_mut().enumerate(), |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | pub fn drain(&mut self) -> Drain<'_, K, V> { |
| 74 | Drain { |
| 75 | _keys: PhantomData, |
| 76 | base: self.values.drain(..).enumerate(), |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | #[must_use] |
| 81 | pub fn values(&self) -> Values<'_, V> { |
| 82 | Values { |
| 83 | base: self.values.iter(), |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | pub fn values_mut(&mut self) -> ValuesMut<'_, V> { |
| 88 | ValuesMut { |
| 89 | base: self.values.iter_mut(), |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | pub fn retain<F>(&mut self, mut f: F) |
| 94 | where |
| 95 | F: FnMut(K, &V) -> bool, |
| 96 | K: From<usize>, |
| 97 | { |
| 98 | for (k, v) in self.values.iter_mut().enumerate() { |
| 99 | let remove = if let Some(value) = v { |
| 100 | !f(K::from(k), value) |
| 101 | } else { |
| 102 | false |
| 103 | }; |
| 104 | if remove { |
| 105 | *v = None; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | pub fn clear(&mut self) { |
| 111 | self.values.clear(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | impl<K: Into<usize>, V> IndexMap<K, V> { |
| 116 | pub fn insert(&mut self, key: K, value: V) { |
| 117 | let index = key.into(); |
| 118 | if index >= self.values.len() { |
| 119 | self.values.resize_with(index + 1, || None); |
| 120 | } |
| 121 | self.values[index] = Some(value); |
| 122 | } |
| 123 | |
| 124 | pub fn contains_key(&self, key: K) -> bool { |
| 125 | let index: usize = key.into(); |
| 126 | self.values.get(index).is_some_and(Option::is_some) |
| 127 | } |
| 128 | |
| 129 | pub fn get(&self, key: K) -> Option<&V> { |
| 130 | let index: usize = key.into(); |
| 131 | self.values.get(index).and_then(Option::as_ref) |
| 132 | } |
| 133 | |
| 134 | pub fn get_mut(&mut self, key: K) -> Option<&mut V> { |
| 135 | let index: usize = key.into(); |
| 136 | self.values.get_mut(index).and_then(Option::as_mut) |
| 137 | } |
| 138 | |
| 139 | pub fn remove(&mut self, key: K) { |
| 140 | let index: usize = key.into(); |
| 141 | if index < self.values.len() { |
| 142 | self.values[index] = None; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | impl<K, V: Clone> Clone for IndexMap<K, V> { |
| 148 | fn clone(&self) -> Self { |
| 149 | Self { |
| 150 | _keys: PhantomData, |
| 151 | values: self.values.clone(), |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | impl<K, V: Debug> Debug for IndexMap<K, V> { |
| 157 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 158 | f.debug_struct("IndexMap") |
| 159 | .field( |
| 160 | "values", |
| 161 | &self |
| 162 | .values |
| 163 | .iter() |
| 164 | .enumerate() |
| 165 | .filter_map(|(k, v)| v.as_ref().map(|val| format!("{k:?}: {val:?}"))) |
| 166 | .collect::<Vec<_>>(), |
| 167 | ) |
| 168 | .finish() |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | impl<K, V> Default for IndexMap<K, V> { |
| 173 | fn default() -> Self { |
| 174 | Self { |
| 175 | _keys: PhantomData, |
| 176 | values: Vec::default(), |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | impl<K: From<usize>, V> IntoIterator for IndexMap<K, V> { |
| 182 | type Item = (K, V); |
| 183 | |
| 184 | type IntoIter = IntoIter<K, V>; |
| 185 | |
| 186 | fn into_iter(self) -> Self::IntoIter { |
| 187 | IntoIter { |
| 188 | _keys: PhantomData, |
| 189 | base: self.values.into_iter().enumerate(), |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | impl<'a, K: From<usize>, V> IntoIterator for &'a IndexMap<K, V> { |
| 195 | type Item = (K, &'a V); |
| 196 | |
| 197 | type IntoIter = Iter<'a, K, V>; |
| 198 | |
| 199 | fn into_iter(self) -> Self::IntoIter { |
| 200 | self.iter() |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | impl<K: Into<usize>, V> FromIterator<(K, V)> for IndexMap<K, V> { |
| 205 | fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self { |
| 206 | let iter = iter.into_iter(); |
| 207 | let mut map = Self::new(); |
| 208 | let (lo, hi) = iter.size_hint(); |
| 209 | map.values.reserve(hi.unwrap_or(lo)); |
| 210 | for (key, value) in iter { |
| 211 | map.insert(key, value); |
| 212 | } |
| 213 | map |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | pub struct Iter<'a, K, V> { |
| 218 | _keys: PhantomData<K>, |
| 219 | base: Enumerate<slice::Iter<'a, Option<V>>>, |
| 220 | } |
| 221 | |
| 222 | impl<'a, K: From<usize>, V> Iterator for Iter<'a, K, V> { |
| 223 | type Item = (K, &'a V); |
| 224 | |
| 225 | fn next(&mut self) -> Option<Self::Item> { |
| 226 | loop { |
| 227 | if let (index, Some(value)) = self.base.next()? { |
| 228 | break Some((index.into(), value)); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | pub struct IterMut<'a, K, V> { |
| 235 | _keys: PhantomData<K>, |
| 236 | base: Enumerate<slice::IterMut<'a, Option<V>>>, |
| 237 | } |
| 238 | |
| 239 | impl<K: From<usize>, V> DoubleEndedIterator for Iter<'_, K, V> { |
| 240 | fn next_back(&mut self) -> Option<Self::Item> { |
| 241 | loop { |
| 242 | if let (index, Some(value)) = self.base.next_back()? { |
| 243 | break Some((index.into(), value)); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | impl<'a, K: From<usize>, V> Iterator for IterMut<'a, K, V> { |
| 250 | type Item = (K, &'a mut V); |
| 251 | |
| 252 | fn next(&mut self) -> Option<Self::Item> { |
| 253 | loop { |
| 254 | if let (index, Some(value)) = self.base.next()? { |
| 255 | break Some((index.into(), value)); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | pub struct IntoIter<K, V> { |
| 262 | _keys: PhantomData<K>, |
| 263 | base: Enumerate<vec::IntoIter<Option<V>>>, |
| 264 | } |
| 265 | |
| 266 | impl<K: From<usize>, V> Iterator for IntoIter<K, V> { |
| 267 | type Item = (K, V); |
| 268 | |
| 269 | fn next(&mut self) -> Option<Self::Item> { |
| 270 | loop { |
| 271 | if let (index, Some(value)) = self.base.next()? { |
| 272 | break Some((index.into(), value)); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | pub struct Drain<'a, K, V> { |
| 279 | _keys: PhantomData<K>, |
| 280 | base: Enumerate<vec::Drain<'a, Option<V>>>, |
| 281 | } |
| 282 | |
| 283 | impl<K: From<usize>, V> Iterator for Drain<'_, K, V> { |
| 284 | type Item = (K, V); |
| 285 | |
| 286 | fn next(&mut self) -> Option<Self::Item> { |
| 287 | loop { |
| 288 | if let (index, Some(value)) = self.base.next()? { |
| 289 | break Some((index.into(), value)); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | pub struct Values<'a, V> { |
| 296 | base: slice::Iter<'a, Option<V>>, |
| 297 | } |
| 298 | |
| 299 | impl<'a, V> Iterator for Values<'a, V> { |
| 300 | type Item = &'a V; |
| 301 | |
| 302 | fn next(&mut self) -> Option<Self::Item> { |
| 303 | loop { |
| 304 | if let Some(value) = self.base.next()? { |
| 305 | break Some(value); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | pub struct ValuesMut<'a, V> { |
| 312 | base: slice::IterMut<'a, Option<V>>, |
| 313 | } |
| 314 | |
| 315 | impl<'a, V> Iterator for ValuesMut<'a, V> { |
| 316 | type Item = &'a mut V; |
| 317 | |
| 318 | fn next(&mut self) -> Option<Self::Item> { |
| 319 | loop { |
| 320 | if let Some(value) = self.base.next()? { |
| 321 | break Some(value); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | impl Index<usize> for IndexMap<usize, usize> { |
| 328 | type Output = usize; |
| 329 | |
| 330 | fn index(&self, index: usize) -> &Self::Output { |
| 331 | self.get(index) |
| 332 | .expect("IndexMap::index: index out of bounds") |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | impl IndexMut<usize> for IndexMap<usize, usize> { |
| 337 | fn index_mut(&mut self, index: usize) -> &mut Self::Output { |
| 338 | self.get_mut(index) |
| 339 | .expect("IndexMap::index_mut: index out of bounds") |
| 340 | } |
| 341 | } |
| 342 | |