Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dense VecMap #48

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions src/vec_map/dense.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::marker::PhantomData;

use crate::vecmap_base2_impl;
use crate::vecmap_base_impl;
use crate::IndexKey;

pub struct DenseVecMap<K, V> {
data: Vec<V>,
len: usize,
_marker: PhantomData<K>,
}

#[inline(always)]
fn identity<T>(t: T) -> T {
t
}

vecmap_base_impl!(DenseVecMap, (V: Clone + Default), identity);
vecmap_base2_impl!(DenseVecMap, (V: Clone + Default), identity);

impl<K: IndexKey, V: Clone + Default> DenseVecMap<K, V> {
// TODO what is expected here? Should it panic if out of bounds? Should it fill up the gaps with default values?
// / Inserts a key-value pair into the map.
// /
// / If the key is present in the map, the value is updated and the old value
// / is returned. Otherwise, [`None`] is returned.
// pub fn insert(&mut self, key: K, value: V) -> Option<V> {
// let index = key.as_index();
// if index >= self.capacity() {
// self.data
// .extend((0..=(index - self.data.len())).map(|_| Default::default()));
// }

// let existing = self.data[index].replace(value);
// if existing.is_none() {
// self.len += 1;
// }
// existing
// }
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test() {
let map: DenseVecMap<usize, usize> = DenseVecMap::with_capacity(7);
}
}
67 changes: 67 additions & 0 deletions src/vec_map/gen_macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use crate::vec_map::dense::DenseVecMap;

#[doc(hidden)]
#[macro_export]
macro_rules! vecmap_base_impl {
($name:ident, ($($value_bound:tt)*), $wrap_elem:expr) => {
impl<K: IndexKey, $($value_bound)*> $name<K, V> {
#[doc = concat!("Initializes [`", stringify!($name),"`] with ")]
#[doc = "capacity to hold exactly `n` elements in the index range of `0..n`."]
pub fn with_capacity(n: usize) -> Self {
Self {
data: vec![Default::default(); n],
len: 0,
_marker: PhantomData,
}
}

#[doc = concat!("Initializes [`", stringify!($name),"`] with `n` occurences of `elem`")]
pub fn from_elem(elem: V, n: usize) -> Self {
Self {
data: vec![$wrap_elem(elem); n],
len: n,
_marker: PhantomData,
}
}

#[doc = concat!("Clears all data from the [`", stringify!($name),"`] without changing the capacity.")]
pub fn clear(&mut self) {
self.len = 0;
self.data = vec![Default::default(); self.capacity()];
}

/// Reserve capacity for `additional` key-value pairs.
pub fn reserve(&mut self, additional: usize) {
self.data.extend(vec![Default::default(); additional]);
}
}
};
}

#[doc(hidden)]
#[macro_export]
macro_rules! vecmap_base2_impl {
($name:ident, ($($value_bound:tt)*), $wrap_elem:expr) => {
impl<K: IndexKey, $($value_bound)*> $name<K, V> {
/// Initializes an empty map.
///
/// For performance reasons it's almost always better to avoid dynamic
/// resizing by using [`Self::with_capacity()`] instead.
pub const fn new() -> Self {
Self {
data: vec![],
len: 0,
_marker: PhantomData,
}
}

/// Returns the number of elements the map can hold without reallocating.
///
/// The index range of items that the map can hold without reallocating is
/// `0..capacity`.
pub fn capacity(&self) -> usize {
self.data.len()
}
}
};
}
57 changes: 6 additions & 51 deletions src/vec_map/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![warn(missing_docs, missing_debug_implementations)]
//! [`VecMap`] is a [`Vec`]-backed map, for faster random access.
pub mod dense;
mod gen_macros;
mod iter;

use std::cmp::Ordering;
Expand All @@ -9,6 +11,8 @@ use std::ops::Index;
use std::ops::IndexMut;

pub use crate::vec_map::iter::*;
use crate::vecmap_base2_impl;
use crate::vecmap_base_impl;

/// A key that can be used in a map without needing a hasher.
///
Expand Down Expand Up @@ -46,59 +50,10 @@ pub struct VecMap<K, V> {
_marker: PhantomData<K>,
}

impl<K: IndexKey, V: Clone> VecMap<K, V> {
/// Initializes [`VecMap`] with capacity to hold exactly `n` elements in the
/// index range of `0..n`.
pub fn with_capacity(n: usize) -> Self {
Self {
data: vec![None; n],
len: 0,
_marker: PhantomData,
}
}

/// Initializes [`VecMap`] with `n` occurences of `elem`.
pub fn from_elem(elem: V, n: usize) -> Self {
Self {
data: vec![Some(elem); n],
len: n,
_marker: PhantomData,
}
}

/// Clears all data from the [`VecMap`] without changing the capacity.
pub fn clear(&mut self) {
self.len = 0;
self.data = vec![None; self.capacity()];
}

/// Reserve capacity for `additional` key-value pairs.
pub fn reserve(&mut self, additional: usize) {
self.data.extend(vec![None; additional]);
}
}
vecmap_base_impl!(VecMap, (V: Clone), Some);
vecmap_base2_impl!(VecMap, (V), Some);

impl<K: IndexKey, V> VecMap<K, V> {
/// Initializes an empty [`VecMap`].
///
/// For performance reasons it's almost always better to avoid dynamic
/// resizing by using [`Self::with_capacity()`] instead.
pub const fn new() -> Self {
Self {
data: vec![],
len: 0,
_marker: PhantomData,
}
}

/// Returns the number of elements the map can hold without reallocating.
///
/// The index range of items that the map can hold without reallocating is
/// `0..capacity`.
pub fn capacity(&self) -> usize {
self.data.len()
}

/// Inserts a key-value pair into the map.
///
/// If the key is present in the map, the value is updated and the old value
Expand Down
Loading