OMNeT++ Simulation Library  5.6.1
chasher.h
1 //==========================================================================
2 // CHASHER.H - part of
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_CHASHER_H
17 #define __OMNETPP_CHASHER_H
18 
19 #include <cstdint>
20 #include <cstring>
21 #include <cstdlib>
22 #include "simkerneldefs.h"
23 #include "cobject.h"
24 #include "cexception.h"
25 
26 namespace omnetpp {
27 
28 
37 class SIM_API cHasher : noncopyable
38 {
39  private:
40  uint32_t value;
41 
42  void merge(uint32_t x) {
43  // rotate value left by one bit, and xor with new data
44  uint32_t carry = (value & 0x80000000U) >> 31;
45  value = ((value<<1)|carry) ^ x;
46  }
47 
48  void merge2(uint64_t x) {
49  merge((uint32_t)x);
50  merge((uint32_t)(x>>32));
51  }
52 
53  public:
57  cHasher() {ASSERT(sizeof(uint32_t)==4); ASSERT(sizeof(double)==8); value = 0;}
58 
61  void reset() {value = 0;}
62  void add(const char *p, size_t length);
63  void add(char d) {merge((uint32_t)d);}
64  void add(short d) {merge((uint32_t)d);}
65  void add(int d) {merge((uint32_t)d);}
66  void add(long d) {int64_t tmp=d; merge2((uint64_t)tmp);}
67  void add(long long d) {merge2((uint64_t)d);}
68  void add(unsigned char d) {merge((uint32_t)d);}
69  void add(unsigned short d) {merge((uint32_t)d);}
70  void add(unsigned int d) {merge((uint32_t)d);}
71  void add(unsigned long d) {uint64_t tmp=d; merge2(tmp);}
72  void add(unsigned long long d) {merge2(d);}
73  // note: safe(r) type punning, see http://cocoawithlove.decenturl.com/type-punning
74  void add(double d) {union _ {double d; uint64_t i;}; merge2(((union _ *)&d)->i);}
75  void add(const char *s) {if (s) add(s, strlen(s)+1); else add(0);}
77 
83  uint32_t getHash() const {return value;}
84 
90  uint32_t parse(const char *hash) const;
91 
95  bool equals(const char *hash) const;
96 
100  std::string str() const;
102 };
103 
104 } // namespace omnetpp
105 
106 #endif
107 
108 
Utility class to calculate the hash of some data.
Definition: chasher.h:37
uint32_t getHash() const
Definition: chasher.h:83
Utility class, to make it impossible to call the operator= and copy constructor of any class derived ...
Definition: cobject.h:311
cHasher()
Definition: chasher.h:57
Definition: cabstracthistogram.h:21