Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Create hash method for QgsPoint (Fix #8775)
  • 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
Expand Up @@ -126,4 +126,9 @@ class QgsPoint
PyErr_SetString(PyExc_IndexError, msg.toAscii().constData()); PyErr_SetString(PyExc_IndexError, msg.toAscii().constData());
} }
%End %End

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


friend uint qHash( const QgsPoint& pnt );


}; // class QgsPoint }; // class QgsPoint


Expand All @@ -212,4 +213,13 @@ inline std::ostream& operator << ( std::ostream& os, const QgsPoint &p )
return os; 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 #endif //QGSPOINT_H
11 changes: 11 additions & 0 deletions tests/src/python/test_qgspoint.py
Expand Up @@ -49,6 +49,17 @@ def test_pointToString(self):
myMessage = 'Expected: %s Got: %s' % (myExpectedValue, myActualValue) myMessage = 'Expected: %s Got: %s' % (myExpectedValue, myActualValue)
assert myExpectedValue == myActualValue, myMessage 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__': if __name__ == '__main__':
unittest.main() unittest.main()

0 comments on commit 44b7767

Please sign in to comment.