Skip to content

Commit 44b7767

Browse files
committedOct 12, 2013
Create hash method for QgsPoint (Fix #8775)
1 parent 231f4e8 commit 44b7767

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed
 

‎python/core/qgspoint.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,9 @@ class QgsPoint
126126
PyErr_SetString(PyExc_IndexError, msg.toAscii().constData());
127127
}
128128
%End
129+
130+
long __hash__() const;
131+
%MethodCode
132+
sipRes = qHash( *sipCpp );
133+
%End
129134
}; // class QgsPoint

‎src/core/qgspoint.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ class CORE_EXPORT QgsPoint
193193
//! y coordinate
194194
double m_y;
195195

196+
friend uint qHash( const QgsPoint& pnt );
196197

197198
}; // class QgsPoint
198199

@@ -212,4 +213,13 @@ inline std::ostream& operator << ( std::ostream& os, const QgsPoint &p )
212213
return os;
213214
}
214215

216+
inline uint qHash( const QgsPoint& p )
217+
{
218+
uint hash;
219+
uint h1 = qHash(( quint64 )p.m_x );
220+
uint h2 = qHash(( quint64 )p.m_y );
221+
hash = h1 ^( h2 << 1 );
222+
return hash;
223+
}
224+
215225
#endif //QGSPOINT_H

‎tests/src/python/test_qgspoint.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ def test_pointToString(self):
4949
myMessage = 'Expected: %s Got: %s' % (myExpectedValue, myActualValue)
5050
assert myExpectedValue == myActualValue, myMessage
5151

52+
def test_hash(self):
53+
a = QgsPoint( 2.0, 1.0 )
54+
b = QgsPoint( 2.0, 2.0 )
55+
c = QgsPoint( 1.0, 2.0 )
56+
d = QgsPoint( 1.0, 1.0 )
57+
e = QgsPoint( 2.0, 1.0 )
58+
assert a.__hash__() != b.__hash__()
59+
assert e.__hash__() == a.__hash__()
60+
61+
mySet = set( [ a, b, c, d, e ] )
62+
assert len( mySet ) == 4
5263

5364
if __name__ == '__main__':
5465
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.