datasketches-cpp
Loading...
Searching...
No Matches
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 <algorithm>
24#include <cstdint>
25#include <stdexcept>
26#include <string>
27#include <vector>
28
29#include "theta_constants.hpp"
30#include "theta_comparators.hpp"
31
32namespace datasketches {
33
52template<typename ExtractKey, typename Entry, typename Allocator>
53static uint64_t trim_to_nominal(std::vector<Entry, Allocator>& entries, uint32_t nominal_size, uint64_t theta) {
54 if (entries.size() <= nominal_size) return theta;
55 std::nth_element(entries.begin(), entries.begin() + nominal_size, entries.end(), compare_by_key<ExtractKey>());
56 const uint64_t new_theta = ExtractKey()(entries[nominal_size]);
57 entries.erase(entries.begin() + nominal_size, entries.end());
58 entries.shrink_to_fit();
59 return new_theta;
60}
61
62template<typename T>
63static void check_value(T actual, T expected, const char* description) {
64 if (actual != expected) {
65 throw std::invalid_argument(std::string(description) + " mismatch: expected " + std::to_string(expected) + ", actual " + std::to_string(actual));
66 }
67}
68
69template<bool dummy>
70class checker {
71public:
72 static void check_serial_version(uint8_t actual, uint8_t expected) {
73 check_value(actual, expected, "serial version");
74 }
75 static void check_sketch_family(uint8_t actual, uint8_t expected) {
76 check_value(actual, expected, "sketch family");
77 }
78 static void check_sketch_type(uint8_t actual, uint8_t expected) {
79 check_value(actual, expected, "sketch type");
80 }
81 static void check_seed_hash(uint16_t actual, uint16_t expected) {
82 check_value(actual, expected, "seed hash");
83 }
84};
85
86template<bool dummy>
87class theta_build_helper{
88public:
89 // consistent way of initializing theta from p
90 // avoids multiplication if p == 1 since it might not yield MAX_THETA exactly
91 static uint64_t starting_theta_from_p(float p) {
92 if (p < 1) return static_cast<uint64_t>(static_cast<double>(theta_constants::MAX_THETA) * p);
94 }
95
96 static uint8_t starting_sub_multiple(uint8_t lg_tgt, uint8_t lg_min, uint8_t lg_rf) {
97 return (lg_tgt <= lg_min) ? lg_min : (lg_rf == 0) ? lg_tgt : ((lg_tgt - lg_min) % lg_rf) + lg_min;
98 }
99};
100
101} /* namespace datasketches */
102
103#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