| 1 | // This file is part of ICU4X. For terms of use, please see the file | 
| 2 | // called LICENSE at the top level of the ICU4X source tree | 
|---|
| 3 | // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). | 
|---|
| 4 |  | 
|---|
| 5 | use super::*; | 
|---|
| 6 | use alloc::vec::Vec; | 
|---|
| 7 |  | 
|---|
| 8 | type MapF<K, V> = fn(&(K, V)) -> (&K, &V); | 
|---|
| 9 |  | 
|---|
| 10 | #[ inline] | 
|---|
| 11 | fn map_f<K, V>(input: &(K, V)) -> (&K, &V) { | 
|---|
| 12 | (&input.0, &input.1) | 
|---|
| 13 | } | 
|---|
| 14 |  | 
|---|
| 15 | type MapFMut<K, V> = fn(&mut (K, V)) -> (&K, &mut V); | 
|---|
| 16 |  | 
|---|
| 17 | #[ inline] | 
|---|
| 18 | fn map_f_mut<K, V>(input: &mut (K, V)) -> (&K, &mut V) { | 
|---|
| 19 | (&input.0, &mut input.1) | 
|---|
| 20 | } | 
|---|
| 21 |  | 
|---|
| 22 | impl<K, V> StoreConstEmpty<K, V> for Vec<(K, V)> { | 
|---|
| 23 | const EMPTY: Vec<(K, V)> = Vec::new(); | 
|---|
| 24 | } | 
|---|
| 25 |  | 
|---|
| 26 | impl<K, V> Store<K, V> for Vec<(K, V)> { | 
|---|
| 27 | #[ inline] | 
|---|
| 28 | fn lm_len(&self) -> usize { | 
|---|
| 29 | self.as_slice().len() | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 | #[ inline] | 
|---|
| 33 | fn lm_is_empty(&self) -> bool { | 
|---|
| 34 | self.as_slice().is_empty() | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | #[ inline] | 
|---|
| 38 | fn lm_get(&self, index: usize) -> Option<(&K, &V)> { | 
|---|
| 39 | self.as_slice().get(index).map(map_f) | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | #[ inline] | 
|---|
| 43 | fn lm_last(&self) -> Option<(&K, &V)> { | 
|---|
| 44 | self.as_slice().last().map(map_f) | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | #[ inline] | 
|---|
| 48 | fn lm_binary_search_by<F>(&self, mut cmp: F) -> Result<usize, usize> | 
|---|
| 49 | where | 
|---|
| 50 | F: FnMut(&K) -> Ordering, | 
|---|
| 51 | { | 
|---|
| 52 | self.as_slice().binary_search_by(|(k, _)| cmp(k)) | 
|---|
| 53 | } | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | impl<K, V> StoreSlice<K, V> for Vec<(K, V)> { | 
|---|
| 57 | type Slice = [(K, V)]; | 
|---|
| 58 |  | 
|---|
| 59 | fn lm_get_range(&self, range: Range<usize>) -> Option<&Self::Slice> { | 
|---|
| 60 | self.get(index:range) | 
|---|
| 61 | } | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | impl<K, V> StoreMut<K, V> for Vec<(K, V)> { | 
|---|
| 65 | #[ inline] | 
|---|
| 66 | fn lm_with_capacity(capacity: usize) -> Self { | 
|---|
| 67 | Self::with_capacity(capacity) | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | #[ inline] | 
|---|
| 71 | fn lm_reserve(&mut self, additional: usize) { | 
|---|
| 72 | self.reserve(additional) | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | #[ inline] | 
|---|
| 76 | fn lm_get_mut(&mut self, index: usize) -> Option<(&K, &mut V)> { | 
|---|
| 77 | self.as_mut_slice().get_mut(index).map(map_f_mut) | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | #[ inline] | 
|---|
| 81 | fn lm_push(&mut self, key: K, value: V) { | 
|---|
| 82 | self.push((key, value)) | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | #[ inline] | 
|---|
| 86 | fn lm_insert(&mut self, index: usize, key: K, value: V) { | 
|---|
| 87 | self.insert(index, (key, value)) | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | #[ inline] | 
|---|
| 91 | fn lm_remove(&mut self, index: usize) -> (K, V) { | 
|---|
| 92 | self.remove(index) | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | #[ inline] | 
|---|
| 96 | fn lm_clear(&mut self) { | 
|---|
| 97 | self.clear() | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | #[ inline] | 
|---|
| 101 | fn lm_retain<F>(&mut self, mut predicate: F) | 
|---|
| 102 | where | 
|---|
| 103 | F: FnMut(&K, &V) -> bool, | 
|---|
| 104 | { | 
|---|
| 105 | self.retain(|(k, v)| predicate(k, v)) | 
|---|
| 106 | } | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | impl<K: Ord, V> StoreFromIterable<K, V> for Vec<(K, V)> { | 
|---|
| 110 | fn lm_sort_from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self { | 
|---|
| 111 | let iter = iter.into_iter(); | 
|---|
| 112 | let mut container = match iter.size_hint() { | 
|---|
| 113 | (_, Some(upper)) => Self::with_capacity(upper), | 
|---|
| 114 | (lower, None) => Self::with_capacity(lower), | 
|---|
| 115 | }; | 
|---|
| 116 |  | 
|---|
| 117 | for (key, value) in iter { | 
|---|
| 118 | if let Some(last) = container.lm_last() { | 
|---|
| 119 | if last.0 >= &key { | 
|---|
| 120 | match container.lm_binary_search_by(|k| k.cmp(&key)) { | 
|---|
| 121 | #[ allow(clippy::unwrap_used)] // Index came from binary_search | 
|---|
| 122 | Ok(found) => { | 
|---|
| 123 | let _ = | 
|---|
| 124 | core::mem::replace(container.lm_get_mut(found).unwrap().1, value); | 
|---|
| 125 | } | 
|---|
| 126 | Err(ins) => { | 
|---|
| 127 | container.insert(ins, (key, value)); | 
|---|
| 128 | } | 
|---|
| 129 | } | 
|---|
| 130 | } else { | 
|---|
| 131 | container.push((key, value)) | 
|---|
| 132 | } | 
|---|
| 133 | } else { | 
|---|
| 134 | container.push((key, value)) | 
|---|
| 135 | } | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | container | 
|---|
| 139 | } | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | impl<'a, K: 'a, V: 'a> StoreIterable<'a, K, V> for Vec<(K, V)> { | 
|---|
| 143 | type KeyValueIter = core::iter::Map<core::slice::Iter<'a, (K, V)>, MapF<K, V>>; | 
|---|
| 144 |  | 
|---|
| 145 | #[ inline] | 
|---|
| 146 | fn lm_iter(&'a self) -> Self::KeyValueIter { | 
|---|
| 147 | self.as_slice().iter().map(map_f) | 
|---|
| 148 | } | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | impl<'a, K: 'a, V: 'a> StoreIterableMut<'a, K, V> for Vec<(K, V)> { | 
|---|
| 152 | type KeyValueIterMut = core::iter::Map<core::slice::IterMut<'a, (K, V)>, MapFMut<K, V>>; | 
|---|
| 153 |  | 
|---|
| 154 | #[ inline] | 
|---|
| 155 | fn lm_iter_mut(&'a mut self) -> Self::KeyValueIterMut { | 
|---|
| 156 | self.as_mut_slice().iter_mut().map(map_f_mut) | 
|---|
| 157 | } | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | impl<K, V> StoreIntoIterator<K, V> for Vec<(K, V)> { | 
|---|
| 161 | type KeyValueIntoIter = alloc::vec::IntoIter<(K, V)>; | 
|---|
| 162 |  | 
|---|
| 163 | #[ inline] | 
|---|
| 164 | fn lm_into_iter(self) -> Self::KeyValueIntoIter { | 
|---|
| 165 | IntoIterator::into_iter(self) | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | #[ inline] | 
|---|
| 169 | fn lm_extend_end(&mut self, other: Self) { | 
|---|
| 170 | self.extend(iter:other) | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | #[ inline] | 
|---|
| 174 | fn lm_extend_start(&mut self, other: Self) { | 
|---|
| 175 | self.splice(range:0..0, replace_with:other); | 
|---|
| 176 | } | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | impl<K, V> StoreFromIterator<K, V> for Vec<(K, V)> {} | 
|---|
| 180 |  | 
|---|
| 181 | #[ test] | 
|---|
| 182 | fn test_vec_impl() { | 
|---|
| 183 | crate::testing::check_store_full::<Vec<(u32, u64)>>(); | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|