Create hash method for QgsPoint (Fix #8775)

This commit is contained in:
Matthias Kuhn 2013-10-12 11:22:33 +02:00
parent 231f4e8444
commit 44b7767134
3 changed files with 26 additions and 0 deletions

View File

@ -126,4 +126,9 @@ class QgsPoint
PyErr_SetString(PyExc_IndexError, msg.toAscii().constData());
}
%End
long __hash__() const;
%MethodCode
sipRes = qHash( *sipCpp );
%End
}; // class QgsPoint

View File

@ -193,6 +193,7 @@ class CORE_EXPORT QgsPoint
//! y coordinate
double m_y;
friend uint qHash( const QgsPoint& pnt );
}; // class QgsPoint
@ -212,4 +213,13 @@ inline std::ostream& operator << ( std::ostream& os, const QgsPoint &p )
return os;
}
inline uint qHash( const QgsPoint& p )
{
uint hash;
uint h1 = qHash(( quint64 )p.m_x );
uint h2 = qHash(( quint64 )p.m_y );
hash = h1 ^( h2 << 1 );
return hash;
}
#endif //QGSPOINT_H

View File

@ -49,6 +49,17 @@ class TestQgsPoint(TestCase):
myMessage = 'Expected: %s Got: %s' % (myExpectedValue, myActualValue)
assert myExpectedValue == myActualValue, myMessage
def test_hash(self):
a = QgsPoint( 2.0, 1.0 )
b = QgsPoint( 2.0, 2.0 )
c = QgsPoint( 1.0, 2.0 )
d = QgsPoint( 1.0, 1.0 )
e = QgsPoint( 2.0, 1.0 )
assert a.__hash__() != b.__hash__()
assert e.__hash__() == a.__hash__()
mySet = set( [ a, b, c, d, e ] )
assert len( mySet ) == 4
if __name__ == '__main__':
unittest.main()