tahoma2d/toonz/sources/include/tlin/tlin_matrix.h

93 lines
2.5 KiB
C
Raw Normal View History

2016-05-17 03:04:11 +12:00
#pragma once
2016-03-19 06:57:51 +13:00
#ifndef TLIN_MATRIX_H
#define TLIN_MATRIX_H
#include "assert.h"
#include "tlin_basicops.h"
2016-06-15 18:43:10 +12:00
namespace tlin {
2016-03-19 06:57:51 +13:00
//----------------------------------------------------------------------------
/*!
The tlin::matrix class represents matrices in tlin-compatible algorithms.
This implementation both serves as a reference to other would-be matrix types,
2016-06-15 18:43:10 +12:00
specifying the core methods they must provide to work in place of a
tcg::matrix;
2016-03-19 06:57:51 +13:00
plus, it provides the naive, universal dense-format of a matrix type.
2016-06-15 18:43:10 +12:00
Please, observe that a tlin::matrix is stored in column-major order, unlike
common
C matrices, since it has to comply with the usual Fortran-style matrices
supported
2016-03-19 06:57:51 +13:00
by every BLAS implementation. In practice, this means that the values() array
stores \b columns in consecutive data blocks.
*/
template <typename T>
2016-06-15 18:43:10 +12:00
class matrix {
int m_rows;
int m_cols;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
T *m_entries;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
matrix() : m_rows(0), m_cols(0), m_entries(0) {}
matrix(int rows, int cols)
: m_rows(rows), m_cols(cols), m_entries(new T[rows * cols]) {
memset(m_entries, 0, m_rows * m_cols * sizeof(T));
}
~matrix() { delete[] m_entries; }
matrix(const matrix &mat)
: m_rows(mat.m_rows)
, m_cols(mat.m_cols)
, m_entries(new T[m_rows * m_cols]) {
memcpy(m_entries, mat.m_entries, m_rows * m_cols * sizeof(T));
}
matrix &operator=(const matrix &mat) {
if (m_rows != mat.m_rows || m_cols != mat.m_cols) {
delete[] m_entries;
m_rows = mat.m_rows, m_cols = mat.m_cols,
m_entries = new T[m_rows * m_cols];
}
memcpy(m_entries, mat.m_entries, m_rows * m_cols * sizeof(T));
return *this;
}
int rows() const { return m_rows; }
int cols() const { return m_cols; }
T &at(int row, int col) { return m_entries[m_rows * col + row]; }
const T &get(int row, int col) const { return m_entries[m_rows * col + row]; }
const T &operator()(int row, int col) const { return get(row, col); }
//-----------------------------------------------------------------------------
// Dense-specific methods
matrix(int rows, int cols, T val)
: m_rows(rows), m_cols(cols), m_entries(new T[rows * cols]) {
fill(val);
}
T *values() { return m_entries; }
const T *values() const { return m_entries; }
void fill(const T &val) {
memset(m_entries, val, m_rows * m_cols * sizeof(T));
}
2016-03-19 06:57:51 +13:00
};
//-------------------------------------------------------------------------
// The Standard matrix data type in tlin is double
typedef tlin::matrix<double> mat;
2016-06-15 18:43:10 +12:00
} // namespace tlin
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
#endif // TLIN_MATRIX_H