From 015b36f5b2f42a37e213e1560b8b84d94f6241bb Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Sun, 15 May 2022 01:50:23 +0000 Subject: [PATCH] unmodified linkedhashmap --- linkedhashmap/Cargo.toml | 5 ++++- linkedhashmap/src/lib.rs | 21 +++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/linkedhashmap/Cargo.toml b/linkedhashmap/Cargo.toml index 201ea429..ba5800ea 100644 --- a/linkedhashmap/Cargo.toml +++ b/linkedhashmap/Cargo.toml @@ -4,9 +4,12 @@ version = "0.2.0" authors = ["quininer "] edition = "2018" +[features] +inline-more = [ "hashbrown" ] + [dependencies] slab = "0.4.6" -hashbrown = "0.12.1" +hashbrown = { version = "0.12.1", optional = true } [dev-dependencies] criterion = "0.3.5" diff --git a/linkedhashmap/src/lib.rs b/linkedhashmap/src/lib.rs index 2835be16..83834259 100644 --- a/linkedhashmap/src/lib.rs +++ b/linkedhashmap/src/lib.rs @@ -1,11 +1,16 @@ pub mod linkedlist; -use hashbrown::HashMap; use linkedlist::{LinkedList, NodeSlab}; use std::borrow::Borrow; use std::collections::hash_map::RandomState; use std::hash::{BuildHasher, Hash}; +#[cfg(feature = "hashbrown")] +use hashbrown::HashMap; + +#[cfg(not(feature = "hashbrown"))] +use std::collections::HashMap; + pub struct LinkedHashMap { slab: NodeSlab<(K, V)>, list: LinkedList, @@ -96,7 +101,7 @@ where self.map.clear(); } - #[inline] + #[cfg_attr(feature = "inline-more", inline(always))] pub fn insert(&mut self, key: K, value: V) -> Option { let index = self.list.push(&mut self.slab, (key.clone(), value)); let index = self.map.insert(key, index)?; @@ -104,7 +109,7 @@ where Some(value) } - #[inline] + #[cfg_attr(feature = "inline-more", inline(always))] pub fn get(&self, key: &Q) -> Option<&V> where K: Borrow, @@ -115,7 +120,7 @@ where Some(value) } - #[inline] + #[cfg_attr(feature = "inline-more", inline(always))] pub fn get_mut(&mut self, key: &Q) -> Option<&mut V> where K: Borrow, @@ -126,7 +131,7 @@ where Some(value) } - #[inline] + #[cfg_attr(feature = "inline-more", inline(always))] pub fn touch(&mut self, key: &Q) -> Option<&mut V> where K: Borrow, @@ -137,7 +142,7 @@ where Some(value) } - #[inline] + #[cfg_attr(feature = "inline-more", inline(always))] pub fn remove(&mut self, key: &Q) -> Option where K: Borrow, @@ -148,14 +153,14 @@ where Some(value) } - #[inline] + #[cfg_attr(feature = "inline-more", inline(always))] pub fn pop_front(&mut self) -> Option<(K, V)> { let (k, v) = self.list.pop_front(&mut self.slab)?; self.map.remove(&k)?; Some((k, v)) } - #[inline] + #[cfg_attr(feature = "inline-more", inline(always))] pub fn pop_last(&mut self) -> Option<(K, V)> { let (k, v) = self.list.pop_last(&mut self.slab)?; self.map.remove(&k)?;