|
| 1 | +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use std::collections::btree_map; |
| 12 | +use std::collections::BTreeMap; |
| 13 | + |
| 14 | +/// A thin wrapper around BTreeMap that avoids allocating upon creation. |
| 15 | +/// |
| 16 | +/// Vec, HashSet and HashMap all have the nice feature that they don't do any |
| 17 | +/// heap allocation when creating a new structure of the default size. In |
| 18 | +/// contrast, BTreeMap *does* allocate in that situation. The compiler uses |
| 19 | +/// B-Tree maps in some places such that many maps are created but few are |
| 20 | +/// inserted into, so having a BTreeMap alternative that avoids allocating on |
| 21 | +/// creation is a performance win. |
| 22 | +/// |
| 23 | +/// Only a fraction of BTreeMap's functionality is currently supported. |
| 24 | +/// Additional functionality should be added on demand. |
| 25 | +#[derive(Debug)] |
| 26 | +pub struct LazyBTreeMap<K, V>(Option<BTreeMap<K, V>>); |
| 27 | + |
| 28 | +impl<K, V> LazyBTreeMap<K, V> { |
| 29 | + pub fn new() -> LazyBTreeMap<K, V> { |
| 30 | + LazyBTreeMap(None) |
| 31 | + } |
| 32 | + |
| 33 | + pub fn iter(&self) -> Iter<K, V> { |
| 34 | + Iter(self.0.as_ref().map(|btm| btm.iter())) |
| 35 | + } |
| 36 | + |
| 37 | + pub fn is_empty(&self) -> bool { |
| 38 | + self.0.as_ref().map_or(true, |btm| btm.is_empty()) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl<K: Ord, V> LazyBTreeMap<K, V> { |
| 43 | + fn instantiate(&mut self) -> &mut BTreeMap<K, V> { |
| 44 | + if let Some(ref mut btm) = self.0 { |
| 45 | + btm |
| 46 | + } else { |
| 47 | + let btm = BTreeMap::new(); |
| 48 | + self.0 = Some(btm); |
| 49 | + self.0.as_mut().unwrap() |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + pub fn insert(&mut self, key: K, value: V) -> Option<V> { |
| 54 | + self.instantiate().insert(key, value) |
| 55 | + } |
| 56 | + |
| 57 | + pub fn entry(&mut self, key: K) -> btree_map::Entry<K, V> { |
| 58 | + self.instantiate().entry(key) |
| 59 | + } |
| 60 | + |
| 61 | + pub fn values<'a>(&'a self) -> Values<'a, K, V> { |
| 62 | + Values(self.0.as_ref().map(|btm| btm.values())) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl<K: Ord, V> Default for LazyBTreeMap<K, V> { |
| 67 | + fn default() -> LazyBTreeMap<K, V> { |
| 68 | + LazyBTreeMap::new() |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl<'a, K: 'a, V: 'a> IntoIterator for &'a LazyBTreeMap<K, V> { |
| 73 | + type Item = (&'a K, &'a V); |
| 74 | + type IntoIter = Iter<'a, K, V>; |
| 75 | + |
| 76 | + fn into_iter(self) -> Iter<'a, K, V> { |
| 77 | + self.iter() |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +pub struct Iter<'a, K: 'a, V: 'a>(Option<btree_map::Iter<'a, K, V>>); |
| 82 | + |
| 83 | +impl<'a, K: 'a, V: 'a> Iterator for Iter<'a, K, V> { |
| 84 | + type Item = (&'a K, &'a V); |
| 85 | + |
| 86 | + fn next(&mut self) -> Option<(&'a K, &'a V)> { |
| 87 | + self.0.as_mut().and_then(|iter| iter.next()) |
| 88 | + } |
| 89 | + |
| 90 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 91 | + self.0.as_ref().map_or_else(|| (0, Some(0)), |iter| iter.size_hint()) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +pub struct Values<'a, K: 'a, V: 'a>(Option<btree_map::Values<'a, K, V>>); |
| 96 | + |
| 97 | +impl<'a, K, V> Iterator for Values<'a, K, V> { |
| 98 | + type Item = &'a V; |
| 99 | + |
| 100 | + fn next(&mut self) -> Option<&'a V> { |
| 101 | + self.0.as_mut().and_then(|values| values.next()) |
| 102 | + } |
| 103 | + |
| 104 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 105 | + self.0.as_ref().map_or_else(|| (0, Some(0)), |values| values.size_hint()) |
| 106 | + } |
| 107 | +} |
| 108 | + |
0 commit comments