toon-members
[Top][All Lists]
Advanced

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

[Toon-members] TooN helpers.h


From: Christopher Mei
Subject: [Toon-members] TooN helpers.h
Date: Thu, 30 Jul 2009 13:51:51 +0000

CVSROOT:        /sources/toon
Module name:    TooN
Changes by:     Christopher Mei <cmei>  09/07/30 13:51:51

Modified files:
        .              : helpers.h 

Log message:
        min/max functions now return values instead of references.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/helpers.h?cvsroot=toon&r1=1.78&r2=1.79

Patches:
Index: helpers.h
===================================================================
RCS file: /sources/toon/TooN/helpers.h,v
retrieving revision 1.78
retrieving revision 1.79
diff -u -b -r1.78 -r1.79
--- helpers.h   10 Jul 2009 16:21:49 -0000      1.78
+++ helpers.h   30 Jul 2009 13:51:51 -0000      1.79
@@ -471,14 +471,16 @@
         };
         template<typename Precision, typename ComparisonFunctor>
         class accumulate_vertical_functor {
-            Vector<Dynamic,Precision>& bestVal;
+            Vector<Dynamic,Precision>* bestVal;
         public:
-            accumulate_vertical_functor() :
-                bestVal( *(new Vector<Dynamic,Precision>( 0 )) ) {}
-            accumulate_vertical_functor( int nNumCols ) :
-                bestVal( *(new Vector<Dynamic,Precision>( nNumCols )) ) {}
-            Vector<Dynamic,Precision>& null() {
-                return ret();
+            accumulate_vertical_functor() {
+                bestVal = NULL;
+            }
+            accumulate_vertical_functor( int nNumCols ) {
+                bestVal = new Vector<Dynamic,Precision>( nNumCols );
+            }
+            Vector<Dynamic,Precision> null() {
+                return Vector<Dynamic,Precision>();
             }
             void initialise( Precision initialVal, int nRow, int nCol ) {
                 bestVal[nCol] = initialVal;
@@ -488,23 +490,30 @@
                     bestVal[nCol] = curVal;
                 }
             }
-            Vector<Dynamic,Precision>& ret() { return bestVal; }            
+            Vector<Dynamic,Precision> ret() {
+                if( bestVal == NULL ) {
+                    return null();
+                }
+                Vector<Dynamic,Precision> vRet = *bestVal; 
+                delete bestVal;
+                return vRet;
+            }            
         };
         template<typename Precision, typename ComparisonFunctor>
         class accumulate_element_vertical_functor {
-            Vector<Dynamic,Precision>& bestVal;
-            Vector<Dynamic,Precision>& bestIndices;
+            Vector<Dynamic,Precision>* bestVal;
+            Vector<Dynamic,Precision>* bestIndices;
         public:
-            accumulate_element_vertical_functor() :
-                bestVal( *(new Vector<Dynamic,Precision>( 0 )) ),
-                bestIndices( *(new Vector<Dynamic,Precision>( 0 )) )
-            {}
-            accumulate_element_vertical_functor( int nNumCols ) :
-                bestVal( *(new Vector<Dynamic,Precision>( nNumCols )) ),
-                bestIndices( *(new Vector<Dynamic,Precision>( nNumCols )) )
-            {}
-            std::pair<Vector<Dynamic,Precision>&,Vector<Dynamic,Precision>&> 
null() {
-                return ret();
+            accumulate_element_vertical_functor() {
+                bestVal = NULL;
+                bestIndices = NULL;
+            }
+            accumulate_element_vertical_functor( int nNumCols ) {
+                bestVal = new Vector<Dynamic,Precision>( nNumCols );
+                bestIndices = new Vector<Dynamic,Precision>( nNumCols );
+            }
+            std::pair<Vector<Dynamic,Precision>,Vector<Dynamic,Precision> > 
null() {
+                return 
std::pair<Vector<Dynamic,Precision>,Vector<Dynamic,Precision> >();
             }
             void initialise( Precision initialVal, int nRow, int nCol ) {
                 bestVal[nCol] = initialVal;
@@ -516,20 +525,29 @@
                     bestIndices[nCol] = nRow;
                 }
             }
-            std::pair<Vector<Dynamic,Precision>&,Vector<Dynamic,Precision>&> 
ret() {
-                return 
std::pair<Vector<Dynamic,Precision>&,Vector<Dynamic,Precision>&>( bestVal, 
bestIndices );
+            std::pair<Vector<Dynamic,Precision>,Vector<Dynamic,Precision> > 
ret() {
+                if( bestVal == NULL ) {
+                    retrurn null();
+                }
+                std::pair<Vector<Dynamic,Precision>,Vector<Dynamic,Precision> 
> vRet = 
+                    
std::pair<Vector<Dynamic,Precision>,Vector<Dynamic,Precision> > (*bestVal, 
*bestIndices );
+                delete bestVal; bestVal = NULL;
+                delete bestIndices; bestIndices = NULL;
+                return vRet;
             }            
         };
         template<typename Precision, typename ComparisonFunctor>
         class accumulate_horizontal_functor {
-            Vector<Dynamic,Precision>& bestVal;
+            Vector<Dynamic,Precision>* bestVal;
         public: 
-            accumulate_horizontal_functor() :
-                bestVal( *(new Vector<Dynamic,Precision>( 0 )) ) {}
-            accumulate_horizontal_functor( int nNumRows ) :
-                bestVal( *(new Vector<Dynamic,Precision>( nNumRows )) ) {}
-            Vector<Dynamic,Precision>& null() {
-                return bestVal;
+            accumulate_horizontal_functor() {
+                bestVal = NULL;
+            }
+            accumulate_horizontal_functor( int nNumRows ) {
+                bestVal = new Vector<Dynamic,Precision>( nNumRows );
+            }
+            Vector<Dynamic,Precision> null() {
+                return Vector<Dynamic,Precision>();
             }
             void initialise( Precision initialVal, int nRow, int nCol ) {
                 bestVal[nRow] = initialVal;
@@ -539,21 +557,30 @@
                     bestVal[nRow] = curVal;
                 }
             }
-            Vector<Dynamic,Precision>& ret() { return bestVal; }            
+            Vector<Dynamic,Precision> ret() { 
+                if( bestVal == NULL ) {
+                    return null();
+                }
+                Vector<Dynamic,Precision> vRet = *bestVal;
+                delete bestVal; bestVal = NULL;
+                return vRet; 
+            }            
         };
         template<typename Precision, typename ComparisonFunctor>
         class accumulate_element_horizontal_functor {
-            Vector<Dynamic,Precision>& bestVal;
-            Vector<Dynamic,Precision>& bestIndices;
+            Vector<Dynamic,Precision>* bestVal;
+            Vector<Dynamic,Precision>* bestIndices;
         public:
-            accumulate_element_horizontal_functor() :
-                bestVal( *(new Vector<Dynamic,Precision>( 0 )) ),
-                bestIndices( *(new Vector<Dynamic,Precision>( 0 )) ) {}
-            accumulate_element_horizontal_functor( int nNumRows ) :
-                bestVal( *(new Vector<Dynamic,Precision>( nNumRows )) ),
-                bestIndices( *(new Vector<Dynamic,Precision>( nNumRows )) ) {}
-            std::pair<Vector<Dynamic,Precision>&,Vector<Dynamic,Precision>&> 
null() {
-                return ret();
+            accumulate_element_horizontal_functor() {
+                bestVal = NULL;
+                bestIndices = NULL;
+            }
+            accumulate_element_horizontal_functor( int nNumRows ) {
+                bestVal = new Vector<Dynamic,Precision>( nNumRows );
+                bestIndices = new Vector<Dynamic,Precision>( nNumRows );
+            }
+            std::pair<Vector<Dynamic,Precision>,Vector<Dynamic,Precision> > 
null() {
+                return 
std::pair<Vector<Dynamic,Precision>,Vector<Dynamic,Precision> >();
             }
             void initialise( Precision initialVal, int nRow, int nCol ) {
                 bestVal[nRow] = initialVal;
@@ -565,8 +592,15 @@
                     bestIndices[nRow] = nCol;
                 }
             }
-            std::pair<Vector<Dynamic,Precision>&,Vector<Dynamic,Precision>&> 
ret() {
-                return 
std::pair<Vector<Dynamic,Precision>&,Vector<Dynamic,Precision>&>( bestVal, 
bestIndices );
+            std::pair<Vector<Dynamic,Precision>,Vector<Dynamic,Precision> > 
ret() {
+                if( bestVal == NULL ) {
+                    return null();
+                }
+                std::pair<Vector<Dynamic,Precision>,Vector<Dynamic,Precision> 
> vRet = 
+                    
std::pair<Vector<Dynamic,Precision>,Vector<Dynamic,Precision>>( *bestVal, 
*bestIndices );
+                delete bestVal; bestVal = NULL;
+                delete bestIndices; bestIndices = NULL;
+                return vRet;
             }            
         };
     }
@@ -606,34 +640,34 @@
        /// Finds the smallest values of each column of a matrix.
        /// @param m a matrix
        /// @return a vector of size C
-    template<int R, int C, typename Precision, typename Base> inline 
Vector<Dynamic,Precision>& min_vertical( const Matrix<R, C, Precision, Base> & 
m) {
+    template<int R, int C, typename Precision, typename Base> inline 
Vector<Dynamic,Precision> min_vertical( const Matrix<R, C, Precision, Base> & 
m) {
         typedef 
Internal::accumulate_vertical_functor<Precision,std::less<Precision> > 
matrix_accumulate_vertical_functor;
         return Internal::accumulate_vertical<R,C,Precision,Base,
-            matrix_accumulate_vertical_functor, Vector<Dynamic,Precision>& >( 
m );
+            matrix_accumulate_vertical_functor, Vector<Dynamic,Precision> >( m 
);
     }
        /// Finds the largest values of each column of a matrix.
        /// @param m a matrix
        /// @return a vector of size C
-    template<int R, int C, typename Precision, typename Base> inline 
Vector<Dynamic,Precision>& max_vertical( const Matrix<R, C, Precision, Base> & 
m) {
+    template<int R, int C, typename Precision, typename Base> inline 
Vector<Dynamic,Precision> max_vertical( const Matrix<R, C, Precision, Base> & 
m) {
         typedef 
Internal::accumulate_vertical_functor<Precision,std::greater<Precision> > 
matrix_accumulate_vertical_functor;
         return Internal::accumulate_vertical<R,C,Precision,Base,
-            matrix_accumulate_vertical_functor, Vector<Dynamic,Precision>& >( 
m );
+            matrix_accumulate_vertical_functor, Vector<Dynamic,Precision> >( m 
);
     }
        /// Finds the smallest values of each row of a matrix.
        /// @param m a matrix
        /// @return a vector of size R
-    template<int R, int C, typename Precision, typename Base> inline 
Vector<Dynamic,Precision>& min_horizontal( const Matrix<R, C, Precision, Base> 
& m) {
+    template<int R, int C, typename Precision, typename Base> inline 
Vector<Dynamic,Precision> min_horizontal( const Matrix<R, C, Precision, Base> & 
m) {
         typedef 
Internal::accumulate_horizontal_functor<Precision,std::less<Precision> > 
matrix_accumulate_horizontal_functor;
         return Internal::accumulate_horizontal<R,C,Precision,Base,
-            matrix_accumulate_horizontal_functor, Vector<Dynamic,Precision>& 
>( m );
+            matrix_accumulate_horizontal_functor, Vector<Dynamic,Precision> >( 
m );
     }
        /// Finds the largest values of each row of a matrix.
        /// @param m a matrix
        /// @return a vector of size R
-    template<int R, int C, typename Precision, typename Base> inline 
Vector<Dynamic,Precision>& max_horizontal( const Matrix<R, C, Precision, Base> 
& m) {
+    template<int R, int C, typename Precision, typename Base> inline 
Vector<Dynamic,Precision> max_horizontal( const Matrix<R, C, Precision, Base> & 
m) {
         typedef 
Internal::accumulate_horizontal_functor<Precision,std::greater<Precision> > 
matrix_accumulate_horizontal_functor;
         return Internal::accumulate_horizontal<R,C,Precision,Base,
-            matrix_accumulate_horizontal_functor, Vector<Dynamic,Precision>& 
>( m );
+            matrix_accumulate_horizontal_functor, Vector<Dynamic,Precision> >( 
m );
     }
        /// Finds the smallest value of a vector and its index.
        /// @param v a vector
@@ -676,9 +710,9 @@
        /// @param m a matrix
        /// @return a pair of vectors of size C containg the values and
        /// their indices
-    template<int R, int C, typename Precision, typename Base> inline 
std::pair<Vector<Dynamic,Precision>&,Vector<Dynamic,Precision>&> 
min_element_vertical( const Matrix<R, C, Precision, Base> & m) {
+    template<int R, int C, typename Precision, typename Base> inline 
std::pair<Vector<Dynamic,Precision>,Vector<Dynamic,Precision> > 
min_element_vertical( const Matrix<R, C, Precision, Base> & m) {
         typedef 
Internal::accumulate_element_vertical_functor<Precision,std::less<Precision> > 
matrix_accumulate_vertical_functor;
-        typedef std::pair<Vector< Dynamic, Precision >&,Vector< Dynamic, 
Precision >&> Ret;
+        typedef std::pair<Vector< Dynamic, Precision >,Vector< Dynamic, 
Precision > > Ret;
         return Internal::accumulate_vertical<R,C,Precision,Base,
             matrix_accumulate_vertical_functor, Ret >( m );
     }
@@ -687,9 +721,9 @@
        /// @param m a matrix
        /// @return a pair of vectors of size C containg the values and
        /// their indices
-    template<int R, int C, typename Precision, typename Base> inline 
std::pair<Vector< Dynamic, Precision >&,Vector< Dynamic, Precision >&> 
max_element_vertical( const Matrix<R, C, Precision, Base> & m) {
+    template<int R, int C, typename Precision, typename Base> inline 
std::pair<Vector< Dynamic, Precision >,Vector< Dynamic, Precision > > 
max_element_vertical( const Matrix<R, C, Precision, Base> & m) {
         typedef 
Internal::accumulate_element_vertical_functor<Precision,std::greater<Precision> 
> matrix_accumulate_vertical_functor;
-        typedef std::pair<Vector< Dynamic, Precision >&,Vector< Dynamic, 
Precision >&> Ret;
+        typedef std::pair<Vector< Dynamic, Precision >,Vector< Dynamic, 
Precision > > Ret;
         return Internal::accumulate_vertical<R,C,Precision,Base,
             matrix_accumulate_vertical_functor, Ret >( m );
     }
@@ -698,9 +732,9 @@
        /// @param m a matrix
        /// @return a pair of vectors of size R containg the values and
        /// their indices
-    template<int R, int C, typename Precision, typename Base> inline 
std::pair<Vector< Dynamic, Precision >&,Vector< Dynamic, Precision >&> 
min_element_horizontal( const Matrix<R, C, Precision, Base> & m) {
+    template<int R, int C, typename Precision, typename Base> inline 
std::pair<Vector< Dynamic, Precision >,Vector< Dynamic, Precision > > 
min_element_horizontal( const Matrix<R, C, Precision, Base> & m) {
         typedef 
Internal::accumulate_element_horizontal_functor<Precision,std::less<Precision> 
> matrix_accumulate_vertical_functor;
-        typedef std::pair<Vector< Dynamic, Precision >&,Vector< Dynamic, 
Precision >&> Ret;
+        typedef std::pair<Vector< Dynamic, Precision >,Vector< Dynamic, 
Precision > > Ret;
         return Internal::accumulate_horizontal<R,C,Precision,Base,
             matrix_accumulate_vertical_functor, Ret >( m );
     }
@@ -709,9 +743,9 @@
        /// @param m a matrix
        /// @return a pair of vectors of size R containg the values and
        /// their indices
-    template<int R, int C, typename Precision, typename Base> inline 
std::pair<Vector< Dynamic, Precision >&,Vector< Dynamic, Precision >&> 
max_element_horizontal( const Matrix<R, C, Precision, Base> & m) {
+    template<int R, int C, typename Precision, typename Base> inline 
std::pair<Vector< Dynamic, Precision >,Vector< Dynamic, Precision > > 
max_element_horizontal( const Matrix<R, C, Precision, Base> & m) {
         typedef 
Internal::accumulate_element_horizontal_functor<Precision,std::greater<Precision>
 > matrix_accumulate_vertical_functor;
-        typedef std::pair<Vector< Dynamic, Precision >&,Vector< Dynamic, 
Precision >&> Ret;
+        typedef std::pair<Vector< Dynamic, Precision >,Vector< Dynamic, 
Precision > > Ret;
         return Internal::accumulate_horizontal<R,C,Precision,Base,
             matrix_accumulate_vertical_functor, Ret >( m );
     }




reply via email to

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