Skip to content

Commit

Permalink
Replace lazy_static with OnceLock
Browse files Browse the repository at this point in the history
  • Loading branch information
inthar-raven committed Apr 24, 2024
1 parent 1dc0b1b commit f5da84a
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 102 deletions.
1 change: 0 additions & 1 deletion common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ nalgebra = { workspace = true, features = ["rand", "serde-serialize"] }
bincode = "1.2.1"
anyhow = "1.0.26"
quinn = { workspace = true }
lazy_static = "1.4.0"
libm = "0.2.6"
fxhash = "0.2.1"
tracing = "0.1.10"
Expand Down
36 changes: 19 additions & 17 deletions common/src/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::sync::OnceLock;

use crate::dodeca::{Side, Vertex, SIDE_COUNT};
use crate::graph::{Graph, NodeId};
use crate::node::ChunkId;

use lazy_static::lazy_static;

/// Navigates the cubic dual of a graph
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Cursor {
Expand All @@ -28,9 +28,9 @@ impl Cursor {
// in both the dodecahedron sharing the face unique to the new vertex and that sharing the
// face that the new vertex isn't incident to.
let (a, b, c) = (self.a, self.b, self.c);
let a_prime = NEIGHBORS[a as usize][b as usize][c as usize].unwrap();
let b_prime = NEIGHBORS[b as usize][a as usize][c as usize].unwrap();
let c_prime = NEIGHBORS[c as usize][b as usize][a as usize].unwrap();
let a_prime = neighbors_static()[a as usize][b as usize][c as usize].unwrap();
let b_prime = neighbors_static()[b as usize][a as usize][c as usize].unwrap();
let c_prime = neighbors_static()[c as usize][b as usize][a as usize].unwrap();
use Dir::*;
let (sides, neighbor) = match dir {
Left => ((a, b, c_prime), c),
Expand Down Expand Up @@ -103,9 +103,10 @@ impl std::ops::Neg for Dir {
}
}

lazy_static! {
/// Maps every (A, B, C) sharing a vertex to A', the side that shares edges with B and C but not A
static ref NEIGHBORS: [[[Option<Side>; SIDE_COUNT]; SIDE_COUNT]; SIDE_COUNT] = {
/// Maps every (A, B, C) sharing a vertex to A', the side that shares edges with B and C but not A
fn neighbors_static() -> &'static [[[Option<Side>; SIDE_COUNT]; SIDE_COUNT]; SIDE_COUNT] {
static LOCK: OnceLock<[[[Option<Side>; SIDE_COUNT]; SIDE_COUNT]; SIDE_COUNT]> = OnceLock::new();
LOCK.get_or_init(|| {
let mut result = [[[None; SIDE_COUNT]; SIDE_COUNT]; SIDE_COUNT];
for a in Side::iter() {
for b in Side::iter() {
Expand All @@ -114,19 +115,20 @@ lazy_static! {
if s == a || s == b || s == c {
continue;
}
let (opposite, shared) = match (s.adjacent_to(a), s.adjacent_to(b), s.adjacent_to(c)) {
(false, true, true) => (a, (b, c)),
(true, false, true) => (b, (a, c)),
(true, true, false) => (c, (a, b)),
_ => continue,
};
let (opposite, shared) =
match (s.adjacent_to(a), s.adjacent_to(b), s.adjacent_to(c)) {
(false, true, true) => (a, (b, c)),
(true, false, true) => (b, (a, c)),
(true, true, false) => (c, (a, b)),
_ => continue,
};
result[opposite as usize][shared.0 as usize][shared.1 as usize] = Some(s);
}
}
}
}
result
};
})
}

#[cfg(test)]
Expand All @@ -139,8 +141,8 @@ mod tests {
for v in Vertex::iter() {
let [a, b, c] = v.canonical_sides();
assert_eq!(
NEIGHBORS[a as usize][b as usize][c as usize],
NEIGHBORS[a as usize][c as usize][b as usize]
neighbors_static()[a as usize][b as usize][c as usize],
neighbors_static()[a as usize][c as usize][b as usize]
);
}
}
Expand Down
Loading

0 comments on commit f5da84a

Please sign in to comment.