1 #ifndef __CDistribution_HPP__
2 #define __CDistribution_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 #include "ini.hpp"
22 #include <iostream>
23 #include <string>
24 
25 class CDistribution { /* utility provides a very configurable random number with a bias */
26 	public:
27 		typedef CDistribution *(*ImplFactoryCallback)(const CDistribution &copy);
ImplFactory(const CDistribution & copy)28 		static CDistribution *ImplFactory(const CDistribution &copy) { return new CDistribution(copy); }
29 		CDistribution(const std::string &context,const int min,const int max);
30 		~CDistribution();
31 		void set(const int min,const int max); // change this into a flat distribution between these points
32 		void read_ini(INIFile &ini,bool required = true);
33 		CDistribution *read_override_ini(INIFile &ini,ImplFactoryCallback factory=ImplFactory);
34 		void write_ini(std::ostream &os);
35 		void write_override_ini(std::ostream &os,const CDistribution *parent = 0);
36 		int rnd() const; /* return a random number within this distribution */
min() const37 		int min() const { return _data[0]; }
max() const38 		int max() const { return _data[_size-1]; }
ok(const int val) const39 		bool ok(const int val) const { return ((min() <= val) && (val < max())); }
40 	private:
41 		CDistribution();
42 		std::string _context;
43 		int	_size, /* number of points in the data */
44 			*_data; /* the actual series */
45 };
46 
47 #endif // __CDistribution_HPP__
48