Skip to content

WIP: Add views of OrderMap #47

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

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
49 changes: 14 additions & 35 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ mod serde;
mod util;
mod equivalent;
mod mutable_keys;
mod view;
mod view_mut;

pub mod set;

Expand All @@ -32,6 +34,8 @@ use util::{third, ptrdistance, enumerate};
pub use equivalent::Equivalent;
pub use mutable_keys::MutableKeys;
pub use set::OrderSet;
pub use view::OrderMapView;
pub use view_mut::OrderMapViewMut;

fn hash_elem_using<B: BuildHasher, K: ?Sized + Hash>(build: &B, k: &K) -> HashValue {
let mut h = build.build_hasher();
Expand Down Expand Up @@ -328,20 +332,6 @@ fn to_raw_capacity(n: usize) -> usize {
n + n / 3
}

// this could not be captured in an efficient iterator
macro_rules! probe_loop {
($probe_var: ident < $len: expr, $body: expr) => {
loop {
if $probe_var < $len {
$body
$probe_var += 1;
} else {
$probe_var = 0;
}
}
}
}

impl<K, V> OrderMap<K, V> {
/// Create a new map. (Does not allocate.)
pub fn new() -> Self {
Expand Down Expand Up @@ -431,6 +421,16 @@ impl<K, V, S> OrderMap<K, V, S>
pub fn capacity(&self) -> usize {
usable_capacity(self.raw_capacity())
}

/// Creates a read-only view of the map.
pub fn view(&self) -> OrderMapView<K, V, S> {
OrderMapView::new(self)
}

/// Creates a mutable view of the map.
pub fn view_mut(&mut self) -> OrderMapViewMut<K, V, S> {
OrderMapViewMut::new(self)
}
}

/// Trait for the "size class". Either u32 or u64 depending on the index
Expand All @@ -452,27 +452,6 @@ impl Size for u64 {
fn is_64_bit() -> bool { true }
}

/// Call self.method(args) with `::<u32>` or `::<u64>` depending on `self`
/// size class.
///
/// The u32 or u64 is *prepended* to the type parameter list!
macro_rules! dispatch_32_vs_64 {
($self_:ident . $method:ident::<$($t:ty),*>($($arg:expr),*)) => {
if $self_.size_class_is_64bit() {
$self_.$method::<u64, $($t),*>($($arg),*)
} else {
$self_.$method::<u32, $($t),*>($($arg),*)
}
};
($self_:ident . $method:ident ($($arg:expr),*)) => {
if $self_.size_class_is_64bit() {
$self_.$method::<u64>($($arg),*)
} else {
$self_.$method::<u32>($($arg),*)
}
};
}

/// FIXME: Remove dependence on the `S` parameter
/// (to match HashMap).
pub enum Entry<'a, K: 'a, V: 'a, S: 'a = RandomState> {
Expand Down
35 changes: 35 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,38 @@ macro_rules! double_ended_iterator_methods {
}
}
}

// this could not be captured in an efficient iterator
macro_rules! probe_loop {
($probe_var: ident < $len: expr, $body: expr) => {
loop {
if $probe_var < $len {
$body
$probe_var += 1;
} else {
$probe_var = 0;
}
}
}
}

/// Call self.method(args) with `::<u32>` or `::<u64>` depending on `self`
/// size class.
///
/// The u32 or u64 is *prepended* to the type parameter list!
macro_rules! dispatch_32_vs_64 {
($self_:ident . $method:ident::<$($t:ty),*>($($arg:expr),*)) => {
if $self_.size_class_is_64bit() {
$self_.$method::<u64, $($t),*>($($arg),*)
} else {
$self_.$method::<u32, $($t),*>($($arg),*)
}
};
($self_:ident . $method:ident ($($arg:expr),*)) => {
if $self_.size_class_is_64bit() {
$self_.$method::<u64>($($arg),*)
} else {
$self_.$method::<u32>($($arg),*)
}
};
}
Loading