| 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 |
//! Traits for pluggable [`LiteMap`] backends. |
| 6 |
//! |
| 7 |
//! By default, [`LiteMap`] is backed by a `Vec`. However, in some environments, it may be desirable |
| 8 |
//! to use a different data store while still using [`LiteMap`] to manage proper ordering of items. |
| 9 |
//! |
| 10 |
//! The general guidelines for a performant data store are: |
| 11 |
//! |
| 12 |
//! 1. Must support efficient random access for binary search |
| 13 |
//! 2. Should support efficient append operations for deserialization |
| 14 |
//! |
| 15 |
//! To plug a custom data store into [`LiteMap`], implement: |
| 16 |
//! |
| 17 |
//! - [`Store`] for most of the methods |
| 18 |
//! - [`StoreIterable`] for methods that return iterators |
| 19 |
//! - [`StoreFromIterator`] to enable `FromIterator` for [`LiteMap`] |
| 20 |
//! |
| 21 |
//! To test your implementation, enable the `"testing"` Cargo feature and use [`check_store()`]. |
| 22 |
//! |
| 23 |
//! [`check_store()`]: crate::testing::check_store |
| 24 |
|
| 25 |
mod slice_impl; |
| 26 |
#[cfg(feature = "alloc")] |
| 27 |
mod vec_impl; |
| 28 |
|
| 29 |
#[cfg(doc)] |
| 30 |
use crate::LiteMap; |
| 31 |
use core::cmp::Ordering; |
| 32 |
use core::iter::DoubleEndedIterator; |
| 33 |
use core::iter::FromIterator; |
| 34 |
use core::iter::Iterator; |
| 35 |
use core::ops::Range; |
| 36 |
|
| 37 |
/// Trait to enable const construction of empty store. |
| 38 |
pub trait StoreConstEmpty<K: ?Sized, V: ?Sized> { |
| 39 |
/// An empty store |
| 40 |
const EMPTY: Self; |
| 41 |
} |
| 42 |
|
| 43 |
/// Trait to enable pluggable backends for [`LiteMap`]. |
| 44 |
/// |
| 45 |
/// Some methods have default implementations provided for convenience; however, it is generally |
| 46 |
/// better to implement all methods that your data store supports. |
| 47 |
pub trait Store<K: ?Sized, V: ?Sized>: Sized { |
| 48 |
/// Returns the number of elements in the store. |
| 49 |
fn lm_len(&self) -> usize; |
| 50 |
|
| 51 |
/// Returns whether the store is empty (contains 0 elements). |
| 52 |
fn lm_is_empty(&self) -> bool { |
| 53 |
self.lm_len() == 0 |
| 54 |
} |
| 55 |
|
| 56 |
/// Gets a key/value pair at the specified index. |
| 57 |
fn lm_get(&self, index: usize) -> Option<(&K, &V)>; |
| 58 |
|
| 59 |
/// Gets the last element in the store, or `None` if the store is empty. |
| 60 |
fn lm_last(&self) -> Option<(&K, &V)> { |
| 61 |
let len = self.lm_len(); |
| 62 |
if len == 0 { |
| 63 |
None |
| 64 |
} else { |
| 65 |
self.lm_get(len - 1) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/// Searches the store for a particular element with a comparator function. |
| 70 |
/// |
| 71 |
/// See the binary search implementation on `slice` for more information. |
| 72 |
fn lm_binary_search_by<F>(&self, cmp: F) -> Result<usize, usize> |
| 73 |
where |
| 74 |
F: FnMut(&K) -> Ordering; |
| 75 |
} |
| 76 |
|
| 77 |
pub trait StoreFromIterable<K, V>: Store<K, V> { |
| 78 |
/// Create a sorted store from `iter`. |
| 79 |
fn lm_sort_from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self; |
| 80 |
} |
| 81 |
|
| 82 |
pub trait StoreSlice<K: ?Sized, V: ?Sized>: Store<K, V> { |
| 83 |
type Slice: ?Sized; |
| 84 |
|
| 85 |
fn lm_get_range(&self, range: Range<usize>) -> Option<&Self::Slice>; |
| 86 |
} |
| 87 |
|
| 88 |
pub trait StoreMut<K, V>: Store<K, V> { |
| 89 |
/// Creates a new store with the specified capacity hint. |
| 90 |
/// |
| 91 |
/// Implementations may ignore the argument if they do not support pre-allocating capacity. |
| 92 |
fn lm_with_capacity(capacity: usize) -> Self; |
| 93 |
|
| 94 |
/// Reserves additional capacity in the store. |
| 95 |
/// |
| 96 |
/// Implementations may ignore the argument if they do not support pre-allocating capacity. |
| 97 |
fn lm_reserve(&mut self, additional: usize); |
| 98 |
|
| 99 |
/// Gets a key/value pair at the specified index, with a mutable value. |
| 100 |
fn lm_get_mut(&mut self, index: usize) -> Option<(&K, &mut V)>; |
| 101 |
|
| 102 |
/// Pushes one additional item onto the store. |
| 103 |
fn lm_push(&mut self, key: K, value: V); |
| 104 |
|
| 105 |
/// Inserts an item at a specific index in the store. |
| 106 |
/// |
| 107 |
/// # Panics |
| 108 |
/// |
| 109 |
/// Panics if `index` is greater than the length. |
| 110 |
fn lm_insert(&mut self, index: usize, key: K, value: V); |
| 111 |
|
| 112 |
/// Removes an item at a specific index in the store. |
| 113 |
/// |
| 114 |
/// # Panics |
| 115 |
/// |
| 116 |
/// Panics if `index` is greater than the length. |
| 117 |
fn lm_remove(&mut self, index: usize) -> (K, V); |
| 118 |
|
| 119 |
/// Removes all items from the store. |
| 120 |
fn lm_clear(&mut self); |
| 121 |
} |
| 122 |
|
| 123 |
pub trait StoreBulkMut<K, V>: StoreMut<K, V> { |
| 124 |
/// Retains items satisfying a predicate in this store. |
| 125 |
fn lm_retain<F>(&mut self, predicate: F) |
| 126 |
where |
| 127 |
F: FnMut(&K, &V) -> bool; |
| 128 |
|
| 129 |
/// Extends this store with items from an iterator. |
| 130 |
fn lm_extend<I>(&mut self, other: I) |
| 131 |
where |
| 132 |
I: IntoIterator<Item = (K, V)>; |
| 133 |
} |
| 134 |
|
| 135 |
/// Iterator methods for the [`LiteMap`] store. |
| 136 |
pub trait StoreIterable<'a, K: 'a + ?Sized, V: 'a + ?Sized>: Store<K, V> { |
| 137 |
type KeyValueIter: Iterator<Item = (&'a K, &'a V)> + DoubleEndedIterator + 'a; |
| 138 |
|
| 139 |
/// Returns an iterator over key/value pairs. |
| 140 |
fn lm_iter(&'a self) -> Self::KeyValueIter; |
| 141 |
} |
| 142 |
|
| 143 |
pub trait StoreIterableMut<'a, K: 'a, V: 'a>: StoreMut<K, V> + StoreIterable<'a, K, V> { |
| 144 |
type KeyValueIterMut: Iterator<Item = (&'a K, &'a mut V)> + DoubleEndedIterator + 'a; |
| 145 |
|
| 146 |
/// Returns an iterator over key/value pairs, with a mutable value. |
| 147 |
fn lm_iter_mut(&'a mut self) -> Self::KeyValueIterMut; |
| 148 |
} |
| 149 |
|
| 150 |
pub trait StoreIntoIterator<K, V>: StoreMut<K, V> { |
| 151 |
type KeyValueIntoIter: Iterator<Item = (K, V)>; |
| 152 |
|
| 153 |
/// Returns an iterator that moves every item from this store. |
| 154 |
fn lm_into_iter(self) -> Self::KeyValueIntoIter; |
| 155 |
|
| 156 |
/// Adds items from another store to the end of this store. |
| 157 |
fn lm_extend_end(&mut self, other: Self) |
| 158 |
where |
| 159 |
Self: Sized, |
| 160 |
{ |
| 161 |
for item in other.lm_into_iter() { |
| 162 |
self.lm_push(item.0, item.1); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/// Adds items from another store to the beginning of this store. |
| 167 |
fn lm_extend_start(&mut self, other: Self) |
| 168 |
where |
| 169 |
Self: Sized, |
| 170 |
{ |
| 171 |
for (i, item) in other.lm_into_iter().enumerate() { |
| 172 |
self.lm_insert(i, item.0, item.1); |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/// A store that can be built from a tuple iterator. |
| 178 |
pub trait StoreFromIterator<K, V>: FromIterator<(K, V)> {} |