1 // This file is part of Golly. 2 // See docs/License.html for the copyright notice. 3 4 #ifndef RULETABLE_ALGO_H 5 #define RULETABLE_ALGO_H 6 #include "ghashbase.h" 7 #include <string> 8 #include <vector> 9 #include <utility> 10 /** 11 * An algo that takes a rule table. 12 */ 13 class ruletable_algo : public ghashbase { 14 15 public: 16 17 ruletable_algo() ; 18 virtual ~ruletable_algo() ; 19 virtual state slowcalc(state nw, state n, state ne, state w, state c, 20 state e, state sw, state s, state se) ; 21 virtual const char* setrule(const char* s) ; 22 virtual const char* getrule() ; 23 virtual const char* DefaultRule() ; 24 virtual int NumCellStates() ; 25 static void doInitializeAlgoInfo(staticAlgoInfo &) ; 26 27 // these two methods are needed for RuleLoader algo 28 bool IsDefaultRule(const char* rulename); 29 const char* LoadTable(FILE* rulefile, int lineno, char endchar, const char* s); 30 31 protected: 32 33 std::string LoadRuleTable(std::string filename); 34 void PackTransitions(const std::string& symmetries, int n_inputs, 35 const std::vector< std::pair< std::vector< std::vector<state> >, state> > & transition_table); 36 void PackTransition(const std::vector< std::vector<state> > & inputs, state output); 37 38 protected: 39 40 std::string current_rule; 41 unsigned int n_states; 42 enum TNeighborhood { vonNeumann, Moore, hexagonal, oneDimensional } neighborhood; 43 static const int N_SUPPORTED_NEIGHBORHOODS = 4; 44 static const std::string neighborhood_value_keywords[N_SUPPORTED_NEIGHBORHOODS]; 45 46 // we use a lookup table to match inputs to outputs: 47 typedef unsigned long long int TBits; // we can use unsigned int if we hit portability issues (not much slower) 48 std::vector< std::vector< std::vector<TBits> > > lut; // TBits lut[neighbourhood_size][n_states][n_compressed_rules]; 49 unsigned int n_compressed_rules; 50 std::vector<state> output; // state output[n_rules]; 51 52 }; 53 #endif 54