datasketches-cpp
theta_helpers.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 THETA_HELPERS_HPP_
21 #define THETA_HELPERS_HPP_
22 
23 #include <stdexcept>
24 #include <string>
25 
26 #include "theta_constants.hpp"
27 
28 namespace datasketches {
29 
30 template<typename T>
31 static void check_value(T actual, T expected, const char* description) {
32  if (actual != expected) {
33  throw std::invalid_argument(std::string(description) + " mismatch: expected " + std::to_string(expected) + ", actual " + std::to_string(actual));
34  }
35 }
36 
37 template<bool dummy>
38 class checker {
39 public:
40  static void check_serial_version(uint8_t actual, uint8_t expected) {
41  check_value(actual, expected, "serial version");
42  }
43  static void check_sketch_family(uint8_t actual, uint8_t expected) {
44  check_value(actual, expected, "sketch family");
45  }
46  static void check_sketch_type(uint8_t actual, uint8_t expected) {
47  check_value(actual, expected, "sketch type");
48  }
49  static void check_seed_hash(uint16_t actual, uint16_t expected) {
50  check_value(actual, expected, "seed hash");
51  }
52 };
53 
54 template<bool dummy>
55 class theta_build_helper{
56 public:
57  // consistent way of initializing theta from p
58  // avoids multiplication if p == 1 since it might not yield MAX_THETA exactly
59  static uint64_t starting_theta_from_p(float p) {
60  if (p < 1) return static_cast<uint64_t>(static_cast<double>(theta_constants::MAX_THETA) * p);
62  }
63 
64  static uint8_t starting_sub_multiple(uint8_t lg_tgt, uint8_t lg_min, uint8_t lg_rf) {
65  return (lg_tgt <= lg_min) ? lg_min : (lg_rf == 0) ? lg_tgt : ((lg_tgt - lg_min) % lg_rf) + lg_min;
66  }
67 };
68 
69 } /* namespace datasketches */
70 
71 #endif
const uint64_t MAX_THETA
max theta - signed max for compatibility with Java
Definition: theta_constants.hpp:36
DataSketches namespace.
Definition: binomial_bounds.hpp:38