microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
bb481c14648739fdfd94e1f49109610f7d0daecc

Branches

Tags

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

Clone

HTTPS

Download ZIP

compiler/qsc_data_structures/src/index_map.rs

252lines · 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 clear(&mut self) {
88 self.values.clear();
89 }
90}
91
92impl<K, V: Clone> Clone for IndexMap<K, V> {
93 fn clone(&self) -> Self {
94 Self {
95 _keys: PhantomData,
96 values: self.values.clone(),
97 }
98 }
99}
100
101impl<K, V: Debug> Debug for IndexMap<K, V> {
102 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
103 f.debug_struct("IndexMap")
104 .field("values", &self.values)
105 .finish()
106 }
107}
108
109impl<K, V> Default for IndexMap<K, V> {
110 fn default() -> Self {
111 Self {
112 _keys: PhantomData,
113 values: Vec::default(),
114 }
115 }
116}
117
118impl<K: From<usize>, V> IntoIterator for IndexMap<K, V> {
119 type Item = (K, V);
120
121 type IntoIter = IntoIter<K, V>;
122
123 fn into_iter(self) -> Self::IntoIter {
124 IntoIter {
125 _keys: PhantomData,
126 base: self.values.into_iter().enumerate(),
127 }
128 }
129}
130
131impl<'a, K: From<usize>, V> IntoIterator for &'a IndexMap<K, V> {
132 type Item = (K, &'a V);
133
134 type IntoIter = Iter<'a, K, V>;
135
136 fn into_iter(self) -> Self::IntoIter {
137 self.iter()
138 }
139}
140
141impl<K: Into<usize>, V> FromIterator<(K, V)> for IndexMap<K, V> {
142 fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
143 let iter = iter.into_iter();
144 let mut map = Self::new();
145 let (lo, hi) = iter.size_hint();
146 map.values.reserve(hi.unwrap_or(lo));
147 for (key, value) in iter {
148 map.insert(key, value);
149 }
150 map
151 }
152}
153
154pub struct Iter<'a, K, V> {
155 _keys: PhantomData<K>,
156 base: Enumerate<slice::Iter<'a, Option<V>>>,
157}
158
159impl<'a, K: From<usize>, V> Iterator for Iter<'a, K, V> {
160 type Item = (K, &'a V);
161
162 fn next(&mut self) -> Option<Self::Item> {
163 loop {
164 if let (index, Some(value)) = self.base.next()? {
165 break Some((index.into(), value));
166 }
167 }
168 }
169}
170
171pub struct IterMut<'a, K, V> {
172 _keys: PhantomData<K>,
173 base: Enumerate<slice::IterMut<'a, Option<V>>>,
174}
175
176impl<'a, K: From<usize>, V> Iterator for IterMut<'a, K, V> {
177 type Item = (K, &'a mut V);
178
179 fn next(&mut self) -> Option<Self::Item> {
180 loop {
181 if let (index, Some(value)) = self.base.next()? {
182 break Some((index.into(), value));
183 }
184 }
185 }
186}
187
188pub struct IntoIter<K, V> {
189 _keys: PhantomData<K>,
190 base: Enumerate<vec::IntoIter<Option<V>>>,
191}
192
193impl<K: From<usize>, V> Iterator for IntoIter<K, V> {
194 type Item = (K, V);
195
196 fn next(&mut self) -> Option<Self::Item> {
197 loop {
198 if let (index, Some(value)) = self.base.next()? {
199 break Some((index.into(), value));
200 }
201 }
202 }
203}
204
205pub struct Drain<'a, K, V> {
206 _keys: PhantomData<K>,
207 base: Enumerate<vec::Drain<'a, Option<V>>>,
208}
209
210impl<K: From<usize>, V> Iterator for Drain<'_, K, V> {
211 type Item = (K, V);
212
213 fn next(&mut self) -> Option<Self::Item> {
214 loop {
215 if let (index, Some(value)) = self.base.next()? {
216 break Some((index.into(), value));
217 }
218 }
219 }
220}
221
222pub struct Values<'a, V> {
223 base: slice::Iter<'a, Option<V>>,
224}
225
226impl<'a, V> Iterator for Values<'a, V> {
227 type Item = &'a V;
228
229 fn next(&mut self) -> Option<Self::Item> {
230 loop {
231 if let Some(value) = self.base.next()? {
232 break Some(value);
233 }
234 }
235 }
236}
237
238pub struct ValuesMut<'a, V> {
239 base: slice::IterMut<'a, Option<V>>,
240}
241
242impl<'a, V> Iterator for ValuesMut<'a, V> {
243 type Item = &'a mut V;
244
245 fn next(&mut self) -> Option<Self::Item> {
246 loop {
247 if let Some(value) = self.base.next()? {
248 break Some(value);
249 }
250 }
251 }
252}