00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "LanguageTable.h"
00020 #include "Sym.h"
00021
00022 #include <utils/eoRNG.h>
00023
00024 using namespace std;
00025
00026 extern Sym default_const();
00027
00028 class LanguageImpl {
00029 public :
00030 std::vector<Sym> vars;
00031 LanguageTable::erc_func erc;
00032
00033 std::vector<functor_t> functions;
00034 std::vector< std::vector<token_t> > functions_per_arity;
00035
00036 LanguageImpl() : erc(default_const) {}
00037 };
00038
00039 LanguageTable::LanguageTable() {
00040 pimpl = new LanguageImpl;
00041 }
00042
00043 LanguageTable::~LanguageTable() {
00044 delete pimpl;
00045 }
00046
00047 LanguageTable::LanguageTable(const LanguageTable& that) {
00048 pimpl = new LanguageImpl(*that.pimpl);
00049 }
00050
00051 LanguageTable& LanguageTable::operator=(const LanguageTable& that) {
00052 *pimpl = *that.pimpl;
00053 return *this;
00054 }
00055
00056 void LanguageTable::add_function(token_t token, unsigned arity) {
00057 functor_t f = {token, arity};
00058 add_function( f );
00059 }
00060
00061 void LanguageTable::add_function(functor_t f) {
00062
00063 if (f.arity > 0) {
00064 pimpl->functions.push_back(f);
00065
00066 } else {
00067 pimpl->vars.push_back(Sym(f.token));
00068 }
00069
00070 if (pimpl->functions_per_arity.size() <= f.arity) pimpl->functions_per_arity.resize(f.arity+1);
00071 pimpl->functions_per_arity[f.arity].push_back(f.token);
00072
00073 }
00074
00075 void LanguageTable::set_erc( erc_func func) { pimpl->erc = func; }
00076
00077
00078
00079 extern Sym SymConst(double val);
00080
00081 Sym LanguageTable::get_random_var() const { return rng.choice(pimpl->vars); }
00082 Sym LanguageTable::get_random_const() const { return pimpl->erc(); }
00083
00084 functor_t LanguageTable::get_random_function() const
00085 {
00086 return rng.choice(pimpl->functions);
00087 }
00088
00089 token_t LanguageTable::get_random_function(token_t token, unsigned arity) const
00090 {
00091 if (pimpl->functions_per_arity.size() <= arity || pimpl->functions_per_arity[arity].empty()) {
00092 return token;
00093 }
00094 return rng.choice(pimpl->functions_per_arity[arity]);
00095 }
00096
00097
00098