| 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 | |
| 7 | type MapF<K, V> = fn(&(K, V)) -> (&K, &V); |
| 8 | |
| 9 | #[inline ] |
| 10 | fn map_f<K, V>(input: &(K, V)) -> (&K, &V) { |
| 11 | (&input.0, &input.1) |
| 12 | } |
| 13 | |
| 14 | impl<'a, K: 'a, V: 'a> StoreConstEmpty<K, V> for &'a [(K, V)] { |
| 15 | const EMPTY: &'a [(K, V)] = &[]; |
| 16 | } |
| 17 | |
| 18 | impl<'a, K: 'a, V: 'a> Store<K, V> for &'a [(K, V)] { |
| 19 | #[inline ] |
| 20 | fn lm_len(&self) -> usize { |
| 21 | self.len() |
| 22 | } |
| 23 | |
| 24 | #[inline ] |
| 25 | fn lm_is_empty(&self) -> bool { |
| 26 | self.is_empty() |
| 27 | } |
| 28 | |
| 29 | #[inline ] |
| 30 | fn lm_get(&self, index: usize) -> Option<(&K, &V)> { |
| 31 | self.get(index).map(map_f) |
| 32 | } |
| 33 | |
| 34 | #[inline ] |
| 35 | fn lm_last(&self) -> Option<(&K, &V)> { |
| 36 | self.last().map(map_f) |
| 37 | } |
| 38 | |
| 39 | #[inline ] |
| 40 | fn lm_binary_search_by<F>(&self, mut cmp: F) -> Result<usize, usize> |
| 41 | where |
| 42 | F: FnMut(&K) -> Ordering, |
| 43 | { |
| 44 | self.binary_search_by(|(k, _)| cmp(k)) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | impl<K, V> StoreSlice<K, V> for &[(K, V)] { |
| 49 | type Slice = [(K, V)]; |
| 50 | |
| 51 | fn lm_get_range(&self, range: Range<usize>) -> Option<&Self::Slice> { |
| 52 | self.get(index:range) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | impl<'a, K: 'a, V: 'a> StoreIterable<'a, K, V> for &'a [(K, V)] { |
| 57 | type KeyValueIter = core::iter::Map<core::slice::Iter<'a, (K, V)>, MapF<K, V>>; |
| 58 | |
| 59 | #[inline ] |
| 60 | fn lm_iter(&'a self) -> Self::KeyValueIter { |
| 61 | self.iter().map(map_f) |
| 62 | } |
| 63 | } |
| 64 | |