datasketches-cpp
cpc_compressor.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 // author Kevin Lang, Oath Research
21 
22 #ifndef CPC_COMPRESSOR_HPP_
23 #define CPC_COMPRESSOR_HPP_
24 
25 #include "cpc_common.hpp"
26 
27 namespace datasketches {
28 
29 /*
30  * This is a very efficient compressor specialized for use by the CPC Sketch.
31  * There are two very different compression schemes here: one for the sliding window
32  * and another for the table of so-called surprising values.
33  * These two compression schemes are designed for specific probability distributions of entries
34  * in these data structures and make some compromises for performance. As a result
35  * the compression is slightly less effective than theoretically achievable but is very fast.
36  */
37 
38 // forward declarations
39 template<typename A> class cpc_sketch_alloc;
40 template<typename A> class cpc_compressor;
41 
42 // the compressor is not instantiated directly
43 // the sketch implementation uses this global function to statically allocate and construct on the first use
44 template<typename A>
45 inline cpc_compressor<A>& get_compressor();
46 
47 template<typename A>
48 class cpc_compressor {
49 public:
50  using vector_bytes = std::vector<uint8_t, typename std::allocator_traits<A>::template rebind_alloc<uint8_t>>;
51  using vector_u32 = std::vector<uint32_t, typename std::allocator_traits<A>::template rebind_alloc<uint32_t>>;
52 
53  void compress(const cpc_sketch_alloc<A>& source, compressed_state<A>& target) const;
54  void uncompress(const compressed_state<A>& source, uncompressed_state<A>& target, uint8_t lg_k, uint32_t num_coupons) const;
55 
56  // methods below are public for testing
57 
58  // This returns the number of compressed words that were actually used. It is the caller's
59  // responsibility to ensure that the compressed_words array is long enough to prevent over-run.
60  uint32_t low_level_compress_bytes(
61  const uint8_t* byte_array, // input
62  uint32_t num_bytes_to_encode,
63  const uint16_t* encoding_table,
64  uint32_t* compressed_words // output
65  ) const;
66 
67  void low_level_uncompress_bytes(
68  uint8_t* byte_array, // output
69  uint32_t num_bytes_to_decode,
70  const uint16_t* decoding_table,
71  const uint32_t* compressed_words,
72  uint32_t num_compressed_words // input
73  ) const;
74 
75  // Here "pairs" refers to row-column pairs that specify
76  // the positions of surprising values in the bit matrix.
77 
78  // returns the number of compressedWords actually used
79  uint32_t low_level_compress_pairs(
80  const uint32_t* pair_array, // input
81  uint32_t num_pairs_to_encode,
82  uint8_t num_base_bits,
83  uint32_t* compressed_words // output
84  ) const;
85 
86  void low_level_uncompress_pairs(
87  uint32_t* pair_array, // output
88  uint32_t num_pairs_to_decode,
89  uint8_t num_base_bits,
90  const uint32_t* compressed_words, // input
91  uint32_t num_compressed_words // input
92  ) const;
93 
94 private:
95  // These decoding tables are created at library startup time by inverting the encoding tables
96  uint16_t* decoding_tables_for_high_entropy_byte[22] = {
97  // sixteen tables for the steady state (chosen based on the "phase" of C/K)
98  NULL, NULL, NULL, NULL,
99  NULL, NULL, NULL, NULL,
100  NULL, NULL, NULL, NULL,
101  NULL, NULL, NULL, NULL,
102  // six more tables for the gradual transition between warmup mode and the steady state.
103  NULL, NULL, NULL, NULL, NULL, NULL
104  };
105  uint16_t* length_limited_unary_decoding_table65;
106  uint8_t* column_permutations_for_decoding[16] = {
107  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
108  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
109  };
110 
111  cpc_compressor();
112  template<typename T> friend cpc_compressor<T>& get_compressor();
113  ~cpc_compressor();
114 
115  void make_decoding_tables(); // call this at startup
116  void free_decoding_tables(); // call this at the end
117 
118  void compress_sparse_flavor(const cpc_sketch_alloc<A>& source, compressed_state<A>& target) const;
119  void compress_hybrid_flavor(const cpc_sketch_alloc<A>& source, compressed_state<A>& target) const;
120  void compress_pinned_flavor(const cpc_sketch_alloc<A>& source, compressed_state<A>& target) const;
121  void compress_sliding_flavor(const cpc_sketch_alloc<A>& source, compressed_state<A>& target) const;
122 
123  void uncompress_sparse_flavor(const compressed_state<A>& source, uncompressed_state<A>& target, uint8_t lg_k) const;
124  void uncompress_hybrid_flavor(const compressed_state<A>& source, uncompressed_state<A>& target, uint8_t lg_k) const;
125  void uncompress_pinned_flavor(const compressed_state<A>& source, uncompressed_state<A>& target, uint8_t lg_k, uint32_t num_coupons) const;
126  void uncompress_sliding_flavor(const compressed_state<A>& source, uncompressed_state<A>& target, uint8_t lg_k, uint32_t num_coupons) const;
127 
128  uint8_t* make_inverse_permutation(const uint8_t* permu, unsigned length);
129  uint16_t* make_decoding_table(const uint16_t* encoding_table, unsigned num_byte_values);
130  void validate_decoding_table(const uint16_t* decoding_table, const uint16_t* encoding_table) const;
131 
132  void compress_surprising_values(const vector_u32& pairs, uint8_t lg_k, compressed_state<A>& result) const;
133  void compress_sliding_window(const uint8_t* window, uint8_t lg_k, uint32_t num_coupons, compressed_state<A>& target) const;
134 
135  vector_u32 uncompress_surprising_values(const uint32_t* data, uint32_t data_words, uint32_t num_pairs, uint8_t lg_k, const A& allocator) const;
136  void uncompress_sliding_window(const uint32_t* data, uint32_t data_words, vector_bytes& window, uint8_t lg_k, uint32_t num_coupons) const;
137 
138  static size_t safe_length_for_compressed_pair_buf(uint32_t k, uint32_t num_pairs, uint8_t num_base_bits);
139  static size_t safe_length_for_compressed_window_buf(uint32_t k);
140  static uint8_t determine_pseudo_phase(uint8_t lg_k, uint32_t c);
141 
142  static inline vector_u32 tricky_get_pairs_from_window(const uint8_t* window, uint32_t k, uint32_t num_pairs_to_get, uint32_t empty_space, const A& allocator);
143  static inline uint8_t golomb_choose_number_of_base_bits(uint32_t k, uint64_t count);
144 };
145 
146 } /* namespace datasketches */
147 
148 #include "cpc_compressor_impl.hpp"
149 
150 #endif
DataSketches namespace.
Definition: binomial_bounds.hpp:38