forked from Expander/FlexibleSUSY
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding C++ wrapper around raw GSL vector
- Loading branch information
Alexander Voigt
authored and
Alexander Voigt
committed
Aug 21, 2016
1 parent
35033f6
commit 605606e
Showing
5 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// ==================================================================== | ||
// This file is part of FlexibleSUSY. | ||
// | ||
// FlexibleSUSY is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, | ||
// or (at your option) any later version. | ||
// | ||
// FlexibleSUSY is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with FlexibleSUSY. If not, see | ||
// <http://www.gnu.org/licenses/>. | ||
// ==================================================================== | ||
|
||
#include "gsl_vector.hpp" | ||
#include <cassert> | ||
|
||
namespace flexiblesusy { | ||
|
||
GSL_vector::GSL_vector() | ||
: vec(NULL) | ||
{ | ||
} | ||
|
||
GSL_vector::GSL_vector(std::size_t size) | ||
{ | ||
if (size) | ||
vec = gsl_vector_calloc(size); | ||
else | ||
vec = NULL; | ||
} | ||
|
||
GSL_vector::GSL_vector(const GSL_vector& other) | ||
{ | ||
vec = gsl_vector_alloc(other.size()); | ||
gsl_vector_memcpy(vec, other.vec); | ||
} | ||
|
||
GSL_vector::GSL_vector(GSL_vector&& other) | ||
{ | ||
vec = other.vec; | ||
other.vec = NULL; | ||
} | ||
|
||
GSL_vector::~GSL_vector() | ||
{ | ||
gsl_vector_free(vec); | ||
} | ||
|
||
const GSL_vector& GSL_vector::operator=(const GSL_vector& other) | ||
{ | ||
if (this != &other) { | ||
gsl_vector_free(vec); | ||
vec = gsl_vector_alloc(other.size()); | ||
gsl_vector_memcpy(vec, other.vec); | ||
} | ||
|
||
return *this; | ||
} | ||
|
||
double& GSL_vector::operator[](std::size_t n) | ||
{ | ||
assert(vec); | ||
assert(n < vec->size && "GSL_vector::operator[]: index out of range"); | ||
return *gsl_vector_ptr(vec, n); | ||
} | ||
|
||
double GSL_vector::operator[](std::size_t n) const | ||
{ | ||
assert(vec); | ||
assert(n < vec->size && "GSL_vector::operator[]: index out of range"); | ||
return gsl_vector_get(vec, n); | ||
} | ||
|
||
std::size_t GSL_vector::size() const | ||
{ | ||
if (!vec) return 0; | ||
return vec->size; | ||
} | ||
|
||
const gsl_vector* GSL_vector::raw() const | ||
{ | ||
return vec; | ||
} | ||
|
||
gsl_vector* GSL_vector::raw() | ||
{ | ||
return vec; | ||
} | ||
|
||
std::ostream& operator<<(std::ostream& ostr, const GSL_vector& vec) | ||
{ | ||
std::cout << "("; | ||
|
||
for (std::size_t i = 0; i < vec.size(); i++) { | ||
std::cout << vec[i]; | ||
if (i < vec.size() - 1) | ||
std::cout << ", "; | ||
} | ||
|
||
std::cout << ")"; | ||
} | ||
|
||
} // namespace flexiblesusy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// ==================================================================== | ||
// This file is part of FlexibleSUSY. | ||
// | ||
// FlexibleSUSY is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, | ||
// or (at your option) any later version. | ||
// | ||
// FlexibleSUSY is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with FlexibleSUSY. If not, see | ||
// <http://www.gnu.org/licenses/>. | ||
// ==================================================================== | ||
|
||
#ifndef GSL_VECTOR_H | ||
#define GSL_VECTOR_H | ||
|
||
#include <gsl/gsl_vector.h> | ||
#include <iostream> | ||
|
||
namespace flexiblesusy { | ||
|
||
class GSL_vector { | ||
public: | ||
GSL_vector(); | ||
explicit GSL_vector(std::size_t); | ||
GSL_vector(const GSL_vector&); | ||
GSL_vector(GSL_vector&&); | ||
~GSL_vector(); | ||
|
||
const GSL_vector& operator=(const GSL_vector&); | ||
double& operator[](std::size_t); | ||
double operator[](std::size_t) const; | ||
|
||
const gsl_vector* raw() const; | ||
gsl_vector* raw(); | ||
std::size_t size() const; | ||
|
||
private: | ||
gsl_vector* vec; | ||
}; | ||
|
||
std::ostream& operator<<(std::ostream&, const GSL_vector&); | ||
|
||
} // namespace flexiblesusy | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#define BOOST_TEST_DYN_LINK | ||
#define BOOST_TEST_MODULE test_gsl_vector | ||
|
||
#include <boost/test/unit_test.hpp> | ||
|
||
#include "gsl_vector.hpp" | ||
|
||
using namespace flexiblesusy; | ||
|
||
BOOST_AUTO_TEST_CASE( test_init_default ) | ||
{ | ||
GSL_vector v; | ||
BOOST_CHECK_EQUAL(v.size(), 0); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE( test_init_0 ) | ||
{ | ||
GSL_vector v(0); | ||
BOOST_CHECK_EQUAL(v.size(), 0); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE( test_init ) | ||
{ | ||
GSL_vector v(3); | ||
BOOST_CHECK_EQUAL(v[0], 0.); | ||
BOOST_CHECK_EQUAL(v[1], 0.); | ||
BOOST_CHECK_EQUAL(v[2], 0.); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE( test_operator ) | ||
{ | ||
GSL_vector v(3); | ||
v[0] = 1.; | ||
v[1] = 2.; | ||
v[2] = 3.; | ||
|
||
BOOST_CHECK_EQUAL(v[0], 1.); | ||
BOOST_CHECK_EQUAL(v[1], 2.); | ||
BOOST_CHECK_EQUAL(v[2], 3.); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE( test_copy ) | ||
{ | ||
GSL_vector v(3); | ||
v[0] = 1.; | ||
v[1] = 2.; | ||
v[2] = 3.; | ||
|
||
GSL_vector v2(v); | ||
|
||
BOOST_CHECK_EQUAL(v2[0], 1.); | ||
BOOST_CHECK_EQUAL(v2[1], 2.); | ||
BOOST_CHECK_EQUAL(v2[2], 3.); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE( test_assign ) | ||
{ | ||
GSL_vector v(3); | ||
v[0] = 1.; | ||
v[1] = 2.; | ||
v[2] = 3.; | ||
|
||
GSL_vector v2; | ||
v2 = v; | ||
|
||
BOOST_CHECK_EQUAL(v2[0], 1.); | ||
BOOST_CHECK_EQUAL(v2[1], 2.); | ||
BOOST_CHECK_EQUAL(v2[2], 3.); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE( test_move ) | ||
{ | ||
GSL_vector v(3); | ||
v[0] = 1.; | ||
v[1] = 2.; | ||
v[2] = 3.; | ||
|
||
GSL_vector v2(std::move(v)); | ||
|
||
BOOST_CHECK_EQUAL(v2[0], 1.); | ||
BOOST_CHECK_EQUAL(v2[1], 2.); | ||
BOOST_CHECK_EQUAL(v2[2], 3.); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE( test_output ) | ||
{ | ||
GSL_vector v(3); | ||
v[0] = 1.; | ||
v[1] = 2.; | ||
v[2] = 3.; | ||
|
||
BOOST_MESSAGE(v); | ||
} |