Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think it depends very much on the purpose of why you want a unique numerical identifier. Timestamps can change, inodes can change, disknumbers can change, MAC adresses can change. (Still, +1 for duskwuff)</p> <p>In some scenarios you can simply make a table, where each path you add gets a new, unique number, just like a numerical key column in a database.</p> <p>Although hashes <strong>can</strong> collide, in every actual environment this is absolutely unlikely (if you don't use the lousiest algorithm around...) It is much more likely that you get errors due to flaws in your implementation, for example you treat "/tmp" differently than "/tmp/", because you don't normalize the paths before hashing them. Or, you <strong>want</strong> to distinguish physical folders, but forget to check for hardlinks and symlinks to the same folder, thus you get multiple hashes / id's for the same dir.</p> <p>Again, depending on your usecase, a collision is not necessarily fatal: if you find that a new path results in the same hash as an existing one (will not happen!) you can still react on that case. (*)</p> <p>Just to help your imagination: If you use a 64 bit hash, you could fill 150 000 000 of 1TB hard disk drives with empty folders (nothing on them but short folder names...) and then you will have a collision for sure. If you think, that's too risky (blink, blink), use a 128 bit hash which makes it 18 446 744 073 710 000 times less likely.</p> <p>Hashes are designed to make collisions unlikely, and even good old MD5 will do its job well if nobody <strong>willingly</strong> tries to produce a collision.</p> <p><strong>(*) Edit:</strong> Your pigeonhole article already points that out: a collision just mean that lookup is no longer O(1), but slightly slower. As it rarely ever happens, you can easily live with that. If you use a std::map (no hash) or std::hashmap, you'll not have to worry about collisions. See <a href="https://stackoverflow.com/questions/5139859/what-the-difference-between-map-and-hashmap-in-stl">what the difference between map and hashmap in STL</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload