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

Commit

Permalink
Allow disable bound-check for int indices; int indices for matrices; …
Browse files Browse the repository at this point in the history
…operator<< for std::vector
  • Loading branch information
tdegeus committed May 30, 2018
1 parent a030178 commit b23f448
Show file tree
Hide file tree
Showing 20 changed files with 1,121 additions and 373 deletions.
115 changes: 109 additions & 6 deletions src/cppmat/fix_diagonal_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ matrix<X,M,N> matrix<X,M,N>::CopyDense(Iterator first, Iterator last)

template<class X, size_t M, size_t N>
inline
std::vector<X> matrix<X,M,N>::asVector() const
matrix<X,M,N>::operator std::vector<X> () const
{
std::vector<X> out(mSize);

Expand All @@ -230,6 +230,17 @@ std::vector<X> matrix<X,M,N>::asVector() const
return out;
}

// =================================================================================================
// modify bounds check
// =================================================================================================

template<class X, size_t M, size_t N>
inline
void matrix<X,M,N>::setPeriodic(bool periodic)
{
mPeriodic = periodic;
}

// =================================================================================================
// get dimensions
// =================================================================================================
Expand Down Expand Up @@ -320,7 +331,44 @@ const X& matrix<X,M,N>::operator[](size_t i) const

template<class X, size_t M, size_t N>
inline
X& matrix<X,M,N>::operator()(size_t a, size_t b)
X& matrix<X,M,N>::operator()(int a, int b)
{
int n = static_cast<int>(N);

assert( ( a < n && a >= -n ) or mPeriodic );
assert( ( b < n && b >= -n ) or mPeriodic );

if ( a != b ) return mZero[0];

size_t A = static_cast<size_t>( (n+(a%n)) % n );

return mData[A];
}

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

template<class X, size_t M, size_t N>
inline
const X& matrix<X,M,N>::operator()(int a, int b) const
{
int n = static_cast<int>(N);

assert( ( a < n && a >= -n ) or mPeriodic );
assert( ( b < n && b >= -n ) or mPeriodic );

if ( a != b ) return mZero[0];

size_t A = static_cast<size_t>( (n+(a%n)) % n );

return mData[A];
}

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

template<class X, size_t M, size_t N>
template<typename T, typename S>
inline
X& matrix<X,M,N>::operator()(T a, T b)
{
assert( a < N );
assert( b < N );
Expand All @@ -332,8 +380,9 @@ X& matrix<X,M,N>::operator()(size_t a, size_t b)
// -------------------------------------------------------------------------------------------------

template<class X, size_t M, size_t N>
template<typename T, typename S>
inline
const X& matrix<X,M,N>::operator()(size_t a, size_t b) const
const X& matrix<X,M,N>::operator()(T a, T b) const
{
assert( a < N );
assert( b < N );
Expand All @@ -348,7 +397,25 @@ const X& matrix<X,M,N>::operator()(size_t a, size_t b) const

template<class X, size_t M, size_t N>
inline
size_t matrix<X,M,N>::compress(size_t a, size_t b) const
size_t matrix<X,M,N>::compress(int a, int b) const
{
assert( a == b );

int n = static_cast<int>(N);

assert( ( a < n && a >= -n ) or mPeriodic );

size_t A = static_cast<size_t>( (n+(a%n)) % n );

return A;
}

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

template<class X, size_t M, size_t N>
template<typename T, typename S>
inline
size_t matrix<X,M,N>::compress(T a, T b) const
{
assert( a < N );
assert( b < N );
Expand Down Expand Up @@ -465,7 +532,42 @@ auto matrix<X,M,N>::index(size_t i) const

template<class X, size_t M, size_t N>
inline
auto matrix<X,M,N>::item(size_t a, size_t b)
auto matrix<X,M,N>::item(int a, int b)
{
assert( a == b );

int n = static_cast<int>(N);

assert( ( a < n && a >= -n ) or mPeriodic );

size_t A = static_cast<size_t>( (n+(a%n)) % n );

return this->begin() + A;
}

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

template<class X, size_t M, size_t N>
inline
auto matrix<X,M,N>::item(int a, int b) const
{
assert( a == b );

int n = static_cast<int>(N);

assert( ( a < n && a >= -n ) or mPeriodic );

size_t A = static_cast<size_t>( (n+(a%n)) % n );

return this->begin() + A;
}

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

template<class X, size_t M, size_t N>
template<typename T, typename S>
inline
auto matrix<X,M,N>::item(T a, T b)
{
assert( a < N );
assert( b < N );
Expand All @@ -477,8 +579,9 @@ auto matrix<X,M,N>::item(size_t a, size_t b)
// -------------------------------------------------------------------------------------------------

template<class X, size_t M, size_t N>
template<typename T, typename S>
inline
auto matrix<X,M,N>::item(size_t a, size_t b) const
auto matrix<X,M,N>::item(T a, T b) const
{
assert( a < N );
assert( b < N );
Expand Down
42 changes: 32 additions & 10 deletions src/cppmat/fix_diagonal_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ class matrix

protected:

static const size_t mSize=N; // total size == data.size()
static const size_t mRank=2; // rank (number of axes)
X mData[N]; // data container
X mZero[1]; // pointer to a zero entry
static const size_t mSize=N; // total size == data.size()
static const size_t mRank=2; // rank (number of axes)
X mData[N]; // data container
X mZero[1]; // pointer to a zero entry
bool mPeriodic=false; // if true: disable bounds-check where possible

public:

Expand Down Expand Up @@ -65,7 +66,10 @@ class matrix
template<typename Itr> static matrix<X,M,N> CopyDense(Itr first, Itr last);

// return plain storage as vector
std::vector<X> asVector() const;
operator std::vector<X> () const;

// modify bounds-checks
void setPeriodic(bool periodic);

// get dimensions
size_t size() const;
Expand All @@ -79,11 +83,22 @@ class matrix
const X& operator[](size_t i) const;

// index operators: access using matrix-indices
X& operator()(size_t a, size_t b);
const X& operator()(size_t a, size_t b) const;
X& operator()(int a, int b);
const X& operator()(int a, int b) const;

// index operators: access using matrix-indices
template<typename T, typename=typename std::enable_if<std::is_unsigned<T>::value,void>::type>
X& operator()(T a, T b);

template<typename T, typename=typename std::enable_if<std::is_unsigned<T>::value,void>::type>
const X& operator()(T a, T b) const;

// index operators: matrix-indices -> plain storage (a,b -> i)
size_t compress(size_t a, size_t b) const;
size_t compress(int a, int b) const;

// index operators: matrix-indices -> plain storage (a,b -> i)
template<typename T, typename=typename std::enable_if<std::is_unsigned<T>::value,void>::type>
size_t compress(T a, T b) const;

// index operators: plain storage -> matrix-indices (i -> a,b)
std::vector<size_t> decompress(size_t i) const;
Expand All @@ -103,8 +118,15 @@ class matrix
auto index(size_t i) const;

// iterator to specific entry: access using matrix-indices
auto item(size_t a, size_t b);
auto item(size_t a, size_t b) const;
auto item(int a, int b);
auto item(int a, int b) const;

// iterator to specific entry: access using matrix-indices
template<typename T, typename=typename std::enable_if<std::is_unsigned<T>::value,void>::type>
auto item(T a, T b);

template<typename T, typename=typename std::enable_if<std::is_unsigned<T>::value,void>::type>
auto item(T a, T b) const;

// initialization
void setRandom(X lower=(X)0, X upper=(X)1);
Expand Down

0 comments on commit b23f448

Please sign in to comment.