Skip to content

Implement Rayon traits (optional) #45

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

Merged
merged 13 commits into from
Aug 20, 2019
Merged
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
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ matrix:
- rust: stable
env:
- FEATURES='serde-1'
- rust: stable
env:
- FEATURES='rayon'
- rust: beta
- rust: nightly
- rust: nightly
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ bench = false

[dependencies]
serde = { version = "1.0", optional = true }
rayon = { version = "1.0", optional = true }

[dev-dependencies]
itertools = "0.8"
Expand All @@ -53,4 +54,4 @@ debug = true
no-dev-version = true

[package.metadata.docs.rs]
features = ["serde-1"]
features = ["serde-1", "rayon"]
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ mod mutable_keys;
pub mod set;
pub mod map;

// Placed after `map` and `set` so new `rayon` methods on the types
// are documented after the "normal" methods.
#[cfg(feature = "rayon")]
mod rayon;

pub use equivalent::Equivalent;
pub use map::IndexMap;
pub use set::IndexSet;
Expand Down Expand Up @@ -78,3 +83,11 @@ impl<K, V> Bucket<K, V> {
fn muts(&mut self) -> (&mut K, &mut V) { (&mut self.key, &mut self.value) }
}

trait Entries {
type Entry;
fn into_entries(self) -> Vec<Self::Entry>;
fn as_entries(&self) -> &[Self::Entry];
fn as_entries_mut(&mut self) -> &mut [Self::Entry];
fn with_entries<F>(&mut self, f: F)
where F: FnOnce(&mut [Self::Entry]);
}
42 changes: 38 additions & 4 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

pub use mutable_keys::MutableKeys;

#[cfg(feature = "rayon")]
pub use ::rayon::map as rayon;

use std::hash::Hash;
use std::hash::BuildHasher;
use std::hash::Hasher;
Expand All @@ -19,6 +22,7 @@ use util::{third, ptrdistance, enumerate};
use equivalent::Equivalent;
use {
Bucket,
Entries,
HashValue,
};

Expand Down Expand Up @@ -280,6 +284,30 @@ fn desired_pos(mask: usize, hash: HashValue) -> usize {
hash.0 & mask
}

impl<K, V, S> Entries for IndexMap<K, V, S> {
type Entry = Bucket<K, V>;

fn into_entries(self) -> Vec<Self::Entry> {
self.core.entries
}

fn as_entries(&self) -> &[Self::Entry] {
&self.core.entries
}

fn as_entries_mut(&mut self) -> &mut [Self::Entry] {
&mut self.core.entries
}

fn with_entries<F>(&mut self, f: F)
where F: FnOnce(&mut [Self::Entry])
{
let side_index = self.core.save_hash_index();
f(&mut self.core.entries);
self.core.restore_hash_index(side_index);
}
}

/// The number of steps that `current` is forward of the desired position for hash
#[inline(always)]
fn probe_distance(mask: usize, hash: HashValue, current: usize) -> usize {
Expand Down Expand Up @@ -1437,15 +1465,21 @@ impl<K, V> OrderMapCore<K, V> {
fn sort_by<F>(&mut self, mut compare: F)
where F: FnMut(&K, &V, &K, &V) -> Ordering,
{
let side_index = self.save_hash_index();
self.entries.sort_by(move |ei, ej| compare(&ei.key, &ei.value, &ej.key, &ej.value));
self.restore_hash_index(side_index);
}

fn save_hash_index(&mut self) -> Vec<usize> {
// Temporarily use the hash field in a bucket to store the old index.
// Save the old hash values in `side_index`. Then we can sort
// `self.entries` in place.
let mut side_index = Vec::from_iter(enumerate(&mut self.entries).map(|(i, elt)| {
Vec::from_iter(enumerate(&mut self.entries).map(|(i, elt)| {
replace(&mut elt.hash, HashValue(i)).get()
}));

self.entries.sort_by(move |ei, ej| compare(&ei.key, &ei.value, &ej.key, &ej.value));
}))
}

fn restore_hash_index(&mut self, mut side_index: Vec<usize>) {
// Write back the hash values from side_index and fill `side_index` with
// a mapping from the old to the new index instead.
for (i, ent) in enumerate(&mut self.entries) {
Expand Down
Loading