JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
HashProvider.h
Go to the documentation of this file.
1 //
2 // Created by Nico on 16/07/2019.
3 //
4 #include <unordered_map>
6 #include "ValueAccepter.h"
7 #ifndef JODA_HASHPROVIDER_H
8 #define JODA_HASHPROVIDER_H
10 class HashHandler {
11  public:
12  typedef char Ch;
13 
14  bool Null() {
15  std::string v = "[JODA]Nullval";
16  hash_combine(hash, v);
17  return true;
18  }
19 
20  bool Bool(bool b) {
21  hash_combine(hash, b);
22  return true;
23  }
24 
25  bool Int(int i) {
26  hash_combine(hash, i);
27  return true;
28  }
29 
30  bool Uint(unsigned i) {
31  hash_combine(hash, i);
32  return true;
33  }
34 
35  bool Int64(int64_t i) {
36  hash_combine(hash, i);
37  return true;
38  }
39 
40  bool Uint64(uint64_t i) {
41  hash_combine(hash, i);
42  return true;
43  }
44 
45  bool Double(double d) {
46  hash_combine(hash, d);
47  return true;
48  }
49 
50  bool RawNumber(const Ch *str, rapidjson::SizeType length, bool copy) {
51  hash_combine(hash, std::string(str, length));
52  return true;
53  }
54 
55  bool String(const Ch *str, rapidjson::SizeType length, bool copy) {
56  hash_combine(hash, std::string(str, length));
57  return true;
58  }
59 
60  bool StartObject() { return true; }
61 
62  bool Key(const Ch *str, rapidjson::SizeType length, bool copy) {
63  hash_combine(hash, std::string(str, length));
64  return true;
65  }
66 
67  bool EndObject(rapidjson::SizeType memberCount) {
68  stack--;
69  return true;
70  }
71 
72  constexpr bool StartArray() {
73  stack++;
74  return true;
75  }
76 
77  constexpr bool EndArray(rapidjson::SizeType elementCount) {
78  stack--;
79  return true;
80  }
81 
82  size_t getHash() const { return hash; }
83 
84  void reset() {
85  stack = 0;
86  hash = 0;
87  }
88 
89  private:
90  int stack = 0;
91  size_t hash = 0;
92 
93  template <class T>
94  inline void hash_combine(std::size_t &seed, T const &v) {
95  std::hash<T> hasher;
96  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
97  }
98 };
99 
101  public:
102  static constexpr IValueType retType = IV_Number;
103  static constexpr IValueType inType = IV_Any;
104  static constexpr bool acceptAll = true;
105  static constexpr auto name = "HASH";
106 
107  static RJValue accept(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc,
108  const ValueAccepter &accepter) {
109  HashHandler handler;
110  accepter.Accept(json, alloc, handler);
111  return RJValue(handler.getHash());
112  }
113 
114  static RJValue pointer(const RJValue *ptr) {
115  if (ptr == nullptr) return RJValue();
116  HashHandler handler;
117  ptr->Accept(handler);
118  return RJValue(handler.getHash());
119  }
120 
121  static RJValue virtualObject(const VirtualObject *vo) {
122  if (vo == nullptr) return RJValue();
123  HashHandler handler;
124  vo->Accept(handler);
125  return RJValue(handler.getHash());
126  }
127 };
128 } // namespace joda::query::providers::hash
129 
130 namespace joda::query {
131 template class UnaryPointerAcceptProvider<
133 
136 } // namespace joda::query
137 #endif // JODA_HASHPROVIDER_H
rapidjson::MemoryPoolAllocator< RJBaseAlloc > RJMemoryPoolAlloc
Definition: RJFwd.h:26
rapidjson::GenericValue< RJChar, RJMemoryPoolAlloc > RJValue
Definition: RJFwd.h:29
Definition: RapidJsonDocument.h:22
Definition: VirtualObject.h:13
bool Accept(Handler &handler) const
Definition: VirtualObject.h:74
Definition: UnaryPointerAcceptProvider.h:25
Definition: ValueAccepter.h:16
bool Accept(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc, Handler &h) const
Definition: ValueAccepter.h:32
Definition: HashProvider.h:100
static RJValue accept(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc, const ValueAccepter &accepter)
Definition: HashProvider.h:107
static constexpr IValueType inType
Definition: HashProvider.h:103
static constexpr IValueType retType
Definition: HashProvider.h:102
static RJValue virtualObject(const VirtualObject *vo)
Definition: HashProvider.h:121
static constexpr bool acceptAll
Definition: HashProvider.h:104
static constexpr auto name
Definition: HashProvider.h:105
static RJValue pointer(const RJValue *ptr)
Definition: HashProvider.h:114
Definition: HashProvider.h:10
char Ch
Definition: HashProvider.h:12
bool Bool(bool b)
Definition: HashProvider.h:20
constexpr bool EndArray(rapidjson::SizeType elementCount)
Definition: HashProvider.h:77
bool StartObject()
Definition: HashProvider.h:60
bool RawNumber(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: HashProvider.h:50
bool Null()
Definition: HashProvider.h:14
bool EndObject(rapidjson::SizeType memberCount)
Definition: HashProvider.h:67
bool Key(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: HashProvider.h:62
constexpr bool StartArray()
Definition: HashProvider.h:72
bool String(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: HashProvider.h:55
bool Int64(int64_t i)
Definition: HashProvider.h:35
bool Uint(unsigned i)
Definition: HashProvider.h:30
bool Uint64(uint64_t i)
Definition: HashProvider.h:40
bool Double(double d)
Definition: HashProvider.h:45
void reset()
Definition: HashProvider.h:84
size_t getHash() const
Definition: HashProvider.h:82
bool Int(int i)
Definition: HashProvider.h:25
Definition: HashProvider.h:9
Definition: AttributeStatAggregator.h:12
IValueType
Definition: IValueProvider.h:33
@ IV_Number
Definition: IValueProvider.h:35
@ IV_Any
Definition: IValueProvider.h:39
UnaryPointerAcceptProvider< joda::query::providers::hash::HashCalculator > HashProvider
Definition: HashProvider.h:135