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 0e3690a
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 104 deletions.
35 changes: 18 additions & 17 deletions common/src/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::dodeca::{Side, Vertex, SIDE_COUNT};
use crate::graph::{Graph, NodeId};
use crate::node::ChunkId;

use lazy_static::lazy_static;
use std::sync::OnceLock;

/// Navigates the cubic dual of a graph
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
Expand All @@ -28,9 +27,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 +102,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 +114,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 +140,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 0e3690a

Please sign in to comment.