1

Writing a good hash function is tricky and Mike Ash talks a good talk on his blog about it. He goes on to talk about integer rotation and I was wondering how I would do something like this in Swift.

#define NSUINT_BIT (CHAR_BIT * sizeof(NSUInteger))
#define NSUINTROTATE(val, howmuch) ((((NSUInteger)val) << howmuch) | (((NSUInteger)val) >> (NSUINT_BIT - howmuch)))

- (NSUInteger)hash
{
    return NSUINTROTATE([_firstName hash], NSUINT_BIT / 2) ^ [_lastName hash];
}

And how would you handle a third parameter?

  • 1
    Not a direct answer to your question, but perhaps useful: https://stackoverflow.com/questions/42627014/implementing-a-hash-combiner-in-swift. – Martin R Jun 05 '17 at 10:36
  • Thanks @MartinR looks like a good strategy for hashing. I'll save it as a code snippet! –  Jun 05 '17 at 12:43

0 Answers0