Skip to content
Permalink
Browse files

Create hash method for QgsPoint (Fix #8775)

  • Loading branch information
m-kuhn committed Oct 12, 2013
1 parent 231f4e8 commit 44b7767134e442b95b6d99a1cbe612d2aeb856c7
Showing with 26 additions and 0 deletions.
  1. +5 −0 python/core/qgspoint.sip
  2. +10 −0 src/core/qgspoint.h
  3. +11 −0 tests/src/python/test_qgspoint.py
@@ -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 @@ def test_pointToString(self):
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()

0 comments on commit 44b7767

Please sign in to comment.
You can’t perform that action at this time.