Fast hash for 2 coordinates where order doesn't matter?
<p>Is there a formula that is a one way hash for 2 coordinates (a, b) and (c, d) to one integer where a, b, c, and d are positive? Order <strong>doesn't</strong> matter here, so the formula should give the same results when given <code>(a, b), (c, d)</code> and <code>(c, d), (a, b)</code>. The order of the actual numbers in each coordinate point matter (<code>(a, b)</code> is not the same as <code>(b, a)</code>). Speed is the key here, the formula should be fast and have O(1) complexity. </p>

<p>Note - what I'm doing right now is sorting the two coordinates using Python's building in sort, and then using them as keys in Python's built-in dictionary (so, built-in hashing). I need a faster way of doing this so that I can hash the two coordinates to an integer myself.</p>

<p>You can use the <a href="https://docs.python.org/3/library/functions.html#hash" rel="nofollow">hash()</a> of a <a href="https://docs.python.org/3/library/functions.html#func-frozenset" rel="nofollow">frozenset</a> for this.</p>

<pre><code>&gt;&gt;&gt; hash(frozenset([(10, 20), (11, 22)]))
1735850283064117985
&gt;&gt;&gt; hash(frozenset([(11, 22), (10, 20)]))
1735850283064117985
</code></pre>

<p>Frozensets were specifically designed for this kind of use case (i.e. frozensets are intrinsically unordered collections that are immutable and hashable).</p>

<p>Hope this answer takes your right to what you needed :-)</p>

python hash hashmap key hashcode