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
5use super::*;
6use alloc::vec::Vec;
7
8type MapF<K, V> = fn(&(K, V)) -> (&K, &V);
9
10#[inline]
11fn map_f<K, V>(input: &(K, V)) -> (&K, &V) {
12 (&input.0, &input.1)
13}
14
15type MapFMut<K, V> = fn(&mut (K, V)) -> (&K, &mut V);
16
17#[inline]
18fn map_f_mut<K, V>(input: &mut (K, V)) -> (&K, &mut V) {
19 (&input.0, &mut input.1)
20}
21
22impl<K, V> StoreConstEmpty<K, V> for Vec<(K, V)> {
23 const EMPTY: Vec<(K, V)> = Vec::new();
24}
25
26impl<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
56impl<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
64impl<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
109impl<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
142impl<'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
151impl<'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 type KeyValueIntoIter = alloc::vec::IntoIter<(K, V)>;
154
155 #[inline]
156 fn lm_iter_mut(&'a mut self) -> Self::KeyValueIterMut {
157 self.as_mut_slice().iter_mut().map(map_f_mut)
158 }
159
160 #[inline]
161 fn lm_into_iter(self) -> Self::KeyValueIntoIter {
162 IntoIterator::into_iter(self)
163 }
164
165 #[inline]
166 fn lm_extend_end(&mut self, other: Self) {
167 self.extend(iter:other)
168 }
169
170 #[inline]
171 fn lm_extend_start(&mut self, other: Self) {
172 self.splice(range:0..0, replace_with:other);
173 }
174}
175
176impl<K, V> StoreFromIterator<K, V> for Vec<(K, V)> {}
177
178#[test]
179fn test_vec_impl() {
180 crate::testing::check_store_full::<Vec<(u32, u64)>>();
181}
182