microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
96bf691b4143a23ef0dee6ae42e27c8a9fafda42

Branches

Tags

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

Clone

HTTPS

Download ZIP

compiler/qsc_data_structures/src/index_map.rs

264lines · 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
68impl<K: Into<usize>, V> IndexMap<K, V> {
69 pub fn insert(&mut self, key: K, value: V) {
70 let index = key.into();
71 if index >= self.values.len() {
72 self.values.resize_with(index + 1, || None);
73 }
74 self.values[index] = Some(value);
75 }
76
77 pub fn contains_key(&self, key: K) -> bool {
78 let index: usize = key.into();
79 self.values.get(index).map_or(false, Option::is_some)
80 }
81
82 pub fn get(&self, key: K) -> Option<&V> {
83 let index: usize = key.into();
84 self.values.get(index).and_then(Option::as_ref)
85 }
86
87 pub fn get_mut(&mut self, key: K) -> Option<&mut V> {
88 let index: usize = key.into();
89 self.values.get_mut(index).and_then(Option::as_mut)
90 }
91
92 pub fn remove(&mut self, key: K) {
93 let index: usize = key.into();
94 if index < self.values.len() {
95 self.values[index] = None;
96 }
97 }
98
99 pub fn clear(&mut self) {
100 self.values.clear();
101 }
102}
103
104impl<K, V: Clone> Clone for IndexMap<K, V> {
105 fn clone(&self) -> Self {
106 Self {
107 _keys: PhantomData,
108 values: self.values.clone(),
109 }
110 }
111}
112
113impl<K, V: Debug> Debug for IndexMap<K, V> {
114 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
115 f.debug_struct("IndexMap")
116 .field("values", &self.values)
117 .finish()
118 }
119}
120
121impl<K, V> Default for IndexMap<K, V> {
122 fn default() -> Self {
123 Self {
124 _keys: PhantomData,
125 values: Vec::default(),
126 }
127 }
128}
129
130impl<K: From<usize>, V> IntoIterator for IndexMap<K, V> {
131 type Item = (K, V);
132
133 type IntoIter = IntoIter<K, V>;
134
135 fn into_iter(self) -> Self::IntoIter {
136 IntoIter {
137 _keys: PhantomData,
138 base: self.values.into_iter().enumerate(),
139 }
140 }
141}
142
143impl<'a, K: From<usize>, V> IntoIterator for &'a IndexMap<K, V> {
144 type Item = (K, &'a V);
145
146 type IntoIter = Iter<'a, K, V>;
147
148 fn into_iter(self) -> Self::IntoIter {
149 self.iter()
150 }
151}
152
153impl<K: Into<usize>, V> FromIterator<(K, V)> for IndexMap<K, V> {
154 fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
155 let iter = iter.into_iter();
156 let mut map = Self::new();
157 let (lo, hi) = iter.size_hint();
158 map.values.reserve(hi.unwrap_or(lo));
159 for (key, value) in iter {
160 map.insert(key, value);
161 }
162 map
163 }
164}
165
166pub struct Iter<'a, K, V> {
167 _keys: PhantomData<K>,
168 base: Enumerate<slice::Iter<'a, Option<V>>>,
169}
170
171impl<'a, K: From<usize>, V> Iterator for Iter<'a, K, V> {
172 type Item = (K, &'a V);
173
174 fn next(&mut self) -> Option<Self::Item> {
175 loop {
176 if let (index, Some(value)) = self.base.next()? {
177 break Some((index.into(), value));
178 }
179 }
180 }
181}
182
183pub struct IterMut<'a, K, V> {
184 _keys: PhantomData<K>,
185 base: Enumerate<slice::IterMut<'a, Option<V>>>,
186}
187
188impl<'a, K: From<usize>, V> Iterator for IterMut<'a, K, V> {
189 type Item = (K, &'a mut 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 IntoIter<K, V> {
201 _keys: PhantomData<K>,
202 base: Enumerate<vec::IntoIter<Option<V>>>,
203}
204
205impl<K: From<usize>, V> Iterator for IntoIter<K, V> {
206 type Item = (K, 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 Drain<'a, K, V> {
218 _keys: PhantomData<K>,
219 base: Enumerate<vec::Drain<'a, Option<V>>>,
220}
221
222impl<K: From<usize>, V> Iterator for Drain<'_, 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 Values<'a, V> {
235 base: slice::Iter<'a, Option<V>>,
236}
237
238impl<'a, V> Iterator for Values<'a, V> {
239 type Item = &'a V;
240
241 fn next(&mut self) -> Option<Self::Item> {
242 loop {
243 if let Some(value) = self.base.next()? {
244 break Some(value);
245 }
246 }
247 }
248}
249
250pub struct ValuesMut<'a, V> {
251 base: slice::IterMut<'a, Option<V>>,
252}
253
254impl<'a, V> Iterator for ValuesMut<'a, V> {
255 type Item = &'a mut V;
256
257 fn next(&mut self) -> Option<Self::Item> {
258 loop {
259 if let Some(value) = self.base.next()? {
260 break Some(value);
261 }
262 }
263 }
264}
265