toon-members
[Top][All Lists]
Advanced

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

[Toon-members] TooN TODO internal/allocator.hh internal/mbase....


From: Edward Rosten
Subject: [Toon-members] TooN TODO internal/allocator.hh internal/mbase....
Date: Sat, 07 Feb 2009 18:49:03 +0000

CVSROOT:        /cvsroot/toon
Module name:    TooN
Changes by:     Edward Rosten <edrosten>        09/02/07 18:49:03

Modified files:
        .              : TODO 
        internal       : allocator.hh mbase.hh vbase.hh 
        test           : mat_test.cc 

Log message:
        ColMajor and .T() implemented.
        
        test/mat_test.cc now compiles and runs correctly.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/TODO?cvsroot=toon&r1=1.1&r2=1.2
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/allocator.hh?cvsroot=toon&r1=1.5&r2=1.6
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/mbase.hh?cvsroot=toon&r1=1.7&r2=1.8
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/vbase.hh?cvsroot=toon&r1=1.4&r2=1.5
http://cvs.savannah.gnu.org/viewcvs/TooN/test/mat_test.cc?cvsroot=toon&r1=1.2&r2=1.3

Patches:
Index: TODO
===================================================================
RCS file: /cvsroot/toon/TooN/TODO,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- TODO        7 Feb 2009 15:42:04 -0000       1.1
+++ TODO        7 Feb 2009 18:49:02 -0000       1.2
@@ -1,5 +1,5 @@
 const slices of vector
 const slices of matrices
 const vector indexing of matrices
-col major matrives and transpose
 vector with static size and dynamic stride
+bounds checking for matrices

Index: internal/allocator.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/allocator.hh,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -b -r1.5 -r1.6
--- internal/allocator.hh       7 Feb 2009 15:16:54 -0000       1.5
+++ internal/allocator.hh       7 Feb 2009 18:49:02 -0000       1.6
@@ -125,8 +125,8 @@
        MatrixSlice(Precision* p)
        :my_data(p){}
 
-       //MatrixSlice(Precision* p, int /*rows*/, int /*cols*/)
-       //:my_data(p){}
+       MatrixSlice(Precision* p, int /*rows*/, int /*cols*/)
+       :my_data(p){}
 };
 
 template<class Precision> struct MatrixSlice<-1, -1, Precision>

Index: internal/mbase.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/mbase.hh,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- internal/mbase.hh   7 Feb 2009 15:32:56 -0000       1.7
+++ internal/mbase.hh   7 Feb 2009 18:49:03 -0000       1.8
@@ -23,7 +23,43 @@
 
 template<int,int,class,class> class Matrix;
 template<int Rows, int Cols, class Precision, int Stride, class Mem> struct 
GenericRowMajor;
+template<int Rows, int Cols, class Precision, int Stride, class Mem> struct 
GenericColMajor;
 
+
+////////////////////////////////////////////////////////////////////////////////
+//
+// 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<> struct StrideHolder<-1>
+{
+       StrideHolder(int s)
+       :stride(s){}
+
+       const int stride;
+       int get(const void*) const {
+               return stride;
+       }
+};
+
+template<> struct StrideHolder<-2>{
+       template<class C> int get(const C* c) const {
+               return c->tied_stride();
+       }
+};
+
+////////////////////////////////////////////////////////////////////////////////
 //Closure used to acquire strides
 //-1 means dynamic stride
 //-2 means dynamic stride is tied to size
@@ -57,9 +93,43 @@
 
                };
        };
+
+       struct ColMajor
+       {
+               template<int Rows, int Cols, class Precision> struct Layout: 
public GenericColMajor<Rows, Cols, Precision, Stride, MatrixSlice<Rows, Cols, 
Precision> >
+               {
+                       //Optional constructors.
+                       
+                       Layout(Precision* p)
+                       
:GenericColMajor<Rows,Cols,Precision,Stride,MatrixSlice<Rows, Cols, Precision> 
>(p)
+                       {
+                       }
+
+                       Layout(Precision* p, int stride)
+                       
:GenericColMajor<Rows,Cols,Precision,Stride,MatrixSlice<Rows, Cols, Precision> 
>(p, stride)
+                       {
+                       }
+
+                       Layout(Precision* p, int rows, int cols)
+                       
:GenericColMajor<Rows,Cols,Precision,Stride,MatrixSlice<Rows, Cols, Precision> 
>(p, rows, cols)
+                       {
+                       }
+
+                       Layout(Precision* p, int rows, int cols, int stride)
+                       
:GenericColMajor<Rows,Cols,Precision,Stride,MatrixSlice<Rows, Cols, Precision> 
>(p, rows, cols, stride)
+                       {
+                       }
+
+               };
+       };
 };
 
 
