[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Toon-members] TooN TooN.h operators.h internal/allocator.hh i...
From: |
Edward Rosten |
Subject: |
[Toon-members] TooN TooN.h operators.h internal/allocator.hh i... |
Date: |
Mon, 26 Jan 2009 18:41:43 +0000 |
CVSROOT: /cvsroot/toon
Module name: TooN
Changes by: Edward Rosten <edrosten> 09/01/26 18:41:43
Modified files:
. : TooN.h operators.h
Added files:
internal : allocator.hh matrix.hh mbase.hh
test : mat_test.cc
Log message:
Very rickety version of Matrix.
Currently, static slices of static matrices work. test/mat_test.cc
compiles
and runs.
CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/TooN.h?cvsroot=toon&r1=1.16&r2=1.17
http://cvs.savannah.gnu.org/viewcvs/TooN/operators.h?cvsroot=toon&r1=1.2&r2=1.3
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/allocator.hh?cvsroot=toon&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/matrix.hh?cvsroot=toon&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/mbase.hh?cvsroot=toon&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/TooN/test/mat_test.cc?cvsroot=toon&rev=1.1
Patches:
Index: TooN.h
===================================================================
RCS file: /cvsroot/toon/TooN/TooN.h,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -b -r1.16 -r1.17
--- TooN.h 13 Jan 2009 15:06:52 -0000 1.16
+++ TooN.h 26 Jan 2009 18:41:42 -0000 1.17
@@ -5,6 +5,10 @@
#include <cstdlib>
namespace TooN
{
+
+ static const unsigned int max_bytes_on_stack=1000;
+
+
#ifdef TOON_TEST_INTERNALS
namespace Internal
{
@@ -17,11 +21,16 @@
#endif
+ #include <TooN/internal/allocator.hh>
#include <TooN/internal/size_mismatch.hh>
#include <TooN/internal/slice_error.hh>
#include <TooN/internal/debug.hh>
#include <TooN/internal/vbase.hh>
#include <TooN/internal/vector.hh>
+
+ #include <TooN/internal/mbase.hh>
+ #include <TooN/internal/matrix.hh>
+
#include <TooN/internal/make_vector.hh>
#include <TooN/operators.h>
}
Index: operators.h
===================================================================
RCS file: /cvsroot/toon/TooN/operators.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- operators.h 9 Jan 2009 14:45:01 -0000 1.2
+++ operators.h 26 Jan 2009 18:41:42 -0000 1.3
@@ -75,3 +75,20 @@
}
+template<int Rows, int Cols, typename Precision, template<int, int, class>
class Base>
+inline std::ostream& operator<< (std::ostream& os, const Matrix<Rows, Cols,
Precision, Base>& m){
+ for(int i=0; i < m.num_rows(); i++)
+ {
+ for(int j=0; j < m.num_cols(); j++)
+ {
+ if(j != 0)
+ os << " ";
+ os << m(i,j);
+ }
+ os << std::endl;
+ }
+ return os;
+}
+
+
+
Index: internal/allocator.hh
===================================================================
RCS file: internal/allocator.hh
diff -N internal/allocator.hh
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ internal/allocator.hh 26 Jan 2009 18:41:42 -0000 1.1
@@ -0,0 +1,56 @@
+// Allocators never copy
+
+template<int Size, class Precision, bool heap> class StackOrHeap
+{
+ public:
+ StackOrHeap()
+ {}
+
+ StackOrHeap(const StackOrHeap&)
+ {}
+
+ Precision my_data[Size];
+};
+
+template<int Size, class Precision> class StackOrHeap<Size, Precision, 1>
+{
+ public:
+ StackOrHeap()
+ :my_data(new Precision[Size]){}
+
+
+ ~StackOrHeap()
+ {
+ delete[] my_data;
+ }
+
+ Precision *my_data;
+
+ private:
+ StackOrHeap(const StackOrHeap&);
+};
+
+
+template<class Precision> class DynamicSizedAllocator
+{
+ public:
+ DynamicSizedAllocator(int size)
+ :my_data(new Precision[size]){}
+
+
+ ~DynamicSizedAllocator()
+ {
+ delete[] my_data;
+ }
+
+ Precision *my_data;
+
+ private:
+ DynamicSizedAllocator(const DynamicSizedAllocator& d);
+};
+
+
+
+template<int Size, class Precision> class StaticSizedAllocator: public
StackOrHeap<Size, Precision, (sizeof(Precision)*Size>max_bytes_on_stack) >
+{
+};
Index: internal/matrix.hh
===================================================================
RCS file: internal/matrix.hh
diff -N internal/matrix.hh
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ internal/matrix.hh 26 Jan 2009 18:41:42 -0000 1.1
@@ -0,0 +1,18 @@
+template <int Rows=-1, int Cols=Rows, class Precision=double, template<int R,
int C, class P> class Layout = RowMajor>
+class Matrix : public Layout<Rows, Cols, Precision>
+{
+ public:
+ //Use Tom's sneaky constructor hack...
+
+ Matrix(){}
+
+ Matrix(Precision* data, Slicing)
+ :Layout<Rows, Cols, Precision>(data){}
+
+
+
+
+
+
+
+};
Index: internal/mbase.hh
===================================================================
RCS file: internal/mbase.hh
diff -N internal/mbase.hh
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ internal/mbase.hh 26 Jan 2009 18:41:43 -0000 1.1
@@ -0,0 +1,97 @@
+// -*- c++ -*-
+
+//Forward declarations
+
+
+template<int Rows, int Cols, class Precision, template<int, int, class> class
Layout> class Matrix;
+
+struct Slicing{};
+
+//Static stride Static slice
+template<int Stride> struct SSSlice
+{
+ template<int Rows, int Cols, class Precision>
+ struct RowMajor
+ {
+ private:
+ Precision* my_data;
+
+ public:
+ int num_rows() const { return Rows; }
+ int num_cols() const { return Cols; }
+ //Construction
+
+ RowMajor(Precision* d)
+ :my_data(d)
+ {}
+
+ //Indexing
+
+ Vector<Cols, Precision, SVBase<Cols,1,Precision> > operator[](int row){
+ Internal::check_index(Rows, row);
+ return Vector<Cols, Precision, SVBase<Cols,1,Precision>
>(my_data+Stride*row);
+ }
+
+ Precision& operator()(int row, int col){
+ Internal::check_index(Rows, row);
+ Internal::check_index(Cols, col);
+ return my_data[row*Stride+col];
+ }
+
+ const Precision& operator()(int row, int col) const {
+ Internal::check_index(Rows, row);
+ Internal::check_index(Cols, col);
+ return my_data[row*Stride+col];
+ }
+
+ //Slices go here
+ template<int Rstart, int Cstart, int Rlength, int Clength>
+ Matrix<Rlength, Clength, Precision, SSSlice<Stride>::template RowMajor>
slice(){
+ Internal::CheckSlice<Rows, Rstart, Rlength>::check();
+ Internal::CheckSlice<Cols, Cstart, Clength>::check();
+
+ return Matrix<Rlength, Clength, Precision, SSSlice<Stride>::template
RowMajor>(my_data + Stride*Rstart + Cstart, Slicing());
+ }
+
+ //Transpose goes here
+ };
+};
+
+template<int Rows, int Cols, class Precision>
+struct RowMajor: public StaticSizedAllocator<Rows*Cols, Precision>
+{
+ int num_rows() const { return Rows; }
+ int num_cols() const { return Cols; }
+
+ // has to handle operator[row] and operator(row,col)
+
+ using StaticSizedAllocator<Rows*Cols, Precision>::my_data;
+
+ Vector<Cols, Precision, SVBase<Cols,1,Precision> > operator[](int row){
+ Internal::check_index(Rows, row);
+ return Vector<Cols, Precision, SVBase<Cols,1,Precision>
>(my_data+Cols*row);
+ }
+
+
+ Precision& operator()(int row, int col){
+ Internal::check_index(Rows, row);
+ Internal::check_index(Cols, col);
+ return my_data[row*Cols+col];
+ }
+ const Precision& operator()(int row, int col) const {
+ Internal::check_index(Rows, row);
+ Internal::check_index(Cols, col);
+ return my_data[row*Cols+col];
+ }
+
+
+ template<int Rstart, int Cstart, int Rlength, int Clength>
+ Matrix<Rlength, Clength, Precision, SSSlice<Cols>::template RowMajor>
slice(){
+ Internal::CheckSlice<Rows, Rstart, Rlength>::check();
+ Internal::CheckSlice<Cols, Cstart, Clength>::check();
+
+ return Matrix<Rlength, Clength, Precision, SSSlice<Cols>::template
RowMajor>(my_data + Cols*Rstart + Cstart, Slicing());
+ }
+
+ //Transpose goes here
+};
Index: test/mat_test.cc
===================================================================
RCS file: test/mat_test.cc
diff -N test/mat_test.cc
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ test/mat_test.cc 26 Jan 2009 18:41:43 -0000 1.1
@@ -0,0 +1,24 @@
+#include <TooN/TooN.h>
+
+using namespace TooN;
+using namespace std;
+
+int main()
+{
+ Matrix<3> m;
+ m[0][0] = 1;
+ m[0][1] = 2;
+ m[0][2] = 3;
+ m[1][0] = 4;
+ m[1][1] = 5;
+ m[1][2] = 6;
+ m[2][0] = 7;
+ m[2][1] = 8;
+ m[2][2] = 9;
+
+ cout << m << endl;
+
+ cout << m.slice<0,0,2,3>() << endl;
+
+ cout << m.slice<0,0,2,3>().slice<0,1,2,2>() << endl;
+}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Toon-members] TooN TooN.h operators.h internal/allocator.hh i...,
Edward Rosten <=