Skip to content

Commit

Permalink
Create hash method for QgsPoint (Fix #8775)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Oct 12, 2013
1 parent 231f4e8 commit 44b7767
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python/core/qgspoint.sip
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,9 @@ class QgsPoint
PyErr_SetString(PyExc_IndexError, msg.toAscii().constData());
}
%End

long __hash__() const;
%MethodCode
sipRes = qHash( *sipCpp );
%End
}; // class QgsPoint
10 changes: 10 additions & 0 deletions src/core/qgspoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class CORE_EXPORT QgsPoint
//! y coordinate
double m_y;

friend uint qHash( const QgsPoint& pnt );

}; // class QgsPoint

Expand All @@ -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
11 changes: 11 additions & 0 deletions tests/src/python/test_qgspoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.