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
use std::cmp::Ordering; use std::slice::{Iter, IterMut}; use std::vec::IntoIter; use SortedVec; pub struct SortableKeyPair<K: Ord, V>(pub K, pub V); impl<K: Ord, V> PartialEq for SortableKeyPair<K, V> { fn eq(&self, y: &Self) -> bool { self.0 == y.0 } fn ne(&self, y: &Self) -> bool { self.0 != y.0 } } impl<K: Ord, V> Eq for SortableKeyPair<K, V> { } impl<K: Ord, V> PartialOrd for SortableKeyPair<K, V> { fn partial_cmp(&self, y: &Self) -> Option<Ordering> { self.0.partial_cmp(&y.0) } } impl<K: Ord, V> Ord for SortableKeyPair<K, V> { fn cmp(&self, y: &Self) -> Ordering { self.0.cmp(&y.0) } } pub struct SortedMap<K: Ord, V> { v: SortedVec<SortableKeyPair<K, V>> } impl<K: Ord, V> SortedMap<K, V> { pub fn new() -> Self { SortedMap { v: SortedVec::new() } } pub fn with_capacity(size: usize) -> Self { SortedMap { v: SortedVec::with_capacity(size) } } pub fn from_vec(mut v: Vec<SortableKeyPair<K, V>>) -> Self { v.sort_by(|x, y| x.0.cmp(&y.0)); SortedMap::from_vec_unchecked(v) } pub fn from_vec_unchecked(v: Vec<SortableKeyPair<K, V>>) -> Self { SortedMap { v: SortedVec::from_vec_unchecked(v) } } pub fn into_vec(self) -> Vec<SortableKeyPair<K, V>> { self.v.into_vec() } pub fn capacity(&self) -> usize { self.v.capacity() } pub fn reserve(&mut self, additional: usize) { self.v.reserve(additional) } pub fn shrink_to_fit(&mut self) { self.v.shrink_to_fit() } pub fn iter(&self) -> Iter<SortableKeyPair<K, V>> { self.v.iter() } pub fn iter_mut(&mut self) -> IterMut<SortableKeyPair<K, V>> { self.v.iter_mut() } pub fn into_iter(self) -> IntoIter<SortableKeyPair<K, V>> { self.v.into_iter() } pub fn first(&self) -> Option<&SortableKeyPair<K, V>> { self.v.first() } pub fn last(&self) -> Option<&SortableKeyPair<K, V>> { self.v.last() } pub fn binary_search(&self, k: &K) -> Result<usize, usize> { self.v.binary_search_by(|x| x.0.cmp(k)) } pub fn contains_key(&self, k: &K) -> bool { self.binary_search(k).is_ok() } pub fn insert(&mut self, x: SortableKeyPair<K, V>) -> bool { let bs = self.binary_search(&x.0); match bs { Ok(idx) => { self.v[idx].1 = x.1; false }, Err(idx) => { self.v.insert_at(idx, x); true }, } } pub fn remove(&mut self, k: &K) -> bool { let bs = self.binary_search(k); match bs { Ok(idx) => { self.v.remove_at(idx); true }, Err(_) => false, } } pub fn remove_at(&mut self, idx: usize) -> SortableKeyPair<K, V> { self.v.remove_at(idx) } pub fn clear(&mut self) { self.v.clear() } } impl<K: Ord, V: PartialEq> SortedMap<K, V> { pub fn contains_value(&self, v: &V) -> bool { self.v.iter().any(|x| x.1 == *v) } pub fn contains(&self, kv: &SortableKeyPair<K, V>) -> bool { let bs = self.binary_search(&kv.0); match bs { Ok(idx) => self.v[idx].1 == kv.1, Err(_) => false, } } }