Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Search for

# LanguageTable.cpp

    00001 /*
    00002  *             Copyright (C) 2005 Maarten Keijzer
    00003  *
    00004  *          This program is free software; you can redistribute it and/or modify
    00005  *          it under the terms of version 2 of the GNU General Public License as
    00006  *          published by the Free Software Foundation.
    00007  *
    00008  *          This program is distributed in the hope that it will be useful,
    00009  *          but WITHOUT ANY WARRANTY; without even the implied warranty of
    00010  *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00011  *          GNU General Public License for more details.
    00012  *
    00013  *          You should have received a copy of the GNU General Public License
    00014  *          along with this program; if not, write to the Free Software
    00015  *          Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    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 /* Getting info out */
    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; // return original token if no functions of this arity are found
    00093     }
    00094     return rng.choice(pimpl->functions_per_arity[arity]);
    00095 }
    00096
    00097
    00098