-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c8c56ab
Showing
4 changed files
with
388 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,97 @@ | ||
/* | ||
* Array.h | ||
* | ||
* Created on: Jul 13, 2012 | ||
* Author: Chris | ||
*/ | ||
|
||
#ifndef ARRAY_H_ | ||
#define ARRAY_H_ | ||
|
||
#include "ArrayBase.h" | ||
#include <assert.h> | ||
#include <memory> | ||
|
||
namespace linear_algebra{ | ||
namespace array{ | ||
|
||
/** | ||
* N-Dimensional array class. | ||
* The array object owns pointers to its data. (create and delete) | ||
* Array proxies returned by operator[] do not own pointers to their data | ||
* but point to the same data as the full Array object. Any changes performed | ||
* on data in the proxy is reflected in the Array. | ||
*/ | ||
template<typename T, std::size_t N> | ||
class Array | ||
: public base::ArrayBase<T,N> | ||
{ | ||
typedef base::ArrayBase<T,N> Super; | ||
public: | ||
typedef typename Super::Size Size; | ||
typedef typename Super::Index Index; | ||
typedef typename Super::Reference Reference; | ||
typedef typename Super::ConstReference ConstReference; | ||
|
||
typedef T* TPtr; | ||
typedef Size* SizePtr; | ||
|
||
static const Size DIMENSIONALITY = N; | ||
|
||
|
||
/** | ||
* Template constructor. Takes an object that describes the size of each dimension. | ||
* Dimensions object must implement size() and operator[] element access. | ||
*/ | ||
template<typename Dimensions> | ||
Array(Dimensions dimensions){ | ||
// assert((dimensions.size()==DIMENSIONALITY) && (DIMENSIONALITY > 0)); | ||
Size size = 1; | ||
// strides will be an N-1 dimensional array holding the distance between the heads of each | ||
// subarray. For example, strides[0] is the number of elements in each of the | ||
// dimensions[0] subarrays of dimensionality N-1. | ||
SizePtr strides = new Size[DIMENSIONALITY-1]; | ||
SizePtr dim = new Size[DIMENSIONALITY]; | ||
// Build up the array of strides from the bottom. | ||
for(unsigned int i=DIMENSIONALITY-1;i>0;--i){ | ||
dim[i]=dimensions[i]; | ||
size = size*dimensions[i]; | ||
strides[i-1]=size; | ||
} | ||
dim[0]=dimensions[0]; | ||
size = size*dimensions[0]; | ||
TPtr head = new T[size]; | ||
this->_head = head; | ||
this->_dimensions = dim; | ||
this->_size = size; | ||
this->_strides = strides; | ||
} | ||
|
||
~Array(){ | ||
delete[] _strides; | ||
delete[] _head; | ||
} | ||
|
||
/* Returns a pointer to the head of the contiguous block of data stored by the array. */ | ||
TPtr data(void){return this->_head;} | ||
/* Returns the total number of elements in the array. */ | ||
Size& size(void){return this->_size;} | ||
/* Returns the size of a particular dimension of the array */ | ||
Size& size(Index dimension){return this->_dimensions[dimension];} | ||
|
||
Reference operator[](Index index){return Super::access(types::TypeParameter<Reference>(), this->_head,this->_strides,index);} | ||
ConstReference operator[](Index index)const{return Super::access(types::TypeParameter<Reference>(), this->_head,this->_strides,index);} | ||
protected: | ||
TPtr _head; | ||
SizePtr _dimensions; | ||
Size _size; | ||
SizePtr _strides; | ||
}; | ||
|
||
|
||
} // namespace array | ||
} // namespace linear_algebra | ||
|
||
|
||
|
||
#endif /* ARRAY_H_ */ |
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,212 @@ | ||
/* | ||
* ArrayNBase.h | ||
* | ||
* Created on: Jul 10, 2012 | ||
* Author: Chris | ||
*/ | ||
|
||
#ifndef ARRAYBASE_H_ | ||
#define ARRAYBASE_H_ | ||
|
||
#include "Types.h" | ||
#include <memory> | ||
#include <assert.h> | ||
#include <typeinfo> | ||
|
||
namespace linear_algebra { | ||
namespace array { | ||
namespace base { | ||
|
||
template<typename T, std::size_t N> | ||
class Array; | ||
template<typename T, std::size_t N> | ||
class ConstArrayProxy; | ||
template<typename T, std::size_t N> | ||
class ArrayProxy; | ||
|
||
|
||
|
||
|
||
/** Data typedef for use with Arrays | ||
* | ||
*/ | ||
template<typename T> | ||
struct ArrayTypes{ | ||
typedef std::size_t Size; | ||
typedef Size Index; | ||
typedef T* TPtr; | ||
typedef const T* ConstTPtr; | ||
typedef Size* SizePtr; | ||
typedef const Size* ConstSizePtr; | ||
}; | ||
|
||
/** N dimensional array accessor | ||
* Acts as a base class for N dimensional arrays allowing nested operator[] overloading | ||
* to access data. Cannot be instantiated as constructors are protected. | ||
*/ | ||
template<typename T, std::size_t N> | ||
class Accessor | ||
: public ArrayTypes<T> | ||
{ | ||
typedef ArrayTypes<T> Super; | ||
public: | ||
// Inherited types | ||
typedef typename Super::Size Size; | ||
typedef typename Super::Index Index; | ||
typedef typename Super::TPtr TPtr; | ||
typedef typename Super::ConstTPtr ConstTPtr; | ||
typedef typename Super::SizePtr SizePtr; | ||
typedef typename Super::ConstSizePtr ConstSizePtr; | ||
|
||
|
||
typedef ArrayProxy<T,N-1> Reference; | ||
typedef ConstArrayProxy<T,N-1> ConstReference; | ||
|
||
protected: | ||
/** Template array access function, takes a reference type as a parameter for return. | ||
* ReferenceType can be SubArray<T,N-1> or ConstSubArray<T,N-1> | ||
*/ | ||
template<typename ReferenceType> | ||
ReferenceType access( types::TypeParameter<ReferenceType>, TPtr head, SizePtr strides, Index index){ | ||
assert( (typeid(ReferenceType)==typeid(Reference)) || (typeid(ReferenceType)==typeid(ConstReference)) ); | ||
TPtr newhead = head + strides[0]*index; | ||
return ReferenceType(newhead, strides+1); | ||
} | ||
Accessor(){} | ||
~Accessor(){} | ||
}; | ||
|
||
|
||
/** Template specialization for 1 dimensional array Accessor | ||
* | ||
*/ | ||
template<typename T> | ||
class Accessor<T,1> | ||
: public ArrayTypes<T> | ||
{ | ||
typedef ArrayTypes<T> Super; | ||
public: | ||
// Inherited types | ||
typedef typename Super::Size Size; | ||
typedef typename Super::Index Index; | ||
typedef typename Super::TPtr TPtr; | ||
typedef typename Super::ConstTPtr ConstTPtr; | ||
typedef typename Super::SizePtr SizePtr; | ||
typedef typename Super::ConstSizePtr ConstSizePtr; | ||
|
||
typedef T& Reference; | ||
typedef const T& ConstReference; | ||
|
||
public: | ||
/** Template array access function, takes a reference type as a parameter for return. | ||
* ReferenceType can be T& or const T& | ||
*/ | ||
template<typename ReferenceType> | ||
ReferenceType access(types::TypeParameter<ReferenceType>, TPtr head, SizePtr strides, Index index){ | ||
assert( (typeid(ReferenceType)==typeid(Reference)) || (typeid(ReferenceType)==typeid(ConstReference)) ); | ||
return head[index]; | ||
} | ||
Accessor(){} | ||
~Accessor(){} | ||
}; | ||
|
||
|
||
// Intermediate Accessor class template to avoid template compilation size errors due to nesting std::size_t | ||
template<typename T, class N> | ||
struct AccessorType | ||
{ | ||
static const std::size_t DIMENSION = N::Value; | ||
typedef Accessor<T,DIMENSION> Type; | ||
}; | ||
|
||
/** | ||
* Base class for N dimensional array | ||
* Inherits from N dimensional Accessor chosen by AccessorType<T,N>::Type | ||
* Implements nested operator[] element access | ||
*/ | ||
template<typename T, std::size_t N> | ||
class ArrayBase | ||
: public AccessorType<T,types::SizeType<N>>::Type | ||
{ | ||
public: | ||
typedef typename AccessorType<T,types::SizeType<N> >::Type Super; | ||
public: | ||
// Inherited types | ||
typedef typename Super::Size Size; | ||
typedef typename Super::Index Index; | ||
typedef typename Super::TPtr TPtr; | ||
typedef typename Super::ConstTPtr ConstTPtr; | ||
typedef typename Super::SizePtr SizePtr; | ||
typedef typename Super::ConstSizePtr ConstSizePtr; | ||
typedef typename Super::Reference Reference; | ||
typedef typename Super::ConstReference ConstReference; | ||
|
||
}; | ||
|
||
|
||
template<typename T, std::size_t N> | ||
class ConstArrayProxy | ||
: public ArrayBase<T,N> | ||
{ | ||
typedef ArrayBase<T,N> Super; | ||
public: | ||
// Inherited types | ||
typedef typename Super::Size Size; | ||
typedef typename Super::Index Index; | ||
typedef typename Super::TPtr TPtr; | ||
typedef typename Super::ConstTPtr ConstTPtr; | ||
typedef typename Super::SizePtr SizePtr; | ||
typedef typename Super::ConstSizePtr ConstSizePtr; | ||
typedef typename Super::Reference Reference; | ||
typedef typename Super::ConstReference ConstReference; | ||
|
||
ConstArrayProxy(TPtr head, SizePtr strides) | ||
: _head(head), | ||
_strides(strides) | ||
{ | ||
|
||
} | ||
~ConstArrayProxy(){} | ||
|
||
Reference operator[](Index index){return Super::access(types::TypeParameter<Reference>(), this->_head,this->_strides,index);} | ||
ConstReference operator[](Index index)const{return Super::access(types::TypeParameter<Reference>(), this->_head,this->_strides,index);} | ||
protected: | ||
ConstTPtr _head; | ||
SizePtr _strides; | ||
}; | ||
|
||
template<typename T, std::size_t N> | ||
class ArrayProxy | ||
: public ArrayBase<T,N> | ||
{ | ||
typedef ArrayBase<T,N> Super; | ||
public: | ||
// Inherited types | ||
typedef typename Super::Size Size; | ||
typedef typename Super::Index Index; | ||
typedef typename Super::TPtr TPtr; | ||
typedef typename Super::ConstTPtr ConstTPtr; | ||
typedef typename Super::SizePtr SizePtr; | ||
typedef typename Super::ConstSizePtr ConstSizePtr; | ||
typedef typename Super::Reference Reference; | ||
typedef typename Super::ConstReference ConstReference; | ||
|
||
ArrayProxy(TPtr head, SizePtr strides) | ||
: _head(head), | ||
_strides(strides) | ||
{ | ||
|
||
} | ||
~ArrayProxy(){} | ||
|
||
Reference operator[](Index index){return Super::access(types::TypeParameter<Reference>(), this->_head,this->_strides,index);} | ||
ConstReference operator[](Index index)const{return Super::access(types::TypeParameter<Reference>(), this->_head,this->_strides,index);} | ||
protected: | ||
TPtr _head; | ||
SizePtr _strides; | ||
}; | ||
|
||
} /* namespace base */ | ||
} /* namespace array */ | ||
} /* namespace linear_algebra */ | ||
#endif /* ARRAYBASE_H_ */ |
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,42 @@ | ||
/* | ||
* LinearAlgebraSubroutines.h | ||
* | ||
* Created on: Jul 15, 2012 | ||
* Author: Chris | ||
*/ | ||
|
||
#ifndef LINEARALGEBRA_H_ | ||
#define LINEARALGEBRA_H_ | ||
|
||
#include "Array.h" | ||
|
||
namespace linear_algebra { | ||
|
||
template<typename T> | ||
class Vector : public array::Array<T,1>{}; | ||
|
||
template<typename T> | ||
class Matrix | ||
: public array::Array<T,2> | ||
{ | ||
typedef array::Array<T,2> Super; | ||
public: | ||
template<typename Dimensions> | ||
Matrix(Dimensions dimensions) | ||
: Super(dimensions) | ||
{ | ||
|
||
} | ||
}; | ||
|
||
/** | ||
* Base class declaring common linear algebra subroutines. | ||
*/ | ||
class Subroutines { | ||
public: | ||
Subroutines(); | ||
virtual ~Subroutines(); | ||
}; | ||
|
||
} /* namespace linear_algebra */ | ||
#endif /* LINEARALGEBRA_H_ */ |
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,37 @@ | ||
/* | ||
* Types.h | ||
* | ||
* Created on: Jul 7, 2012 | ||
* Author: Chris | ||
*/ | ||
|
||
#ifndef TYPES_H_ | ||
#define TYPES_H_ | ||
|
||
#include <cstddef> | ||
#include <vector> | ||
|
||
|
||
namespace types { | ||
|
||
template<bool Condition, typename T1, typename T2> | ||
struct ConditionalType{ typedef T1 Type; }; | ||
template<typename T1, typename T2> | ||
struct ConditionalType<false,T1,T2>{ typedef T2 Type;}; | ||
|
||
// Simple size_t wrapper | ||
template<std::size_t N> | ||
struct SizeType{ | ||
typedef std::size_t Type; | ||
static const Type Value = N; | ||
}; | ||
|
||
// Wrapper for passing a type as a function or template parameter | ||
template<typename T> | ||
struct TypeParameter {}; | ||
|
||
|
||
} /* namespace types */ | ||
|
||
#endif /* TYPES_H_ */ | ||
|