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 // function called atexit to clean up compression tables
48 template<typename A>
49 void destroy_compressor();
50 
51 template<typename A>
52 class cpc_compressor {
53 public:
54  using vector_bytes = std::vector<uint8_t, typename std::allocator_traits<A>::template rebind_alloc<uint8_t>>;
55  using vector_u32 = std::vector<uint32_t, typename std::allocator_traits<A>::template rebind_alloc<uint32_t>>;
56 
57  void compress(const cpc_sketch_alloc<A>& source, compressed_state<A>& target) const;
58  void uncompress(const compressed_state<A>& source, uncompressed_state<A>& target, uint8_t lg_k, uint32_t num_coupons) const;
59 
60  // methods below are public for testing
61 
62  // This returns the number of compressed words that were actually used. It is the caller's
63  // responsibility to ensure that the compressed_words array is long enough to prevent over-run.
64  uint32_t low_level_compress_bytes(
65  const uint8_t* byte_array, // input
66  uint32_t num_bytes_to_encode,
67  const uint16_t* encoding_table,
68  uint32_t* compressed_words // output
69  ) const;
70 
71  void low_level_uncompress_bytes(
72  uint8_t* byte_array, // output
73  uint32_t num_bytes_to_decode,
74  const uint16_t* decoding_table,
75  const uint32_t* compressed_words,
76  uint32_t num_compressed_words // input
77  ) const;
78 
79  // Here "pairs" refers to row-column pairs that specify
80  // the positions of surprising values in the bit matrix.
81 
82  // returns the number of compressedWords actually used
83  uint32_t low_level_compress_pairs(
84  const uint32_t* pair_array, // input
85  uint32_t num_pairs_to_encode,
86  uint8_t num_base_bits,
87  uint32_t* compressed_words // output
88  ) const;
89 
90  void low_level_uncompress_pairs(
91  uint32_t* pair_array, // output
92  uint32_t num_pairs_to_decode,
93  uint8_t num_base_bits,
94  const uint32_t* compressed_words, // input
95  uint32_t num_compressed_words // input
96  ) const;
97 
98 private:
99  // These decoding tables are created at library startup time by inverting the encoding tables
100  uint16_t* decoding_tables_for_high_entropy_byte[22] = {
101  // sixteen tables for the steady state (chosen based on the "phase" of C/K)
102  NULL, NULL, NULL, NULL,
103  NULL, NULL, NULL, NULL,
104  NULL, NULL, NULL, NULL,
105  NULL, NULL, NULL, NULL,
106  // six more tables for the gradual transition between warmup mode and the steady state.
107  NULL, NULL, NULL, NULL, NULL, NULL
108  };
109  uint16_t* length_limited_unary_decoding_table65;
110  uint8_t* column_permutations_for_decoding[16] = {
111  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
112  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
113  };
114 
115  cpc_compressor();
116  friend cpc_compressor& get_compressor<A>();
117 
118  ~cpc_compressor();
119  friend void destroy_compressor<A>();
120 
121  void make_decoding_tables(); // call this at startup
122  void free_decoding_tables(); // call this at the end
123 
124  void compress_sparse_flavor(const cpc_sketch_alloc<A>& source, compressed_state<A>& target) const;
125  void compress_hybrid_flavor(const cpc_sketch_alloc<A>& source, compressed_state<A>& target) const;
126  void compress_pinned_flavor(const cpc_sketch_alloc<A>& source, compressed_state<A>& target) const;
127  void compress_sliding_flavor(const cpc_sketch_alloc<A>& source, compressed_state<A>& target) const;
128 
129  void uncompress_sparse_flavor(const compressed_state<A>& source, uncompressed_state<A>& target, uint8_t lg_k) const;
130  void uncompress_hybrid_flavor(const compressed_state<A>& source, uncompressed_state<A>& target, uint8_t lg_k) const;
131  void uncompress_pinned_flavor(const compressed_state<A>& source, uncompressed_state<A>& target, uint8_t lg_k, uint32_t num_coupons) const;
132  void uncompress_sliding_flavor(const compressed_state<A>& source, uncompressed_state<A>& target, uint8_t lg_k, uint32_t num_coupons) const;
133 
134  uint8_t* make_inverse_permutation(const uint8_t* permu, unsigned length);
135  uint16_t* make_decoding_table(const uint16_t* encoding_table, unsigned num_byte_values);
136  void validate_decoding_table(const uint16_t* decoding_table, const uint16_t* encoding_table) const;
137 
138  void compress_surprising_values(const vector_u32& pairs, uint8_t lg_k, compressed_state<A>& result) const;
139  void compress_sliding_window(const uint8_t* window, uint8_t lg_k, uint32_t num_coupons, compressed_state<A>& target) const;
140 
141  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;
142  void uncompress_sliding_window(const uint32_t* data, uint32_t data_words, vector_bytes& window, uint8_t lg_k, uint32_t num_coupons) const;
143 
144  static size_t safe_length_for_compressed_pair_buf(uint32_t k, uint32_t num_pairs, uint8_t num_base_bits);
145  static size_t safe_length_for_compressed_window_buf(uint32_t k);
146  static uint8_t determine_pseudo_phase(uint8_t lg_k, uint32_t c);
147 
148  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);
149  static inline uint8_t golomb_choose_number_of_base_bits(uint32_t k, uint64_t count);
150 };
151 
152 } /* namespace datasketches */
153 
154 #include "cpc_compressor_impl.hpp"
155 
156 #endif
DataSketches namespace.
Definition: binomial_bounds.hpp:38