mirror of
https://codeberg.org/icewind/vbsp.git
synced 2026-06-03 18:54:05 +02:00
remove itertools for custom cluster iter
This commit is contained in:
parent
1aedf96f25
commit
f22e79269d
3 changed files with 64 additions and 10 deletions
|
|
@ -13,7 +13,6 @@ readme = "README.md"
|
|||
arrayvec = "0.7.2"
|
||||
bitflags = "1.0"
|
||||
bv = "0.11.1"
|
||||
itertools = "0.10.3"
|
||||
thiserror = "1.0.30"
|
||||
lzma-rs = "0.2.0"
|
||||
binrw = "0.8.0"
|
||||
|
|
|
|||
|
|
@ -280,7 +280,7 @@ pub struct Node {
|
|||
|
||||
static_assertions::const_assert_eq!(size_of::<Node>(), 32);
|
||||
|
||||
#[derive(Debug, Clone, BinRead)]
|
||||
#[derive(Default, Debug, Clone, BinRead)]
|
||||
pub struct Leaf {
|
||||
pub contents: i32,
|
||||
pub cluster: i16,
|
||||
|
|
|
|||
71
src/lib.rs
71
src/lib.rs
|
|
@ -11,7 +11,6 @@ use binrw::io::Cursor;
|
|||
use binrw::BinRead;
|
||||
use bspfile::BspFile;
|
||||
pub use error::{BspError, StringError};
|
||||
use itertools::{GroupBy, Itertools};
|
||||
use reader::LumpReader;
|
||||
use std::{io::Read, ops::Deref};
|
||||
|
||||
|
|
@ -71,14 +70,70 @@ impl Leaves {
|
|||
self.leaves
|
||||
}
|
||||
|
||||
// TODO: There's no syntax for `-> T where &T: IntoIterator<...>` and `GroupBy`
|
||||
// doesn't implement `IntoIterator` directly, only `&GroupBy`, so we have
|
||||
// to explicitly specify the type.
|
||||
pub fn clusters<'this>(
|
||||
&'this self,
|
||||
) -> GroupBy<i16, impl Iterator<Item = &'this Leaf>, impl FnMut(&&'this Leaf) -> i16> {
|
||||
self.leaves.iter().group_by(|leaf: &&Leaf| leaf.cluster)
|
||||
pub fn clusters(&self) -> impl Iterator<Item = impl Iterator<Item = &Leaf>> {
|
||||
LeafClusters {
|
||||
leaves: &self.leaves,
|
||||
index: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct LeafClusters<'a> {
|
||||
leaves: &'a [Leaf],
|
||||
index: usize,
|
||||
}
|
||||
|
||||
impl<'a> Iterator for LeafClusters<'a> {
|
||||
type Item = <&'a [Leaf] as IntoIterator>::IntoIter;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let cluster = self.leaves.get(self.index)?.cluster;
|
||||
let remaining_leaves = self.leaves.get(self.index..)?;
|
||||
let cluster_size = remaining_leaves
|
||||
.iter()
|
||||
.take_while(|leaf| leaf.cluster == cluster)
|
||||
.count();
|
||||
self.index += cluster_size;
|
||||
Some(remaining_leaves[0..cluster_size].iter())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_leaf_clusters() {
|
||||
let leaves: Leaves = vec![
|
||||
Leaf {
|
||||
contents: 0,
|
||||
cluster: 0,
|
||||
..Default::default()
|
||||
},
|
||||
Leaf {
|
||||
contents: 1,
|
||||
cluster: 0,
|
||||
..Default::default()
|
||||
},
|
||||
Leaf {
|
||||
contents: 2,
|
||||
cluster: 1,
|
||||
..Default::default()
|
||||
},
|
||||
Leaf {
|
||||
contents: 3,
|
||||
cluster: 2,
|
||||
..Default::default()
|
||||
},
|
||||
Leaf {
|
||||
contents: 4,
|
||||
cluster: 2,
|
||||
..Default::default()
|
||||
},
|
||||
]
|
||||
.into();
|
||||
|
||||
let clustered: Vec<Vec<i32>> = leaves
|
||||
.clusters()
|
||||
.map(|cluster| cluster.map(|leaf| leaf.contents).collect())
|
||||
.collect();
|
||||
assert_eq!(vec![vec![0, 1], vec![2], vec![3, 4],], clustered);
|
||||
}
|
||||
|
||||
impl From<Vec<Leaf>> for Leaves {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue