use std::collections::HashMap;
use ltk_hash::WadHash;
use super::WadChunk;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone)]
pub struct WadChunks {
chunks: Vec<WadChunk>,
#[cfg_attr(feature = "serde", serde(skip))]
index: HashMap<WadHash, usize>,
}
impl WadChunks {
pub(crate) fn from_iter(iter: impl IntoIterator<Item = WadChunk>) -> Self {
let mut chunks: Vec<WadChunk> = iter.into_iter().collect();
chunks.sort_by_key(|c| c.path_hash);
let index = chunks
.iter()
.enumerate()
.map(|(i, c)| (c.path_hash, i))
.collect();
Self { chunks, index }
}
pub fn len(&self) -> usize {
self.chunks.len()
}
pub fn is_empty(&self) -> bool {
self.chunks.is_empty()
}
pub fn get(&self, path_hash: WadHash) -> Option<&WadChunk> {
self.index.get(&path_hash).map(|&i| &self.chunks[i])
}
pub fn iter(&self) -> impl Iterator<Item = &WadChunk> {
self.chunks.iter()
}
pub fn as_slice(&self) -> &[WadChunk] {
&self.chunks
}
pub fn contains(&self, path_hash: WadHash) -> bool {
self.index.contains_key(&path_hash)
}
}
impl<'a> IntoIterator for &'a WadChunks {
type Item = &'a WadChunk;
type IntoIter = std::slice::Iter<'a, WadChunk>;
fn into_iter(self) -> Self::IntoIter {
self.chunks.iter()
}
}
impl IntoIterator for WadChunks {
type Item = WadChunk;
type IntoIter = std::vec::IntoIter<WadChunk>;
fn into_iter(self) -> Self::IntoIter {
self.chunks.into_iter()
}
}