| 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::ShortBoxSlice; |
| 6 | use super::ShortBoxSliceInner; |
| 7 | use super::ShortBoxSliceIntoIter; |
| 8 | use alloc::vec::Vec; |
| 9 | use litemap::store::*; |
| 10 | |
| 11 | impl<K, V> StoreConstEmpty<K, V> for ShortBoxSlice<(K, V)> { |
| 12 | const EMPTY: ShortBoxSlice<(K, V)> = ShortBoxSlice::new(); |
| 13 | } |
| 14 | |
| 15 | impl<K, V> Store<K, V> for ShortBoxSlice<(K, V)> { |
| 16 | #[inline ] |
| 17 | fn lm_len(&self) -> usize { |
| 18 | self.len() |
| 19 | } |
| 20 | |
| 21 | #[inline ] |
| 22 | fn lm_is_empty(&self) -> bool { |
| 23 | use ShortBoxSliceInner::*; |
| 24 | matches!(self.0, ZeroOne(None)) |
| 25 | } |
| 26 | |
| 27 | #[inline ] |
| 28 | fn lm_get(&self, index: usize) -> Option<(&K, &V)> { |
| 29 | self.get(index).map(|elt| (&elt.0, &elt.1)) |
| 30 | } |
| 31 | |
| 32 | #[inline ] |
| 33 | fn lm_last(&self) -> Option<(&K, &V)> { |
| 34 | use ShortBoxSliceInner::*; |
| 35 | match self.0 { |
| 36 | ZeroOne(ref v) => v.as_ref(), |
| 37 | Multi(ref v) => v.last(), |
| 38 | } |
| 39 | .map(|elt| (&elt.0, &elt.1)) |
| 40 | } |
| 41 | |
| 42 | #[inline ] |
| 43 | fn lm_binary_search_by<F>(&self, mut cmp: F) -> Result<usize, usize> |
| 44 | where |
| 45 | F: FnMut(&K) -> core::cmp::Ordering, |
| 46 | { |
| 47 | self.binary_search_by(|(k, _)| cmp(k)) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | impl<K: Ord, V> StoreFromIterable<K, V> for ShortBoxSlice<(K, V)> { |
| 52 | fn lm_sort_from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self { |
| 53 | let v: Vec<(K, V)> = Vec::lm_sort_from_iter(iter); |
| 54 | v.into() |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | impl<K, V> StoreMut<K, V> for ShortBoxSlice<(K, V)> { |
| 59 | fn lm_with_capacity(_capacity: usize) -> Self { |
| 60 | ShortBoxSlice::new() |
| 61 | } |
| 62 | |
| 63 | fn lm_reserve(&mut self, _additional: usize) {} |
| 64 | |
| 65 | fn lm_get_mut(&mut self, index: usize) -> Option<(&K, &mut V)> { |
| 66 | self.get_mut(index).map(|elt| (&elt.0, &mut elt.1)) |
| 67 | } |
| 68 | |
| 69 | fn lm_push(&mut self, key: K, value: V) { |
| 70 | self.push((key, value)) |
| 71 | } |
| 72 | |
| 73 | fn lm_insert(&mut self, index: usize, key: K, value: V) { |
| 74 | self.insert(index, (key, value)) |
| 75 | } |
| 76 | |
| 77 | fn lm_remove(&mut self, index: usize) -> (K, V) { |
| 78 | self.remove(index) |
| 79 | } |
| 80 | |
| 81 | fn lm_clear(&mut self) { |
| 82 | self.clear(); |
| 83 | } |
| 84 | |
| 85 | fn lm_retain<F>(&mut self, mut predicate: F) |
| 86 | where |
| 87 | F: FnMut(&K, &V) -> bool, |
| 88 | { |
| 89 | self.retain(|(k, v)| predicate(k, v)) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | impl<'a, K: 'a, V: 'a> StoreIterable<'a, K, V> for ShortBoxSlice<(K, V)> { |
| 94 | type KeyValueIter = |
| 95 | core::iter::Map<core::slice::Iter<'a, (K, V)>, for<'r> fn(&'r (K, V)) -> (&'r K, &'r V)>; |
| 96 | |
| 97 | fn lm_iter(&'a self) -> Self::KeyValueIter { |
| 98 | self.iter().map(|elt: &(K, V)| (&elt.0, &elt.1)) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | impl<K, V> StoreFromIterator<K, V> for ShortBoxSlice<(K, V)> {} |
| 103 | |
| 104 | impl<'a, K: 'a, V: 'a> StoreIterableMut<'a, K, V> for ShortBoxSlice<(K, V)> { |
| 105 | type KeyValueIterMut = core::iter::Map< |
| 106 | core::slice::IterMut<'a, (K, V)>, |
| 107 | for<'r> fn(&'r mut (K, V)) -> (&'r K, &'r mut V), |
| 108 | >; |
| 109 | |
| 110 | fn lm_iter_mut( |
| 111 | &'a mut self, |
| 112 | ) -> <Self as litemap::store::StoreIterableMut<'a, K, V>>::KeyValueIterMut { |
| 113 | self.iter_mut().map(|elt: &mut (K, V)| (&elt.0, &mut elt.1)) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | impl<K, V> StoreIntoIterator<K, V> for ShortBoxSlice<(K, V)> { |
| 118 | type KeyValueIntoIter = ShortBoxSliceIntoIter<(K, V)>; |
| 119 | |
| 120 | fn lm_into_iter(self) -> Self::KeyValueIntoIter { |
| 121 | self.into_iter() |
| 122 | } |
| 123 | |
| 124 | // leave lm_extend_end as default |
| 125 | |
| 126 | // leave lm_extend_start as default |
| 127 | } |
| 128 | |
| 129 | #[test ] |
| 130 | fn test_short_slice_impl() { |
| 131 | litemap::testing::check_store::<ShortBoxSlice<(u32, u64)>>(); |
| 132 | } |
| 133 | |
| 134 | #[test ] |
| 135 | fn test_short_slice_impl_full() { |
| 136 | litemap::testing::check_store_full::<ShortBoxSlice<(u32, u64)>>(); |
| 137 | } |
| 138 | |