Phoenix
Object-oriented orthogonally persistent operating system
hash.h
Go to the documentation of this file.
00001 /*
00002  * /phoenix/include/common/hash.h
00003  *
00004  * This file is a part of Phoenix operating system.
00005  * Copyright (c) 2011-2012, Artyom Lebedev <artyom.lebedev@gmail.com>
00006  * All rights reserved.
00007  * See COPYING file for copyright details.
00008  */
00009 
00014 #ifndef HASH_H_
00015 #define HASH_H_
00016 
00020 class Hash {
00021 public:
00026     inline
00027     Hash(u32 initial = 0)
00028     {
00029         _a = _b = _c = INITIAL_VALUE + initial;
00030     }
00031 
00036     inline void
00037     Reset(u32 initial = 0)
00038     {
00039         _a = _b = _c = INITIAL_VALUE + initial;
00040         _resid = 0;
00041         _length = 0;
00042     }
00043 
00049     static void Mix(u32 &a, u32 &b, u32 &c);
00050 
00057     static void Final(u32 &a, u32 &b, u32 &c);
00058 
00066     void Feed(const void *data, size_t size);
00067 
00072     inline u32
00073     Get32()
00074     {
00075         u32 a, b, c;
00076         _Finalize(a, b, c);
00077         return c;
00078     }
00079 
00084     inline u64
00085     Get64()
00086     {
00087         u32 a, b, c;
00088         _Finalize(a, b, c);
00089         return (static_cast<u64>(b) << 32) | c;
00090     }
00091 
00098     inline
00099     operator u32()
00100     {
00101         return Get32();
00102     }
00103 
00108     inline
00109     operator u64()
00110     {
00111         return Get64();
00112     }
00113 
00118     inline size_t
00119     GetLength()
00120     {
00121         return _length;
00122     }
00123 
00124 private:
00125     enum {
00127         INITIAL_VALUE = 0x9e3779b8,
00128     };
00130     u32 _a, _b, _c;
00135     size_t _resid = 0;
00137     size_t _length = 0;
00138 
00148     void _Finalize(u32 &a, u32 &b, u32 &c);
00149 };
00150 
00151 
00152 #endif /* HASH_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines