Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
day03: And now the semi-efficient point set.
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod geom; | ||
mod point_set; | ||
|
||
fn main() { | ||
println!("Hello, world!"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::geom::{Point, Coord}; | ||
|
||
type BitSet = u64; | ||
|
||
fn split(p: Point) -> (Point, BitSet) { | ||
let xh = p.x >> 3; | ||
let yh = p.y >> 3; | ||
let xl = p.x & 7; | ||
let yl = p.y & 7; | ||
let b = (yl << 3) + xl; | ||
(Point { x: xh, y: yh }, 1 << b) | ||
} | ||
|
||
pub struct PointSet { | ||
inner: HashMap<Point, BitSet> | ||
} | ||
|
||
impl PointSet { | ||
pub fn new() -> Self { | ||
Self { inner: HashMap::new() } | ||
} | ||
|
||
pub fn contains(&self, p: Point) -> bool { | ||
let (key, mask) = split(p); | ||
self.inner.get(&key).cloned().unwrap_or(0) & mask != 0 | ||
} | ||
|
||
pub fn insert(&mut self, p: Point) -> bool { | ||
let (key, mask) = split(p); | ||
let grid_ptr = self.inner.entry(key).or_insert(0); | ||
let old = *grid_ptr & mask != 0; | ||
*grid_ptr |= mask; | ||
return old; | ||
} | ||
|
||
pub fn remove(&mut self, p: Point) -> bool { | ||
let (key, mask) = split(p); | ||
let grid_ptr = self.inner.entry(key).or_insert(0); | ||
let old = *grid_ptr & mask != 0; | ||
*grid_ptr &= !mask; | ||
return old; | ||
} | ||
} |