toon-members
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Toon-members] TooN internal/allocator.hh internal/matrix.hh i...


From: Edward Rosten
Subject: [Toon-members] TooN internal/allocator.hh internal/matrix.hh i...
Date: Sat, 07 Feb 2009 12:17:54 +0000

CVSROOT:        /cvsroot/toon
Module name:    TooN
Changes by:     Edward Rosten <edrosten>        09/02/07 12:17:54

Modified files:
        internal       : allocator.hh matrix.hh mbase.hh 
Added files:
        test           : mat_test2.cc 

Log message:
        Reimlementation of RowMajor, and temporary removal of ColMajor.
        
        This time, there is a single implementation of ColMajor for static,
        dynamic, normal and sliced matrices.
        
        Currently only static works, and test/mat_test2.cc compiles and produces
        the correct output.
        
        The general principle behind this version is to pass around metadata at 
run
        time in cases it is needed, and then ignore it if it is not.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/allocator.hh?cvsroot=toon&r1=1.2&r2=1.3
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/matrix.hh?cvsroot=toon&r1=1.3&r2=1.4
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/mbase.hh?cvsroot=toon&r1=1.3&r2=1.4
http://cvs.savannah.gnu.org/viewcvs/TooN/test/mat_test2.cc?cvsroot=toon&rev=1.1

Patches:
Index: internal/allocator.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/allocator.hh,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- internal/allocator.hh       3 Feb 2009 18:04:30 -0000       1.2
+++ internal/allocator.hh       7 Feb 2009 12:17:52 -0000       1.3
@@ -66,3 +66,96 @@
 };
 
 
+
+
+
+
+
+
+
+
+template<int R, int C, class Precision> struct MatrixAlloc: public 
StaticSizedAllocator<R*C, Precision>
+{
+       int num_rows() const {
+               return R;
+       }
+
+       int num_cols() const {
+               return C;
+       }
+};
+
+template<class Precision> struct MatrixAlloc<-1, -1, Precision>
+{
+       const int my_rows;
+       const int my_cols;
+       Precision* const my_data;
+
+       MatrixAlloc(int r, int c)
+       :my_rows(r),my_cols(c),my_data(new Precision[r*c]) {
+       }
+
+       ~MatrixAlloc() {
+               delete[] my_data;
+       }
+
+       int num_rows() const {
+               return num_rows;
+       }
+
+       int num_cols() const {
+               return num_cols;
+       }
+};
+
+
+
+template<int R, int C, class Precision> struct MatrixSlice
+{
+       int num_rows() const {
+               return R;
+       }
+
+       int num_cols() const {
+               return C;
+       }
+       //Optional Constructors
+       
+       Precision* const my_data;
+       MatrixSlice(Precision* p)
+       :my_data(p){}
+
+       //MatrixSlice(Precision* p, int /*rows*/, int /*cols*/)
+       //:my_data(p){}
+};
+
+template<class Precision> struct MatrixSlice<-1, -1, Precision>
+{
+       Precision* const my_data;
+       const int my_rows;
+       const int my_cols;
+
+       MatrixSlice(Precision d, int r, int c)
+       :my_data(d), my_rows(r),my_cols(c),my_data(d)
+       {
+       }
+
+       int num_rows() const {
+               return num_rows;
+       }
+
+       int num_cols() const {
+               return num_cols;
+       }
+};
+
+
+
+
+
+
+
+
+
+
+

Index: internal/matrix.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/matrix.hh,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- internal/matrix.hh  3 Feb 2009 18:04:30 -0000       1.3
+++ internal/matrix.hh  7 Feb 2009 12:17:52 -0000       1.4
@@ -9,8 +9,12 @@
        Matrix(){}
 
        Matrix(Precision* data, Slicing)
-       :Layout<Rows, Cols, Precision>(data,Slicing()){}
+       :Layout<Rows, Cols, Precision>(data){}
 
