datasketches-cpp
Loading...
Searching...
No Matches
req_compactor.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 REQ_COMPACTOR_HPP_
21#define REQ_COMPACTOR_HPP_
22
23#include <memory>
24
25namespace datasketches {
26
27template<
28typename T,
29typename Comparator,
30typename Allocator
31>
32class req_compactor {
33public:
34 req_compactor(bool hra, uint8_t lg_weight, uint32_t section_size, const Comparator& comparator,
35 const Allocator& allocator, bool sorted = true);
36 ~req_compactor();
37 req_compactor(const req_compactor& other);
38 req_compactor(req_compactor&& other) noexcept;
39 req_compactor& operator=(const req_compactor& other);
40 req_compactor& operator=(req_compactor&& other);
41
42 template<typename TT, typename CC, typename AA>
43 req_compactor(const req_compactor<TT, CC, AA>& other, const Comparator& comparator, const Allocator& allocator);
44
45 bool is_sorted() const;
46 uint32_t get_num_items() const;
47 uint32_t get_nom_capacity() const;
48 uint8_t get_lg_weight() const;
49 const T* begin() const;
50 const T* end() const;
51 T* begin();
52 T* end();
53
54 uint64_t compute_weight(const T& item, bool inclusive) const;
55
56 template<typename FwdT>
57 void append(FwdT&& item);
58
59 template<typename FwdC>
60 void merge(FwdC&& other);
61
62 void sort();
63
64 std::pair<uint32_t, uint32_t> compact(req_compactor& next);
65
71 template<typename S, typename TT = T, typename std::enable_if<std::is_arithmetic<TT>::value, int>::type = 0>
72 size_t get_serialized_size_bytes(const S& serde) const;
73
79 template<typename S, typename TT = T, typename std::enable_if<!std::is_arithmetic<TT>::value, int>::type = 0>
80 size_t get_serialized_size_bytes(const S& serde) const;
81
82 template<typename S>
83 void serialize(std::ostream& os, const S& serde) const;
84
85 template<typename S>
86 size_t serialize(void* dst, size_t capacity, const S& serde) const;
87
88 template<typename S>
89 static req_compactor deserialize(std::istream& is, const S& serde, const Comparator& comparator,
90 const Allocator& allocator, bool sorted, bool hra);
91
92 template<typename S>
93 static std::pair<req_compactor, size_t> deserialize(const void* bytes, size_t size, const S& serde,
94 const Comparator& comparator, const Allocator& allocator, bool sorted, bool hra);
95
96 template<typename S>
97 static req_compactor deserialize(std::istream& is, const S& serde, const Comparator& comparator,
98 const Allocator& allocator, bool sorted, uint16_t k, uint8_t num_items, bool hra);
99
100 template<typename S>
101 static std::pair<req_compactor, size_t> deserialize(const void* bytes, size_t size, const S& serde,
102 const Comparator& comparator, const Allocator& allocator, bool sorted, uint16_t k, uint8_t num_items, bool hra);
103
104private:
105 Comparator comparator_;
106 Allocator allocator_;
107 uint8_t lg_weight_;
108 bool hra_;
109 bool coin_; // random bit for compaction
110 bool sorted_;
111 float section_size_raw_;
112 uint32_t section_size_;
113 uint8_t num_sections_;
114 uint64_t state_; // state of the deterministic compaction schedule
115 uint32_t num_items_;
116 uint32_t capacity_;
117 T* items_;
118
119 bool ensure_enough_sections();
120 std::pair<uint32_t, uint32_t> compute_compaction_range(uint32_t secs_to_compact) const;
121 void grow(uint32_t new_capacity);
122 void ensure_space(uint32_t num);
123
124 static uint32_t nearest_even(float value);
125
126 template<typename InIter, typename OutIter>
127 static void promote_evens_or_odds(InIter from, InIter to, bool flag, OutIter dst);
128
129 // for deserialization
130 class items_deleter;
131 req_compactor(bool hra, uint8_t lg_weight, bool sorted, float section_size_raw, uint8_t num_sections, uint64_t state,
132 std::unique_ptr<T, items_deleter> items, uint32_t num_items, const Comparator& comparator, const Allocator& allocator);
133
134 template<typename S>
135 static std::unique_ptr<T, items_deleter> deserialize_items(std::istream& is, const S& serde, const Allocator& allocator, uint32_t num);
136
137 template<typename S>
138 static std::pair<std::unique_ptr<T, items_deleter>, size_t> deserialize_items(const void* bytes, size_t size, const S& serde, const Allocator& allocator, uint32_t num);
139
140 // for type converting constructor
141 template<typename TT, typename CC, typename AA>
142 friend class req_compactor;
143};
144
145} /* namespace datasketches */
146
147#include "req_compactor_impl.hpp"
148
149#endif
DataSketches namespace.
Definition binomial_bounds.hpp:38