fixed a few compile problems in SparseMat code; fixed iteration over an empty sparse...
authorVadim Pisarevsky <no@email>
Tue, 27 Mar 2012 14:43:24 +0000 (14:43 +0000)
committerVadim Pisarevsky <no@email>
Tue, 27 Mar 2012 14:43:24 +0000 (14:43 +0000)
modules/core/include/opencv2/core/mat.hpp
modules/core/src/matrix.cpp
modules/core/test/test_operations.cpp

index ee3a3af..427952d 100644 (file)
@@ -2294,7 +2294,7 @@ inline SparseMatConstIterator::SparseMatConstIterator(const SparseMatConstIterat
 }
 
 static inline bool operator == (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2)
-{ return it1.m == it2.m && it1.hashidx == it2.hashidx && it1.ptr == it2.ptr; }
+{ return it1.m == it2.m && it1.ptr == it2.ptr; }
 
 static inline bool operator != (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2)
 { return !(it1 == it2); }
@@ -2527,7 +2527,9 @@ SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMatConstIterat
 
 template<typename _Tp> inline SparseMatConstIterator_<_Tp>&
 SparseMatConstIterator_<_Tp>::operator = (const SparseMatConstIterator_<_Tp>& it)
-{ return ((SparseMatConstIterator&)*this = it); }
+{ return reinterpret_cast<SparseMatConstIterator_<_Tp>&>
+    (*reinterpret_cast<SparseMatConstIterator*>(this) =
+     reinterpret_cast<const SparseMatConstIterator&>(it)); }
 
 template<typename _Tp> inline const _Tp&
 SparseMatConstIterator_<_Tp>::operator *() const
@@ -2564,7 +2566,9 @@ SparseMatIterator_<_Tp>::SparseMatIterator_(const SparseMatIterator_<_Tp>& it)
 
 template<typename _Tp> inline SparseMatIterator_<_Tp>&
 SparseMatIterator_<_Tp>::operator = (const SparseMatIterator_<_Tp>& it)
-{ return ((SparseMatIterator&)*this = it); }
+{ return reinterpret_cast<SparseMatIterator_<_Tp>&>
+    (*reinterpret_cast<SparseMatConstIterator*>(this) =
+     reinterpret_cast<const SparseMatConstIterator&>(it)); }
 
 template<typename _Tp> inline _Tp&
 SparseMatIterator_<_Tp>::operator *() const
index c38ff2b..ff9714a 100644 (file)
@@ -3506,6 +3506,28 @@ SparseMat::operator CvSparseMat*() const
     return m;
 }
 
+uchar* SparseMat::ptr(int i0, bool createMissing, size_t* hashval)
+{
+    CV_Assert( hdr && hdr->dims == 1 );
+    size_t h = hashval ? *hashval : hash(i0);
+    size_t hidx = h & (hdr->hashtab.size() - 1), nidx = hdr->hashtab[hidx];
+    uchar* pool = &hdr->pool[0];
+    while( nidx != 0 )
+    {
+        Node* elem = (Node*)(pool + nidx);
+        if( elem->hashval == h && elem->idx[0] == i0 )
+            return &value<uchar>(elem);
+        nidx = elem->next;
+    }
+    
+    if( createMissing )
+    {
+        int idx[] = { i0 };
+        return newNode( idx, h );
+    }
+    return 0;
+}
+    
 uchar* SparseMat::ptr(int i0, int i1, bool createMissing, size_t* hashval)
 {
     CV_Assert( hdr && hdr->dims == 2 );
index 6e7968f..8528fc3 100644 (file)
@@ -885,3 +885,92 @@ void CV_OperationsTest::run( int /* start_from */)
 }
 
 TEST(Core_Array, expressions) { CV_OperationsTest test; test.safe_run(); }
+
+class CV_SparseMatTest : public cvtest::BaseTest
+{
+public:
+    CV_SparseMatTest() {}
+    ~CV_SparseMatTest() {}   
+protected:
+    void run(int)
+    {
+        try
+        {
+            RNG& rng = theRNG();
+            const int MAX_DIM=3;
+            int sizes[MAX_DIM], idx[MAX_DIM];
+            for( int iter = 0; iter < 100; iter++ )
+            {
+                ts->printf(cvtest::TS::LOG, ".");
+                ts->update_context(this, iter, true);
+                int k, dims = rng.uniform(1, MAX_DIM+1), p = 1;
+                for( k = 0; k < dims; k++ )
+                {
+                    sizes[k] = rng.uniform(1, 30);
+                    p *= sizes[k];
+                }
+                int j, nz = rng.uniform(0, (p+2)/2), nz0 = 0;
+                SparseMat_<int> v(dims,sizes);
+                
+                CV_Assert( (int)v.nzcount() == 0 );
+                
+                SparseMatIterator_<int> it = v.begin();
+                SparseMatIterator_<int> it_end = v.end();
+                
+                for( k = 0; it != it_end; ++it, ++k )
+                    ;
+                CV_Assert( k == 0 );
+                
+                int sum0 = 0, sum = 0;
+                for( j = 0; j < nz; j++ )
+                {
+                    int val = rng.uniform(1, 100);
+                    for( k = 0; k < dims; k++ )
+                        idx[k] = rng.uniform(0, sizes[k]);
+                    if( dims == 1 )
+                    {
+                        CV_Assert( v.ref(idx[0]) == v(idx[0]) );
+                    }
+                    else if( dims == 2 )
+                    {
+                        CV_Assert( v.ref(idx[0], idx[1]) == v(idx[0], idx[1]) );
+                    }
+                    else if( dims == 3 )
+                    {
+                        CV_Assert( v.ref(idx[0], idx[1], idx[2]) == v(idx[0], idx[1], idx[2]) );
+                    }
+                    CV_Assert( v.ref(idx) == v(idx) );
+                    v.ref(idx) += val;
+                    if( v(idx) == val )
+                        nz0++;
+                    sum0 += val;
+                }
+            
+                CV_Assert( (int)v.nzcount() == nz0 );
+                
+                it = v.begin();
+                it_end = v.end();
+                
+                for( k = 0; it != it_end; ++it, ++k )
+                    sum += *it;
+                CV_Assert( k == nz0 && sum == sum0 );
+                
+                v.clear();
+                CV_Assert( (int)v.nzcount() == 0 );
+                
+                it = v.begin();
+                it_end = v.end();
+                
+                for( k = 0; it != it_end; ++it, ++k )
+                    ;
+                CV_Assert( k == 0 );
+            }
+        }
+        catch(...)
+        {
+            ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
+        }
+    }
+};
+
+TEST(Core_SparseMat, iterations) { CV_SparseMatTest test; test.safe_run(); }