Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <variant>
  4. #include <functional>
  5. #include <chrono>
  6.  
  7. class Timer
  8. {
  9. private:
  10. std::chrono::high_resolution_clock
  11. public:
  12. Timer()
  13. {
  14. std::chrono::high_resolution_clock::now();
  15. }
  16. };
  17.  
  18. template<class T>
  19. class Matrix;
  20.  
  21. template<class T>
  22. class IGenerator
  23. {
  24. public:
  25. virtual T generate() = 0;
  26. };
  27.  
  28. template<class T>
  29. class RandomGenerator
  30. {
  31. public:
  32. T generate() { return 0; }
  33. };
  34.  
  35.  
  36. template<>
  37. class Matrix<int>
  38. {
  39. private:
  40. int* ptr;
  41.  
  42. const size_t Ncol;
  43. const size_t Nrow;
  44.  
  45. const size_t length;
  46.  
  47. private:
  48. void FillValues(const std::function<int()>& generator)
  49. {
  50. for (size_t i = 0; i < length; i++)
  51. {
  52. *(ptr + i) = generator();
  53. }
  54. }
  55.  
  56. public:
  57. Matrix(const size_t nrow, const size_t ncol, const std::function<int()>& generator) :Ncol(ncol), Nrow(nrow), length(ncol*nrow)
  58. {
  59. ptr = new int[Ncol*Nrow];
  60.  
  61. FillValues(generator);
  62. };
  63.  
  64. Matrix<int> SlowMultiply(const Matrix<int>& other)
  65. {
  66. if (Nrow != other.Ncol)
  67. {
  68. exit(0);
  69. }
  70.  
  71. Matrix<int> ret(Nrow, Ncol, []() {return 0; });
  72.  
  73. for (size_t iRow = 0; iRow < Nrow; iRow++)
  74. {
  75. for (size_t iCol = 0; iCol < other.Ncol; iCol++)
  76. {
  77. int sum = 0;
  78. for (size_t k = 0; k < Ncol; k++)
  79. {
  80. sum += (*(this->ptr + iRow * Ncol + k))*(*(other.ptr + k * Ncol + iCol));
  81. }
  82.  
  83. *(ret.ptr + iRow * Ncol + iCol) = sum;
  84. }
  85. }
  86. return ret;
  87. }
  88.  
  89. void DebugPrintAll()
  90. {
  91. for (size_t i = 0; i < Nrow; i++)
  92. {
  93. for (size_t j = 0; j < Ncol; j++)
  94. {
  95. std::cout << "\t" << *(ptr + i * Ncol + j);
  96. }
  97. std::cout << std::endl;
  98. }
  99. }
  100.  
  101. ~Matrix()
  102. {
  103. //delete[] ptr;
  104. }
  105. };
  106.  
  107. int main() {
  108.  
  109. Matrix<int> m1(3, 3, []() {static int i = 0; i++; return i; });
  110. Matrix<int> m2(3, 3, []() {static int i = 0; i++; return i; });
  111.  
  112. auto&& a = m1.SlowMultiply(m2);
  113. a.DebugPrintAll();
  114.  
  115. system("pause");
  116. return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement