microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
ac22b7ee6ec9d38efb5324d7543b832c55b56f2c

Branches

Tags

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

Clone

HTTPS

Download ZIP

compiler/qsc_data_structures/src/index_map.rs

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