1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
|
//! A key-value map that can also model array-like structures.
use std::collections::BTreeMap;
use std::fmt::{self, Debug, Display, Formatter};
use std::iter::{Extend, FromIterator};
use std::ops::Index;
use crate::syntax::{Span, Spanned};
/// A dictionary data structure, which maps from integers and strings to a
/// generic value type.
///
/// The dictionary can be used to model arrays by assigning values to successive
/// indices from `0..n`. The `push` method offers special support for this
/// pattern.
#[derive(Clone)]
pub struct Dict<V> {
nums: BTreeMap<u64, V>,
strs: BTreeMap<String, V>,
lowest_free: u64,
}
impl<V> Dict<V> {
/// Create a new empty dictionary.
pub fn new() -> Self {
Self {
nums: BTreeMap::new(),
strs: BTreeMap::new(),
lowest_free: 0,
}
}
/// The total number of entries in the dictionary.
pub fn len(&self) -> usize {
self.nums.len() + self.strs.len()
}
/// Whether the dictionary contains no entries.
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// The first number key-value pair (with lowest number).
pub fn first(&self) -> Option<(u64, &V)> {
self.nums.iter().next().map(|(&k, v)| (k, v))
}
/// The last number key-value pair (with highest number).
pub fn last(&self) -> Option<(u64, &V)> {
self.nums.iter().next_back().map(|(&k, v)| (k, v))
}
/// Get a reference to the value with the given key.
pub fn get<'a, K>(&self, key: K) -> Option<&V>
where
K: Into<RefKey<'a>>,
{
match key.into() {
RefKey::Num(num) => self.nums.get(&num),
RefKey::Str(string) => self.strs.get(string),
}
}
/// Borrow the value with the given key mutably.
pub fn get_mut<'a, K>(&mut self, key: K) -> Option<&mut V>
where
K: Into<RefKey<'a>>,
{
match key.into() {
RefKey::Num(num) => self.nums.get_mut(&num),
RefKey::Str(string) => self.strs.get_mut(string),
}
}
/// Insert a value into the dictionary.
pub fn insert<K>(&mut self, key: K, value: V)
where
K: Into<DictKey>,
{
match key.into() {
DictKey::Num(num) => {
self.nums.insert(num, value);
if self.lowest_free == num {
self.lowest_free += 1;
}
}
DictKey::Str(string) => {
self.strs.insert(string, value);
}
}
}
/// Remove the value with the given key from the dictionary.
pub fn remove<'a, K>(&mut self, key: K) -> Option<V>
where
K: Into<RefKey<'a>>,
{
match key.into() {
RefKey::Num(num) => {
self.lowest_free = self.lowest_free.min(num);
self.nums.remove(&num)
}
RefKey::Str(string) => self.strs.remove(string),
}
}
/// Append a value to the dictionary.
///
/// This will associate the `value` with the lowest free number key (zero if
/// there is no number key so far).
pub fn push(&mut self, value: V) {
while self.nums.contains_key(&self.lowest_free) {
self.lowest_free += 1;
}
self.nums.insert(self.lowest_free, value);
self.lowest_free += 1;
}
}
impl<'a, K, V> Index<K> for Dict<V>
where
K: Into<RefKey<'a>>,
{
type Output = V;
fn index(&self, index: K) -> &Self::Output {
self.get(index).expect("key not in dict")
}
}
impl<V: Eq> Eq for Dict<V> {}
impl<V: PartialEq> PartialEq for Dict<V> {
fn eq(&self, other: &Self) -> bool {
self.iter().eq(other.iter())
}
}
impl<V> Default for Dict<V> {
fn default() -> Self {
Self::new()
}
}
impl<V: Debug> Debug for Dict<V> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
if self.is_empty() {
return f.write_str("()");
}
let mut builder = f.debug_tuple("");
struct Entry<'a>(bool, &'a dyn Display, &'a dyn Debug);
impl<'a> Debug for Entry<'a> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
if self.0 {
f.write_str("\"")?;
}
self.1.fmt(f)?;
if self.0 {
f.write_str("\"")?;
}
if f.alternate() {
f.write_str(" = ")?;
} else {
f.write_str("=")?;
}
self.2.fmt(f)
}
}
for (key, value) in self.nums() {
builder.field(&Entry(false, &key, &value));
}
for (key, value) in self.strs() {
builder.field(&Entry(key.contains(' '), &key, &value));
}
builder.finish()
}
}
/// Iteration.
impl<V> Dict<V> {
/// Iterator over all borrowed keys and values.
pub fn iter(&self) -> impl Iterator<Item = (RefKey, &V)> {
self.into_iter()
}
/// Iterator over all borrowed keys and values.
pub fn iter_mut(&mut self) -> impl Iterator<Item = (RefKey, &mut V)> {
self.into_iter()
}
/// Iterate over all values in the dictionary.
pub fn values(&self) -> impl Iterator<Item = &V> {
self.nums().map(|(_, v)| v).chain(self.strs().map(|(_, v)| v))
}
/// Iterate over all values in the dictionary.
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V> {
self.nums
.iter_mut()
.map(|(_, v)| v)
.chain(self.strs.iter_mut().map(|(_, v)| v))
}
/// Move into an owned iterator over all values in the dictionary.
pub fn into_values(self) -> impl Iterator<Item = V> {
self.nums
.into_iter()
.map(|(_, v)| v)
.chain(self.strs.into_iter().map(|(_, v)| v))
}
/// Iterate over the number key-value pairs.
pub fn nums(&self) -> std::collections::btree_map::Iter<u64, V> {
self.nums.iter()
}
/// Iterate mutably over the number key-value pairs.
pub fn nums_mut(&mut self) -> std::collections::btree_map::IterMut<u64, V> {
self.nums.iter_mut()
}
/// Iterate over the number key-value pairs.
pub fn into_nums(self) -> std::collections::btree_map::IntoIter<u64, V> {
self.nums.into_iter()
}
/// Iterate over the string key-value pairs.
pub fn strs(&self) -> std::collections::btree_map::Iter<String, V> {
self.strs.iter()
}
/// Iterate mutably over the string key-value pairs.
pub fn strs_mut(&mut self) -> std::collections::btree_map::IterMut<String, V> {
self.strs.iter_mut()
}
/// Iterate over the string key-value pairs.
pub fn into_strs(self) -> std::collections::btree_map::IntoIter<String, V> {
self.strs.into_iter()
}
}
impl<V> Extend<(DictKey, V)> for Dict<V> {
fn extend<T: IntoIterator<Item = (DictKey, V)>>(&mut self, iter: T) {
for (key, value) in iter.into_iter() {
self.insert(key, value);
}
}
}
impl<V> FromIterator<(DictKey, V)> for Dict<V> {
fn from_iter<T: IntoIterator<Item = (DictKey, V)>>(iter: T) -> Self {
let mut v = Self::new();
v.extend(iter);
v
}
}
impl<V> IntoIterator for Dict<V> {
type Item = (DictKey, V);
type IntoIter = std::iter::Chain<
std::iter::Map<
std::collections::btree_map::IntoIter<u64, V>,
fn((u64, V)) -> (DictKey, V),
>,
std::iter::Map<
std::collections::btree_map::IntoIter<String, V>,
fn((String, V)) -> (DictKey, V),
>,
>;
fn into_iter(self) -> Self::IntoIter {
let nums = self.nums.into_iter().map((|(k, v)| (DictKey::Num(k), v)) as _);
let strs = self.strs.into_iter().map((|(k, v)| (DictKey::Str(k), v)) as _);
nums.chain(strs)
}
}
impl<'a, V> IntoIterator for &'a Dict<V> {
type Item = (RefKey<'a>, &'a V);
type IntoIter = std::iter::Chain<
std::iter::Map<
std::collections::btree_map::Iter<'a, u64, V>,
fn((&'a u64, &'a V)) -> (RefKey<'a>, &'a V),
>,
std::iter::Map<
std::collections::btree_map::Iter<'a, String, V>,
fn((&'a String, &'a V)) -> (RefKey<'a>, &'a V),
>,
>;
fn into_iter(self) -> Self::IntoIter {
let nums = self.nums().map((|(k, v): (&u64, _)| (RefKey::Num(*k), v)) as _);
let strs = self.strs().map((|(k, v): (&'a String, _)| (RefKey::Str(k), v)) as _);
nums.chain(strs)
}
}
impl<'a, V> IntoIterator for &'a mut Dict<V> {
type Item = (RefKey<'a>, &'a mut V);
type IntoIter = std::iter::Chain<
std::iter::Map<
std::collections::btree_map::IterMut<'a, u64, V>,
fn((&'a u64, &'a mut V)) -> (RefKey<'a>, &'a mut V),
>,
std::iter::Map<
std::collections::btree_map::IterMut<'a, String, V>,
fn((&'a String, &'a mut V)) -> (RefKey<'a>, &'a mut V),
>,
>;
fn into_iter(self) -> Self::IntoIter {
let nums = self
.nums
.iter_mut()
.map((|(k, v): (&u64, _)| (RefKey::Num(*k), v)) as _);
let strs = self
.strs
.iter_mut()
.map((|(k, v): (&'a String, _)| (RefKey::Str(k), v)) as _);
nums.chain(strs)
}
}
/// The owned variant of a dictionary key.
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum DictKey {
Num(u64),
Str(String),
}
impl From<&Self> for DictKey {
fn from(key: &Self) -> Self {
key.clone()
}
}
impl From<RefKey<'_>> for DictKey {
fn from(key: RefKey<'_>) -> Self {
match key {
RefKey::Num(num) => Self::Num(num),
RefKey::Str(string) => Self::Str(string.to_string()),
}
}
}
impl From<u64> for DictKey {
fn from(num: u64) -> Self {
Self::Num(num)
}
}
impl From<String> for DictKey {
fn from(string: String) -> Self {
Self::Str(string)
}
}
impl From<&'static str> for DictKey {
fn from(string: &'static str) -> Self {
Self::Str(string.to_string())
}
}
/// The borrowed variant of a dictionary key.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum RefKey<'a> {
Num(u64),
Str(&'a str),
}
impl From<u64> for RefKey<'static> {
fn from(num: u64) -> Self {
Self::Num(num)
}
}
impl<'a> From<&'a String> for RefKey<'a> {
fn from(string: &'a String) -> Self {
Self::Str(&string)
}
}
impl<'a> From<&'a str> for RefKey<'a> {
fn from(string: &'a str) -> Self {
Self::Str(string)
}
}
/// A dictionary entry which combines key span and value.
///
/// This exists because a key in a directory can't track its span by itself.
#[derive(Clone, PartialEq)]
pub struct SpannedEntry<V> {
pub key_span: Span,
pub value: Spanned<V>,
}
impl<V> SpannedEntry<V> {
/// Create a new entry.
pub fn new(key: Span, val: Spanned<V>) -> Self {
Self { key_span: key, value: val }
}
/// Create an entry with the same span for key and value.
pub fn value(val: Spanned<V>) -> Self {
Self { key_span: val.span, value: val }
}
/// Convert from `&SpannedEntry<T>` to `SpannedEntry<&T>`
pub fn as_ref(&self) -> SpannedEntry<&V> {
SpannedEntry {
key_span: self.key_span,
value: self.value.as_ref(),
}
}
/// Map the entry to a different value type.
pub fn map<U>(self, f: impl FnOnce(V) -> U) -> SpannedEntry<U> {
SpannedEntry {
key_span: self.key_span,
value: self.value.map(f),
}
}
}
impl<V: Debug> Debug for SpannedEntry<V> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
if f.alternate() {
f.write_str("key")?;
self.key_span.fmt(f)?;
f.write_str(" ")?;
}
self.value.fmt(f)
}
}
#[cfg(test)]
mod tests {
use super::Dict;
#[test]
fn test_dict_different_key_types_dont_interfere() {
let mut dict = Dict::new();
dict.insert(10, "hello");
dict.insert("twenty", "there");
assert_eq!(dict.len(), 2);
assert_eq!(dict[10], "hello");
assert_eq!(dict["twenty"], "there");
}
#[test]
fn test_dict_push_skips_already_inserted_keys() {
let mut dict = Dict::new();
dict.insert(2, "2");
dict.push("0");
dict.insert(3, "3");
dict.push("1");
dict.push("4");
assert_eq!(dict.len(), 5);
assert_eq!(dict[0], "0");
assert_eq!(dict[1], "1");
assert_eq!(dict[2], "2");
assert_eq!(dict[3], "3");
assert_eq!(dict[4], "4");
}
#[test]
fn test_dict_push_remove_push_reuses_index() {
let mut dict = Dict::new();
dict.push("0");
dict.push("1");
dict.push("2");
dict.remove(1);
dict.push("a");
dict.push("3");
assert_eq!(dict.len(), 4);
assert_eq!(dict[0], "0");
assert_eq!(dict[1], "a");
assert_eq!(dict[2], "2");
assert_eq!(dict[3], "3");
}
#[test]
fn test_dict_first_and_last_are_correct() {
let mut dict = Dict::new();
assert_eq!(dict.first(), None);
assert_eq!(dict.last(), None);
dict.insert(4, "hi");
dict.insert("string", "hi");
assert_eq!(dict.first(), Some((4, &"hi")));
assert_eq!(dict.last(), Some((4, &"hi")));
dict.insert(2, "bye");
assert_eq!(dict.first(), Some((2, &"bye")));
assert_eq!(dict.last(), Some((4, &"hi")));
}
#[test]
fn test_dict_format_debug() {
let mut dict = Dict::new();
assert_eq!(format!("{:?}", dict), "()");
assert_eq!(format!("{:#?}", dict), "()");
dict.insert(10, "hello");
dict.insert("twenty", "there");
dict.insert("sp ace", "quotes");
assert_eq!(
format!("{:?}", dict),
r#"(10="hello", "sp ace"="quotes", twenty="there")"#,
);
assert_eq!(format!("{:#?}", dict).lines().collect::<Vec<_>>(), [
"(",
r#" 10 = "hello","#,
r#" "sp ace" = "quotes","#,
r#" twenty = "there","#,
")",
]);
}
}
|