jitcraft/Utility.h
changeset 54 8e22792a3921
parent 53 13bd3a8df334
child 55 e073a9fe8e57
     1.1 --- a/jitcraft/Utility.h	Sat Jul 07 17:11:31 2012 -0700
     1.2 +++ b/jitcraft/Utility.h	Sat Jul 07 20:28:29 2012 -0700
     1.3 @@ -70,6 +70,45 @@
     1.4      }
     1.5  };
     1.6  
     1.7 +// Bob Jenkin's one-at-a-time hash function[1].
     1.8 +//
     1.9 +// [1] http://burtleburtle.net/bob/hash/doobs.html
    1.10 +class CharacterStreamHasher
    1.11 +{
    1.12 +    uint32 hash;
    1.13 +
    1.14 +  public:
    1.15 +    CharacterStreamHasher()
    1.16 +      : hash(0)
    1.17 +    { }
    1.18 +
    1.19 +    void add(char c) {
    1.20 +        hash += c;
    1.21 +        hash += (hash << 10);
    1.22 +        hash ^= (hash >> 6);
    1.23 +    }
    1.24 +
    1.25 +    void add(const char *s, size_t length) {
    1.26 +        for (size_t i = 0; i < length; i++)
    1.27 +            add(s[i]);
    1.28 +    }
    1.29 +
    1.30 +    uint32 result() {
    1.31 +        hash += (hash << 3);
    1.32 +        hash ^= (hash >> 11);
    1.33 +        hash += (hash << 15);
    1.34 +        return hash;
    1.35 +    }
    1.36 +};
    1.37 +
    1.38 +static inline uint32
    1.39 +HashCharSequence(const char *s, size_t length)
    1.40 +{
    1.41 +    CharacterStreamHasher hasher;
    1.42 +    hasher.add(s, length);
    1.43 +    return hasher.result();
    1.44 +}
    1.45 +
    1.46  static inline size_t
    1.47  Log2(size_t number)
    1.48  {