Skip to content

Commit

Permalink
Fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
david-bermejo committed Mar 7, 2021
1 parent 41dcbaa commit 6fce44e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
10 changes: 5 additions & 5 deletions example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ using namespace gopt;

int main()
{
Matrix<double> m(3, 4);
Vector<double> v(5);
v.fill([]() { return 122; });
Vector<double> u(v);

std::default_random_engine gen;
std::normal_distribution<double> dist(0.0, 1.0);
m.fill([&]() { return dist(gen); });
u = v;

std::cout << m << std::endl;
std::cout << v << ", " << u << std::endl;

return 0;
}
4 changes: 1 addition & 3 deletions math/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,6 @@ namespace gopt
m._rows = 0;
m._cols = 0;
m.data = nullptr;

std::cout << "Called" << std::endl;
}

~Matrix()
Expand Down Expand Up @@ -417,7 +415,7 @@ namespace gopt
return *this;
}

template <typename F, typename... Args>
template <typename F, typename... Args, typename = std::enable_if_t<std::is_invocable_v<F, Args...>>>
Matrix& fill(F&& f, Args... args)
{
for (int i = 0; i < _rows; i++)
Expand Down
21 changes: 18 additions & 3 deletions math/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,14 @@ namespace gopt
Vector(const Vector& v)
: len(v.len), data(new T[v.len])
{
for (unsigned int i = 0; i < len; i++)
data[i] = v.data[i];
std::copy(v.data, v.data + len, data);
}

Vector(Vector&& v)
: len(v.len), data(v.data)
{
v.len = 0;
v.data = nullptr;
}

~Vector()
Expand All @@ -320,6 +326,15 @@ namespace gopt
delete[] data;
}

Vector& operator=(const Vector& v)
{
len = v.len;
data = new T[len];
std::copy(v.data, v.data + len, data);

return *this;
}

Vector& fill(const T& s)
{
std::fill_n(data, len, s);
Expand All @@ -343,7 +358,7 @@ namespace gopt
return *this;
}

template <typename F, typename... Args>
template <typename F, typename... Args, typename = std::enable_if_t<std::is_invocable_v<F, Args...>>>
Vector& fill(F&& f, Args... args)
{
for (int i = 0; i < len; i++)
Expand Down

0 comments on commit 6fce44e

Please sign in to comment.