Skip to main content

azalea_registry/tags/
mod.rs

1use 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/// A set of registry items.
13#[derive(Clone, Debug)]
14pub struct RegistryTag<R: Registry + 'static> {
15    // yes, this *could* be a Box<[R]>, but there are cases in which the user
16    // may want to cheaply mutate this.
17    // yes, it could also be a [R; N] using a const generic, but that makes
18    // RegistryTag annoying if the user wants to put it in their own code.
19    // yes, if the aforementioned issues are ignored then we could even avoid a
20    // LazyLock. but that would provide nearly no benefit and result in wasted
21    // memory for unused tags. having it be a vec is fine.
22    entries: Vec<R>,
23}
24impl<R: Registry + 'static> RegistryTag<R> {
25    pub(crate) fn new(entries: Vec<R>) -> Self {
26        // must be sorted for binary search
27        debug_assert!(entries.is_sorted());
28
29        Self { entries }
30    }
31}
32impl<R: Registry + 'static> RegistryTag<R> {
33    /// Returns whether the given item is contained in this registry.
34    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        // TODO: tune this number; when does binary search actually start making
44        // a difference?
45        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    // this exists for convenience since we can't always do HashSet::from if
59    // it's a LazyLock
60    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}