OMNeT++ API 6.1
Discrete Event Simulation Library
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 <cmath> // NAN
20 #include <cstdint>
21 #include <cstring>
22 #include <cstdlib>
23 #include "simkerneldefs.h"
24 #include "cobject.h"
25 #include "cexception.h"
26 
27 namespace omnetpp {
28 
29 
40 class SIM_API cHasher : noncopyable
41 {
42  private:
43  uint32_t value;
44 
45  void merge(uint32_t x) {
46  // rotate value left by one bit, and xor with new data
47  value = ((value << 1) | (value >> 31)) ^ x;
48  }
49 
50  void merge2(uint64_t x) {
51  merge((uint32_t)x);
52  merge((uint32_t)(x>>32));
53  }
54 
55  public:
59  cHasher() {ASSERT(sizeof(uint32_t)==4); ASSERT(sizeof(double)==8); value = 0;}
60 
63  void reset() {value = 0;}
64  void add(char d) {merge((uint32_t)d);}
65  void add(short d) {merge((uint32_t)d);}
66  void add(int d) {merge((uint32_t)d);}
67  void add(long d) {int64_t tmp=d; merge2((uint64_t)tmp);}
68  void add(long long d) {merge2((uint64_t)d);}
69  void add(unsigned char d) {merge((uint32_t)d);}
70  void add(unsigned short d) {merge((uint32_t)d);}
71  void add(unsigned int d) {merge((uint32_t)d);}
72  void add(unsigned long d) {uint64_t tmp=d; merge2(tmp);}
73  void add(unsigned long long d) {merge2(d);}
74  void add(double d) {
75  if (std::isnan(d)) d = NAN; // force standard representation
76  //merge2(std::bit_cast<uint64_t>(d)); // requires C++20
77  union _ {double _; uint64_t i;};
78  merge2(((union _ *)&d)->i); // hint: "safe type punning in C++"
79  }
80  void add(simtime_t t) {merge2(t.raw());}
81  void add(const char *s) {if (s) add(s, strlen(s)+1); else add(0);}
82  void add(const std::string& s) {add(s.c_str(), s.size()+1);}
83  void add(const void *p, size_t length);
84  template<typename T>
85  cHasher& operator<<(const T& x) {add(x); return *this;} // allows chaining
87 
93  uint32_t getHash() const {return value;}
94 
100  uint32_t parse(const char *hash) const;
101 
105  bool equals(const char *hash) const;
106 
110  std::string str() const;
112 };
113 
114 } // namespace omnetpp
115 
116 #endif
117 
118 
omnetpp::cHasher
Utility class to calculate the hash of some data.
Definition: chasher.h:40
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:59
omnetpp::simtime_t
SimTime simtime_t
Represents simulation time.
Definition: simtime_t.h:40
omnetpp::cHasher::getHash
uint32_t getHash() const
Definition: chasher.h:93