A cellular automata library in Rust.
Heavily WIP, not tested whatsoever yet.
License: MIT License
Currently we create a new HashMap
on every loop iteration.
There are a few ways to improve upon this:
HashMap
(might be bad for parallelism?)HashMap
based implementation and perform the whole thing inlineHashMap
but is less DRY, requiring an automaton implementer to duplicate the neighborhood code (check every direction, if there can be a cell there then grab that cell, use the neighborhood to calculate the next state of the current cell).Both methods are less flexible (what if an automaton requires more than one type of neighborhood for its algorithm?). A possible solution for the issues with the solutions I proposed can look like this:
Neighborhood
struct that acts as wrappers for the HashMap
with handy methods for mutating its values. The HashMap
can have as a key every key we use currently for the 3 neighborhood types, which is 12 in total. Maybe the user of the Neighborhood
can decide how many keys they want to allocate in the neighborhood's constructor.*_neighborhood_of
functions with Neighborhood::*
(perhaps new_*
?) functions that take the same parameters as the current versions and return a Self
with the right values in the HashMap
(note that this is less data-oriented [a-la-Clojure] but the performance might be worth it)get_neighborhood_instance
function1 to Automaton
that returns a reference to the specified neighborhood type of the specified type which is stored as a member variable in the automaton implementer struct. The implementer is responsible for populating it with the right values, taking care of None
s and avoiding panics.The implementer can then implement their automaton's logic like this:
fn next_state_of(&self, idx: (usize, usize)) -> Self::State {
// Unsure about the notation.
// Naming will probably be different in the actual code.
let &mut neighborhood = self.get_neighborhood_mut();
neighborhood.modify_according_to(idx, NeighborhoodKind::VonNeumann);
// If the neighborhood has the right keys for the VN kind:
if let Some([n, e, s, w]) = neighborhood.as(NeighborhoodKind::VonNeumann) {
// Do something with n, e, s and w.
}
}
This might count as less functional since there's mutation involved, but I'm not sure there even is a fast immutable way to solve this (unless the current naive implementation isn't actually that slow).
What would a Haskell programmer do? Is it doable in Rust?
1: Naming is subject to change.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.