Program Listing for File vector.h¶
↰ Return to documentation for file (include/amici/vector.h
)
#ifndef AMICI_VECTOR_H
#define AMICI_VECTOR_H
#include <vector>
#include <type_traits>
#include <amici/exception.h>
#include <nvector/nvector_serial.h>
#include <gsl/gsl-lite.hpp>
namespace amici {
using const_N_Vector =
std::add_const<typename std::remove_pointer<N_Vector>::type>::type *;
inline const realtype* N_VGetArrayPointerConst(const_N_Vector x) {
return N_VGetArrayPointer(const_cast<N_Vector>(x));
}
class AmiVector {
public:
AmiVector() = default;
explicit AmiVector(const long int length)
: vec_(static_cast<decltype(vec_)::size_type>(length), 0.0),
nvec_(N_VMake_Serial(length, vec_.data())) {}
explicit AmiVector(std::vector<realtype> rvec)
: vec_(std::move(rvec)),
nvec_(N_VMake_Serial(static_cast<long int>(vec_.size()), vec_.data())) {}
explicit AmiVector(gsl::span<realtype> rvec)
: AmiVector(std::vector<realtype>(rvec.begin(), rvec.end())) {}
AmiVector(const AmiVector &vold) : vec_(vold.vec_) {
nvec_ =
N_VMake_Serial(static_cast<long int>(vold.vec_.size()), vec_.data());
}
AmiVector(AmiVector&& other) noexcept : nvec_(nullptr) {
vec_ = std::move(other.vec_);
synchroniseNVector();
}
~AmiVector();
AmiVector &operator=(AmiVector const &other);
realtype *data();
const realtype *data() const;
N_Vector getNVector();
const_N_Vector getNVector() const;
std::vector<realtype> const &getVector() const;
int getLength() const;
void zero();
void minus();
void set(realtype val);
realtype &operator[](int pos);
realtype &at(int pos);
const realtype &at(int pos) const;
void copy(const AmiVector &other);
private:
std::vector<realtype> vec_;
N_Vector nvec_ {nullptr};
void synchroniseNVector();
};
class AmiVectorArray {
public:
AmiVectorArray() = default;
AmiVectorArray(long int length_inner, long int length_outer);
AmiVectorArray(const AmiVectorArray &vaold);
~AmiVectorArray() = default;
AmiVectorArray &operator=(AmiVectorArray const &other);
realtype *data(int pos);
const realtype *data(int pos) const;
realtype &at(int ipos, int jpos);
const realtype &at(int ipos, int jpos) const;
N_Vector *getNVectorArray();
N_Vector getNVector(int pos);
const_N_Vector getNVector(int pos) const;
AmiVector &operator[](int pos);
const AmiVector &operator[](int pos) const;
int getLength() const;
void zero();
void flatten_to_vector(std::vector<realtype> &vec) const;
void copy(const AmiVectorArray &other);
private:
std::vector<AmiVector> vec_array_;
std::vector<N_Vector> nvec_array_;
};
} // namespace amici
namespace gsl {
inline span<realtype> make_span(N_Vector nv)
{
return span<realtype>(N_VGetArrayPointer(nv), N_VGetLength_Serial(nv));
}
} // namespace gsl
#endif /* AMICI_VECTOR_H */