| 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 litemap::store::*; |
| 6 |
use litemap::testing::check_store_full; |
| 7 |
use std::cmp::Ordering; |
| 8 |
|
| 9 |
/// A Vec wrapper that leverages the default function impls from `Store` |
| 10 |
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 11 |
struct VecWithDefaults<T>(Vec<T>); |
| 12 |
|
| 13 |
type MapF<K, V> = fn(&(K, V)) -> (&K, &V); |
| 14 |
|
| 15 |
#[inline] |
| 16 |
fn map_f<K, V>(input: &(K, V)) -> (&K, &V) { |
| 17 |
(&input.0, &input.1) |
| 18 |
} |
| 19 |
|
| 20 |
type MapFMut<K, V> = fn(&mut (K, V)) -> (&K, &mut V); |
| 21 |
|
| 22 |
#[inline] |
| 23 |
fn map_f_mut<K, V>(input: &mut (K, V)) -> (&K, &mut V) { |
| 24 |
(&input.0, &mut input.1) |
| 25 |
} |
| 26 |
|
| 27 |
impl<K, V> StoreConstEmpty<K, V> for VecWithDefaults<(K, V)> { |
| 28 |
const EMPTY: VecWithDefaults<(K, V)> = VecWithDefaults(Vec::new()); |
| 29 |
} |
| 30 |
|
| 31 |
impl<K, V> Store<K, V> for VecWithDefaults<(K, V)> { |
| 32 |
#[inline] |
| 33 |
fn lm_len(&self) -> usize { |
| 34 |
self.0.as_slice().len() |
| 35 |
} |
| 36 |
|
| 37 |
// leave lm_is_empty as default |
| 38 |
|
| 39 |
#[inline] |
| 40 |
fn lm_get(&self, index: usize) -> Option<(&K, &V)> { |
| 41 |
self.0.as_slice().get(index).map(map_f) |
| 42 |
} |
| 43 |
|
| 44 |
// leave lm_last as default |
| 45 |
|
| 46 |
#[inline] |
| 47 |
fn lm_binary_search_by<F>(&self, mut cmp: F) -> Result<usize, usize> |
| 48 |
where |
| 49 |
F: FnMut(&K) -> Ordering, |
| 50 |
{ |
| 51 |
self.0.as_slice().binary_search_by(|(k, _)| cmp(k)) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
impl<K: Ord, V> StoreFromIterable<K, V> for VecWithDefaults<(K, V)> { |
| 56 |
fn lm_sort_from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self { |
| 57 |
let v: Vec<_> = Vec::lm_sort_from_iter(iter); |
| 58 |
Self(v) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
impl<K, V> StoreMut<K, V> for VecWithDefaults<(K, V)> { |
| 63 |
#[inline] |
| 64 |
fn lm_with_capacity(capacity: usize) -> Self { |
| 65 |
Self(Vec::with_capacity(capacity)) |
| 66 |
} |
| 67 |
|
| 68 |
#[inline] |
| 69 |
fn lm_reserve(&mut self, additional: usize) { |
| 70 |
self.0.reserve(additional) |
| 71 |
} |
| 72 |
|
| 73 |
#[inline] |
| 74 |
fn lm_get_mut(&mut self, index: usize) -> Option<(&K, &mut V)> { |
| 75 |
self.0.as_mut_slice().get_mut(index).map(map_f_mut) |
| 76 |
} |
| 77 |
|
| 78 |
#[inline] |
| 79 |
fn lm_push(&mut self, key: K, value: V) { |
| 80 |
self.0.push((key, value)) |
| 81 |
} |
| 82 |
|
| 83 |
#[inline] |
| 84 |
fn lm_insert(&mut self, index: usize, key: K, value: V) { |
| 85 |
self.0.insert(index, (key, value)) |
| 86 |
} |
| 87 |
|
| 88 |
#[inline] |
| 89 |
fn lm_remove(&mut self, index: usize) -> (K, V) { |
| 90 |
self.0.remove(index) |
| 91 |
} |
| 92 |
#[inline] |
| 93 |
fn lm_clear(&mut self) { |
| 94 |
self.0.clear() |
| 95 |
} |
| 96 |
|
| 97 |
// leave lm_retain as default |
| 98 |
} |
| 99 |
|
| 100 |
impl<'a, K: 'a, V: 'a> StoreIterable<'a, K, V> for VecWithDefaults<(K, V)> { |
| 101 |
type KeyValueIter = core::iter::Map<core::slice::Iter<'a, (K, V)>, MapF<K, V>>; |
| 102 |
|
| 103 |
#[inline] |
| 104 |
fn lm_iter(&'a self) -> Self::KeyValueIter { |
| 105 |
self.0.as_slice().iter().map(map_f) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
impl<'a, K: 'a, V: 'a> StoreIterableMut<'a, K, V> for VecWithDefaults<(K, V)> { |
| 110 |
type KeyValueIterMut = core::iter::Map<core::slice::IterMut<'a, (K, V)>, MapFMut<K, V>>; |
| 111 |
|
| 112 |
#[inline] |
| 113 |
fn lm_iter_mut(&'a mut self) -> Self::KeyValueIterMut { |
| 114 |
self.0.as_mut_slice().iter_mut().map(map_f_mut) |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
impl<K, V> StoreIntoIterator<K, V> for VecWithDefaults<(K, V)> { |
| 119 |
type KeyValueIntoIter = std::vec::IntoIter<(K, V)>; |
| 120 |
|
| 121 |
#[inline] |
| 122 |
fn lm_into_iter(self) -> Self::KeyValueIntoIter { |
| 123 |
IntoIterator::into_iter(self.0) |
| 124 |
} |
| 125 |
|
| 126 |
// leave lm_extend_end as default |
| 127 |
|
| 128 |
// leave lm_extend_start as default |
| 129 |
} |
| 130 |
|
| 131 |
impl<A> FromIterator<A> for VecWithDefaults<A> { |
| 132 |
fn from_iter<I: IntoIterator<Item = A>>(iter: I) -> Self { |
| 133 |
Self(Vec::from_iter(iter)) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
impl<K, V> StoreFromIterator<K, V> for VecWithDefaults<(K, V)> {} |
| 138 |
|
| 139 |
impl<K: Ord, V> StoreBulkMut<K, V> for VecWithDefaults<(K, V)> { |
| 140 |
fn lm_retain<F>(&mut self, predicate: F) |
| 141 |
where |
| 142 |
F: FnMut(&K, &V) -> bool, |
| 143 |
{ |
| 144 |
self.0.lm_retain(predicate) |
| 145 |
} |
| 146 |
|
| 147 |
fn lm_extend<I>(&mut self, other: I) |
| 148 |
where |
| 149 |
I: IntoIterator<Item = (K, V)>, |
| 150 |
{ |
| 151 |
self.0.lm_extend(other) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
#[test] |
| 156 |
fn test_default_impl() { |
| 157 |
check_store_full::<VecWithDefaults<(u32, u64)>>(); |
| 158 |
} |