OMNeT++ Simulation Library  6.0.3
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 
39 class SIM_API cHasher : noncopyable
40 {
41  private:
42  uint32_t value;
43 
44  void merge(uint32_t x) {
45  // rotate value left by one bit, and xor with new data
46  value = ((value << 1) | (value >> 31)) ^ x;
47  }
48 
49  void merge2(uint64_t x) {
50  merge((uint32_t)x);
51  merge((uint32_t)(x>>32));
52  }
53 
54  public:
58  cHasher() {ASSERT(sizeof(uint32_t)==4); ASSERT(sizeof(double)==8); value = 0;}
59 
62  void reset() {value = 0;}
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  void add(double d) {union _ {double d; uint64_t i;}; merge2(((union _ *)&d)->i);} // hint: "safe type punning in C++"
74  void add(simtime_t t) {merge2(t.raw());}
75  void add(const char *s) {if (s) add(s, strlen(s)+1); else add(0);}
76  void add(const std::string& s) {add(s.c_str(), s.size()+1);}
77  void add(const void *p, size_t length);
78  template<typename T>
79  cHasher& operator<<(const T& x) {add(x); return *this;} // allows chaining
81 
87  uint32_t getHash() const {return value;}
88 
94  uint32_t parse(const char *hash) const;
95 
99  bool equals(const char *hash) const;
100 
104  std::string str() const;
106 };
107 
108 } // namespace omnetpp
109 
110 #endif
111 
112 
omnetpp::cHasher
Utility class to calculate the hash of some data.
Definition: chasher.h:39
omnetpp::noncopyable
Utility class, to make it impossible to call the operator= and copy constructor of any class derived ...
Definition: cobject.h:415
omnetpp::cHasher::cHasher
cHasher()
Definition: chasher.h:58
omnetpp::simtime_t
SimTime simtime_t
Represents simulation time.
Definition: simtime_t.h:40
omnetpp::cHasher::getHash
uint32_t getHash() const
Definition: chasher.h:87