Skip to content
This repository has been archived by the owner on Oct 25, 2019. It is now read-only.

Commit

Permalink
Used default pybind11 interface for vectors. WARNING: no bounds checks
Browse files Browse the repository at this point in the history
  • Loading branch information
tdegeus committed Jul 17, 2018
1 parent f0c69c3 commit 11a31a9
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 208 deletions.
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ cppmat

.. tip::

This document should be considered as a quick-start guide. A lot effort has been spent on the readability of the code itself. One is highly encouraged to answer more advanced questions that arise from this guide directly using the code.
This document should be considered as a quick-start guide. A lot effort has been spent on the readability of the code itself (in particular the ``*.h`` files should be instructive). One is highly encouraged to answer more advanced questions that arise from this guide directly using the code. Download buttons to the relevant files are included throughout this reader.

This header-only module provides C++ classes and several accompanying methods to work with n-d arrays and/or tensors. It's usage, programmatically and from a compilation perspective, is really simple. One just has to ``#include <cppmat/cppmat.h>`` and tell your compiler where cppmat is located (and to use the C++14 or younger standard). Really, that's it!

Expand Down
8 changes: 8 additions & 0 deletions src/cppmat/fix_regular_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ namespace tiny {
template<typename X, size_t N>
class vector : public cppmat::tiny::array<X,1,N>
{
private:
size_t mIstore=0;

public:

// constructor: allocate, don't initialize
Expand All @@ -48,6 +51,11 @@ class vector : public cppmat::tiny::array<X,1,N>
// forward difference (x0, x1-x0, x2-x1, ...)
vector<X,N> diff() const;

// STL-like access
void push_back(const X &value);
void clear();


};

// =================================================================================================
Expand Down
26 changes: 26 additions & 0 deletions src/cppmat/fix_regular_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,32 @@ vector<X,N> vector<X,N>::diff() const
return out;
}

// =================================================================================================
// STL-like behaviour
// =================================================================================================

template<typename X, size_t N>
inline
void vector<X,N>::push_back(const X &value)
{
assert( mIstore < N );

this->mData[mIstore] = value;

mIstore++;
}

// -------------------------------------------------------------------------------------------------

template<typename X, size_t N>
inline
void vector<X,N>::clear()
{
mIstore = 0;

for ( size_t i = 0 ; i < N ; ++i ) this->mData[i] = static_cast<X>(0);
}

// =================================================================================================

}} // namespace ...
Expand Down
58 changes: 4 additions & 54 deletions src/cppmat/pybind11_fix_cartesian_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
================================================================================================= */

#ifndef CPPMAT_TENSOR2_PYBIND11_HPP
#define CPPMAT_TENSOR2_PYBIND11_HPP
#ifndef CPPMAT_FIX_CARTESIAN_VECTOR_PYBIND11_HPP
#define CPPMAT_FIX_CARTESIAN_VECTOR_PYBIND11_HPP

#include "pybind11.h"

