chasher.h

00001 //==========================================================================
00002 //   CHASHER.H  - part of
00003 //                     OMNeT++/OMNEST
00004 //            Discrete System Simulation in C++
00005 //
00006 //  Author: Andras Varga
00007 //
00008 //==========================================================================
00009 
00010 /*--------------------------------------------------------------*
00011   Copyright (C) 1992-2008 Andras Varga
00012   Copyright (C) 2006-2008 OpenSim Ltd.
00013 
00014   This file is distributed WITHOUT ANY WARRANTY. See the file
00015   `license' for details on this and other legal matters.
00016 *--------------------------------------------------------------*/
00017 
00018 #ifndef __CHASHER_H
00019 #define __CHASHER_H
00020 
00021 #include <string.h>
00022 #include <stdlib.h>
00023 #include "simkerneldefs.h"
00024 #include "cobject.h"
00025 #include "cexception.h"
00026 #include "platdep/intxtypes.h"
00027 
00028 NAMESPACE_BEGIN
00029 
00030 
00039 class SIM_API cHasher : noncopyable
00040 {
00041   private:
00042     uint32 value;
00043 
00044     void merge(uint32 x) {
00045         // rotate value left by one bit, and xor with new data
00046         uint32 carry = (value & 0x80000000U) >> 31;
00047         value = ((value<<1)|carry) ^ x;
00048     }
00049 
00050     void merge2(uint64 x) {
00051         merge((uint32)x);
00052         merge((uint32)(x>>32));
00053     }
00054 
00055   public:
00059     cHasher() {ASSERT(sizeof(uint32)==4); ASSERT(sizeof(double)==8); value = 0;}
00060 
00063     void reset() {value = 0;}
00064     void add(const char *p, size_t length);
00065     void add(char d)           {merge((uint32)d);}
00066     void add(short d)          {merge((uint32)d);}
00067     void add(int d)            {merge((uint32)d);}
00068     void add(long d)           {int64 tmp=d; merge2((uint64)tmp);}
00069     void add(opp_long_long d)   {merge2((uint64)d);}
00070     void add(unsigned char d)  {merge((uint32)d);}
00071     void add(unsigned short d) {merge((uint32)d);}
00072     void add(unsigned int d)   {merge((uint32)d);}
00073     void add(unsigned long d)  {uint64 tmp=d; merge2(tmp);}
00074     void add(opp_unsigned_long_long d)  {merge2(d);}
00075     // note: safe(r) type punning, see http://cocoawithlove.decenturl.com/type-punning
00076     void add(double d)         {union _ {double d; uint64 i;}; merge2(((union _ *)&d)->i);}
00077     void add(const char *s)    {if (s) add(s, strlen(s)+1); else add(0);}
00079 
00085     uint32 getHash() const {return value;}
00086 
00092     uint32 parse(const char *fingerprint) const;
00093 
00097     bool equals(const char *fingerprint) const;
00098 
00102     std::string str() const;
00104 };
00105 
00106 NAMESPACE_END
00107 
00108 #endif
00109 
00110 
Generated on Tue Dec 2 11:16:27 2014 for OMNeT++ Simulation Library by  doxygen 1.6.3