瀏覽代碼

SparseVector indexing is now over unsigned int

Alexander Freytag 9 年之前
父節點
當前提交
b4f468545d
共有 3 個文件被更改,包括 108 次插入58 次删除
  1. 48 30
      core/vector/SparseVectorT.h
  2. 29 28
      core/vector/SparseVectorT.tcc
  3. 31 0
      core/vector/tests/TestSparseVector.cpp

+ 48 - 30
core/vector/SparseVectorT.h

@@ -32,7 +32,7 @@ class SparseVectorT : public std::map<I, V>, public Persistent
 
   protected:
     //! dimension of the SparseVector, not properly implemented! FIXME
-    int dim;
+    uint dim;
     
 
   public:
@@ -42,26 +42,30 @@ class SparseVectorT : public std::map<I, V>, public Persistent
      * @param k position of the value
      * @param v value
      */
-    SparseVectorT ( I k, V v );
-    SparseVectorT ( const std::map<I, V> & mymap );
+    SparseVectorT ( I _k, 
+                    V _v
+                  );
+    SparseVectorT ( const std::map<I, V> & _mymap );
 
     /**
      * simple constructor -> does nothing
      */
-    SparseVectorT ():dim(-1) {}
+    SparseVectorT ():dim(0) {}
 
     /**
      * Constructor with the dimension
      * @param _dim dimension of the SparseVector
      */
-    SparseVectorT ( int _dim );
+    SparseVectorT ( uint _dim );
 
     /**
      * converts a ICE::Vector to a SparseVector with a tolerance factor
      * @param v input ICE::Vector
      * @param tolerance tolerance value
      */
-    SparseVectorT ( const NICE::Vector &v, double tolerance = 1e-15 );
+    SparseVectorT ( const NICE::Vector & _v, 
+                    double _tolerance = 1e-15
+                  );
 
     /**
      * simple destructor
@@ -72,44 +76,56 @@ class SparseVectorT : public std::map<I, V>, public Persistent
      * add the elements of a SparseVector to this SparseVector
      * @param v input SparseVector
      */
-    void add ( const SparseVectorT<I,V> & v );
+    void add ( const SparseVectorT<I,V> & _v );
 
     /**
      * add the elements of a SparseVector to this NICE::Vector and multiply each element with a constant lambda
      * @param v
      * @param lambda
      */
-    void add ( const SparseVectorT<I,V> & v, double lambda );
+    void add ( const SparseVectorT<I,V> & _v, 
+               double _lambda 
+             );
     
     /**
      * add to each element a constant
      * @param value
      */
-    void add ( V value );
+    void add ( V _value );
     
     /**
      * sub the elements of a SparseVector from this SparseVector
      * @param v input SparseVector
      */
-    void sub ( const SparseVectorT<I,V> & v );
+    void sub ( const SparseVectorT<I,V> & _v );
 
     /** add a sparse vector given as a STL map with integer values multiplied with a scalar to the current vector */
-    void addMap ( const std::map<int, int> & v, double lambda = 1.0 );
+    void addMap ( const std::map<uint, int> & _v, 
+                  double _lambda = 1.0
+                );
 
     /** add a sparse vector given as a STL map with double values multiplied with a scalar to the current vector */
-    void addMap ( const std::map<int, double> & v, double lambda = 1.0 );
+    void addMap ( const std::map<uint, double> & _v, 
+                  double _lambda = 1.0 
+                );
 
     /** normalize, such that all elements sum to one */
     void normalize ();
     
     /** normalize in given interval between maxv and minv */
-    void normalize (V minv, V maxv);
+    void normalize (V _minv, 
+                    V _maxv
+                   );
 
     /** read from a stream */
-    void restore ( std::istream & is, int format = 0 );
+    void restore ( std::istream & _is, 
+                   int _format = 0 
+                 );
 
     /** write to a stream */
-    void store ( std::ostream & os, int format = 0 ) const;
+    void store ( std::ostream & _os,
+                 int _format = 0 
+               ) const;
 
     /** clear the data of the vector */
     void clear ();
