Small changes

master
hheik 2026-04-24 18:25:30 +03:00
parent 5c0ed07fb2
commit 4d75a0bf97
1 changed files with 8 additions and 4 deletions

View File

@ -6,6 +6,7 @@
/// position, index, or key to a hashmap. It should allow O(1) access to the Node.
pub trait GraphLike<Node, NodeId> {
fn get_node(&self, id: NodeId) -> Option<&Node>;
fn set_node(&mut self, id: NodeId, node: Node);
fn iter_ids(&self) -> impl Iterator<Item = NodeId>;
fn iter_neighbor_ids(&self, id: NodeId) -> impl Iterator<Item = NodeId>;
@ -21,23 +22,26 @@ pub trait GraphLike<Node, NodeId> {
/// TODO: Replace return type with a result that explains the reason for failure. This could
/// help with rollback to recover from impossible generation
fn collapse_one(&mut self, rules: &impl Ruleset<Node, NodeId>) -> Option<NodeId> where NodeId: Copy, Self: Sized {
fn single_step(&mut self, rules: &impl Ruleset<Node, NodeId>) -> Option<NodeId> where NodeId: Copy, Self: Sized {
let id = rules.find_lowest_entropy(self)?;
rules.choose(self, id);
let node = rules.choose(self, id).expect("Ruleset::choose should also return Some if Ruleset::entropy returns Some");
let empty_ids = self.iter_empty();
None
}
}
pub trait Ruleset<Node, NodeId> {
fn entropy(&self, graph: &impl GraphLike<Node, NodeId>, id: NodeId) -> Option<f32> where NodeId: Copy;
fn choose(&self, graph: &impl GraphLike<Node, NodeId>, id: NodeId) -> Node where NodeId: Copy;
fn choose(&self, graph: &impl GraphLike<Node, NodeId>, id: NodeId) -> Option<Node> where NodeId: Copy;
fn find_lowest_entropy(&self, graph: &impl GraphLike<Node, NodeId>) -> Option<NodeId> where NodeId: Copy {
graph.iter_empty()
.map(|id| (id, self.entropy(graph, id)))
// Option<f32> apparently implements PartialOrd
// NOTE: Option<f32> implements PartialOrd
// None is considered smaller than any Some value
// This works out, since we want to find impossible nodes (entropy returns None)
// But make sure this doesn't cause unexpected behavior later on
.min_by(|a, b| a.1.partial_cmp(&b.1).unwrap())
.map(|(id, _)| id)
}