Commit 27c54197 authored by Sergey Kireev's avatar Sergey Kireev
Browse files

try_ functions fix & refactor in cpp

parent 83e682b9
......@@ -3,8 +3,8 @@
namespace ca2d {
template<class State,Neighborhood_type hood_type>
std::string CA2D<State,hood_type>::construct_filename(const std::string &fn,int it,const std::string &ext) const {
template<class State,Neighborhood_type dirs_type,Neighborhood_type hood_type>
std::string CA2D<State,dirs_type,hood_type>::construct_filename(const std::string &fn,int it,const std::string &ext) const {
std::string filename;
if (it<0) filename = fn + ext;
else {
......@@ -15,8 +15,8 @@ std::string CA2D<State,hood_type>::construct_filename(const std::string &fn,int
return filename;
}
template<class State,Neighborhood_type hood_type>
void CA2D<State,hood_type>::fill(const State &sb,const State &sf,double fracf) {
template<class State,Neighborhood_type dirs_type,Neighborhood_type hood_type>
void CA2D<State,dirs_type,hood_type>::fill(const State &sb,const State &sf,double fracf) {
for (auto &cell: fld)
if (rnd01() < fracf)
cell = sf;
......@@ -24,15 +24,15 @@ void CA2D<State,hood_type>::fill(const State &sb,const State &sf,double fracf) {
cell = sb;
}
template<class State,Neighborhood_type hood_type>
void CA2D<State,hood_type>::fill_part(int xb,int xe,int yb,int ye,const State &s) {
template<class State,Neighborhood_type dirs_type,Neighborhood_type hood_type>
void CA2D<State,dirs_type,hood_type>::fill_part(int xb,int xe,int yb,int ye,const State &s) {
for (int xi = xb; xi < xe; xi++)
for (int yi = ye; yi < ye; yi++)
get(xi,yi) = s;
}
template<class State,Neighborhood_type hood_type>
void CA2D<State,hood_type>::fill_part(int xb,int xe,int yb,int ye,const State &sb,const State &sf,double fracf) {
template<class State,Neighborhood_type dirs_type,Neighborhood_type hood_type>
void CA2D<State,dirs_type,hood_type>::fill_part(int xb,int xe,int yb,int ye,const State &sb,const State &sf,double fracf) {
for (int xi = xb; xi < xe; xi++)
for (int yi = yb; yi < ye; yi++)
if (rnd01() < fracf)
......@@ -41,9 +41,9 @@ void CA2D<State,hood_type>::fill_part(int xb,int xe,int yb,int ye,const State &s
get(xi,yi) = sb;
}
template<class State,Neighborhood_type hood_type>
template<class State,Neighborhood_type dirs_type,Neighborhood_type hood_type>
template<typename ItemType>
bool CA2D<State,hood_type>::load_binary_array_sqr(const std::string &filename,ItemType *data) {
bool CA2D<State,dirs_type,hood_type>::load_binary_array_sqr(const std::string &filename,ItemType *data) {
std::ifstream file;
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
......@@ -67,9 +67,9 @@ bool CA2D<State,hood_type>::load_binary_array_sqr(const std::string &filename,It
return true;
}
template<class State,Neighborhood_type hood_type>
template<class State,Neighborhood_type dirs_type,Neighborhood_type hood_type>
template<typename ItemType>
bool CA2D<State,hood_type>::load_binary_array_hex(const std::string &filename,ItemType *data) {
bool CA2D<State,dirs_type,hood_type>::load_binary_array_hex(const std::string &filename,ItemType *data) {
std::ifstream file;
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
......@@ -113,17 +113,17 @@ bool CA2D<State,hood_type>::load_binary_array_hex(const std::string &filename,It
return true;
}
template<class State,Neighborhood_type hood_type>
template<class State,Neighborhood_type dirs_type,Neighborhood_type hood_type>
template<typename ItemType>
void CA2D<State,hood_type>::save_binary_array_sqr(const std::string &filename,const ItemType *data) const {
void CA2D<State,dirs_type,hood_type>::save_binary_array_sqr(const std::string &filename,const ItemType *data) const {
std::ofstream file(filename,std::fstream::binary);
file.write(reinterpret_cast<const char*>(data),fld.size()*sizeof(State));
file.close();
}
template<class State,Neighborhood_type hood_type>
template<class State,Neighborhood_type dirs_type,Neighborhood_type hood_type>
template<typename ItemType>
void CA2D<State,hood_type>::save_binary_array_hex(const std::string &filename,const ItemType *data) const {
void CA2D<State,dirs_type,hood_type>::save_binary_array_hex(const std::string &filename,const ItemType *data) const {
std::ofstream file(filename,std::fstream::binary);
int j0 = 0;
for (int iy=0;iy<ny;iy++) {
......@@ -163,8 +163,8 @@ void CA2D<State>::save_char(const std::string &fn,int it,const std::string &ext)
}
*/
template<class State,Neighborhood_type hood_type>
void CA2D<State,hood_type>::print_fraction(const std::vector<State> &states,const std::vector<std::string> &names) const {
template<class State,Neighborhood_type dirs_type,Neighborhood_type hood_type>
void CA2D<State,dirs_type,hood_type>::print_fraction(const std::vector<State> &states,const std::vector<std::string> &names) const {
std::map<State,int> count;
int others = 0;
for (auto state: states)
......@@ -188,8 +188,8 @@ void CA2D<State,hood_type>::print_fraction(const std::vector<State> &states,cons
std::cout << std::endl;
}
template<class State,Neighborhood_type hood_type>
void CA2D<State,hood_type>::iterate(int nt) {
template<class State,Neighborhood_type dirs_type,Neighborhood_type hood_type>
void CA2D<State,dirs_type,hood_type>::iterate(int nt) {
for (int i=0;i<nt;i++) {
int p = rndcell();
int px = p % nx;
......@@ -198,11 +198,11 @@ void CA2D<State,hood_type>::iterate(int nt) {
}
}
template<class State,Neighborhood_type hood_type>
void CA2D<State,hood_type>::iterate() { iterate(nx*ny); }
template<class State,Neighborhood_type dirs_type,Neighborhood_type hood_type>
void CA2D<State,dirs_type,hood_type>::iterate() { iterate(nx*ny); }
template<class State,Neighborhood_type hood_type>
std::map<State,int> CA2D<State,hood_type>::count() const {
template<class State,Neighborhood_type dirs_type,Neighborhood_type hood_type>
std::map<State,int> CA2D<State,dirs_type,hood_type>::count() const {
std::map<State,int> cnt;
for (auto s : fld) {
auto p = cnt.find(s);
......@@ -212,8 +212,8 @@ std::map<State,int> CA2D<State,hood_type>::count() const {
return cnt;
}
template<class State,Neighborhood_type hood_type>
void CA2D<State,hood_type>::fix_group_labels() {
template<class State,Neighborhood_type dirs_type,Neighborhood_type hood_type>
void CA2D<State,dirs_type,hood_type>::fix_group_labels() {
for (int ixy=0;ixy<nx*ny;ixy++) {
int label = group[ixy];
if (label != 0) {
......@@ -224,8 +224,8 @@ void CA2D<State,hood_type>::fix_group_labels() {
}
}
template<class State,Neighborhood_type hood_type>
int CA2D<State,hood_type>::find_groups_sqr4(std::function<bool(const State &)> match) {
template<class State,Neighborhood_type dirs_type,Neighborhood_type hood_type>
int CA2D<State,dirs_type,hood_type>::find_groups_sqr4(std::function<bool(const State &)> match) {
if (LOG_CA2D) printf("CA2D<State,%s>::find_groups_sqr4()\n",hood.name().c_str());
if (group.size() != nx*ny) group.resize(nx*ny);
if (groupsize.size() != groups_max) groupsize.resize(groups_max);
......@@ -329,8 +329,8 @@ int CA2D<State,hood_type>::find_groups_sqr4(std::function<bool(const State &)> m
return k;
}
template<class State,Neighborhood_type hood_type>
int CA2D<State,hood_type>::find_groups_sqr8(std::function<bool(const State &)> match) {
template<class State,Neighborhood_type dirs_type,Neighborhood_type hood_type>
int CA2D<State,dirs_type,hood_type>::find_groups_sqr8(std::function<bool(const State &)> match) {
if (LOG_CA2D) printf("CA2D<State,%s>::find_groups_sqr8()\n",hood.name().c_str());
if (group.size() != nx*ny) group.resize(nx*ny);
if (groupsize.size() != groups_max) groupsize.resize(groups_max);
......@@ -490,8 +490,8 @@ int CA2D<State,hood_type>::find_groups_sqr8(std::function<bool(const State &)> m
return k;
}
template<class State,Neighborhood_type hood_type>
int CA2D<State,hood_type>::find_groups_hex(std::function<bool(const State &)> match) {
template<class State,Neighborhood_type dirs_type,Neighborhood_type hood_type>
int CA2D<State,dirs_type,hood_type>::find_groups_hex(std::function<bool(const State &)> match) {
if (LOG_CA2D) printf("CA2D<State,%s>::find_groups_hex()\n",hood.name().c_str());
if (group.size() != nx*ny) group.resize(nx*ny);
if (groupsize.size() != groups_max) groupsize.resize(groups_max);
......@@ -624,8 +624,8 @@ int CA2D<State,hood_type>::find_groups_hex(std::function<bool(const State &)> ma
return k;
}
template<class State,Neighborhood_type hood_type>
GroupStatistics CA2D<State,hood_type>::get_group_statistics(int threshold) {
template<class State,Neighborhood_type dirs_type,Neighborhood_type hood_type>
GroupStatistics CA2D<State,dirs_type,hood_type>::get_group_statistics(int threshold) {
int Count = 0;
int MinSize = nx*ny;
int SumSize = 0;
......
......@@ -32,7 +32,7 @@ struct XY {
enum Neighborhood_type { hood_default, hood_vonNeumann, hood_Moore, hood_hex };
template<Neighborhood_type t> struct Neighborhood_value { static constexpr int value = 0, nmovedirs = 0; };
template<Neighborhood_type t> struct Neighborhood_value { static constexpr int ndirs = 0, nmovedirs = 0; };
template<> struct Neighborhood_value<hood_vonNeumann> { static constexpr int ndirs = 4, nmovedirs = 4; };
template<> struct Neighborhood_value<hood_Moore> { static constexpr int ndirs = 8, nmovedirs = 4; };
template<> struct Neighborhood_value<hood_hex> { static constexpr int ndirs = 6, nmovedirs = 6; };
......@@ -47,8 +47,8 @@ class Neighborhood_base {
private:
const std::array<int,ndirs> _dx = Neighborhood_dirs<ndirs>::dx;
const std::array<int,ndirs> _dy = Neighborhood_dirs<ndirs>::dy;
const std::array<int,ndirs> dx_ = Neighborhood_dirs<ndirs>::dx;
const std::array<int,ndirs> dy_ = Neighborhood_dirs<ndirs>::dy;
protected:
......@@ -64,15 +64,15 @@ class Neighborhood_base {
int rnd() { return distribution(generator); }
void move(int &ix,int &iy,int dir) const {
ix += _dx[dir];
iy += _dy[dir];
ix += dx_[dir];
iy += dy_[dir];
}
XY &move(XY &xy,int dir) const { move(xy.x,xy.y,dir); return xy; }
int opposite(int dir) const { return (dir+count/2)%count; }
int dx(int dir) { return _dx[dir]; }
int dy(int dir) { return _dy[dir]; }
int dx(int dir) { return dx_[dir]; }
int dy(int dir) { return dy_[dir]; }
void fix(int &ix,int &iy,int nx,int ny) const {
while (ix < 0) ix += nx;
......@@ -133,7 +133,7 @@ struct GroupStatistics {
float grpPart;
};
template<class State,Neighborhood_type hood_type>
template<class State,Neighborhood_type dirs_type,Neighborhood_type hood_type=dirs_type>
class CA2D {
protected:
......@@ -143,6 +143,7 @@ protected:
std::vector<int> group;
std::vector<int> groupsize;
Neighborhood<dirs_type> dirs;
Neighborhood<hood_type> hood;
std::default_random_engine generator;
......@@ -183,7 +184,7 @@ public:
const int nx,ny;
const int groups_max;
CA2D(int _nx,int _ny): nx(_nx),ny(_ny),groups_max(_nx*_ny/2+1),fld(_nx*_ny),d01(0.0,1.0),dnxy(0,_nx*_ny-1) {}
CA2D(int nx_,int ny_): nx(nx_),ny(ny_),groups_max(nx_*ny_/2+1),fld(nx_*ny_),d01(0.0,1.0),dnxy(0,nx_*ny_-1) {}
CA2D(int n): nx(n),ny(n),fld(n*n),d01(0.0,1.0),dnxy(0,n*n-1) {}
int xy1d(const int ix,const int iy) const { return iy*nx+ix; }
......@@ -194,18 +195,28 @@ public:
void fill_part(int xb,int xe,int yb,int ye,const State &s);
void fill_part(int xb,int xe,int yb,int ye,const State &sb,const State &sf,double fracf);
State &getfix(int ix,int iy) { hood.fix(ix,iy,nx,ny); return fld[xy1d(ix,iy)]; }
State getfix(int ix,int iy) const { hood.fix(ix,iy,nx,ny); return fld[xy1d(ix,iy)]; }
State &getfix(XY xy) { hood.fix(xy.x,xy.y,nx,ny); return fld[xy1d(xy)]; }
State getfix(XY xy) const { hood.fix(xy.x,xy.y,nx,ny); return fld[xy1d(xy)]; }
State &getfixd(int ix,int iy) { dirs.fix(ix,iy,nx,ny); return fld[xy1d(ix,iy)]; }
State getfixd(int ix,int iy) const { dirs.fix(ix,iy,nx,ny); return fld[xy1d(ix,iy)]; }
State &getfixd(XY xy) { dirs.fix(xy.x,xy.y,nx,ny); return fld[xy1d(xy)]; }
State getfixd(XY xy) const { dirs.fix(xy.x,xy.y,nx,ny); return fld[xy1d(xy)]; }
State &getfixn(int ix,int iy) { hood.fix(ix,iy,nx,ny); return fld[xy1d(ix,iy)]; }
State getfixn(int ix,int iy) const { hood.fix(ix,iy,nx,ny); return fld[xy1d(ix,iy)]; }
State &getfixn(XY xy) { hood.fix(xy.x,xy.y,nx,ny); return fld[xy1d(xy)]; }
State getfixn(XY xy) const { hood.fix(xy.x,xy.y,nx,ny); return fld[xy1d(xy)]; }
State &get(int ix,int iy) { return fld[xy1d(ix,iy)]; }
State get(int ix,int iy) const { return fld[xy1d(ix,iy)]; }
int get(int ix,int iy,int dir) const { hood.move(ix,iy,dir); hood.fix(ix,iy,nx,ny); return get(ix,iy); }
int get(XY xy,int dir) const { hood.move(xy.x,xy.y,dir); hood.fix(xy.x,xy.y,nx,ny); return get(xy.x,xy.y); }
int &get(int ix,int iy,int dir) { hood.move(ix,iy,dir); hood.fix(ix,iy,nx,ny); return get(ix,iy); }
int &get(XY xy,int dir) { hood.move(xy.x,xy.y,dir); hood.fix(xy.x,xy.y,nx,ny); return get(xy.x,xy.y); }
int getd(int ix,int iy,int dir) const { dirs.move(ix,iy,dir); dirs.fix(ix,iy,nx,ny); return get(ix,iy); }
int getd(XY xy,int dir) const { dirs.move(xy.x,xy.y,dir); dirs.fix(xy.x,xy.y,nx,ny); return get(xy.x,xy.y); }
int &getd(int ix,int iy,int dir) { dirs.move(ix,iy,dir); dirs.fix(ix,iy,nx,ny); return get(ix,iy); }
int &getd(XY xy,int dir) { dirs.move(xy.x,xy.y,dir); dirs.fix(xy.x,xy.y,nx,ny); return get(xy.x,xy.y); }
int getn(int ix,int iy,int dir) const { hood.move(ix,iy,dir); hood.fix(ix,iy,nx,ny); return get(ix,iy); }
int getn(XY xy,int dir) const { hood.move(xy.x,xy.y,dir); hood.fix(xy.x,xy.y,nx,ny); return get(xy.x,xy.y); }
int &getn(int ix,int iy,int dir) { hood.move(ix,iy,dir); hood.fix(ix,iy,nx,ny); return get(ix,iy); }
int &getn(XY xy,int dir) { hood.move(xy.x,xy.y,dir); hood.fix(xy.x,xy.y,nx,ny); return get(xy.x,xy.y); }
State &get(int ixy) { return fld[ixy]; }
State get(int ixy) const { return fld[ixy]; }
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment