Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace lazy_static! with OnceLock #383

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
39 changes: 21 additions & 18 deletions common/src/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::dodeca::{Side, Vertex, SIDE_COUNT};
use std::sync::OnceLock;

use crate::dodeca::SIDE_COUNT;
use crate::dodeca::{Side, Vertex};
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 +29,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()[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();
use Dir::*;
let (sides, neighbor) = match dir {
Left => ((a, b, c_prime), c),
Expand Down Expand Up @@ -103,9 +104,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 [[[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 +116,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 +142,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()[a as usize][b as usize][c as usize],
neighbors()[a as usize][c as usize][b as usize]
);
}
}
Expand Down
Loading
Loading