#ifndef __OPERAND_TABLE_HPP_ #define __OPERAND_TABLE_HPP_ /* "Species" - a CoreWars evolver. Copyright (C) 2003 'Varfar' * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 1, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 675 Mass Ave, Cambridge, MA 02139, USA. */ /* to implement a generator: * 1) derive publically from COperand * 1) give it an identifier in the EImpl enumeration * 2) make the constructor set the _type member * 3) make COperand factories understand this new implementation and how to identify it * 4) implement the read_ini/write_ini functionality */ #include "operand.hpp" #include class COperandTable: public COperand { public: COperandTable(const char *lookup_filename); COperandTable(const field_t coresize); /* constructor creates default values */ // actual query point virtual void rnd(insn_t &instruction,const Field field); // and saving virtual void write_ini(std::ostream &os); virtual void write_override_ini(std::ostream &os,const COperand *parent); // for learning void learn(const char *rc_filename); void dump(std::ostream &out); void save(const char *lookup_filename); protected: // from COperand virtual void read_ini_impl(INIFile &ini); virtual COperand *read_override_ini_impl(INIFile &ini); /* loads freq values; if any freq values are specified, then a copy of this freq will be made; all unspecified values in the new class will be "inherited" from this one, and any specified values will be overriden. If nothing is specified, then this will be returned */ private: static const unsigned FILE_VERSION = 0x0F0001; static const char *FILE_KEY; std::string _filename; void load(const char *lookup_filename,const bool check_constraints=true); // operand statistics class COperandBin { public: COperandBin(const unsigned coresize); unsigned bins() const { return _bins; } // how many bins are there? unsigned bin(const unsigned idx) const; // get the max of this bin unsigned get(const unsigned operand) const; // index of bin this operand falls into void dump(std::ostream &out) const; // trivia unsigned coresize() const { return _coresize; } private: unsigned _coresize, _bins, *_bin; } *_operand_bin; class COperandEntry { public: COperandEntry(const COperandBin *bins); // create a blank to learn COperandEntry(const COperandBin *bins,std::istream &in); // load from a file void learn(const unsigned operand); void save(std::ostream &out) const; void dump(std::ostream &out) const; unsigned suggest() const; private: unsigned _count, *_bin; const COperandBin *_bins; } *_operand[OPCODE_LAST][FIELD_LAST]; // first is A, second is B friend class COperand; // utilities static void bwrite_u16_t(std::ostream &out,const u16_t i); static void bwrite_unsigned(std::ostream &out,const unsigned i); static u16_t bread_u16_t(std::istream &in); static unsigned bread_unsigned(std::istream &in); }; #endif // ifndef __OPERAND_TABLE_HPP_