SHARE
TWEET

Untitled

a guest Dec 9th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <memory>
  3. #include <random>
  4.  
  5. #include <ctime>
  6. #include <cctype>
  7. #include <cassert>
  8.  
  9. using namespace std;
  10.  
  11.  
  12.  
  13. float random(int min, int max) {
  14.     static mt19937 gen(static_cast<uint32_t>(time(0)));
  15.     uniform_int_distribution<int> distribution(min * 100, max * 100);
  16.     return static_cast<float>(distribution(gen)) / 100.0f;
  17. }
  18.  
  19.  
  20.  
  21. class matrix {
  22. protected:
  23.     shared_ptr<float[]> data;
  24.     size_t width_;
  25.     size_t height_;
  26.  
  27. public:
  28.     explicit matrix(size_t width, size_t height) :
  29.         width_(width), height_(height), data(new float[width * height])
  30.     {
  31.         assert(width != 0 && height != 0);
  32.         for (size_t i = 0; i < width * height; ++i) {
  33.             data[i] = random(-10, 10);
  34.         }
  35.     }
  36.  
  37.     matrix(const matrix & mtx) : data(mtx.data), width_(mtx.width_), height_(mtx.height_) { }
  38.  
  39.     virtual ~matrix() { }
  40.  
  41.     virtual float & operator()(size_t x, size_t y) {
  42.         assert(x < width_ && y < height_);
  43.         return data[y * width_ + x];
  44.     }
  45.  
  46.     virtual size_t width()  { return width_;  }
  47.     virtual size_t height() { return height_; }
  48.  
  49.     virtual void print() {
  50.         for (size_t y = 0; y < height_; ++y) {
  51.             for (size_t x = 0; x < width_; ++x) {
  52.                 cout << operator()(x, y) << "\t";
  53.             }
  54.             cout << endl;
  55.         }
  56.     }
  57. };
  58.  
  59.  
  60.  
  61. class slice : public matrix {
  62. private:
  63.     size_t x;
  64.     size_t y;
  65.     size_t partial_width;
  66.     size_t partial_height;
  67.  
  68. public:
  69.     explicit slice(matrix mtx, size_t x, size_t y, size_t width, size_t height) :
  70.         matrix(mtx), x(x), y(y), partial_width(width), partial_height(height)
  71.     {
  72.         assert(x < mtx.width() && y < mtx.height());
  73.         assert(width != 0 && height != 0);
  74.         assert(width + x <= mtx.width() && height + y <= mtx.height());
  75.     }
  76.  
  77.     float & operator()(size_t x, size_t y) override {
  78.         return matrix::operator()(this->x + x, this->y + y);
  79.     }
  80.  
  81.     size_t width()  override { return partial_width;  }
  82.     size_t height() override { return partial_height; }
  83.  
  84.     void print() override {
  85.         for (size_t j = 0; j < partial_height; ++j) {
  86.             for (size_t i = 0; i < partial_width; ++i) {
  87.                 cout << operator()(i, j) << "\t";
  88.             }
  89.             cout << endl;
  90.         }
  91.     }
  92. };
  93.  
  94.  
  95.  
  96. int32_t main() {
  97.     auto m = matrix{ 6, 4 };
  98.     m.print();
  99.  
  100.     cout << endl << endl;
  101.  
  102.     auto s = slice{ m, 3, 1, 3, 2 };
  103.     s.print();
  104. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top