simple_tiled_wfc/
lib.rs

1use bitsetium::{BitSearch, BitSet, BitEmpty, BitIntersection, BitUnion, BitTestNone};
2use std::hash::Hash;
3
4/// A module which contains an info about errors which may occur during Wave Function Collapse
5pub mod errors;
6
7/// A grid variant of algorithm implementation
8pub mod grid_generation;
9
10/// A voxel variant of algorithm implementation
11pub mod voxel_generation;
12mod grid_drawing;
13
14/// An iterator which lets you iterate on bits of an **iterated** bitset which have been set
15///
16/// ### Example usage:
17/// ```
18/// use simple_tiled_wfc::BitsIterator;
19/// let bitset: [u8;2] = [0b00101001; 2];
20/// let mut bits = Vec::new();
21/// for bit in BitsIterator::new(&bitset) {
22///     bits.push(bit);
23/// }
24/// assert_eq!(vec![0, 3, 5, 8, 11, 13], bits);
25/// ```
26pub struct BitsIterator<'a, T: BitSearch>  {
27    iterated: &'a T,
28    idx: usize
29}
30
31impl<'a, T: BitSearch> BitsIterator<'a, T> {
32    /// A constructor for **BitsIterator**
33    pub fn new(iterated: &'a T) -> Self {
34        Self {
35            iterated,
36            idx: 0
37        }
38    }
39}
40
41impl<'a, T: BitSearch> Iterator for BitsIterator<'a, T> {
42    type Item = usize;
43
44    fn next(&mut self) -> Option<Self::Item> {
45        match self.iterated.find_first_set(self.idx) {
46            None => None,
47            Some(id) => {
48                self.idx = id + 1;
49                Some(id)
50            }
51        }
52    }
53}
54
55/// A helper function which lets to know an amount of bits which is are set in a **bit_set**
56pub fn get_bits_set_count<'a, T: BitSearch>(bit_set: &T) -> usize {
57    BitsIterator::new(bit_set).fold(0, |acc, _| acc + 1)
58}
59
60/// A helper function which lets to create a bitset with an exactly one **bit** set
61pub fn make_one_bit_entry<TBitSet: BitEmpty+BitSet>(bit: usize) -> TBitSet {
62    let mut slot = TBitSet::empty();
63    slot.set(bit);
64    slot
65}
66
67/// A helper function which lets to create a bitset with a starting **size** of bits set
68pub fn make_initial_probabilities<TBitSet>(size: usize) -> TBitSet
69    where TBitSet:
70    BitSearch + BitEmpty + BitSet + BitIntersection + BitUnion +
71    BitTestNone + Hash + Eq + Copy + BitIntersection<Output = TBitSet> +
72    BitUnion<Output = TBitSet>
73{
74    (0..size)
75        .fold(
76            TBitSet::empty(),
77            |acc, module_id| {
78                let mut acc = acc;
79                acc.set(module_id);
80                acc
81            }
82        )
83}