1 #[cfg(feature = "vec_map")] 2 pub use vec_map::{Values, VecMap}; 3 4 #[cfg(not(feature = "vec_map"))] 5 pub use self::vec_map::{Values, VecMap}; 6 7 #[cfg(not(feature = "vec_map"))] 8 mod vec_map { 9 use std::collections::btree_map; 10 use std::collections::BTreeMap; 11 use std::fmt::{self, Debug, Formatter}; 12 13 #[derive(Clone, Default, Debug)] 14 pub struct VecMap<V> { 15 inner: BTreeMap<usize, V>, 16 } 17 18 impl<V> VecMap<V> { new() -> Self19 pub fn new() -> Self { 20 VecMap { 21 inner: Default::default(), 22 } 23 } 24 len(&self) -> usize25 pub fn len(&self) -> usize { 26 self.inner.len() 27 } 28 is_empty(&self) -> bool29 pub fn is_empty(&self) -> bool { 30 self.inner.is_empty() 31 } 32 insert(&mut self, key: usize, value: V) -> Option<V>33 pub fn insert(&mut self, key: usize, value: V) -> Option<V> { 34 self.inner.insert(key, value) 35 } 36 values(&self) -> Values<V>37 pub fn values(&self) -> Values<V> { 38 self.inner.values() 39 } 40 iter(&self) -> Iter<V>41 pub fn iter(&self) -> Iter<V> { 42 Iter { 43 inner: self.inner.iter(), 44 } 45 } 46 contains_key(&self, key: usize) -> bool47 pub fn contains_key(&self, key: usize) -> bool { 48 self.inner.contains_key(&key) 49 } 50 entry(&mut self, key: usize) -> Entry<V>51 pub fn entry(&mut self, key: usize) -> Entry<V> { 52 self.inner.entry(key) 53 } 54 get(&self, key: usize) -> Option<&V>55 pub fn get(&self, key: usize) -> Option<&V> { 56 self.inner.get(&key) 57 } 58 } 59 60 pub type Values<'a, V> = btree_map::Values<'a, usize, V>; 61 62 pub type Entry<'a, V> = btree_map::Entry<'a, usize, V>; 63 64 #[derive(Clone)] 65 pub struct Iter<'a, V: 'a> { 66 inner: btree_map::Iter<'a, usize, V>, 67 } 68 69 impl<'a, V: 'a + Debug> Debug for Iter<'a, V> { fmt(&self, f: &mut Formatter) -> fmt::Result70 fn fmt(&self, f: &mut Formatter) -> fmt::Result { 71 f.debug_list().entries(self.inner.clone()).finish() 72 } 73 } 74 75 impl<'a, V: 'a> Iterator for Iter<'a, V> { 76 type Item = (usize, &'a V); 77 next(&mut self) -> Option<Self::Item>78 fn next(&mut self) -> Option<Self::Item> { 79 self.inner.next().map(|(k, v)| (*k, v)) 80 } 81 } 82 83 impl<'a, V: 'a> DoubleEndedIterator for Iter<'a, V> { next_back(&mut self) -> Option<Self::Item>84 fn next_back(&mut self) -> Option<Self::Item> { 85 self.inner.next_back().map(|(k, v)| (*k, v)) 86 } 87 } 88 } 89