1 #ifndef __OPERAND_TABLE_HPP_
2 #define __OPERAND_TABLE_HPP_
3 
4 /* "Species" - a CoreWars evolver.  Copyright (C) 2003 'Varfar'
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 1, or (at your option) any later
9  * version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 /* to implement a generator:
22  * 1) derive publically from COperand
23  * 1) give it an identifier in the EImpl enumeration
24  * 2) make the constructor set the _type member
25  * 3) make COperand factories understand this new implementation and how to identify it
26  * 4) implement the read_ini/write_ini functionality
27  */
28 
29 #include "operand.hpp"
30 #include <string>
31 
32 class COperandTable: public COperand {
33 	public:
34 		COperandTable(const char *lookup_filename);
35 		COperandTable(const field_t coresize); /* constructor creates default values */
36 		// actual query point
37 		virtual void rnd(insn_t &instruction,const Field field);
38 		// and saving
39 		virtual void write_ini(std::ostream &os);
40 		virtual void write_override_ini(std::ostream &os,const COperand *parent);
41 		// for learning
42 		void learn(const char *rc_filename);
43 		void dump(std::ostream &out);
44 		void save(const char *lookup_filename);
45 	protected:
46 		// from COperand
47 		virtual void read_ini_impl(INIFile &ini);
48 		virtual COperand *read_override_ini_impl(INIFile &ini); /* loads freq values; if any freq values are
49 			specified, then a copy of this freq will be made; all unspecified values in the new
50 			class will be "inherited" from this one, and any specified values will be overriden.  If nothing
51 			is specified, then this will be returned */
52 	private:
53 		static const unsigned FILE_VERSION = 0x0F0001;
54 		static const char *FILE_KEY;
55 		std::string _filename;
56 		void load(const char *lookup_filename,const bool check_constraints=true);
57 		// operand statistics
58 		class COperandBin {
59 			public:
60 				COperandBin(const unsigned coresize);
bins() const61 				unsigned bins() const { return _bins; } // how many bins are there?
62 				unsigned bin(const unsigned idx) const; // get the max of this bin
63 				unsigned get(const unsigned operand) const; // index of bin this operand falls into
64 				void dump(std::ostream &out) const; // trivia
coresize() const65 				unsigned coresize() const { return _coresize; }
66 			private:
67 				unsigned _coresize,
68 					_bins,
69 					*_bin;
70 		} *_operand_bin;
71 		class COperandEntry {
72 			public:
73 				COperandEntry(const COperandBin *bins); // create a blank to learn
74 				COperandEntry(const COperandBin *bins,std::istream &in); // load from a file
75 				void learn(const unsigned operand);
76 				void save(std::ostream &out) const;
77 				void dump(std::ostream &out) const;
78 				unsigned suggest() const;
79 			private:
80 				unsigned _count,
81 					*_bin;
82 				const COperandBin *_bins;
83 		} *_operand[OPCODE_LAST][FIELD_LAST]; // first is A, second is B
84 		friend class COperand;
85 		// utilities
86 		static void bwrite_u16_t(std::ostream &out,const u16_t i);
87 		static void bwrite_unsigned(std::ostream &out,const unsigned i);
88 		static u16_t bread_u16_t(std::istream &in);
89 		static unsigned bread_unsigned(std::istream &in);
90 };
91 
92 #endif // ifndef __OPERAND_TABLE_HPP_
93