How do I calculate a good hash code for a list of strings?
Standard java practise, is to simply write final int prime = 31; int result = 1; for( String s : strings ) { result = result * prime + s.hashCode(); } // result is the hashcode.
Standard java practise, is to simply write final int prime = 31; int result = 1; for( String s : strings ) { result = result * prime + s.hashCode(); } // result is the hashcode.
Cast the row to text and use md5 to make a hash: SELECT md5(CAST((f.*)AS text)) FROM foo f;
You can’t change the type of hashCode, nor should you want to. I’d just go with something like: public int hashCode() { return x * 31 + y; } Note that this means that (a, b) is different to (b, a) for most cases (unlike e.g. adding or XOR-ing). This can be useful if you … Read more
“Subclasses” of AnyVal do not behave properly from a hashing perspective: scala> 1.0.hashCode res14: Int = 1072693248 Of course this is boxed to a call to: scala> new java.lang.Double(1.0).hashCode res16: Int = 1072693248 We might prefer it to be: scala> new java.lang.Double(1.0).## res17: Int = 1 scala> 1.0.## res15: Int = 1 We should expect … Read more
Obvious potential answer: because that will cost memory. There’s a cost/benefit analysis here: Cost: 4 bytes for every string (and a quick test on each call to GetHashCode). Also make the string object mutable, which would obviously mean you’d need to be careful about the implementation – unless you always compute the hash code up-front, … Read more
Yes, Resharper can do that. With cursor inside your type, open the “Generate code” menu (Alt+Ins depending on settings or Resharper -> Edit -> Generate Code), and select “Equality members”: This opens a window where you can select which members are used for equality, along with some options about the generated code (e.g. should your … Read more
I don’t think hash values should be negative. Why not? It’s entirely valid to have negative hash codes. Most ways of coming up with a hash code naturally end up with negative values, and anything dealing with them should take account of this. However, I’d consider a different approach to coming up with your hash … Read more
I’d do it the same way I normally combine hash codes – with an addition and a multiplication: public override int GetHashCode() { unchecked { int hash = 19; foreach (var foo in foos) { hash = hash * 31 + foo.GetHashCode(); } return hash; } } (Note that you shouldn’t add anything to the … Read more
Hashcode could be a first-round check for inequality. However, it presents some tradeoffs. String hashcodes are lazily calculated, although they do use a “guard” value. If you’re comparing strings with long lifetimes (ie, they’re likely to have had the hashcode computed), this isn’t a problem. Otherwise, you’re stuck with either computing the hashcode (potentially expensive) … Read more
One hex character can only represent 16 different values, i.e. 4 bits. (16 = 24) 40 × 4 = 160. And no, you need much more than 5 characters in base-36. There are totally 2160 different SHA-1 hashes. 2160 = 1640, so this is another reason why we need 40 hex digits. But 2160 = … Read more