Lens AI Profiler Cpp
common_defs.hpp
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef _COMMON_DEFS_HPP_
21 #define _COMMON_DEFS_HPP_
22 
23 #include <cstdint>
24 #include <string>
25 #include <memory>
26 #include <iostream>
27 #include <random>
28 #include <chrono>
29 #include <thread>
30 
32 namespace datasketches {
33 
34 static const uint64_t DEFAULT_SEED = 9001;
35 
36 enum resize_factor { X1 = 0, X2, X4, X8 };
37 
38 template<typename A> using string = std::basic_string<char, std::char_traits<char>, typename std::allocator_traits<A>::template rebind_alloc<char>>;
39 
40 // common random declarations
41 namespace random_utils {
42  static std::random_device rd; // possibly unsafe in MinGW with GCC < 9.2
43  static thread_local std::mt19937_64 rand(rd());
44  static thread_local std::uniform_real_distribution<> next_double(0.0, 1.0);
45 
46  // thread-safe random bit
47  static thread_local std::independent_bits_engine<std::mt19937, 1, uint32_t>
48  random_bit(static_cast<uint32_t>(std::chrono::system_clock::now().time_since_epoch().count()
49  + std::hash<std::thread::id>{}(std::this_thread::get_id())));
50 
51  inline void override_seed(uint64_t s) {
52  rand.seed(s);
53  }
54 }
55 
56 // utility function to hide unused compiler warning
57 // usually has no additional cost
58 template<typename T> void unused(T&&...) {}
59 
60 // common helping functions
61 // TODO: find a better place for them
62 
63 constexpr uint8_t log2(uint32_t n) {
64  return (n > 1) ? 1 + log2(n >> 1) : 0;
65 }
66 
67 constexpr uint8_t lg_size_from_count(uint32_t n, double load_factor) {
68  return log2(n) + ((n > static_cast<uint32_t>((1 << (log2(n) + 1)) * load_factor)) ? 2 : 1);
69 }
70 
71 // stream helpers to hide casts
72 template<typename T>
73 static inline T read(std::istream& is) {
74  T value;
75  is.read(reinterpret_cast<char*>(&value), sizeof(T));
76  return value;
77 }
78 
79 template<typename T>
80 static inline void read(std::istream& is, T* ptr, size_t size_bytes) {
81  is.read(reinterpret_cast<char*>(ptr), size_bytes);
82 }
83 
84 template<typename T>
85 static inline void write(std::ostream& os, T value) {
86  os.write(reinterpret_cast<const char*>(&value), sizeof(T));
87 }
88 
89 template<typename T>
90 static inline void write(std::ostream& os, const T* ptr, size_t size_bytes) {
91  os.write(reinterpret_cast<const char*>(ptr), size_bytes);
92 }
93 
94 template<typename T>
95 T byteswap(T value) {
96  char* ptr = static_cast<char*>(static_cast<void*>(&value));
97  const int len = sizeof(T);
98  for (size_t i = 0; i < len / 2; ++i) {
99  std::swap(ptr[i], ptr[len - i - 1]);
100  }
101  return value;
102 }
103 
104 template<typename T>
105 static inline T read_big_endian(std::istream& is) {
106  T value;
107  is.read(reinterpret_cast<char*>(&value), sizeof(T));
108  return byteswap(value);
109 }
110 
111 // wrapper for iterators to implement operator-> returning temporary value
112 template<typename T>
114 public:
115  return_value_holder(T value): value_(value) {}
116  const T* operator->() const { return std::addressof(value_); }
117 private:
118  T value_;
119 };
120 
121 } // namespace
122 
123 #endif // _COMMON_DEFS_HPP_
Definition: common_defs.hpp:113
DataSketches namespace.
Definition: common_defs.hpp:32