azalea_registry/tags/
mod.rs1use std::{collections::HashSet, ops::Deref};
2
3use azalea_block::BlockStates;
4
5use crate::{Registry, builtin::BlockKind};
6
7pub mod blocks;
8pub mod entities;
9pub mod fluids;
10pub mod items;
11
12#[derive(Clone, Debug)]
14pub struct RegistryTag<R: Registry + 'static> {
15 entries: Vec<R>,
23}
24impl<R: Registry + 'static> RegistryTag<R> {
25 pub(crate) fn new(entries: Vec<R>) -> Self {
26 debug_assert!(entries.is_sorted());
28
29 Self { entries }
30 }
31}
32impl<R: Registry + 'static> RegistryTag<R> {
33 pub fn contains(&self, value: &R) -> bool {
35 self.find(value).is_some()
36 }
37
38 pub fn remove(&mut self, value: &R) -> Option<R> {
39 self.find(value).map(|index| self.entries.remove(index))
40 }
41
42 fn find(&self, value: &R) -> Option<usize> {
43 if self.entries.len() > 64 {
46 self.binary_search_find(value)
47 } else {
48 self.linear_search_find(value)
49 }
50 }
51 fn linear_search_find(&self, value: &R) -> Option<usize> {
52 self.entries.iter().position(|e| e == value)
53 }
54 fn binary_search_find(&self, value: &R) -> Option<usize> {
55 self.entries.binary_search(value).ok()
56 }
57
58 pub fn into_hashset(&self) -> HashSet<R> {
61 self.clone().into_iter().collect()
62 }
63}
64impl<R: Registry> IntoIterator for RegistryTag<R> {
65 type Item = R;
66
67 type IntoIter = std::vec::IntoIter<R>;
68
69 fn into_iter(self) -> Self::IntoIter {
70 self.entries.into_iter()
71 }
72}
73
74impl<R: Registry> From<RegistryTag<R>> for HashSet<R> {
75 fn from(tag: RegistryTag<R>) -> Self {
76 tag.into_hashset()
77 }
78}
79
80impl<R: Registry> Deref for RegistryTag<R> {
81 type Target = [R];
82
83 fn deref(&self) -> &Self::Target {
84 &self.entries
85 }
86}
87
88impl<R: Registry> FromIterator<R> for RegistryTag<R> {
89 fn from_iter<T: IntoIterator<Item = R>>(iter: T) -> Self {
90 let mut entries = iter.into_iter().collect::<Vec<_>>();
91 entries.sort();
92 Self::new(entries)
93 }
94}
95
96impl From<&RegistryTag<BlockKind>> for BlockStates {
97 fn from(tag: &RegistryTag<BlockKind>) -> Self {
98 Self::from(tag.entries.as_slice())
99 }
100}