+////////////////////////////////////////////////////////////////////////////////
+//
+// Classes for Matrices owning memory
+//
+//
 struct RowMajor
 {
        template<int Rows, int Cols, class Precision> struct Layout: public 
GenericRowMajor<Rows, Cols, Precision, (Cols==-1?-2:Cols), MatrixAlloc<Rows, 
Cols, Precision> >
@@ -74,10 +144,34 @@
        };
 };
 
+struct ColMajor
+{
+       template<int Rows, int Cols, class Precision> struct Layout: public 
GenericColMajor<Rows, Cols, Precision, (Rows==-1?-2:Rows), MatrixAlloc<Rows, 
Cols, Precision> >
+       {
+               //Optional constructors.
+               
+               Layout(){}
+
+               Layout(int rows, int cols)
+               :GenericColMajor<Rows, Cols, Precision, (Rows == -1 ? -2 : 
Rows), MatrixAlloc<Rows, Cols, Precision> >(rows, cols)
+               {}
+       };
+};
+
+
+
 
 
////////////////////////////////////////////////////////////////////////////////
 //
-// Given the rows, cols, and stride, what should the resulting vector type be?
+// Row major matrix implementation
+//
+
+
+////////////////////////////////////////
+//
+// Given the rows, cols, and stride, 
+// what should the resulting vector type 
+// be?
 //
 
 template<int Rows, int Cols, typename Precision> struct RMVec
@@ -100,40 +194,7 @@
        }
 };
 
-////////////////////////////////////////////////////////////////////////////////
-//
-// 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<> struct StrideHolder<-1>
-{
-       StrideHolder(int s)
-       :stride(s){}
-
-       const int stride;
-       int get(const void*) const {
-               return stride;
-       }
-};
-
-template<> struct StrideHolder<-2>{
-       template<class C> int get(const C* c) const {
-               return c->tied_stride();
-       }
-};
-
-////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////
 //
 //Generic access to Row Major data.
 //
@@ -202,4 +263,147 @@
        Matrix<-1, -1, Precision, typename Slice<SliceStride>::RowMajor > 
slice(int rs, int cs, int rl, int cl){
                return Matrix<-1, -1, Precision, typename 
Slice<SliceStride>::RowMajor >(my_data+stride()*rs +cs, rl, cl, stride(), 
Slicing());
        }
+
+
+       Matrix<Cols, Rows, Precision, typename Slice<SliceStride>::ColMajor> 
T(){
+               return Matrix<Cols, Rows, Precision, typename 
Slice<SliceStride>::ColMajor>(my_data, num_rows(), num_cols(), stride(), 
Slicing());
+       }
+};
+
+
+
+
+
+
+////////////////////////////////////////////////////////////////////////////////
+//
+// Column major matrix implementation
+//
+
+
+////////////////////////////////////////
+//
+// Given the rows, cols, and stride, 
+// what should the resulting vector type 
+// be?
+//
+
+template<int Rows, int Cols, int Stride, typename Precision> struct CMVec
+{
+       typedef Vector<Cols, Precision, SVBase<Cols, Stride, Precision> > Type;
+
+       static Type vec(Precision* d, int /*cols*/, int /*stride*/)
+       {
+               return Type(d);
+       }
+};
+
+template<int Rows, int Cols, typename Precision> struct CMVec<Rows, Cols, -1, 
Precision>
+{
+       //FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME 
FIXME FIXME
+       //There should be a statically sized, dynamically strided vector type
+       typedef Vector<-1, Precision, SDDVBase<Precision> > Type;
+
+       static Type vec(Precision* d, int /*cols*/, int stride)
+       {
+               return Type(d, Cols, stride);
+       }
+};
+
+template<int Stride, typename Precision> struct CMVec<-1, -1, Stride, 
Precision>
+{
+       typedef Vector<-1, Precision, SDVBase<Stride, Precision> > Type;
+
+       static Type vec(Precision* d, int cols, int /*stride*/)
+       {
+               return Type(d, cols);
+       }
+};
+
+template<typename Precision> struct CMVec<-1, -1, -1, Precision>
+{
+       typedef Vector<-1, Precision, SDDVBase<Precision> > Type;
+
+       static Type vec(Precision* d, int cols, int stride)
+       {
+               return Type(d, cols, stride);
+       }
+};
+
+////////////////////////////////////////
+//
+//Generic access to Col Major data.
+//
+template<int Rows, int Cols, class Precision, int Stride, class Mem> struct 
GenericColMajor: public Mem
+{
+       //Slices can never have tied strides
+       static const int SliceStride = Stride == -2?-1: Stride;
+
+       //This little hack returns the stride value if it exists,
+       //or one of the implied strides if they exist.
+       int tied_stride() const{ 
+               //Only valid if stride is -2
+               return num_rows();
+       }
+       StrideHolder<Stride> my_stride;
+       int stride() const {
+               return my_stride.get(this);
+       }
+
+
+       //Optional constructors
+       
+       GenericColMajor(){}
+
+       GenericColMajor(Precision* p)
+       :Mem(p) {}
+
+       GenericColMajor(Precision* p, int s)
+       :Mem(p),my_stride(s) {}
+
+       GenericColMajor(Precision* p, int r, int c)
+       :Mem(p, r, c) {}
+
+       GenericColMajor(Precision* p, int r, int c, int stride)
+       :Mem(p, r, c),my_stride(stride) {}
+
+       GenericColMajor(int r, int c)
+       :Mem(r, c) {}
+
+       using Mem::my_data;
+       using Mem::num_cols;
+       using Mem::num_rows;
+
+       Precision& operator()(int r, int c){
+               return my_data[c*stride() + r];
+       }
+
+       const Precision& operator()(int r, int c) const {
+               return my_data[c*stride() + r];
+       }
+       
+       typedef CMVec<Rows, Cols, Stride, Precision> Vec;
+       typename Vec::Type operator[](int r)
+       {
+               return Vec::vec(my_data + r, num_cols(), stride());
+       }
+
+       template<int Rstart, int Cstart, int Rlength, int Clength>
+       Matrix<Rlength, Clength, Precision, typename 
Slice<SliceStride>::ColMajor> slice()
+       {
+               //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, typename 
Slice<SliceStride>::ColMajor>(my_data+Rstart + stride()*Cstart, stride(), 
Slicing());
+       }
+
+       Matrix<-1, -1, Precision, typename Slice<SliceStride>::ColMajor > 
slice(int rs, int cs, int rl, int cl){
+               return Matrix<-1, -1, Precision, typename 
Slice<SliceStride>::ColMajor >(my_data+rs +stride()*cs, rl, cl, stride(), 
Slicing());
+       }
+
+       Matrix<Cols, Rows, Precision, typename Slice<SliceStride>::RowMajor> 
T(){
+               return Matrix<Cols, Rows, Precision, typename 
Slice<SliceStride>::RowMajor>(my_data, num_rows(), num_cols(), stride(), 
Slicing());
+       }
 };
+
+
+

Index: internal/vbase.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/vbase.hh,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- internal/vbase.hh   26 Jan 2009 18:45:18 -0000      1.4
+++ internal/vbase.hh   7 Feb 2009 18:49:03 -0000       1.5
@@ -342,15 +342,15 @@
 // SDVBase is for dynamically sized vectors that do not own their data
 // They have an additional stride member
 template <typename Precision>
-class SSDVBase{
+class SDDVBase{
 public:
-  SSDVBase(Precision* data_in, int size_in, int stride_in):
+  SDDVBase(Precision* data_in, int size_in, int stride_in):
     my_data(data_in) {
     my_size=size_in;
     my_stride=stride_in;
   };
 
-  SSDVBase(const SSDVBase& from)
+  SDDVBase(const SDDVBase& from)
     : my_data(from.my_data),
       my_size(from.my_size),
       my_stride(from.my_stride){

Index: test/mat_test.cc
===================================================================
RCS file: /cvsroot/toon/TooN/test/mat_test.cc,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- test/mat_test.cc    27 Jan 2009 10:55:54 -0000      1.2
+++ test/mat_test.cc    7 Feb 2009 18:49:03 -0000       1.3
@@ -5,7 +5,7 @@
 using namespace TooN;
 using namespace std;
 
-template<int R, int C, class P, template<int,int,class> class L> void 
foo(Matrix<R,C,P,L>& m)
+template<int R, int C, class P, class L> void foo(Matrix<R,C,P,L>& m)
 {
        cout << "In foo:\n";
        m[0][0] = 1;




reply via email to

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