+       //The stride is always passed during a slice. If it is not
+       //needed, it will be ignored later and not stored.
+       Matrix(Precision* data, int stride, Slicing)
+       :Layout<Rows, Cols, Precision>(data, stride){}
 
        Precision* data() {
          return my_data;

Index: internal/mbase.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/mbase.hh,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- internal/mbase.hh   3 Feb 2009 18:04:30 -0000       1.3
+++ internal/mbase.hh   7 Feb 2009 12:17:53 -0000       1.4
@@ -1,7 +1,7 @@
-//Types:
+//Slicing and etc...
 //SS = static size, static stride
 //DS = dynamic size, static stride  (can use DD)
-//Du = dynamic size, unstrided (used for user instantiated Matrix), saves 
implicit stride parameter
+//Dl = dynamic size, linked stride: for RowMajor user instantiated dynamic 
matrices, stride==cols
 //DD = dynamic size, dynamic stride
 //SD = static size, dynamic stride.
 
@@ -9,153 +9,188 @@
 //SS.slice()   -> DS
 //DS.slice<>() -> SS
 //DS.slice()   -> DS
-//Du.slice<>() -> SD
-//Du.silce()   -> DD
+//Dc.slice<>() -> SD
+//Dl.silce()   -> DD
 //DD.slice<>() -> SD
 //DD.slice()   -> DD
 //SD.slice<>() -> SD
 //SD.slice()   -> DD
 
+
+// As usual, a positive integer means static and -1 means dynamic.
+// The new case is that for strides, -2 means that the stride is 
+// the same as num_cols/num_rows, which must be dynamically sized.
+
 template<int,int,class,template<int,int,class> class> class Matrix;
-template<int Rows, int Cols, class Precision, int Stride, class Mem> struct 
SSRowMajor;
-template<int Rows, int Cols, class Precision, int Stride, class Mem> struct 
SSColMajor;
+template<int Rows, int Cols, class Precision, int Stride, class Mem> struct 
GenericRowMajor;
 
-template<int Stride> struct SSSlice
+//Closure used to acquire strides
+//-1 means dynamic stride
+//-2 means dynamic stride is tied to size
+template<int Stride> struct Slice
 {
-       template<int Rows, int Cols, class Precision> struct RowMajor: public 
SSRowMajor<Rows, Cols, Precision, Stride, SliceHolder<Precision> >
+       template<int Rows, int Cols, class Precision> struct RowMajor: public 
GenericRowMajor<Rows, Cols, Precision, Stride, MatrixSlice<Rows, Cols, 
Precision> >
        {
-               RowMajor(Precision* p,  Slicing)
-               :SSRowMajor<Rows,Cols,Precision,Stride,SliceHolder<Precision> 
>(p)
+               //Optional constructors.
+               
+               RowMajor(Precision* p)
+               :GenericRowMajor<Rows,Cols,Precision,Stride,MatrixSlice<Rows, 
Cols, Precision> >(p)
                {
                }
-       };
 
-       template<int Rows, int Cols, class Precision> struct ColMajor: public 
SSColMajor<Rows, Cols, Precision, Stride, SliceHolder<Precision> >
+               RowMajor(Precision* p, int stride)
+               :GenericRowMajor<Rows,Cols,Precision,Stride,MatrixSlice<Rows, 
Cols, Precision> >(p, stride)
        {
-               ColMajor(Precision* p, Slicing)
-               :SSColMajor<Rows,Cols,Precision,Stride,SliceHolder<Precision> 
>(p)
+               }
+
+               RowMajor(Precision* p, int rows, int cols)
+               :GenericRowMajor<Rows,Cols,Precision,Stride,MatrixSlice<Rows, 
Cols, Precision> >(p, rows, cols)
+               {
+               }
+
+               RowMajor(Precision* p, int rows, int cols, int stride)
+               :GenericRowMajor<Rows,Cols,Precision,Stride,MatrixSlice<Rows, 
Cols, Precision> >(p, rows, cols, stride)
                {
                }
+
        };
 };
 
-template<int Rows, int Cols, class Precision> struct RowMajor: public 
SSRowMajor<Rows, Cols, Precision, Cols, StaticSizedAllocator<Rows*Cols, 
Precision> >
-{
-};
 
-template<int Rows, int Cols, class Precision> struct ColMajor: public 
SSColMajor<Rows, Cols, Precision, Rows, StaticSizedAllocator<Rows*Cols, 
Precision> >
+template<int Rows, int Cols, class Precision> struct RowMajor: public 
GenericRowMajor<Rows, Cols, Precision, (Cols==-1?-2:Cols), MatrixAlloc<Rows, 
Cols, Precision> >
 {
+       //Optional constructors.
+       
+       RowMajor(){}
+
+       RowMajor(int rows, int cols)
+       :GenericRowMajor<Rows, Cols, Precision, (Cols == -1 ? -2 : Cols), 
MatrixAlloc<Rows, Cols, Precision> >(rows, cols)
+       {}
 };
 
 
