microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
cesarzc/ssa-panic

Branches

Tags

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

Clone

HTTPS

Download ZIP

compiler/qsc_data_structures/src/index_map.rs

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