Template Class Matrix

Class Documentation

template<class T>
class Matrix

This class is a generic matrix datastructure used to hold point data It can own the data or it may not. It can also take ownserhip of a memory buffer. Over time I have become concerned with this datastructure because of this flexibility which can bring bugs if one is not careful…

Public Functions

Matrix(T *ptr_, size_t rows_, size_t cols_)

Construct a new Matrix< T> object.

Parameters
  • ptr_: Raw pointer to underlying data that Matrix DOES NOT own

  • rows_:

  • cols_:

Matrix(size_t rows_, size_t cols_)

Construct a new Matrix< T> object, with memory allocated and owned for rows*cols.

Matrix()

Construct a new Matrix< T> object, everything is empty but it is owned.

Matrix(std::vector<T> &&old_vector, size_t rows_, size_t cols_)

Construct a new Matrix< T> object. We will take ownership of this data buffer.

Parameters
  • old_vector: Old vector that will moved and managed by this new Matrix Object

  • rows_:

  • cols_:

~Matrix()
Matrix(Matrix<T> &a)

Construct a new Matrix< T> object, Copy Constructor. Will take an existing matrix and will perform a copy. If the data is owned it will be copied, if not it wont be copied.

Parameters
  • a:

Matrix(const Matrix<T> &a)

Construct a new Matrix< T> object, Copy Constructor. Will take an existing matrix and will perform a copy. If the data is owned it will be copied, if not it wont be copied.

Parameters
  • a:

Matrix(Matrix<T> &&other)

Default move constructor, take ownership of all the data of other

Parameters
  • other:

Matrix<T> &operator=(const Matrix<T> &a)

Copy assignment operator This one is a little tricky. We make a copy of every element in the data. However if we own the data we reassign ptr to belong to our new copied buffer.

Return

Matrix<T>&

Parameters
  • a:

void UpdatePtrFromData()

Simple helper function to change our ptr to point to our own buffer.

void UpdatePtrFromData(const size_t rows_, const size_t cols_)

Simply updates our rows and columns and data.

Parameters
  • rows_:

  • cols_:

T &operator()(size_t i, size_t j) const

Acces an element of the Matrix using 2D indices.

Return

const T&

Parameters
  • i:

  • j:

T &operator()(size_t index) const

Access an element in the matrix using its underlying buffer 1D index.

Return

const T&

Parameters
  • index:

Public Members

bool own_data

Does the matrix own the data pointed to by ptr. If yes then ptr == data.data().

std::vector<T> data

Buffer of memory that may/may-not be used based upon own_data flag

T *ptr

This is raw pointer that never needs to be freed. It either point to memory in data which will be automatically freed during object destruction (own_data = true). Or it points some other managed memory controlled by the user (own_data = false)

size_t rows

Rows in the matrix

size_t cols

Columns in the matrix

Public Static Functions

template<class G>
static Matrix<T> CopyFromDifferentType(G *ptr_from, size_t rows, size_t cols)

This actually performs a memory copy from an unknown buffer of one type (G) to our buffer of a different type (T). We own this new copied data.

Return

Matrix<T>

Template Parameters
  • G:

Parameters
  • ptr_from:

  • rows:

  • cols: