toon-members
[Top][All Lists]
Advanced

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

[Toon-members] TooN TooN.h internal/allocator.hh internal/matr...


From: Edward Rosten
Subject: [Toon-members] TooN TooN.h internal/allocator.hh internal/matr...
Date: Tue, 03 Feb 2009 18:04:31 +0000

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

Modified files:
        .              : TooN.h 
        internal       : allocator.hh matrix.hh mbase.hh 

Log message:
        Refactor mbase to remove duplicate code.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/TooN.h?cvsroot=toon&r1=1.17&r2=1.18
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/allocator.hh?cvsroot=toon&r1=1.1&r2=1.2
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/matrix.hh?cvsroot=toon&r1=1.2&r2=1.3
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/mbase.hh?cvsroot=toon&r1=1.2&r2=1.3

Patches:
Index: TooN.h
===================================================================
RCS file: /cvsroot/toon/TooN/TooN.h,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -b -r1.17 -r1.18
--- TooN.h      26 Jan 2009 18:41:42 -0000      1.17
+++ TooN.h      3 Feb 2009 18:04:30 -0000       1.18
@@ -7,6 +7,7 @@
 {
        
        static const unsigned int max_bytes_on_stack=1000;
+       struct Slicing{};
 
 
        #ifdef TOON_TEST_INTERNALS

Index: internal/allocator.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/allocator.hh,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- internal/allocator.hh       26 Jan 2009 18:41:42 -0000      1.1
+++ internal/allocator.hh       3 Feb 2009 18:04:30 -0000       1.2
@@ -54,3 +54,15 @@
 template<int Size, class Precision> class StaticSizedAllocator: public 
StackOrHeap<Size, Precision, (sizeof(Precision)*Size>max_bytes_on_stack) >
 {
 };
+
+
+template<class Precision> struct SliceHolder
+{
+       SliceHolder(Precision* p)
+       :my_data(p)
+       {}
+
+       Precision* my_data;
+};
+
+

Index: internal/matrix.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/matrix.hh,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- internal/matrix.hh  27 Jan 2009 10:55:53 -0000      1.2
+++ internal/matrix.hh  3 Feb 2009 18:04:30 -0000       1.3
@@ -9,7 +9,7 @@
        Matrix(){}
 
        Matrix(Precision* data, Slicing)
-       :Layout<Rows, Cols, Precision>(data){}
+       :Layout<Rows, Cols, Precision>(data,Slicing()){}
 
 
        Precision* data() {

Index: internal/mbase.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/mbase.hh,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- internal/mbase.hh   27 Jan 2009 10:55:53 -0000      1.2
+++ internal/mbase.hh   3 Feb 2009 18:04:30 -0000       1.3
@@ -1,207 +1,161 @@
-// -*- c++ -*-
+//Types:
+//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
+//DD = dynamic size, dynamic stride
+//SD = static size, dynamic stride.
+
+//SS.slice<>() -> SS
+//SS.slice()   -> DS
+//DS.slice<>() -> SS
+//DS.slice()   -> DS
+//Du.slice<>() -> SD
+//Du.silce()   -> DD
+//DD.slice<>() -> SD
+//DD.slice()   -> DD
+//SD.slice<>() -> SD
+//SD.slice()   -> DD
+
+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;
 
-//Forward declarations
-
-//TODO?
-//maybe shift to having Layout have a layout template, so Matrix is 
Matrix<int,int,class,class>
-template<int Rows, int Cols, class Precision, template<int, int, class> class 
Layout> class Matrix;
-
-struct Slicing{};
-
-//Static stride Static slice. Use closure to get Stride parameter
-//in to RowMajor and ColMajor
 template<int Stride> struct SSSlice
 {
-  template<int Rows, int Cols, class Precision> struct ColMajor;
-
-  template<int Rows, int Cols, class Precision>
-  struct RowMajor
+       template<int Rows, int Cols, class Precision> struct RowMajor: public 
SSRowMajor<Rows, Cols, Precision, Stride, SliceHolder<Precision> >
+       {
+               RowMajor(Precision* p,  Slicing)
+               :SSRowMajor<Rows,Cols,Precision,Stride,SliceHolder<Precision> 
>(p)
   {
-    protected: 
-      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];
+       template<int Rows, int Cols, class Precision> struct ColMajor: public 
SSColMajor<Rows, Cols, Precision, Stride, SliceHolder<Precision> >
+       {
+               ColMajor(Precision* p, Slicing)
+               :SSColMajor<Rows,Cols,Precision,Stride,SliceHolder<Precision> 
>(p)
+               {
       }
+       };
+};
 
-      //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());
-      }
+template<int Rows, int Cols, class Precision> struct RowMajor: public 
SSRowMajor<Rows, Cols, Precision, Cols, StaticSizedAllocator<Rows*Cols, 
Precision> >
+{
+};
       
-      //Transpose goes here
-         Matrix<Cols, Rows, Precision, SSSlice<Stride>::template ColMajor> T() 
{
-               return Matrix<Cols, Rows, Precision, SSSlice<Stride>::template 
ColMajor>(my_data, Slicing());
-         }
-  };
+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 ColMajor
-  {
-    protected: 
-      Precision* my_data;
 
-    public:
-      int num_rows() const { return Rows; }
-      int num_cols() const { return Cols; }
-      //Construction
+//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
 
-      ColMajor(Precision* d)
-      :my_data(d)
+       SSRowMajor(){}
+       SSRowMajor(Precision* p)
+       :Mem(p)
       {}
 
-      //Indexing       
+       int num_rows()const {
+               return Rows;
+       }
+       int num_cols()const{
+               return Cols;
+       }
+       using Mem::my_data;
 
-      Vector<Cols, Precision, SVBase<Cols,Stride,Precision> > operator[](int 
row){
-        Internal::check_index(Rows, row);
-        return Vector<Cols, Precision, SVBase<Cols,Stride,Precision> 
>(my_data+row);
+       Precision& operator()(int r, int c){
+               Internal::check_index(Rows, r); 
+               Internal::check_index(Cols, c); 
+               return my_data[r*Stride + c];
       }
 
-      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 r, int c) const {
+               Internal::check_index(Rows, r); 
+               Internal::check_index(Cols, c); 
+               return my_data[r*Stride + c];
       }
 
-      const Precision& operator()(int row, int col) const {
-        Internal::check_index(Rows, row);
-        Internal::check_index(Cols, col);
-        return my_data[row+Stride*col];
+       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);
       }
 
-      //Slices go here
       template<int Rstart, int Cstart, int Rlength, int Clength>
-      Matrix<Rlength, Clength, Precision, SSSlice<Stride>::template ColMajor> 
slice(){
+       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 
ColMajor>(my_data + Stride*Cstart + Rstart, Slicing());
+               return Matrix<Rlength, Clength, Precision, 
SSSlice<Stride>::template RowMajor>(my_data+Stride*Rstart + Cstart, Slicing());
       }
       
-      //Transpose goes here
-         Matrix<Cols, Rows, Precision, SSSlice<Stride>::template RowMajor> T() 
{
-               return Matrix<Cols, Rows, Precision, SSSlice<Stride>::template 
RowMajor>(my_data, Slicing());
+       Matrix<Cols, Rows, Precision, SSSlice<Stride>::template ColMajor> T()
+       {
+               return Matrix<Cols, Rows, Precision, SSSlice<Stride>::template 
ColMajor>(my_data, Slicing());
          }
-       }; 
 };
 
-////////////////////////////////////////////////////////////////////////////////
-//
-// Storage base classes 
-//
-
-template<int Rows, int Cols, class Precision>
-struct RowMajor: public StaticSizedAllocator<Rows*Cols, Precision>
+template<int Rows, int Cols, class Precision, int Stride, class Mem> struct 
SSColMajor: public Mem
 {
+       SSColMajor(){}
+       SSColMajor(Precision* p)
+       :Mem(p)
+       {}
 
-  // has to handle operator[row] and operator(row,col)
-  protected:
-       using StaticSizedAllocator<Rows*Cols, Precision>::my_data;
-
-  public:
-       int num_rows() const { return Rows; }
-       int num_cols() const { return Cols; }
-
-       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);
+       int num_rows()const{
+               return Rows;
+       }
+       int num_cols()const{
+               return Cols;
        }
 
+       using Mem::my_data;
 
-       Precision& operator()(int row, int col){
-         Internal::check_index(Rows, row);
-         Internal::check_index(Cols, col);
-         return my_data[row*Cols+col];
+       Precision& operator()(int r, int c){
+               Internal::check_index(Rows, r); 
+               Internal::check_index(Cols, c); 
+               return my_data[c*Stride + r];
        }
-       const Precision& operator()(int row, int col) const {
-         Internal::check_index(Rows, row);
-         Internal::check_index(Cols, col);
-         return my_data[row*Cols+col];
+
+       const Precision& operator()(int r, int c)const{
+               Internal::check_index(Rows, r); 
+               Internal::check_index(Cols, c); 
+               return my_data[c*Stride + r];
        }
 
+       Vector<Cols, Precision, SVBase<Cols, Stride, Precision> > 
operator[](int r)
+       {
+               Internal::check_index(Rows, r); 
+               return Vector<Cols, Precision, SVBase<Cols, Stride, Precision> 
>(my_data + r);
+       }
 
        template<int Rstart, int Cstart, int Rlength, int Clength>
-       Matrix<Rlength, Clength, Precision, SSSlice<Cols>::template RowMajor> 
slice(){
+       Matrix<Rlength, Clength, Precision, SSSlice<Stride>::template ColMajor> 
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());
+               return Matrix<Rlength, Clength, Precision, 
SSSlice<Stride>::template ColMajor>(my_data+Stride*Cstart + Rstart, Slicing());
        }
        
-  //Transpose goes here
-  Matrix<Cols, Rows, Precision, SSSlice<Cols>::template ColMajor> T() {
-       return Matrix<Cols, Rows, Precision, SSSlice<Cols>::template 
ColMajor>(my_data, Slicing());
+       Matrix<Cols, Rows, Precision, SSSlice<Stride>::template RowMajor> T()
+       {
+               return Matrix<Cols, Rows, Precision, SSSlice<Stride>::template 
RowMajor>(my_data, Slicing());
   }
 };
 
-template<int Rows, int Cols, class Precision>
-struct ColMajor: public StaticSizedAllocator<Rows*Cols, Precision>
-{
 
-  // has to handle operator[row] and operator(row,col)
-  protected:
-       using StaticSizedAllocator<Rows*Cols, Precision>::my_data;
 
-  public:
-       int num_rows() const { return Rows; }
-       int num_cols() const { return Cols; }
 
-       Vector<Cols, Precision, SVBase<Cols,Rows,Precision> > operator[](int 
row){
-         Internal::check_index(Rows, row);
-         return Vector<Cols, Precision, SVBase<Cols,Rows,Precision> 
>(my_data+row);
-       }
 
 
-       Precision& operator()(int row, int col){
-         Internal::check_index(Rows, row);
-         Internal::check_index(Cols, col);
-         return my_data[row+col*Rows];
-       }
-       const Precision& operator()(int row, int col) const {
-         Internal::check_index(Rows, row);
-         Internal::check_index(Cols, col);
-         return my_data[row+col*Rows];
-       }
 
 
-       template<int Rstart, int Cstart, int Rlength, int Clength>
-       Matrix<Rlength, Clength, Precision, SSSlice<Rows>::template ColMajor> 
slice(){
-         Internal::CheckSlice<Rows, Rstart, Rlength>::check();
-         Internal::CheckSlice<Cols, Cstart, Clength>::check();
 
-         return Matrix<Rlength, Clength, Precision, SSSlice<Rows>::template 
ColMajor>(my_data + Rstart + Cstart*Rows, Slicing());
-       }
        
-  //Transpose goes here
-  Matrix<Cols, Rows, Precision, SSSlice<Rows>::template RowMajor> T() {
-       return Matrix<Cols, Rows, Precision, SSSlice<Rows>::template 
RowMajor>(my_data, Slicing());
-  }
-};
+
+




reply via email to

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