@@ -121,32 +137,32 @@ class SparseVectorT : public std::map<I, V>, public Persistent
      * each element of the SparseVector is multiplied by the elements of the input SparseVector
      * @param v input SparseVector
      */
-    void multiply ( const SparseVectorT<I,V> & v );
+    void multiply ( const SparseVectorT<I,V> & _v );
 
     /**
      * each element of the SparseVector is multiplied by a constant value
      * @param val factor
      */
-    void multiply ( V val );
+    void multiply ( V _val );
 
     /**
      * each element of the SparseVector is divided by the elements of the input SparseVector
      * @param v input SparseVector
      */
-    void divide ( const SparseVectorT<I,V> & v );
+    void divide ( const SparseVectorT<I,V> & _v );
 
     /**
      * each element of the SparseVector is divided by a constant value
      * @param v divisor
      */
-    void divide ( V v );
+    void divide ( V _v );
 
     /**
      * computes the inner product of two SparseVectors
      * @param v the second sparse vector
      * @return inner product
      */
-    double innerProduct ( const SparseVectorT<I,V> & v ) const;
+    double innerProduct ( const SparseVectorT<I,V> & _v ) const;
 
     /** get the sum of all elements */
     V sum () const;
@@ -164,25 +180,27 @@ class SparseVectorT : public std::map<I, V>, public Persistent
     I maxElementExclusive ( I key ) const;
 
     /** get all indices of elements with non-zero values as a sorted STL vector */
-    void getSortedIndices ( std::vector<I> & indizes ) const;
+    void getSortedIndices ( std::vector<I> & _indizes ) const;
 
     /** get an element */
-    V get ( I i ) const;
+    V get ( I _i ) const;
 
     /** set an element */
-    bool set ( I i , V newValue );
+    bool set ( I _i , 
+               V _newValue 
+             );
 
     /**
      * set the dimension of the SparseVector
      * @param _dim
      */
-    void setDim ( int _dim );
+    void setDim ( uint _dim );
 
     /**
      * returns the dimension of the SparseVector
      * @return dimension
      */
-    int getDim() const;
+    uint getDim() const;
     
     /**
     * @brief calculate the value of the minimum kernel
@@ -190,7 +208,7 @@ class SparseVectorT : public std::map<I, V>, public Persistent
     * @param b 2nd argument of the minimum kernel, which is symmetric
     * @return resulting value of the minimum kernel
     */
-    double minimumKernel ( const SparseVectorT<I,V> & b ) const;
+    double minimumKernel ( const SparseVectorT<I,V> & _b ) const;
 
     /** pick a random index by interpreting the elements of the vector as
         an unnormalized multinomial distribution */
@@ -200,12 +218,12 @@ class SparseVectorT : public std::map<I, V>, public Persistent
      * @brief computes a full NICE::VectorT from the current SparseVectorT object. the dimension has to be set properly for this method!
      * @param v resulting NICE::VectorT
      */
-    void convertToVectorT(NICE::VectorT<V> & v ) const ;     
+    void convertToVectorT(NICE::VectorT<V> & _v ) const ;     
 };
 
-typedef SparseVectorT<long, double> SparseVectorLong;
-typedef SparseVectorT<int, double> SparseVectorInt;
-typedef SparseVectorT<short, double> SparseVector;
+typedef SparseVectorT<unsigned long, double> SparseVectorLong;
+typedef SparseVectorT<unsigned int, double> SparseVectorInt;
+typedef SparseVectorT<unsigned short, double> SparseVector;
 
 } // namespace
 

+ 29 - 28
core/vector/SparseVectorT.tcc

@@ -27,20 +27,20 @@ SparseVectorT<I,V>::SparseVectorT ( const std::map<I, V> & mymap ):std::map<I, V
 template<typename I, typename V>
 SparseVectorT<I,V>::SparseVectorT ( I k, V v )
 {
-  this->insert ( std::pair<short, double> ( k, v ) );
-  dim = k;
+  this->insert ( std::pair<I, V> ( k, v ) );
+  this->dim = k;
 }
 
 template<typename I, typename V>
