datasketches-cpp
Loading...
Searching...
No Matches
array_of_strings_sketch.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 ARRAY_OF_STRINGS_SKETCH_HPP_
21#define ARRAY_OF_STRINGS_SKETCH_HPP_
22
23#include <memory>
24#include <string>
25
26#include "array_tuple_sketch.hpp"
27#include "xxhash64.h"
28
29namespace datasketches {
30
31using array_of_strings = array<std::string>;
32
33// default update policy for an array of strings
34class default_array_of_strings_update_policy {
35public:
36 default_array_of_strings_update_policy() = default;
37
38 array_of_strings create() const;
39
40 void update(array_of_strings& array, const array_of_strings& input) const;
41
42 void update(array_of_strings& array, const array_of_strings* input) const;
43};
44
53template<typename Allocator = std::allocator<array_of_strings>>
54struct default_array_of_strings_serde {
55 using summary_allocator = typename std::allocator_traits<Allocator>::template rebind_alloc<array_of_strings>;
56
57 explicit default_array_of_strings_serde(const Allocator& allocator = Allocator());
58
59 void serialize(std::ostream& os, const array_of_strings* items, unsigned num) const;
60 void deserialize(std::istream& is, array_of_strings* items, unsigned num) const;
61 size_t serialize(void* ptr, size_t capacity, const array_of_strings* items, unsigned num) const;
62 size_t deserialize(const void* ptr, size_t capacity, array_of_strings* items, unsigned num) const;
63 size_t size_of_item(const array_of_strings& item) const;
64
65private:
66 summary_allocator summary_allocator_;
67 static void check_num_nodes(uint8_t num_nodes);
68 static uint32_t compute_total_bytes(const array_of_strings& item);
69 void destroy_items(array_of_strings* items, unsigned num) const;
70};
71
89uint64_t hash_array_of_strings_key(const array_of_strings& key);
90
103template<typename Allocator = std::allocator<array_of_strings>>
105 public compact_tuple_sketch<array_of_strings, Allocator> {
106public:
108 using vector_bytes = typename Base::vector_bytes;
109
116 template<typename Sketch>
117 compact_array_of_strings_tuple_sketch(const Sketch& sketch, bool ordered = true);
118
124 template<typename SerDe = default_array_of_strings_serde<Allocator>>
125 void serialize(std::ostream& os, const SerDe& sd = SerDe()) const;
126
135 template<typename SerDe = default_array_of_strings_serde<Allocator>>
136 vector_bytes serialize(unsigned header_size_bytes = 0, const SerDe& sd = SerDe()) const;
137
146 template<typename SerDe = default_array_of_strings_serde<Allocator>>
147 static compact_array_of_strings_tuple_sketch deserialize(std::istream& is, uint64_t seed = DEFAULT_SEED,
148 const SerDe& sd = SerDe(), const Allocator& allocator = Allocator());
149
159 template<typename SerDe = default_array_of_strings_serde<Allocator>>
160 static compact_array_of_strings_tuple_sketch deserialize(const void* bytes, size_t size, uint64_t seed = DEFAULT_SEED,
161 const SerDe& sd = SerDe(), const Allocator& allocator = Allocator());
162
163private:
164 explicit compact_array_of_strings_tuple_sketch(Base&& base);
165};
166
170template<typename Allocator = std::allocator<array_of_strings>,
171 typename Policy = default_array_of_strings_update_policy>
173 array_of_strings,
174 array_of_strings,
175 Policy,
176 Allocator
177>;
178
185template<typename Allocator = std::allocator<array_of_strings>, typename Policy = default_array_of_strings_update_policy>
187 const update_array_of_strings_tuple_sketch<Allocator, Policy>& sketch, bool ordered = true);
188
189} /* namespace datasketches */
190
191#include "array_of_strings_sketch_impl.hpp"
192
193#endif
Extended class of compact_tuple_sketch for array of strings.
Definition array_of_strings_sketch.hpp:105
vector_bytes serialize(unsigned header_size_bytes=0, const SerDe &sd=SerDe()) const
This method serializes the sketch as a vector of bytes.
void serialize(std::ostream &os, const SerDe &sd=SerDe()) const
This method serializes the sketch into a given stream in a binary form.
Definition array_of_strings_sketch_impl.hpp:189
static compact_array_of_strings_tuple_sketch deserialize(std::istream &is, uint64_t seed=DEFAULT_SEED, const SerDe &sd=SerDe(), const Allocator &allocator=Allocator())
This method deserializes a sketch from a given stream.
compact_array_of_strings_tuple_sketch(const Sketch &sketch, bool ordered=true)
Copy constructor.
Definition array_of_strings_sketch_impl.hpp:178
static compact_array_of_strings_tuple_sketch deserialize(const void *bytes, size_t size, uint64_t seed=DEFAULT_SEED, const SerDe &sd=SerDe(), const Allocator &allocator=Allocator())
This method deserializes a sketch from a given array of bytes.
compact_tuple_sketch(const Base &other, bool ordered)
Copy constructor.
Definition tuple_sketch_impl.hpp:287
Update Tuple sketch.
Definition tuple_sketch.hpp:222
DataSketches namespace.
Definition binomial_bounds.hpp:38
update_tuple_sketch< array_of_strings, array_of_strings, Policy, Allocator > update_array_of_strings_tuple_sketch
Convenience alias for update_tuple_sketch for array of strings.
Definition array_of_strings_sketch.hpp:172
compact_array_of_strings_tuple_sketch< Allocator > compact_array_of_strings_sketch(const update_array_of_strings_tuple_sketch< Allocator, Policy > &sketch, bool ordered=true)
Converts an array of strings tuple sketch to a compact sketch (ordered or unordered).
Definition array_of_strings_sketch_impl.hpp:170
uint64_t hash_array_of_strings_key(const array_of_strings &key)
Hashes an array of strings key the same way as Java's ArrayOfStringsTupleSketch (org....
Definition array_of_strings_sketch_impl.hpp:157