Skip to content

Commit

Permalink
std hashmap
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu committed Aug 23, 2023
1 parent f124e0b commit 74e49a7
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 67 deletions.
1 change: 0 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ fastnbt-rc = { version = "*", git = "https://github.com/rimecraft-rs/fastnbt" }
parking_lot = "0.12"
lazy-regex = "2"
cesu8 = "*"
hashbrown = "0.14"
dashmap = "5.4"
bimap = "0.6"
2 changes: 1 addition & 1 deletion core/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Block {
id: 0,
states: {
let mut builder = crate::state::StatesBuilder::new();
let mut map = hashbrown::HashMap::new();
let mut map = std::collections::HashMap::new();
for state in states {
builder.add(state.0.clone())?;
map.insert(state.0, state.1);
Expand Down
11 changes: 11 additions & 0 deletions core/src/component.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::any::TypeId;

pub trait Attach {
fn pre_attach(&mut self, components: &mut Components);
}

pub struct Components {
components: <(TypeId, Box<dyn Attach + Send + Sync>)>,
}

impl Components {}
2 changes: 1 addition & 1 deletion core/src/fluid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Fluid {
id: 0,
states: std::sync::Arc::new({
let mut builder = crate::state::StatesBuilder::new();
let mut map = hashbrown::HashMap::new();
let mut map = std::collections::HashMap::new();
for state in states {
builder.add(state.0.clone())?;
map.insert(state.0, state.1);
Expand Down
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod block;
pub mod component;
pub mod entity;
pub mod fluid;
pub mod item;
Expand Down
45 changes: 0 additions & 45 deletions core/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,51 +603,6 @@ mod packet_buf_imp {
}
}

impl<K, V> Encode for hashbrown::HashMap<K, V>
where
K: Encode,
V: Encode,
{
fn encode<B>(&self, buf: &mut B) -> anyhow::Result<()>
where
B: bytes::BufMut,
{
crate::util::VarInt(self.len() as i32).encode(buf)?;

for (key, value) in self.iter() {
key.encode(buf)?;
value.encode(buf)?;
}

Ok(())
}
}

impl<'de, K, V, OK, OV> Decode<'de> for hashbrown::HashMap<K, V>
where
K: for<'a> Decode<'a, Output = OK>,
V: for<'a> Decode<'a, Output = OV>,
OK: Hash + Eq,
{
type Output = hashbrown::HashMap<OK, OV>;

fn decode<B>(buf: &'de mut B) -> anyhow::Result<Self::Output>
where
B: bytes::Buf,
{
let len = crate::util::VarInt::decode(buf)? as usize;
let mut map = hashbrown::HashMap::with_capacity(len);

for _ in 0..len {
let obj = K::decode(buf)?;
let obj1 = V::decode(buf)?;
map.insert(obj, obj1);
}

Ok(map)
}
}

impl<T> Encode for Option<T>
where
T: Encode,
Expand Down
12 changes: 6 additions & 6 deletions core/src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ impl<T> Deref for Entry<T> {
pub struct Registry<T> {
default: Option<usize>,
entries: Vec<Entry<T>>,
id_map: hashbrown::HashMap<Id, usize>,
id_map: std::collections::HashMap<Id, usize>,
/// Key of this registry.
pub key: Key<Self>,
key_map: hashbrown::HashMap<Key<T>, usize>,
key_map: std::collections::HashMap<Key<T>, usize>,
/// Tag to entries mapping of this registry.
pub tags: parking_lot::RwLock<hashbrown::HashMap<tag::Key<T>, Vec<usize>>>,
pub tags: parking_lot::RwLock<std::collections::HashMap<tag::Key<T>, Vec<usize>>>,
}

impl<T> Registry<T> {
Expand Down Expand Up @@ -192,7 +192,7 @@ impl<T: Registration> crate::util::Freeze<Registry<T>> for Builder<T> {
.collect::<Vec<_>>();

let id_map = {
let mut map = hashbrown::HashMap::new();
let mut map = std::collections::HashMap::new();
for e in entries.iter().enumerate() {
map.insert(e.1.key.value().clone(), e.0);
}
Expand All @@ -202,7 +202,7 @@ impl<T: Registration> crate::util::Freeze<Registry<T>> for Builder<T> {
Registry {
default: opts.1.map(|e| id_map.get(&e).copied()).flatten(),
key_map: {
let mut map = hashbrown::HashMap::new();
let mut map = std::collections::HashMap::new();
for e in entries.iter().enumerate() {
map.insert(e.1.key.clone(), e.0);
}
Expand All @@ -211,7 +211,7 @@ impl<T: Registration> crate::util::Freeze<Registry<T>> for Builder<T> {
entries,
id_map,
key: opts.0,
tags: parking_lot::RwLock::new(hashbrown::HashMap::new()),
tags: parking_lot::RwLock::new(std::collections::HashMap::new()),
}
}
}
Expand Down
21 changes: 11 additions & 10 deletions core/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub use property::Property;

pub struct State {
id: usize,
entries: hashbrown::HashMap<property::Property, u8>,
table: once_cell::sync::OnceCell<hashbrown::HashMap<property::Property, Vec<usize>>>,
entries: std::collections::HashMap<property::Property, u8>,
table: once_cell::sync::OnceCell<std::collections::HashMap<property::Property, Vec<usize>>>,
}

impl State {
Expand Down Expand Up @@ -85,7 +85,7 @@ impl State {

fn init_table<T: Deref<Target = Self>>(&self, states: &[T]) {
assert!(self.table.get().is_none());
let mut table = hashbrown::HashMap::new();
let mut table = std::collections::HashMap::new();
for p in self.entries.keys() {
let range = p.range();
let mut vec = Vec::new();
Expand Down Expand Up @@ -126,14 +126,14 @@ impl State {
self.entries.get(property).copied().map(T::from)
}

pub fn entries(&self) -> &hashbrown::HashMap<property::Property, u8> {
pub fn entries(&self) -> &std::collections::HashMap<property::Property, u8> {
&self.entries
}
}

pub struct States<T: Deref<Target = State> + 'static> {
def: usize,
properties: hashbrown::HashMap<String, property::Property>,
properties: std::collections::HashMap<String, property::Property>,
states: Vec<crate::util::Ref<'static, T>>,
}

Expand Down Expand Up @@ -205,8 +205,8 @@ impl<T: Deref<Target = State>> Hash for Shared<T> {

fn new_states<E: Clone, T: Deref<Target = State> + From<(E, State)>>(
data: E,
def_state: hashbrown::HashMap<property::Property, u8>,
properties: hashbrown::HashMap<String, property::Property>,
def_state: std::collections::HashMap<property::Property, u8>,
properties: std::collections::HashMap<String, property::Property>,
) -> States<T> {
let mut states_raw: Vec<State> = Vec::new();
let mut temp: Vec<Vec<(property::Property, u8)>> = Vec::new();
Expand All @@ -225,7 +225,8 @@ fn new_states<E: Clone, T: Deref<Target = State> + From<(E, State)>>(
}

for list2 in temp {
let mut entries: hashbrown::HashMap<property::Property, u8> = hashbrown::HashMap::new();
let mut entries: std::collections::HashMap<property::Property, u8> =
std::collections::HashMap::new();

for e in list2 {
entries.insert(e.0, e.1);
Expand Down Expand Up @@ -262,7 +263,7 @@ fn new_states<E: Clone, T: Deref<Target = State> + From<(E, State)>>(

#[derive(Default)]
pub struct StatesBuilder {
properties: hashbrown::HashMap<String, property::Property>,
properties: std::collections::HashMap<String, property::Property>,
}

impl StatesBuilder {
Expand Down Expand Up @@ -297,7 +298,7 @@ impl StatesBuilder {
pub fn build<E: Clone, S: Deref<Target = State> + From<(E, State)>>(
self,
data: E,
def_state: hashbrown::HashMap<property::Property, u8>,
def_state: std::collections::HashMap<property::Property, u8>,
) -> States<S> {
new_states(data, def_state, self.properties)
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/util/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ pub trait Indexed<T> {
#[derive(Clone)]
pub struct IdList<T: Hash + PartialEq + Eq + Clone> {
next_id: u32,
id_map: hashbrown::HashMap<T, u32>,
id_map: std::collections::HashMap<T, u32>,
vec: Vec<Option<T>>,
}

impl<T: Hash + PartialEq + Eq + Clone> IdList<T> {
pub fn new() -> Self {
Self {
next_id: 0,
id_map: hashbrown::HashMap::new(),
id_map: std::collections::HashMap::new(),
vec: vec![],
}
}

pub fn with_capacity(capacity: usize) -> Self {
Self {
next_id: 0,
id_map: hashbrown::HashMap::with_capacity(capacity),
id_map: std::collections::HashMap::with_capacity(capacity),
vec: Vec::with_capacity(capacity),
}
}
Expand Down

0 comments on commit 74e49a7

Please sign in to comment.