-SparseVectorT<I,V>::SparseVectorT ( int _dim ) : dim ( _dim )
+SparseVectorT<I,V>::SparseVectorT ( uint _dim ) : dim ( _dim )
 {
 }
 
 template<typename I, typename V>
 SparseVectorT<I,V>::SparseVectorT ( const NICE::Vector &v, double tolerance )
 {
-  dim = (int)v.size();
-  for ( int i = 0; i < dim; i++ )
+  this->dim = v.size();
+  for ( uint i = 0; i < dim; i++ )
   {
     if ( fabs ( v[i] ) > tolerance )
       this->insert ( std::pair<I, V> ( i, v[i] ) );
@@ -57,7 +57,7 @@ void SparseVectorT<I,V>::sub ( const SparseVectorT<I,V> & v )
 template<typename I, typename V>
 void SparseVectorT<I,V>::add ( const SparseVectorT<I,V> & v )
 {
-  add ( v, 1.0 );
+  this->add ( v, 1.0 );
 }
 
 template<typename I, typename V>
@@ -326,16 +326,16 @@ void SparseVectorT<I,V>::normalize ( V minv, V maxv )
 }
 
 template<typename I, typename V>
-void SparseVectorT<I,V>::addMap ( const std::map<int, int> & v, double lambda )
+void SparseVectorT<I,V>::addMap ( const std::map<uint, int> & v, double lambda )
 {
-  for ( std::map<int, int>::const_iterator v_it = v.begin(); v_it != v.end(); v_it++ )
+  for ( std::map<uint, int>::const_iterator v_it = v.begin(); v_it != v.end(); v_it++ )
     ( *this ) [(I)v_it->first] += (V)v_it->second * lambda;
 }
 
 template<typename I, typename V>
-void SparseVectorT<I,V>::addMap ( const std::map<int, double> & v, double lambda )
+void SparseVectorT<I,V>::addMap ( const std::map<uint, double> & v, double lambda )
 {
-  for ( std::map<int, double>::const_iterator v_it = v.begin(); v_it != v.end(); v_it++ )
+  for ( std::map<uint, double>::const_iterator v_it = v.begin(); v_it != v.end(); v_it++ )
     ( *this ) [(I)v_it->first] += (V)v_it->second * lambda;
 }
 
@@ -456,15 +456,15 @@ bool SparseVectorT<I,V>::set ( I i , V newValue )
 }
 
 template<typename I, typename V>
-void SparseVectorT<I,V>::setDim ( int _dim )
+void SparseVectorT<I,V>::setDim ( uint _dim )
 {
-  dim = _dim;
+  this->dim = _dim;
 }
 
 template<typename I, typename V>
-int SparseVectorT<I,V>::getDim() const
+uint SparseVectorT<I,V>::getDim() const
 {
-  return dim;
+  return this->dim;
 }
 
 template<typename I, typename V>
@@ -530,16 +530,17 @@ I SparseVectorT<I,V>::pickRandomSample() const
 template<typename I, typename V>
 void SparseVectorT<I,V>::convertToVectorT(NICE::VectorT<V> & v ) const
 {
-  int dimension (this->getDim());
-  //dimension flag was not set properly
-  if (dimension < 0)
-  {
-    typename SparseVectorT<I,V>::const_iterator i = this->end(); i--;
-    int dist (distance(this->begin(), this->end()) );
-    if (dist > 0)
-      dimension = i->first+1; //plus one, since this is the index, but we need the resulting size
-    //we're not allowed here to set the dimension flag, since we want to have this method to be a const one
-  }
+  uint dimension ( this->getDim() );
+
+  // if dimension is zero, it could either be the case that the sparse vector is empty, or that the dimension flag was not set properly. 
+  // thus, let's check out the largest dimension
+  // could be removed later... only needed for backwards compatibility
+  typename SparseVectorT<I,V>::const_iterator svIt = this->end(); 
+  svIt--;
+  uint dist (distance(this->begin(), this->end()) );
+  if (dist > 0)
+    dimension = svIt->first+1; //plus one, since this is the index, but we need the resulting size
+  //we're not allowed here to set the dimension flag, since we want to have this method to be a const one
 
   //our sparse vector is empty
   if (dimension <= 0)
@@ -556,12 +557,12 @@ void SparseVectorT<I,V>::convertToVectorT(NICE::VectorT<V> & v ) const
   v.set( (V) 0.0);
 
   //add the actual content
-  typename SparseVectorT<I,V>::const_iterator i = this->begin();
-  for ( ; i != this->end(); i++ )
+  svIt = this->begin();
+  for ( ; svIt != this->end(); svIt++ )
   {
     //just to be sure that we do not get some errors due to badly set dimension flags
-    if (i->first < dimension)
-      v[i->first] = i->second;
+    if (svIt->first < dimension)
+      v[svIt->first] = svIt->second;
   }
 }
 

