Skip to content

Commit

Permalink
Внёс все исправления, а также инкапсулировал вспомогательные функции …
Browse files Browse the repository at this point in the history
…и немного причесал cmake
  • Loading branch information
KotDath2 committed Apr 7, 2021
1 parent e4d04b3 commit e0b1c5b
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 70 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ set(OUTOFRANGEEXCEPTION_FILES src/OutOfRangeException.cpp include/OutOfRangeExce
set(TESTS Tests/VectorArithmetic.cpp Tests/Exception.cpp Tests/VectorUnary.cpp Tests/Complex.cpp Tests/Matrix.cpp)
set(INCOMPATIBLEDIMEXCEPTION_FILES include/IncompatibleDimException.h src/IncompatibleDimException.cpp)

include_directories(include)

add_executable(TEST main.cpp ${VECTOR_FILES} ${CSRMATRIX_FILES} ${OUTOFRANGEEXCEPTION_FILES}
${TESTS} ${INCOMPATIBLEDIMEXCEPTION_FILES})

Expand Down
4 changes: 2 additions & 2 deletions Tests/Complex.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "gtest/gtest.h"
#include "../include/Vector.h"
#include "../include/OutOfRangeException.h"
#include "Vector.h"
#include "OutOfRangeException.h"

TEST(ComplexTest, First) {
Vector first(3), second(3), third(3), result(3);
Expand Down
5 changes: 2 additions & 3 deletions Tests/Exception.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "gtest/gtest.h"
#include "../include/Vector.h"
#include "../include/OutOfRangeException.h"
#include "../include/IncompatibleDimException.h"
#include "Vector.h"
#include "OutOfRangeException.h"


TEST(OutOfRangeExceptionTest, SimpleVector) {
Expand Down
4 changes: 2 additions & 2 deletions Tests/Matrix.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "gtest/gtest.h"
#include "../include/Vector.h"
#include "../include/OutOfRangeException.h"
#include "Vector.h"
#include "OutOfRangeException.h"

TEST(MatrixTest, GetNonZero) {
double a[] = {1, 2, 3, 4, 1, 11};
Expand Down
2 changes: 1 addition & 1 deletion Tests/VectorArithmetic.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "gtest/gtest.h"
#include "../include/Vector.h"
#include "Vector.h"

TEST(BinaryPlusTest, SimpleAB) {
Vector a(3);
Expand Down
15 changes: 12 additions & 3 deletions Tests/VectorUnary.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "gtest/gtest.h"
#include "../include/Vector.h"
#include "Vector.h"

TEST(VectorUnary, UnaryMinus) {
Vector a(3);
Expand All @@ -14,7 +14,8 @@ TEST(VectorUnary, CopyConstrucor) {
Vector a(5);
a[0] = 1.12, a[1] = -0.1, a[2] = 13.7, a[3] = 3.33, a[4] = -0;
Vector b(a);
Vector c = a;
Vector c(3);
c = a;
ASSERT_EQ(b, c);
}

Expand All @@ -36,11 +37,19 @@ TEST(VectorUnary, DimTest) {
TEST(PointerTest, Simple) {
Vector a(3);
a[0] = 1, a[1] = 2, a[2] = 3;
double* pointer = a;
double* pointer = static_cast<double*>(a);
ASSERT_EQ(*(pointer + 1), 2);
}

TEST(ConstTest, Simple) {
const Vector a(3, 5);
ASSERT_EQ(a[1], 5);
}

TEST(VectorUnary, ZeroSize) {
Vector a(0);
Vector b(3);
b[0] = 1, b[1] = 2, b[2] = 3;
a = b;
ASSERT_EQ(a, b);
}
21 changes: 11 additions & 10 deletions include/CSRMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@ class CSRMatrix {
int GetCountOfNoneZeroElem() const;
std::pair<int, int> GetShape() const;

//!!! operator= ?

CSRMatrix& operator=(const CSRMatrix& mat);
friend std::ostream& operator<<(std::ostream& out, const CSRMatrix& mat);
friend std::istream& operator>>(std::istream& in, CSRMatrix& mat);
friend void PrintMatrix(std::ostream& out, const CSRMatrix& mat);

~CSRMatrix();
private:
template<typename T>
static void PrintArray(std::ostream& out, T* arr, int size);

template<typename T>
static T* ReadArray(std::istream& in, T* arr, int size);

static void PrintMatrix(std::ostream& out, const CSRMatrix& mat);

template<typename T>
static void ResizeIfDifferentSize(T*& arr, T* newArr, int& size, int newSize);
double* values;
int valuesSize;
int* indices;
Expand All @@ -39,10 +47,3 @@ Vector operator*(const Vector& vec, const CSRMatrix& mat);
std::ostream& operator<<(std::ostream& out, const CSRMatrix& mat);
std::istream& operator>>(std::istream& in, CSRMatrix& mat);

template<typename T>
void PrintArray(std::ostream& out, T* arr, int size);

template<typename T>
T* ReadArray(std::istream& in, T* arr, int size);

void PrintMatrix(std::ostream& out, const CSRMatrix& mat);
4 changes: 2 additions & 2 deletions include/Vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Vector {
public:
Vector(int size, double defaultValue);

Vector(int size); //!!! Запретить неявные преобразования
explicit Vector(int size);

Vector();
Vector(const Vector& other);
Expand Down Expand Up @@ -37,7 +37,7 @@ class Vector {
friend std::ostream& operator<<(std::ostream& out, const Vector& vec);
friend std::istream& operator>>(std::istream& out, Vector& vec);

operator double*(); //!!! Запретить неявные преобразования
explicit operator double*();
private:
int size;
double* coordinates;
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <gtest/gtest.h>
#include "include/CSRMatrix.h"
#include "CSRMatrix.h"

int main() {
testing::InitGoogleTest();
Expand Down
49 changes: 33 additions & 16 deletions src/CSRMatrix.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
//
// Created by KotDath on 16.03.2021.
//

#include "../include/CSRMatrix.h"
#include "CSRMatrix.h"

CSRMatrix::CSRMatrix(double* values_, int valuesSize, int* indices_, int indicesSize, int* rowPointer_, int rowPointerSize) :
valuesSize(valuesSize), indicesSize(indicesSize), rowPointerSize(rowPointerSize) {
Expand Down Expand Up @@ -88,26 +84,26 @@ std::pair<int, int> CSRMatrix::GetShape() const {
std::ostream& operator<<(std::ostream& out, const CSRMatrix& mat) {
out << "Source arrays:" << std::endl;
int size = mat.GetShape().first;
PrintArray(out, mat.values, mat.valuesSize);
PrintArray(out, mat.indices, mat.indicesSize);
PrintArray(out, mat.rowPointer, mat.rowPointerSize);
CSRMatrix::PrintArray(out, mat.values, mat.valuesSize);
CSRMatrix::PrintArray(out, mat.indices, mat.indicesSize);
CSRMatrix::PrintArray(out, mat.rowPointer, mat.rowPointerSize);
out << "Matrix " << size << 'x' << size << ':' << std::endl;
PrintMatrix(out, mat);
CSRMatrix::PrintMatrix(out, mat);
return out;
}

std::istream& operator>>(std::istream& in, CSRMatrix& mat) {

in >> mat.valuesSize >> mat.indicesSize >> mat.rowPointerSize;
mat.values = ReadArray(in, mat.values, mat.valuesSize);
mat.indices = ReadArray(in, mat.indices, mat.indicesSize);
mat.rowPointer = ReadArray(in, mat.rowPointer, mat.rowPointerSize);
mat.values = CSRMatrix::ReadArray(in, mat.values, mat.valuesSize);
mat.indices = CSRMatrix::ReadArray(in, mat.indices, mat.indicesSize);
mat.rowPointer = CSRMatrix::ReadArray(in, mat.rowPointer, mat.rowPointerSize);

return in;
}

template<typename T>
void PrintArray(std::ostream& out, T* arr, int size) {
void CSRMatrix::PrintArray(std::ostream& out, T* arr, int size) {
out << '[';
for (int i = 0; i < size; ++i) {
out << arr[i];
Expand All @@ -119,7 +115,7 @@ void PrintArray(std::ostream& out, T* arr, int size) {
}

template<typename T>
T* ReadArray(std::istream& in, T* arr, int size) {
T* CSRMatrix::ReadArray(std::istream& in, T* arr, int size) {
delete[] arr;
arr = new T[size];
for (int i = 0; i < size; ++i) {
Expand All @@ -129,7 +125,7 @@ T* ReadArray(std::istream& in, T* arr, int size) {
return arr;
}

void PrintMatrix(std::ostream& out, const CSRMatrix& mat) {
void CSRMatrix::PrintMatrix(std::ostream& out, const CSRMatrix& mat) {
int size = mat.GetShape().first;
for (int row = 0; row < size; ++row) {
int start = mat.rowPointer[row];
Expand All @@ -142,7 +138,6 @@ void PrintMatrix(std::ostream& out, const CSRMatrix& mat) {
out << 0 << ' ';
}
out << mat.values[start] << ' ';
int column = 0;
for (int i = mat.rowPointer[row]; i < mat.rowPointer[row + 1] - 1; ++i) {
for (int j = mat.indices[i]; j < mat.indices[i + 1] - 1; ++j) {
out << 0 << ' ';
Expand All @@ -167,3 +162,25 @@ CSRMatrix::CSRMatrix(const CSRMatrix& mat) : valuesSize(mat.valuesSize), indices
memcpy(indices, mat.indices, sizeof(int) * indicesSize);
memcpy(rowPointer, mat.rowPointer, sizeof(int) * rowPointerSize);
}

CSRMatrix& CSRMatrix::operator=(const CSRMatrix& mat) {
if (this != &mat) {
ResizeIfDifferentSize(values, mat.values, valuesSize, mat.valuesSize);
ResizeIfDifferentSize(indices, mat.indices, indicesSize, mat.indicesSize);
ResizeIfDifferentSize(rowPointer, mat.rowPointer, rowPointerSize, mat.rowPointerSize);
memcpy(values, mat.values, sizeof(double) * valuesSize);
memcpy(indices, mat.indices, sizeof(int) * indicesSize);
memcpy(rowPointer, mat.rowPointer, sizeof(int) * rowPointerSize);
}

return *this;
}

template<typename T>
void CSRMatrix::ResizeIfDifferentSize(T*& arr, T* newArr, int& size, int newSize) {
if (size != newSize) {
delete[] arr;
arr = new T[newSize];
size = newSize;
}
}
2 changes: 1 addition & 1 deletion src/IncompatibleDimException.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../include/IncompatibleDimException.h"
#include "IncompatibleDimException.h"

const char* IncompatibleDimException::what() {
return "Incompatible size of Vectors or Matrix and Vectors";
Expand Down
2 changes: 1 addition & 1 deletion src/OutOfRangeException.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../include/OutOfRangeException.h"
#include "OutOfRangeException.h"


const char* OutOfRangeException::what() {
Expand Down
42 changes: 14 additions & 28 deletions src/Vector.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "../include/Vector.h"
#include "Vector.h"

Vector::Vector(int size, double defaultValue) : size(size) {
coordinates = new double[size];
Vector::Vector(int size, double defaultValue) : size(size), coordinates(new double[size]) {
for (int i = 0; i < size; ++i) {
coordinates[i] = defaultValue;
}
Expand All @@ -11,26 +10,20 @@ Vector::~Vector() {
delete[] coordinates;
}

Vector::Vector(const Vector& other) {
size = other.size;
coordinates = new double[size];
Vector::Vector(const Vector& other) : size(other.size), coordinates(new double[size]) {
for (int i = 0; i < size; ++i) {
coordinates[i] = other[i];
}

}

Vector::Vector(int size) : size(size) {
coordinates = new double[size];
Vector::Vector(int size) : size(size), coordinates(new double[size]) {
for (int i = 0; i < size; ++i) {
coordinates[i] = 0;
}
}

Vector::Vector() {
size = 0;
coordinates = new double[0]; //!!! По умолчанию должен быть nullptr. И лучше через список инициализации (:)
}
Vector::Vector() : size(0), coordinates(nullptr) {}

int Vector::GetDimentionCount() const {
return size;
Expand Down Expand Up @@ -105,23 +98,16 @@ double operator*(const Vector& first, const Vector& second) {
}

Vector& Vector::operator=(const Vector& other) {
if (this == &other) {
return *this;
} else {
size = other.size;

//!!! Выделять память стоит только, если размеры не совпадают

delete[] coordinates;
coordinates = new double[size];


for (int i = 0; i < size; ++i) {
coordinates[i] = other[i];
if (this != &other) {
if (size != other.size) {
size = other.size;
delete[] coordinates;
coordinates = new double[size];
}

return *this;
memcpy(coordinates, other.coordinates, sizeof(double) * size);
}

return *this;
}

std::ostream& operator<<(std::ostream& out, const Vector& vec) {
Expand Down Expand Up @@ -177,7 +163,7 @@ bool operator==(const Vector& first, const Vector& second) {
}

for (int i = 0; i < first.size; ++i) {
if (first[i] != second[i]) { //!!! double на != сравнивать сомнительно -> через std::fabs() < epsilon
if (std::fabs(first[i] - second[i]) > DBL_EPSILON) {
return false;
}
}
Expand Down

0 comments on commit e0b1c5b

Please sign in to comment.