microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
billti/qdk-test

Branches

Tags

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

Clone

HTTPS

Download ZIP

compiler/qsc_data_structures/src/index_map.rs

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