Page MenuHomec4science

Matrix.h
No OneTemporary

File Metadata

Created
Wed, Jun 5, 22:27

Matrix.h

//
// Created by Joachim Jacques Koerfer on 06.05.2019.
//
#ifndef MATRIX_H
#define MATRIX_H
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <chrono>
#include "exception.h"
/**
* This matrix class is just a simple wrapper that tries to be as simple as possible without performances sacrifices.
* It stores the element in a row-first format
*/
class Matrix {
private:
using iterator_ = double*;
int size_x_;
int size_y_;
double *data_;
bool init_ = false;
public:
/**
* Constructs an empty Matrix of size size_x*size_y
* @param init if init=false, no value in the matrix is initialized
*/
Matrix(int size_x, int size_y, bool init = true) : size_x_(size_x), size_y_(size_y), init_(true) {
data_ = new double[size_x*size_y];
if(init) {
for(int i = 0;i < size_x*size_y;++i) {
data_[i] = 0;
}
}
};
/**
* Construct Matrix from a binary file
* @param file_name path to the file
* @param size_x
* @param size_y
* @param init if init=false, no value in the matrix is initialized
*/
Matrix(const std::string &file_name, int size_x, int size_y) : size_x_(size_x), size_y_(size_y), init_(true) {
//Check for file existence
std::ifstream file(file_name.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
if(!file.good()) {
throw FileOpenError(file_name);
}
std::streampos size;
size = file.tellg();
char* memblock = new char [size];
file.seekg(0, std::ios::beg);
file.read(memblock, size);
file.close();
data_ = (double*)memblock;
}
/** Copy constructor
* Makes a copy from the parameter copy
* @param copy
*/
Matrix(const Matrix &copy) {
size_x_ = copy.size_x_;
size_y_ = copy.size_y_;
init_ = true;
data_ = new double[size_x_*size_y_];
for(int i = 0;i <size_x_*size_y_;i++) {
data_[i] = copy.data_[i];
}
}
/**
* Destructor
*/
~Matrix() {
if(init_)
delete[] data_ ;
};
/**
* Saves the matrix to the desired file : file_name
* @param file_name
*/
void save(const std::string &file_name) {
//std::ios_base::sync_with_stdio(false);
auto file = std::fstream(file_name.c_str(), std::ios::out | std::ios::binary);
if(!file.good()) {
throw FileWriteError(file_name);
}
double size = size_x_*size_y_*8;
file.write((char*)&data_[0], size);
file.close();
}
/**
* Gets an element from the matrix by reference
* This function doesn't verify if the indices are out of bound.
* @param x index x
* @param y index y
* @return reference to the element
*/
double& operator()(int x, int y) { return data_[size_x_*y + x];};
/**
* Gets an element in the array of the matrix by reference
* This function doesn't verify if the indices are out of bound.
* @param index index which range from 0 to size_x*size_y
* @return reference to the element
*/
double& operator[](int index) { return data_[index]; };
/**
* Iterator function that is there for usage of for(auto d : Matrix) {}
* @return reference to the beginning of the array of doubles
*/
iterator_ begin() { return &data_[0]; }
/**
* Iterator function that is there for usage of for(auto px : Matrix) {}
* @return reference to the end of the array of doubles
*/
iterator_ end() { return &data_[size_x_*size_y_]; };
};
#endif //MATRIX_H

Event Timeline