-//Generic access to Static sized, statically strided Row Major data.
-template<int Rows, int Cols, class Precision, int Stride, class Mem> struct 
SSRowMajor: public Mem
-{
-       //Optional constructors
+////////////////////////////////////////////////////////////////////////////////
+//
+// Given the rows, cols, and stride, what should the resulting vector type be?
+//
        
-       SSRowMajor(){}
-       SSRowMajor(Precision* p)
-       :Mem(p)
-       {}
+template<int Rows, int Cols, typename Precision> struct RMVec
+{
+       typedef Vector<Cols, Precision, SVBase<Cols, 1, Precision> > Type;
 
-       int num_rows()const {
-               return Rows;
-       }
-       int num_cols()const{
-               return Cols;
+       static Type vec(Precision* d, int /*cols*/)
+       {
+               return Type(d);
        }
-       using Mem::my_data;
+};
 
-       Precision& operator()(int r, int c){
-               Internal::check_index(Rows, r); 
-               Internal::check_index(Cols, c); 
-               return my_data[r*Stride + c];
-       }
+template<typename Precision> struct RMVec<-1, -1, Precision>
+{
+       typedef Vector<-1, Precision, SDVBase<1, Precision> > Type;
 
-       const Precision& operator()(int r, int c) const {
-               Internal::check_index(Rows, r); 
-               Internal::check_index(Cols, c); 
-               return my_data[r*Stride + c];
+       static Type vec(Precision* d, int cols)
+       {
+               return Type(d, cols);
        }
+};
 
-       Vector<Cols, Precision, SVBase<Cols, 1, Precision> > operator[](int r)
-       {
-               Internal::check_index(Rows, r); 
-               return Vector<Cols, Precision, SVBase<Cols, 1, Precision> 
>(my_data + Stride* r);
+////////////////////////////////////////////////////////////////////////////////
+//
+// A class similar to mem, but to hold the stride information. It is only 
needed
+// for -1. For +int and -2, the stride is part fo teh type, or implicit.
+
+template<int s> struct StrideHolder
+{
+       //Constructos ignore superfluous arguments
+       StrideHolder(){}
+       StrideHolder(int){}
+
+       int get(const void*) const{
+               return s;
        }
+};
 
-       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();
+template<> struct StrideHolder<-1>
+{
+       StrideHolder(int s)
+       :stride(s){}
 
-               return Matrix<Rlength, Clength, Precision, 
SSSlice<Stride>::template RowMajor>(my_data+Stride*Rstart + Cstart, Slicing());
+       const int stride;
+       int get(const void*) const {
+               return stride;
        }
+};
 
-       Matrix<Cols, Rows, Precision, SSSlice<Stride>::template ColMajor> T()
-       {
-               return Matrix<Cols, Rows, Precision, SSSlice<Stride>::template 
ColMajor>(my_data, Slicing());
+template<> struct StrideHolder<-2>{
+       template<class C> int get(const C* c) const {
+               return c->tied_stride();
        }
 };
 
-template<int Rows, int Cols, class Precision, int Stride, class Mem> struct 
SSColMajor: public Mem
+////////////////////////////////////////////////////////////////////////////////
+//
+//Generic access to Row Major data.
+//
+template<int Rows, int Cols, class Precision, int Stride, class Mem> struct 
GenericRowMajor: public Mem
 {
-       SSColMajor(){}
-       SSColMajor(Precision* p)
+
+       //This little hack returns the stride value if it exists,
+       //or one of the implied strides if they exist.
+       int tied_stride(){ 
+               //Only valid if stride is -2
+               return num_cols();
+       }
+       StrideHolder<Stride> my_stride;
+       int stride() const {
+               return my_stride.get(this);
+       }
+
+
+       //Optional constructors
+       
+       GenericRowMajor(){}
+
+       GenericRowMajor(Precision* p)
        :Mem(p)
        {}
 
-       int num_rows()const{
-               return Rows;
-       }
-       int num_cols()const{
-               return Cols;
-       }
+       GenericRowMajor(Precision* p, int s)
+       :Mem(p),my_stride(s)
+       {}
+
+       GenericRowMajor(Precision* p, int r, int c)
+       :Mem(p, r, c)
+       {}
 
        using Mem::my_data;
+       using Mem::num_cols;
+       using Mem::num_rows;
 
        Precision& operator()(int r, int c){
-               Internal::check_index(Rows, r); 
-               Internal::check_index(Cols, c); 
-               return my_data[c*Stride + r];
+               return my_data[r*stride() + c];
        }
 
-       const Precision& operator()(int r, int c)const{
-               Internal::check_index(Rows, r); 
-               Internal::check_index(Cols, c); 
-               return my_data[c*Stride + r];
+       const Precision& operator()(int r, int c) const {
+               return my_data[r*stride() + c];
        }
 
-       Vector<Cols, Precision, SVBase<Cols, Stride, Precision> > 
operator[](int r)
+       typedef RMVec<Rows, Cols, Precision> Vec;
+       typename Vec::Type operator[](int r)
        {
-               Internal::check_index(Rows, r); 
-               return Vector<Cols, Precision, SVBase<Cols, Stride, Precision> 
>(my_data + r);
+               return Vec::vec(my_data + stride()* r, num_cols());
        }
 
        template<int Rstart, int Cstart, int Rlength, int Clength>
-       Matrix<Rlength, Clength, Precision, SSSlice<Stride>::template ColMajor> 
slice()
+       Matrix<Rlength, Clength, Precision, Slice<Stride>::template RowMajor> 
slice()
        {
-               Internal::CheckSlice<Rows, Rstart, Rlength>::check();
-               Internal::CheckSlice<Cols, Cstart, Clength>::check();
-
-               return Matrix<Rlength, Clength, Precision, 
SSSlice<Stride>::template ColMajor>(my_data+Stride*Cstart + Rstart, Slicing());
+               //Always pass the stride as a run-time parameter. It will be 
ignored
+               //by SliceHolder (above) if it is statically determined.
+               return Matrix<Rlength, Clength, Precision, 
Slice<Stride>::template RowMajor>(my_data+stride()*Rstart + Cstart, stride(), 
Slicing());
        }
 
-       Matrix<Cols, Rows, Precision, SSSlice<Stride>::template RowMajor> T()
-       {
-               return Matrix<Cols, Rows, Precision, SSSlice<Stride>::template 
RowMajor>(my_data, Slicing());
+       Matrix<-1, -1, Precision, Slice<Stride>::template RowMajor > slice(int 
rs, int cs, int rl, int cl){
+               return Matrix<-1, -1, Precision, Slice<Stride>::template 
RowMajor >(my_data+stride()*rs +cs, rl, cl, stride());
        }
-};
-
-
-
-
-
-
-
-
-
-
 
 
+};

Index: test/mat_test2.cc
===================================================================
RCS file: test/mat_test2.cc
diff -N test/mat_test2.cc
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ test/mat_test2.cc   7 Feb 2009 12:17:53 -0000       1.1
@@ -0,0 +1,36 @@
+#include <TooN/TooN.h>
+using namespace TooN;
+using namespace std;
+
+int main()
+{
+       Matrix<3,4> m;
+
+       for(int i=0; i < 12; i++)
+               (&m[0][0])[i] = i;
+
+       cout << m << endl;
+
+       cout << "Accessing rows as vectors:\n";
+       for(int i=0; i < 3; i++)
+               cout << "...  " << m[i] << "...\n";
+       cout << endl;
+
+       cout << "Slice, from [1,1], size [2,3]:\n";
+       cout << m.slice<1,1,2,3>() << endl;
+
+       cout << "Accessing rows of a slice as vectors:\n";
+       for(int i=0; i < 2; i++)
+               cout << "...  " << m.slice<1,1,2,3>()[i] << "...\n";
+       cout << endl;
+
+
+
+       cout << "Slice, of the above slice from [0,0], size [2,1]:\n";
+       cout << m.slice<1,1,2,3>().slice<0,0,2,1>() << endl;
+
+       cout << "Accessing rows of a slice of a slice as vectors:\n";
+       for(int i=0; i < 2; i++)
+               cout << "...  " << m.slice<1,1,2,3>().slice<0,0,2,1>()[i] << 
"...\n";
+       cout << endl;
+}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]