Map

Struct Map 

Source
pub struct Map<K: Indexable, V> { /* private fields */ }
Expand description

An owned map optimized for read-heavy workloads, backed by a PGM-index.

This is a BTreeMap-like container that owns its data and provides efficient lookups using a learned index. Mutations are supported but trigger O(n) index rebuilds; for frequent updates use crate::Dynamic.

Works with any key type that implements Indexable:

  • Numeric types (u64, i32, etc.) are indexed directly
  • String/bytes types use prefix extraction

§Example

use pgm_extra::Map;

// Numeric keys
let entries: Vec<(u64, &str)> = vec![(1, "one"), (2, "two"), (3, "three")];
let map = Map::from_sorted_unique(entries, 64, 4).unwrap();
assert_eq!(map.get(&2), Some(&"two"));

// String keys
let entries = vec![("apple", 1), ("banana", 2), ("cherry", 3)];
let map = Map::from_sorted_unique(entries, 64, 4).unwrap();
assert_eq!(map.get(&"banana"), Some(&2));

Implementations§

Source§

impl<K: Indexable + Ord, V> Map<K, V>
where K::Key: Ord,

Source

pub fn from_sorted_unique( entries: Vec<(K, V)>, epsilon: usize, epsilon_recursive: usize, ) -> Result<Self, Error>

Create a map from pre-sorted, unique key-value pairs.

§Panics

Debug builds will panic if keys are not sorted or contain duplicates.

Source

pub fn build<I>( iter: I, epsilon: usize, epsilon_recursive: usize, ) -> Result<Self, Error>
where I: IntoIterator<Item = (K, V)>,

Create a map from an unsorted iterator of key-value pairs.

If duplicate keys exist, the last value wins (like BTreeMap::from_iter).

Source

pub fn empty(epsilon: usize, epsilon_recursive: usize) -> Self

Source

pub fn new(entries: Vec<(K, V)>) -> Result<Self, Error>

Source

pub fn get(&self, key: &K) -> Option<&V>

Source

pub fn get_key_value(&self, key: &K) -> Option<(&K, &V)>

Source

pub fn contains_key(&self, key: &K) -> bool

Source

pub fn lower_bound(&self, key: &K) -> usize

Find the index of the first key >= the given key.

Source

pub fn upper_bound(&self, key: &K) -> usize

Find the index of the first key > the given key.

Source

pub fn range<R>(&self, range: R) -> impl DoubleEndedIterator<Item = (&K, &V)>
where R: RangeBounds<K>,

Source

pub fn first_key_value(&self) -> Option<(&K, &V)>

Source

pub fn last_key_value(&self) -> Option<(&K, &V)>

Source

pub fn iter( &self, ) -> impl ExactSizeIterator<Item = (&K, &V)> + DoubleEndedIterator

Source

pub fn keys(&self) -> impl ExactSizeIterator<Item = &K> + DoubleEndedIterator

Source

pub fn values(&self) -> impl ExactSizeIterator<Item = &V> + DoubleEndedIterator

Source

pub fn values_mut( &mut self, ) -> impl ExactSizeIterator<Item = &mut V> + DoubleEndedIterator

Source

pub fn get_mut(&mut self, key: &K) -> Option<&mut V>

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn segments_count(&self) -> usize

Get the number of segments in the underlying index.

Source

pub fn height(&self) -> usize

Get the height of the underlying index.

Source

pub fn epsilon(&self) -> usize

Get the epsilon value.

Source

pub fn epsilon_recursive(&self) -> usize

Get the epsilon_recursive value.

Source

pub fn size_in_bytes(&self) -> usize

Approximate memory usage in bytes.

Source

pub fn keys_slice(&self) -> &[K]

Get a reference to the underlying keys slice.

Source

pub fn values_slice(&self) -> &[V]

Get a reference to the underlying values slice.

Source

pub fn into_vec(self) -> Vec<(K, V)>

Consume the map and return the underlying key-value pairs.

Source

pub fn index(&self) -> Option<&Static<K>>

Get a reference to the underlying index.

Source

pub fn insert(&mut self, key: K, value: V) -> Option<V>

Insert a key-value pair into the map.

Returns the old value if the key already existed, or None if it was newly inserted.

Note: This rebuilds the entire index, making it O(n) per insertion. For bulk insertions, prefer collecting into a new map. For frequent mutations, consider using index::owned::Dynamic directly.

Trait Implementations§

Source§

impl<K: Indexable + Clone, V: Clone> Clone for Map<K, V>
where K::Key: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Indexable + Debug, V: Debug> Debug for Map<K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, K, V> Deserialize<'de> for Map<K, V>

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<K: Indexable + Ord, V> Extend<(K, V)> for Map<K, V>
where K::Key: Ord,

Source§

fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)

Extends the map with key-value pairs from an iterator.

If duplicate keys exist, the last value wins.

Note: This rebuilds the entire index, making it O(n) per call. For bulk insertions, prefer collecting into a new map. For frequent mutations, consider using index::owned::Dynamic directly.

Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K: Indexable + Ord, V> FromIterator<(K, V)> for Map<K, V>
where K::Key: Ord,

Source§

fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self

Creates a Map from an iterator.

Returns an empty map if the iterator is empty.

Source§

impl<K: Indexable + Hash, V: Hash> Hash for Map<K, V>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<K: Indexable + Ord, V> Index<&K> for Map<K, V>
where K::Key: Ord,

Source§

fn index(&self, key: &K) -> &Self::Output

Returns a reference to the value corresponding to the key.

§Panics

Panics if the key is not present in the map.

Source§

type Output = V

The returned type after indexing.
Source§

impl<'a, K: Indexable, V> IntoIterator for &'a Map<K, V>

Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Zip<Iter<'a, K>, Iter<'a, V>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K: Indexable + Ord, V> IntoIterator for Map<K, V>
where K::Key: Ord,

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<(K, V)>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K: Indexable + Ord + PartialEq, V: PartialEq> PartialEq for Map<K, V>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K, V> Serialize for Map<K, V>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<K: Indexable + Ord + Eq, V: Eq> Eq for Map<K, V>

Auto Trait Implementations§

§

impl<K, V> Freeze for Map<K, V>

§

impl<K, V> RefUnwindSafe for Map<K, V>

§

impl<K, V> Send for Map<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for Map<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for Map<K, V>
where K: Unpin, V: Unpin, <K as Indexable>::Key: Unpin,

§

impl<K, V> UnwindSafe for Map<K, V>
where K: UnwindSafe, V: UnwindSafe, <K as Indexable>::Key: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,