Expand All @@ -18,58 +18,8 @@ namespace detail {
// type caster: cppmat::tiny::cartesian::vector <-> NumPy-array
// =================================================================================================

template<typename X, size_t ND> struct type_caster<cppmat::tiny::cartesian::vector<X,ND>>
{
public:

using Arr = cppmat::tiny::cartesian::vector<X,ND>;

PYBIND11_TYPE_CASTER(Arr, _("cppmat::tiny::cartesian::vector<X,ND>"));

// Python -> C++
// -------------

bool load(py::handle src, bool convert)
{
// - basic pybind11 check
if ( !convert && !py::array_t<X>::check_(src) ) return false;

// - storage requirements : contiguous and row-major storage from NumPy
auto buf = py::array_t<X, py::array::c_style | py::array::forcecast>::ensure(src);
// - check
if ( !buf ) return false;

// - rank of the input array (number of indices)
auto rank = buf.ndim();
// - check
if ( rank != 1 ) return false;

// - check shape in each direction
for ( ssize_t i = 0 ; i < rank ; ++i )
if ( static_cast<size_t>(buf.shape()[i]) != ND )
return false;

// - all checks passed : create the proper C++ variable
value = cppmat::tiny::cartesian::vector<X,ND>::Copy(buf.data(), buf.data()+buf.size());

// - signal successful variable creation
return true;
}

// C++ -> Python
// -------------

static py::handle cast(
const cppmat::tiny::cartesian::vector<X,ND>& src, py::return_value_policy policy, py::handle parent
)
{
// - create Python variable (all variables are copied)
py::array a(std::move(src.shape()), std::move(src.strides(true)), src.data());

// - release variable to Python
return a.release();
}
};
template <typename X, size_t ND> struct type_caster<cppmat::tiny::cartesian::vector<X,ND>> :
list_caster<cppmat::tiny::cartesian::vector<X,ND>, X> { };

// =================================================================================================

Expand Down
52 changes: 2 additions & 50 deletions src/cppmat/pybind11_fix_regular_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,8 @@ namespace detail {
// type caster: cppmat::tiny::vector <-> NumPy-array
// =================================================================================================

template<typename X, size_t N> struct type_caster<cppmat::tiny::vector<X,N>>
{
public:

using Arr = cppmat::tiny::vector<X,N>;

PYBIND11_TYPE_CASTER(Arr, _("cppmat::tiny::vector<X,N>"));

// Python -> C++
// -------------

bool load(py::handle src, bool convert)
{
// - basic pybind11 check
if ( !convert && !py::array_t<X>::check_(src) ) return false;

// - storage requirements : contiguous and row-major storage from NumPy
auto buf = py::array_t<X, py::array::c_style | py::array::forcecast>::ensure(src);
// - check
if ( !buf ) return false;

// - rank of the input array (number of indices)
auto rank = buf.ndim();
// - check
if ( rank != 1 ) return false;

// - check shape in each direction
if ( static_cast<size_t>(buf.shape()[0]) != N ) return false;

// - all checks passed : create the proper C++ variable
value = cppmat::tiny::vector<X,N>::Copy(buf.data(), buf.data()+buf.size());

// - signal successful variable creation
return true;
}

// C++ -> Python
// -------------

static py::handle cast(
const cppmat::tiny::vector<X,N>& src, py::return_value_policy policy, py::handle parent
)
{
// - create Python variable (all variables are copied)
py::array a(std::move(src.shape()), std::move(src.strides(true)), src.data());

// - release variable to Python
return a.release();
}
};
template <typename X, size_t N> struct type_caster<cppmat::tiny::vector<X,N>> :
list_caster<cppmat::tiny::vector<X,N>, X> { };

// =================================================================================================

Expand Down
54 changes: 2 additions & 52 deletions src/cppmat/pybind11_var_cartesian_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,58 +18,8 @@ namespace detail {
// type caster: cppmat::cartesian::vector <-> NumPy-array
// =================================================================================================

template<typename X> struct type_caster<cppmat::cartesian::vector<X>>
{
public:

PYBIND11_TYPE_CASTER(cppmat::cartesian::vector<X>, _("cppmat::cartesian::vector<X>"));

// Python -> C++
// -------------

bool load(py::handle src, bool convert)
{
// - basic pybind11 check
if ( !convert && !py::array_t<X>::check_(src) ) return false;

// - storage requirements : contiguous and row-major storage from NumPy
auto buf = py::array_t<X, py::array::c_style | py::array::forcecast>::ensure(src);
// - check
if ( !buf ) return false;

// - rank of the input array (number of indices)
auto rank = buf.ndim();
// - check
if ( rank != 1 ) return false;

// - read number of dimensions (shape in each direction)
size_t nd = static_cast<size_t>(buf.shape()[0]);
// - check
for ( ssize_t i = 0 ; i < rank ; ++i )
if ( static_cast<size_t>(buf.shape()[i]) != nd )
return false;

// - all checks passed : create the proper C++ variable
value = cppmat::cartesian::vector<X>::Copy(nd, buf.data(), buf.data()+buf.size());

// - signal successful variable creation
return true;
}

// C++ -> Python
// -------------

static py::handle cast(
const cppmat::cartesian::vector<X>& src, py::return_value_policy policy, py::handle parent
)
{
// - create Python variable (all variables are copied)
py::array a(std::move(src.shape()), std::move(src.strides(true)), src.data());

// - release variable to Python
return a.release();
}
};
template <typename X> struct type_caster<cppmat::cartesian::vector<X>> :
list_caster<cppmat::cartesian::vector<X>, X> { };

// =================================================================================================

Expand Down
4 changes: 2 additions & 2 deletions src/cppmat/pybind11_var_regular_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
================================================================================================= */

#ifndef CPPMAT_VAR_ARRAY_PYBIND11_HPP
#define CPPMAT_VAR_ARRAY_PYBIND11_HPP
#ifndef CPPMAT_VAR_REGULAR_ARRAY_PYBIND11_HPP
#define CPPMAT_VAR_REGULAR_ARRAY_PYBIND11_HPP

#include "pybind11.h"

Expand Down
47 changes: 2 additions & 45 deletions src/cppmat/pybind11_var_regular_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,8 @@ namespace detail {
// type caster: cppmat::vector <-> NumPy-array
// =================================================================================================

template<typename X> struct type_caster<cppmat::vector<X>>
{
public:

PYBIND11_TYPE_CASTER(cppmat::vector<X>, _("cppmat::vector<X>"));

// Python -> C++
// -------------

bool load(py::handle src, bool convert)
{
// - basic pybind11 check
if ( !convert && !py::array_t<X>::check_(src) ) return false;

// - storage requirements : contiguous and row-major storage from NumPy
auto buf = py::array_t<X, py::array::c_style | py::array::forcecast>::ensure(src);
// - check
if ( !buf ) return false;

// - rank of the input array (number of indices)
auto rank = buf.ndim();
// - check
if ( rank != 1 ) return false;

// - all checks passed : create the proper C++ variable
value = cppmat::vector<X>::Copy(buf.data(), buf.data()+buf.size());

// - signal successful variable creation
return true;
}

// C++ -> Python
// -------------

static py::handle cast(
const cppmat::vector<X>& src, py::return_value_policy policy, py::handle parent
)
{
// - create Python variable (all variables are copied)
py::array a(std::move(src.shape()), std::move(src.strides(true)), src.data());

// - release variable to Python
return a.release();
}
};
template <typename X> struct type_caster<cppmat::vector<X>> :
list_caster<cppmat::vector<X>, X> { };

// =================================================================================================

Expand Down
5 changes: 3 additions & 2 deletions src/cppmat/var_regular_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ class vector : public cppmat::array<X>
void resize(size_t n);
void resize(size_t n, const X &D);

// inset item
void push_back(const X &D);
// STL-like access
void push_back(const X &value);
void append(const cppmat::array<X> &A);
void clear();

// discrete difference (x1-x0, x2-x1, ...)
vector<X> diff() const;
Expand Down
13 changes: 11 additions & 2 deletions src/cppmat/var_regular_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ void vector<X>::resize(size_t n, const X &D)

template<typename X>
inline
void vector<X>::push_back(const X &D)
void vector<X>::push_back(const X &value)
{
this->mData.push_back(D);
this->mData.push_back(value);
this->mShape[0]++;
this->mSize++;
}
Expand All @@ -265,6 +265,15 @@ void vector<X>::append(const cppmat::array<X> &A)
this->mSize += A.size();
}

// -------------------------------------------------------------------------------------------------

template<typename X>
inline
void vector<X>::clear()
{
this->mData.clear();
}

// =================================================================================================
// discrete difference (x1-x0, x2-x1, ...)
// =================================================================================================
Expand Down

0 comments on commit 11a31a9

Please sign in to comment.