microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
alex/second-api-refactor

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

compiler/qsc_data_structures/src/index_map.rs

298lines · modecode

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