Move hash functions out of am-utility.h
authorDavid Anderson <dvander@alliedmods.net>
Fri Aug 23 22:37:15 2013 -0700 (2013-08-23)
changeset 5d5deada8801f
parent 4 f3275c49eb7b
child 6 afbfab9b416e
Move hash functions out of am-utility.h
include/am-utility.h
     1.1 --- a/include/am-utility.h	Fri Aug 23 22:36:50 2013 -0700
     1.2 +++ b/include/am-utility.h	Fri Aug 23 22:37:15 2013 -0700
     1.3 @@ -108,80 +108,49 @@
     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 +// Wrapper that automatically deletes its contents. The pointer can be taken
    1.12 +// to avoid destruction.
    1.13 +template <typename T>
    1.14 +class AutoArray
    1.15  {
    1.16 -    uint32 hash;
    1.17 +    T *t_;
    1.18  
    1.19    public:
    1.20 -    CharacterStreamHasher()
    1.21 -      : hash(0)
    1.22 -    { }
    1.23 -
    1.24 -    void add(char c) {
    1.25 -        hash += c;
    1.26 -        hash += (hash << 10);
    1.27 -        hash ^= (hash >> 6);
    1.28 +    AutoArray()
    1.29 +      : t_(NULL)
    1.30 +    {
    1.31 +    }
    1.32 +    explicit AutoArray(T *t)
    1.33 +      : t_(t)
    1.34 +    {
    1.35 +    }
    1.36 +    ~AutoArray() {
    1.37 +        delete [] t_;
    1.38 +    }
    1.39 +    T *take() {
    1.40 +        return ReturnAndVoid(t_);
    1.41      }
    1.42 -
    1.43 -    void add(const char *s, size_t length) {
    1.44 -        for (size_t i = 0; i < length; i++)
    1.45 -            add(s[i]);
    1.46 +    T *operator *() const {
    1.47 +        return t_;
    1.48 +    }
    1.49 +    T &operator [](size_t index) {
    1.50 +      return t_[index];
    1.51 +    }
    1.52 +    const T &operator [](size_t index) const {
    1.53 +      return t_[index];
    1.54      }
    1.55 -
    1.56 -    uint32 result() {
    1.57 -        hash += (hash << 3);
    1.58 -        hash ^= (hash >> 11);
    1.59 -        hash += (hash << 15);
    1.60 -        return hash;
    1.61 +    operator T *() const {
    1.62 +        return t_;
    1.63 +    }
    1.64 +    void operator =(T *t) {
    1.65 +        delete [] t_;
    1.66 +        t_ = t;
    1.67 +    }
    1.68 +    bool operator !() const {
    1.69 +        return !t_;
    1.70      }
    1.71  };
    1.72  
    1.73 -static inline uint32
    1.74 -HashCharSequence(const char *s, size_t length)
    1.75 -{
    1.76 -    CharacterStreamHasher hasher;
    1.77 -    hasher.add(s, length);
    1.78 -    return hasher.result();
    1.79 -}
    1.80 -
    1.81 -// From http://burtleburtle.net/bob/hash/integer.html
    1.82 -static inline uint32
    1.83 -HashInt32(int32 a)
    1.84 -{
    1.85 -    a = (a ^ 61) ^ (a >> 16);
    1.86 -    a = a + (a << 3);
    1.87 -    a = a ^ (a >> 4);
    1.88 -    a = a * 0x27d4eb2d;
    1.89 -    a = a ^ (a >> 15);
    1.90 -    return a;
    1.91 -}
    1.92 -
    1.93 -// From http://www.cris.com/~Ttwang/tech/inthash.htm
    1.94 -static inline uint32
    1.95 -HashInt64(int64 key)
    1.96 -{
    1.97 -  key = (~key) + (key << 18); // key = (key << 18) - key - 1;
    1.98 -  key = key ^ (uint64(key) >> 31);
    1.99 -  key = key * 21; // key = (key + (key << 2)) + (key << 4);
   1.100 -  key = key ^ (uint64(key) >> 11);
   1.101 -  key = key + (key << 6);
   1.102 -  key = key ^ (uint64(key) >> 22);
   1.103 -  return uint32(key);
   1.104 -}
   1.105 -
   1.106 -static inline uint32
   1.107 -HashPointer(void *p)
   1.108 -{
   1.109 -#if defined(KE_32BIT)
   1.110 -    return HashInt32(int32_t(intptr_t(p)));
   1.111 -#elif defined(KE_64BIT)
   1.112 -    return HashInt64(int64_t(intptr_t(p)));
   1.113 -#endif
   1.114 -}
   1.115 -
   1.116  static inline size_t
   1.117  Log2(size_t number)
   1.118  {