mirror of
https://github.com/azalea-rs/simdnbt.git
synced 2025-08-02 23:44:40 +00:00
remove compound approx_len
This commit is contained in:
parent
d7eb3e4ed1
commit
77a541efd4
3 changed files with 34 additions and 17 deletions
|
@ -147,27 +147,28 @@ impl<'a: 'tape, 'tape> NbtCompound<'a, 'tape> {
|
||||||
|
|
||||||
/// Returns the number of tags directly in this compound.
|
/// Returns the number of tags directly in this compound.
|
||||||
///
|
///
|
||||||
/// Note that due to an internal optimization, this function runs at `O(n)`
|
/// Note that this function runs at `O(n)` due to not storing the length
|
||||||
/// if the compound has at least 2^24 items. Use [`Self::approx_len`] if you
|
/// directly.
|
||||||
/// want to avoid that.
|
|
||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
let len = self.approx_len();
|
self.iter().count()
|
||||||
if len < 2u32.pow(24) {
|
|
||||||
len as usize
|
// let len = self.approx_len();
|
||||||
} else {
|
// if len < 2u32.pow(24) {
|
||||||
self.iter().count()
|
// len as usize
|
||||||
}
|
// } else {
|
||||||
|
// self.iter().count()
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A version of [`Self::len`] that saturates at 2^24.
|
// /// A version of [`Self::len`] that saturates at 2^24.
|
||||||
pub fn approx_len(self) -> u32 {
|
// pub fn approx_len(self) -> u32 {
|
||||||
let el = self.element();
|
// let el = self.element();
|
||||||
debug_assert_eq!(el.kind(), TapeTagKind::Compound);
|
// debug_assert_eq!(el.kind(), TapeTagKind::Compound);
|
||||||
el.approx_len_and_offset().0
|
// el.approx_len_and_offset().0
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.approx_len() == 0
|
self.iter().next().is_none()
|
||||||
}
|
}
|
||||||
#[allow(clippy::type_complexity)]
|
#[allow(clippy::type_complexity)]
|
||||||
pub fn keys(
|
pub fn keys(
|
||||||
|
@ -444,6 +445,8 @@ fn handle_compound_end(tapes: &mut Tapes, stack: &mut ParsingStack) {
|
||||||
tapes
|
tapes
|
||||||
.main
|
.main
|
||||||
.get_unchecked_mut(index_of_compound_element as usize)
|
.get_unchecked_mut(index_of_compound_element as usize)
|
||||||
|
// we don't set the approx_len because determining it for compounds
|
||||||
|
// is too expensive
|
||||||
.set_offset(index_after_end_element as u32 - index_of_compound_element);
|
.set_offset(index_after_end_element as u32 - index_of_compound_element);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -722,4 +722,18 @@ mod tests {
|
||||||
let nbt = super::read(&mut Cursor::new(&data)).unwrap().unwrap();
|
let nbt = super::read(&mut Cursor::new(&data)).unwrap().unwrap();
|
||||||
nbt.as_compound().to_owned();
|
nbt.as_compound().to_owned();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn compound_len() {
|
||||||
|
let src = include_bytes!("../../tests/complex_player.dat").to_vec();
|
||||||
|
let mut src_slice = src.as_slice();
|
||||||
|
let mut decoded_src_decoder = GzDecoder::new(&mut src_slice);
|
||||||
|
let mut decoded_src = Vec::new();
|
||||||
|
decoded_src_decoder.read_to_end(&mut decoded_src).unwrap();
|
||||||
|
let nbt = super::read(&mut Cursor::new(&decoded_src))
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(nbt.as_compound().len(), nbt.as_compound().iter().count());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ pub trait ToNbtTag: Sized {
|
||||||
|
|
||||||
impl<K: Display + FromStr + Eq + Hash, V: FromNbtTag> Deserialize for HashMap<K, V> {
|
impl<K: Display + FromStr + Eq + Hash, V: FromNbtTag> Deserialize for HashMap<K, V> {
|
||||||
fn from_compound(compound: crate::borrow::NbtCompound) -> Result<Self, DeserializeError> {
|
fn from_compound(compound: crate::borrow::NbtCompound) -> Result<Self, DeserializeError> {
|
||||||
let mut hashmap = HashMap::with_capacity(compound.approx_len() as usize);
|
let mut hashmap = HashMap::new();
|
||||||
|
|
||||||
for (k, v) in compound.iter() {
|
for (k, v) in compound.iter() {
|
||||||
let k_str = k.to_str();
|
let k_str = k.to_str();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue