Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <variant>
- #include <functional>
- #include <chrono>
- class Timer
- {
- private:
- std::chrono::high_resolution_clock
- public:
- Timer()
- {
- std::chrono::high_resolution_clock::now();
- }
- };
- template<class T>
- class Matrix;
- template<class T>
- class IGenerator
- {
- public:
- virtual T generate() = 0;
- };
- template<class T>
- class RandomGenerator
- {
- public:
- T generate() { return 0; }
- };
- template<>
- class Matrix<int>
- {
- private:
- int* ptr;
- const size_t Ncol;
- const size_t Nrow;
- const size_t length;
- private:
- void FillValues(const std::function<int()>& generator)
- {
- for (size_t i = 0; i < length; i++)
- {
- *(ptr + i) = generator();
- }
- }
- public:
- Matrix(const size_t nrow, const size_t ncol, const std::function<int()>& generator) :Ncol(ncol), Nrow(nrow), length(ncol*nrow)
- {
- ptr = new int[Ncol*Nrow];
- FillValues(generator);
- };
- Matrix<int> SlowMultiply(const Matrix<int>& other)
- {
- if (Nrow != other.Ncol)
- {
- exit(0);
- }
- Matrix<int> ret(Nrow, Ncol, []() {return 0; });
- for (size_t iRow = 0; iRow < Nrow; iRow++)
- {
- for (size_t iCol = 0; iCol < other.Ncol; iCol++)
- {
- int sum = 0;
- for (size_t k = 0; k < Ncol; k++)
- {
- sum += (*(this->ptr + iRow * Ncol + k))*(*(other.ptr + k * Ncol + iCol));
- }
- *(ret.ptr + iRow * Ncol + iCol) = sum;
- }
- }
- return ret;
- }
- void DebugPrintAll()
- {
- for (size_t i = 0; i < Nrow; i++)
- {
- for (size_t j = 0; j < Ncol; j++)
- {
- std::cout << "\t" << *(ptr + i * Ncol + j);
- }
- std::cout << std::endl;
- }
- }
- ~Matrix()
- {
- //delete[] ptr;
- }
- };
- int main() {
- Matrix<int> m1(3, 3, []() {static int i = 0; i++; return i; });
- Matrix<int> m2(3, 3, []() {static int i = 0; i++; return i; });
- auto&& a = m1.SlowMultiply(m2);
- a.DebugPrintAll();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement