datasketches-cpp
compact_theta_sketch_parser.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 COMPACT_THETA_SKETCH_PARSER_HPP_
21 #define COMPACT_THETA_SKETCH_PARSER_HPP_
22 
23 #include <cstdint>
24 
25 namespace datasketches {
26 
27 template<bool dummy>
28 class compact_theta_sketch_parser {
29 public:
30  struct compact_theta_sketch_data {
31  bool is_empty;
32  bool is_ordered;
33  uint16_t seed_hash;
34  uint32_t num_entries;
35  uint64_t theta;
36  const void* entries_start_ptr;
37  uint8_t entry_bits;
38  };
39 
40  static compact_theta_sketch_data parse(const void* ptr, size_t size, uint64_t seed, bool dump_on_error = false);
41 
42 private:
43  // offsets are in sizeof(type)
44  static const size_t COMPACT_SKETCH_PRE_LONGS_BYTE = 0;
45  static const size_t COMPACT_SKETCH_SERIAL_VERSION_BYTE = 1;
46  static const size_t COMPACT_SKETCH_TYPE_BYTE = 2;
47  static const size_t COMPACT_SKETCH_FLAGS_BYTE = 5;
48  static const size_t COMPACT_SKETCH_SEED_HASH_U16 = 3;
49  static const size_t COMPACT_SKETCH_SINGLE_ENTRY_U64 = 1; // ver 3
50  static const size_t COMPACT_SKETCH_NUM_ENTRIES_U32 = 2; // ver 1-3
51  static const size_t COMPACT_SKETCH_ENTRIES_EXACT_U64 = 2; // ver 1-3
52  static const size_t COMPACT_SKETCH_ENTRIES_ESTIMATION_U64 = 3; // ver 1-3
53  static const size_t COMPACT_SKETCH_THETA_U64 = 2; // ver 1-3
54  static const size_t COMPACT_SKETCH_V4_ENTRY_BITS_BYTE = 3;
55  static const size_t COMPACT_SKETCH_V4_NUM_ENTRIES_BYTES_BYTE = 4;
56  static const size_t COMPACT_SKETCH_V4_THETA_U64 = 1;
57  static const size_t COMPACT_SKETCH_V4_PACKED_DATA_EXACT_BYTE = 8;
58  static const size_t COMPACT_SKETCH_V4_PACKED_DATA_ESTIMATION_BYTE = 16;
59 
60  static const uint8_t COMPACT_SKETCH_IS_EMPTY_FLAG = 2;
61  static const uint8_t COMPACT_SKETCH_IS_ORDERED_FLAG = 4;
62 
63  static const uint8_t COMPACT_SKETCH_TYPE = 3;
64 
65  static void check_memory_size(const void* ptr, size_t actual_bytes, size_t expected_bytes, bool dump_on_error);
66  static std::string hex_dump(const uint8_t* ptr, size_t size);
67 };
68 
69 } /* namespace datasketches */
70 
71 #include "compact_theta_sketch_parser_impl.hpp"
72 
73 #endif
DataSketches namespace.
Definition: binomial_bounds.hpp:38