1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#ifndef _BLASR_MATRIX_IMPL_HPP
#define _BLASR_MATRIX_IMPL_HPP
#include <cassert>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <vector>
#include <pbdata/Types.h>
#include <pbdata/utils.hpp>
template <typename T>
void CreateMatrix(VectorIndex rows, int cols, std::vector<T *> matrix)
{
matrix.resize(rows);
if (matrix[0]) {
delete[] matrix[0];
matrix[0] = NULL;
}
matrix[0] = ProtectedNew<T>(rows * cols);
VectorIndex r = 1;
for (r = 1; r < rows; r++) {
matrix[r] = &matrix[cols * r];
}
}
template <typename T>
Matrix<T>::Matrix()
{
nRows = 0;
rowsBufferSize = 0;
nCols = 0;
matrixBufferSize = 0;
matrix = NULL;
}
template <typename T>
unsigned int Matrix<T>::size()
{
return nRows * nCols;
}
template <typename T>
unsigned int Matrix<T>::GetNCols()
{
return nCols;
}
template <typename T>
unsigned int Matrix<T>::GetNRows()
{
return nRows;
}
template <typename T>
void Matrix<T>::Resize(VectorIndex nRowsP, VectorIndex nColsP)
{
nRows = nRowsP;
nCols = nColsP;
matrixSize = nRows * nCols;
if (nRows * nCols > matrixBufferSize) {
matrixBufferSize = nRows * nCols;
if (nRows > rowsBufferSize) {
if (matrix != NULL) {
delete[] matrix;
matrix = NULL;
}
}
if (matrix == NULL) {
matrix = ProtectedNew<T *>(nRows);
} else {
if (matrix[0] != NULL) {
delete[] matrix[0];
matrix[0] = NULL;
}
}
matrix[0] = ProtectedNew<T>(matrixBufferSize);
VectorIndex rowIndex;
for (rowIndex = 1; rowIndex < nRows; rowIndex++) {
matrix[rowIndex] = &matrix[0][nCols * rowIndex];
}
}
}
template <typename T>
Matrix<T>::Matrix(VectorIndex nRowsP, VectorIndex nColsP)
{
Resize(nRowsP, nColsP);
}
template <typename T>
void Matrix<T>::Reference(Matrix<T> &rhs)
{
matrix = rhs.matrix;
}
template <typename T>
void Matrix<T>::Initialize(T value)
{
std::fill(&matrix[0][0], &matrix[0][matrixSize], value);
}
template <typename T>
T *Matrix<T>::operator[](VectorIndex rowIndex)
{
assert(rowIndex < nRows);
return matrix[rowIndex];
}
template <typename T>
void Matrix<T>::Free()
{
if (matrix != NULL) {
if (matrix[0] != NULL) {
delete[] matrix[0];
}
delete[] matrix;
}
matrix = NULL;
}
template <typename T>
void Matrix<T>::Print(std::ofstream &out)
{
VectorIndex i;
VectorIndex j;
for (i = 0; i < nRows; i++) {
for (j = 0; j < nCols; j++) {
out << matrix[i][j] << " ";
}
out << std::endl;
}
}
#endif
|