mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-13 00:03:09 -04:00
Create hash method for QgsPoint (Fix #8775)
This commit is contained in:
parent
231f4e8444
commit
44b7767134
@ -126,4 +126,9 @@ class QgsPoint
|
||||
PyErr_SetString(PyExc_IndexError, msg.toAscii().constData());
|
||||
}
|
||||
%End
|
||||
|
||||
long __hash__() const;
|
||||
%MethodCode
|
||||
sipRes = qHash( *sipCpp );
|
||||
%End
|
||||
}; // class QgsPoint
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user