__gnu_cxx::hash_map question, please help!!!
Working for a while with the gnu hash map I found that I cannot remove
elements from the map whereas if I use a std::map there are no
problems. Is this a bug of the implementation or am I doing something
wrong?
The function is very simple, and I added some code so I can use the
hash map with std::pair as keys (which are not given in the usual
keys).
The code I wrote for this is as follows:
namespace __gnu_cxx {
//! Structure used for hash tables of std::pair objects
template<> struct hash<std::pair<size_t,size_t> > {
size_t operator()(std::pair<size_t,size_t> __p) const {
std::size_t seed = 0;
boost::hash_combine(seed, __p.first);
boost::hash_combine(seed, __p.second);
return seed;
}
};
}
and then my class:
template<typename Value = double, typename Key =
std::pair<size_t,size_t>,
class ContainerPolicy = __gnu_cxx::hash_map<Key,Value> > class
SparseMatrix {
....
// some type defs, constructors, etc, etc...
....
....
void clean(ValueType);
};
template<typename V, typename K, class C> int SparseMatrix<V, K,
C>::clean(V tol) {
size_t count(0);
for(IteratorType it = values_.begin(); it!=values_.end(); ++it){
if(std::abs((*this)(it->first.first,it->first.second)) < tol ) {
// remove element from matrix
cout<<"erasing ("<<it->first.first<<","<<it->first.second<<") =
"<<(*this)(it->first.first,it->first.second)<<endl;
int what = values_.erase(it->first);
cout<<"what -> "<<what<<endl;
++count;
}
}
return count;
}
The function clean just removes those elements from the matrix that
are smaller than a tolerance. Now, the output of using this function
with a 15x15 matrix is
erasing (0,7) = 0
what -> 1
erasing (6,1) = 2.27e-13
what -> 1
terminate called after throwing an instance of 'fea::RuntimeError'
what(): *** ERROR *** Matrix index (24,0) out of bounds.
The sparse matrix has 15 rows and 15 columns.
I throw a runtime error when the access to the matrix is out of
bounds. But the out of bounds happens only when removing an element!
So the only thing that comes to my mind, is that there is a bug in the
implementation of the hash table. Is it correct???? removing and
element from the hash table shouldn't invalidate the iterators, right?
a=B2