+ 31 - 0
core/vector/tests/TestSparseVector.cpp

@@ -19,6 +19,8 @@ CPPUNIT_TEST_SUITE_REGISTRATION( TestSparseVector );
 using namespace NICE;
 using namespace std;
 
+const bool b_verbose = false;
+
 void TestSparseVector::testProducts()
 {
   SparseVector v1;
@@ -41,7 +43,13 @@ void TestSparseVector::testProducts()
   SparseVector v3;
   v3.restore ( iss );
   
+  if ( b_verbose )
+    std::cerr << "TestSparseVector::testProducts -- check1 v3 == v1 " << std::endl;
+  
   CPPUNIT_ASSERT ( v3 == v1 );
+  
+  if ( b_verbose )
+    std::cerr << "TestSparseVector::testProducts -- check1 v3 == v1 passed" << std::endl;
 
   //cerr << "Testing SparseVector::FORMAT_INDEX_LINE" << endl;
   stringstream ss;
@@ -50,8 +58,14 @@ void TestSparseVector::testProducts()
   v3.clear();
   v3.restore ( ss, SparseVector::FORMAT_INDEX_LINE );
 
+  if ( b_verbose )
+    std::cerr << "TestSparseVector::testProducts -- check2 v3 == v1 " << std::endl;
+  
   CPPUNIT_ASSERT ( v3 == v1 );
   
+  if ( b_verbose )
+    std::cerr << "TestSparseVector::testProducts -- check2 v3 == v1 passed" << std::endl;
+  
   //cerr << "Testing SparseVector::FORMAT_INDEX" << endl;
   stringstream ss2;
   v1.store( ss2, SparseVector::FORMAT_INDEX );
@@ -59,7 +73,13 @@ void TestSparseVector::testProducts()
   v3.clear();
   v3.restore ( ss2, SparseVector::FORMAT_INDEX );
 
+  if ( b_verbose )
+    std::cerr << "TestSparseVector::testProducts -- check3 v3 == v1" << std::endl;
+  
   CPPUNIT_ASSERT ( v3 == v1 );
+  
+  if ( b_verbose )
+    std::cerr << "TestSparseVector::testProducts -- check3 v3 == v1 passed" << std::endl;
 
 }
 
@@ -77,7 +97,18 @@ void TestSparseVector::testConversionToVectorT()
   NICE::Vector vGT(5);
   vGT[0] = 0.5;vGT[1] = 1.0;vGT[2] = 0.0;vGT[3] = 0.1;vGT[4] = 2.0;
    
+  if ( b_verbose )
+  {
+    std::cerr << "TestSparseVector::testConversionToVectorT -- v == vGT" << std::endl;
+  
+    std::cerr << v << std::endl;
+    std::cerr << vGT << std::endl;
+  }
+  
   CPPUNIT_ASSERT ( v == vGT );
+  
+  if ( b_verbose )
+    std::cerr << "TestSparseVector::testConversionToVectorT -- v == vGT passed" << std::endl;
 }
 
 #endif