fork(3) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <cassert>
  5.  
  6. using namespace std;
  7.  
  8. class BoolMatr {
  9. size_t start;
  10. std::vector<std::map<size_t, bool>> data;
  11.  
  12. public:
  13. BoolMatr () : start(0) {}
  14.  
  15. BoolMatr (size_t n, size_t m, size_t start) : start(start) {
  16. resize (n, m);
  17. }
  18.  
  19. void resize (size_t n, size_t m) {
  20. data.resize (n);
  21. for (size_t i=0; i < n; ++i)
  22. for (size_t j=0; j < m; ++j)
  23. if (data[i].find (j) == data[i].end())
  24. data[i][j] = false;
  25. }
  26.  
  27. size_t rows() const { return data.size(); }
  28. size_t cols() const { return data[0].size(); }
  29. size_t from() const { return start; }
  30.  
  31. bool& operator()(size_t i, size_t j) {
  32. assert (i-start < data.size() && j-start < data[i-start].size());
  33. return data[i-start][j-start];
  34. }
  35.  
  36. bool operator()(size_t i, size_t j) const {
  37. return const_cast<BoolMatr*>(this)->operator()(i,j);
  38. }
  39.  
  40. BoolMatr& operator+= (const BoolMatr& rhs) {
  41. assert (data.size() != 0 && data.size() == rhs.data.size() && data[0].size() == rhs.data[0].size());
  42. for (size_t i=0; i < data.size(); ++i)
  43. for (size_t j=0; j < data[i].size(); ++j)
  44. (*this)(i+from(), j+from()) ^= rhs(i+rhs.from(), j+rhs.from());
  45. return *this;
  46. }
  47. };
  48.  
  49. BoolMatr operator+ (const BoolMatr& lhs, const BoolMatr& rhs) {
  50. return BoolMatr (lhs)+=rhs;
  51. }
  52.  
  53. std::ostream& operator << (std::ostream& os, const BoolMatr& m) {
  54. for (size_t i=0; i < m.rows(); ++i) {
  55. for (size_t j=0; j < m.cols(); ++j)
  56. std::cout << m(i+m.from(),j+m.from()) << ' ';
  57. std::cout << std::endl;
  58. }
  59. return os;
  60. }
  61.  
  62. int main() {
  63. BoolMatr a(2,3,1), b(2,3,1);
  64.  
  65. for (size_t i=a.from(); i < a.from()+a.rows(); ++i)
  66. for (size_t j=a.from(); j < a.from()+a.cols(); ++j)
  67. a(i,j)= (i+j)%2 == 0;
  68.  
  69. for (size_t i=b.from(); i < b.from()+b.rows(); ++i)
  70. for (size_t j=b.from(); j < b.from()+b.cols(); ++j)
  71. b(i,j)= (i+j)%4 == 0;
  72.  
  73. BoolMatr c = a+b;
  74.  
  75. std::cout << a << std::endl << b << std::endl << c << std::endl;
  76.  
  77. return 0;
  78. }
Success #stdin #stdout 0s 3436KB
stdin
Standard input is empty
stdout
1 0 1 
0 1 0 

0 0 1 
0 1 0 

1 0 0 
0 0 0