神经网络是序列预测,C++实现
源代码在线查看: mask.cpp
#include #include "mask.hpp" using namespace std; mask::~mask() { delete []data; } mask::mask() { data = NULL; s = 0; } mask::mask(const unsigned int& size) { init(size); } mask::mask(const int& size) { init(size); } mask::mask(bool *d, const unsigned int& size) { data = d; s = size; } mask::mask(const mask &m) { if(m.data==NULL|| (m.s==0)) { cerr " argument &m is corrupt or empty." return; } data = new bool[m.s]; if(data==NULL) { cerr s = 0; } s = m.s; memcpy(data,m.data,sizeof(bool)*m.s); } void mask::init(const unsigned int& size) { data = new bool[size]; if(data==NULL) { cout " could not allocate memory." s = 0; } else { // if we can assure that compiler inits to NULL, then drop this line memset(data,'\0',size*sizeof(bool)); s = size; } } void mask::print() const { cout cout for(unsigned int i=0; i < s; i++) cout } unsigned int mask::size() const { return s; } bool& mask::operator[](const unsigned int &i) { return data[i]; } mask& mask::operator=(const mask& right) { // self assignment if(this == &right) return *this; delete []data; data = new bool[right.s]; if(data==NULL) { cout " could not allocate memory." s = 0; } s = right.s; memcpy(data,right.data,sizeof(bool)*right.s); return *this; } mask operator&&(const mask& left, const mask& right) { unsigned int left_size = left.s; if(left_size!=right.s) { cerr cerr return mask(); } bool* ans_data = new bool[left_size]; if(ans_data==NULL) { cout " out of memory." } for(unsigned int i=0; i < left_size; i++) if(left.data[i] && right.data[i]) { ans_data[i] = true; } else { ans_data[i] = false; } return mask(ans_data,left_size); } bool mask::all() const { for(unsigned int i = 0; i < s; i++) { if(data[i]!=true) { return false; } } return true; } bool mask::any() const { for(unsigned int i = 0; i < s; i++) { if(data[i]==true) { return true; } } return false; } unsigned int mask::total_true() const { unsigned int ans = 0; for(unsigned int i = 0; i < s; i++) { if(data[i]) { ans++; } } return ans; }