Skip to content

Commit

Permalink
added insert and remove to tile_set
Browse files Browse the repository at this point in the history
  • Loading branch information
wainwrightmark committed Dec 19, 2024
1 parent a44e1ca commit 9668856
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion src/tile_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ macro_rules! tile_set {
Self(0)
};


/// The set where all tiles are present
#[allow(clippy::cast_possible_truncation)]
pub const ALL: Self = Self(<$inner>::MAX >> (<$inner>::BITS - SIZE as u32));
Expand Down Expand Up @@ -84,6 +83,23 @@ macro_rules! tile_set {
}
}

#[inline]
pub const fn insert(&mut self, tile: &Tile<WIDTH, HEIGHT>) -> bool {
let mask = 1 << tile.inner() as u32;
let r = self.0 & mask == 0;

self.0 |= mask;
r
}

#[inline]
pub const fn remove(&mut self, tile: &Tile<WIDTH, HEIGHT>) -> bool {
let mask = 1 << tile.inner() as u32;
let r = self.0 & mask != 0;
self.0 &= !mask;
r
}

/// Returns a copy of self with the bit at `tile` set to `bit`
#[inline]
pub const fn with_bit_set(&self, tile: &Tile<WIDTH, HEIGHT>, bit: bool) -> Self {
Expand Down Expand Up @@ -255,6 +271,11 @@ macro_rules! tile_set {
Self(self.0 | rhs.0)
}

#[inline]
pub const fn except(&self, rhs: &Self) -> Self {
self.intersect(&rhs.negate())
}

#[must_use]
#[inline]
pub const fn is_subset(&self, rhs: &Self) -> bool {
Expand Down Expand Up @@ -689,6 +710,30 @@ mod tests {
assert_eq!(grid.negate().to_string(), "_*_\n***\n_*_");
}

#[test]
fn test_remove() {
let mut grid: TileSet16<3, 3, 9> = TileSet16::from_fn(|x| x.inner() % 2 == 0);

for tile in Tile::iter_by_row() {
let removed = grid.remove(&tile);
assert_eq!(removed, tile.inner() % 2 == 0);
//removing for the second time
assert!(!grid.remove(&tile));
}
}

#[test]
fn test_insert() {
let mut grid: TileSet16<3, 3, 9> = TileSet16::from_fn(|x| x.inner() % 2 == 0);

for tile in Tile::iter_by_row() {
let inserted = grid.insert(&tile);
assert_eq!(!inserted, tile.inner() % 2 == 0);
//removing for the second time
assert!(!grid.insert(&tile));
}
}

#[test]
fn test_intersect() {
let grid_left: TileSet16<3, 3, 9> = TileSet16::from_fn(|x| x.x() == 1);
Expand Down

0 comments on commit 9668856

Please sign in to comment.