Skip to content

Commit

Permalink
Added noexcept to functions, Rule of 5, and copy-swap idiom for copy …
Browse files Browse the repository at this point in the history
…assigment operator.
  • Loading branch information
alejandroarmas committed May 18, 2022
1 parent 42b4047 commit b1495a1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 65 deletions.
85 changes: 36 additions & 49 deletions include/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,15 @@
#include <cstdint>
#include <stdexcept>
#include <memory>
#include <utility>

#include "strong_types.h"



namespace Matrix {


template <typename T, typename Parameter>
class NamedType {
public:
constexpr explicit NamedType(T const& value) : value_(value) {}
constexpr explicit NamedType(T&& value) : value_(std::move(value)) {}
constexpr T& get() { return value_; }
constexpr T const& get() const {return value_; }
private:
T value_;
};

using Rows = NamedType<u_int64_t, struct RowParameter>;
using Columns = NamedType<u_int64_t, struct ColumnParameter>;

Expand All @@ -34,51 +25,41 @@ namespace Matrix {
using matrix_iter = std::vector<float>::iterator;
using const_matrix_iter = std::vector<float>::const_iterator;

Representation() : rows(0), columns(0) {}
Representation(Rows _l, Columns _w) : rows(_l.get()), columns(_w.get()), data(std::vector<float>(_l.get() * _w.get(), 0)) {}
Representation(const Matrix::Representation& _other) : rows(_other.num_rows()), columns(_other.num_cols()), data(_other.data) {}
Representation(const Matrix::Representation&& _other) : rows(_other.num_rows()), columns(_other.num_cols()), data(std::move(_other.data)) {}
~Representation() noexcept = default;
Representation() noexcept : rows(0), columns(0) {}
Representation(Rows _l, Columns _w) noexcept :
rows(_l.get()),
columns(_w.get()),
data(std::vector<float>(_l.get() * _w.get(), 0)) {}

Representation(const Matrix::Representation& _other) noexcept :
rows(_other.rows),
columns(_other.columns),
data(_other.data) {}
Representation(Matrix::Representation&& _other) noexcept :
rows(std::exchange(_other.rows, 0)),
columns(std::exchange(_other.columns, 0)),
data(std::move(_other.data)) {}

Representation operator=(const Matrix::Representation& _other) {
rows = _other.num_rows(); columns = _other.num_cols(); data = _other.data;
Representation& operator=(Matrix::Representation&& _other) noexcept {
swap(*this, _other);
return *this;
}
Representation operator=(const Matrix::Representation&& _other) {
rows = _other.num_rows(); columns = _other.num_cols(); data = std::move(_other.data);
return *this;
}

bool operator==(const Matrix::Representation _other);
bool operator!=(const Matrix::Representation _other);
// Representation operator=(const Matrix::Representation&& _other) {
// rows = _other.num_rows(); columns = _other.num_cols(); data = std::move(_other.data);
// return *this;
// }

constexpr u_int64_t num_rows() const { return rows; }
constexpr u_int64_t num_cols() const { return columns; }
bool operator==(const Matrix::Representation _other) noexcept;
bool operator!=(const Matrix::Representation _other) noexcept;

constexpr u_int64_t num_rows() const noexcept { return rows; }
constexpr u_int64_t num_cols() const noexcept { return columns; }

// constexpr float get(u_int64_t r, u_int64_t c);
// constexpr void put(u_int64_t r, u_int64_t c, float val);

constexpr float get(u_int64_t r, u_int64_t c) const {

uint64_t calculated_index = c + r * columns;

if (r <= rows && c <= columns) {
return data.at(calculated_index);
}
else throw std::range_error("Index not accepted for this Matrix.");

}


constexpr void put(u_int64_t r, u_int64_t c, float val) {

uint64_t calculated_index = c + r * columns;

if (r <= rows && c <= columns) {
data.at(calculated_index) = val;
}
else throw std::range_error("Index not accepted for this Matrix.");

}
float get(u_int64_t r, u_int64_t c) const noexcept;
void put(u_int64_t r, u_int64_t c, float val) noexcept;


constexpr matrix_iter scanStart() { return data.begin(); }
Expand All @@ -88,6 +69,12 @@ namespace Matrix {
constexpr const_matrix_iter constScanEnd() const { return data.cend(); }


friend void swap(Representation& left, Representation& right) noexcept {
std::swap(left.rows, right.rows);
std::swap(left.columns, right.columns);
std::swap(left.data, right.data);
}

private:
u_int64_t rows;
u_int64_t columns;
Expand Down
30 changes: 14 additions & 16 deletions matrix.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include <iostream>
#include <iomanip>
#include <assert.h>

#include "matrix.h"
#include "functions.h"


bool Matrix::Representation::operator==(const Matrix::Representation _other) {
bool Matrix::Representation::operator==(const Matrix::Representation _other) noexcept {

bool isEqual = this->data.size() == _other.data.size();

Expand All @@ -17,7 +18,7 @@ bool Matrix::Representation::operator==(const Matrix::Representation _other) {
}


bool Matrix::Representation::operator!=(const Matrix::Representation _other) {
bool Matrix::Representation::operator!=(const Matrix::Representation _other) noexcept {

bool isEqual = this->data.size() == _other.data.size();

Expand All @@ -28,30 +29,27 @@ bool Matrix::Representation::operator!=(const Matrix::Representation _other) {
return !isEqual;
}

float Matrix::Representation::get(u_int64_t r, u_int64_t c) const noexcept {

assert(r <= rows && c <= columns && "Invalid Matrix Index.");

// constexpr float Matrix::Representation::get(u_int64_t r, u_int64_t c) {
uint64_t calculated_index = c + r * columns;

// uint64_t calculated_index = c + r * columns;
return data.at(calculated_index);

}

// if (r <= rows && c <= columns) {
// return data.at(calculated_index);
// }
// else throw std::range_error("Index not accepted for this Matrix.");

// }
void Matrix::Representation::put(u_int64_t r, u_int64_t c, float val) noexcept {

assert(r <= rows && c <= columns && "Invalid Matrix Index.");

// constexpr void Matrix::Representation::put(u_int64_t r, u_int64_t c, float val) {
uint64_t calculated_index = c + r * columns;

// uint64_t calculated_index = c + r * columns;
data.at(calculated_index) = val;

// if (r <= rows && c <= columns) {
// data.at(calculated_index) = val;
// }
// else throw std::range_error("Index not accepted for this Matrix.");
}

// }



Expand Down

0 comments on commit b1495a1

Please sign in to comment.