Skip to content

Commit 25ce65c

Browse files
committed
refactor: change allocator only for array_of_strings
1 parent 2a59f11 commit 25ce65c

3 files changed

Lines changed: 30 additions & 54 deletions

File tree

tuple/include/array_of_strings_sketch.hpp

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,24 @@
2828

2929
namespace datasketches {
3030

31+
using array_of_strings = array<std::string>;
32+
3133
// default update policy for an array of strings
32-
template<typename Allocator = std::allocator<std::string>>
3334
class default_array_of_strings_update_policy {
3435
public:
35-
using array_of_strings = array<std::string, Allocator>;
36-
37-
explicit default_array_of_strings_update_policy(const Allocator& allocator = Allocator());
36+
default_array_of_strings_update_policy() = default;
3837

3938
array_of_strings create() const;
4039

4140
void update(array_of_strings& array, const array_of_strings& input) const;
4241

4342
void update(array_of_strings& array, const array_of_strings* input) const;
44-
45-
private:
46-
Allocator allocator_;
4743
};
4844

4945
// serializer/deserializer for an array of strings
5046
// Requirements: all strings must be valid UTF-8 and array size must be <= 127.
51-
template<typename Allocator = std::allocator<std::string>>
47+
template<typename Allocator = std::allocator<array_of_strings>>
5248
struct default_array_of_strings_serde {
53-
using array_of_strings = array<std::string, Allocator>;
5449
using summary_allocator = typename std::allocator_traits<Allocator>::template rebind_alloc<array_of_strings>;
5550

5651
explicit default_array_of_strings_serde(const Allocator& allocator = Allocator());
@@ -62,7 +57,6 @@ struct default_array_of_strings_serde {
6257
size_t size_of_item(const array_of_strings& item) const;
6358

6459
private:
65-
Allocator allocator_;
6660
summary_allocator summary_allocator_;
6761
static void check_num_nodes(uint8_t num_nodes);
6862
static uint32_t compute_total_bytes(const array_of_strings& item);
@@ -72,23 +66,17 @@ struct default_array_of_strings_serde {
7266
/**
7367
* Hashes an array of strings using ArrayOfStrings-compatible hashing.
7468
*/
75-
template<typename Allocator = std::allocator<std::string>>
76-
uint64_t hash_array_of_strings_key(const array<std::string, Allocator>& key);
69+
uint64_t hash_array_of_strings_key(const array_of_strings& key);
7770

7871
/**
7972
* Extended class of compact_tuple_sketch for array of strings
8073
* Requirements: all strings must be valid UTF-8 and array size must be <= 127.
8174
*/
82-
template<typename Allocator = std::allocator<std::string>>
75+
template<typename Allocator = std::allocator<array_of_strings>>
8376
class compact_array_of_strings_tuple_sketch:
84-
public compact_tuple_sketch<
85-
array<std::string, Allocator>,
86-
typename std::allocator_traits<Allocator>::template rebind_alloc<array<std::string, Allocator>>
87-
> {
77+
public compact_tuple_sketch<array_of_strings, Allocator> {
8878
public:
89-
using array_of_strings = array<std::string, Allocator>;
90-
using summary_allocator = typename std::allocator_traits<Allocator>::template rebind_alloc<array_of_strings>;
91-
using Base = compact_tuple_sketch<array_of_strings, summary_allocator>;
79+
using Base = compact_tuple_sketch<array_of_strings, Allocator>;
9280
using vector_bytes = typename Base::vector_bytes;
9381
using Base::serialize;
9482

@@ -133,13 +121,13 @@ class compact_array_of_strings_tuple_sketch:
133121
/**
134122
* Convenience alias for update_tuple_sketch for array of strings
135123
*/
136-
template<typename Allocator = std::allocator<std::string>,
137-
typename Policy = default_array_of_strings_update_policy<Allocator>>
124+
template<typename Allocator = std::allocator<array_of_strings>,
125+
typename Policy = default_array_of_strings_update_policy>
138126
using update_array_of_strings_tuple_sketch = update_tuple_sketch<
139-
array<std::string, Allocator>,
140-
array<std::string, Allocator>,
127+
array_of_strings,
128+
array_of_strings,
141129
Policy,
142-
typename std::allocator_traits<Allocator>::template rebind_alloc<array<std::string, Allocator>>
130+
Allocator
143131
>;
144132

145133
/**
@@ -148,12 +136,12 @@ using update_array_of_strings_tuple_sketch = update_tuple_sketch<
148136
* @param ordered optional flag to specify if an ordered sketch should be produced
149137
* @return compact array of strings sketch
150138
*/
151-
template<typename Allocator = std::allocator<std::string>, typename Policy = default_array_of_strings_update_policy<Allocator>>
139+
template<typename Allocator = std::allocator<array_of_strings>, typename Policy = default_array_of_strings_update_policy>
152140
compact_array_of_strings_tuple_sketch<Allocator> compact_array_of_strings_sketch(
153141
const update_array_of_strings_tuple_sketch<Allocator, Policy>& sketch, bool ordered = true);
154142

155143
} /* namespace datasketches */
156144

157145
#include "array_of_strings_sketch_impl.hpp"
158146

159-
#endif
147+
#endif

tuple/include/array_of_strings_sketch_impl.hpp

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,37 @@
2222

2323
#include <stdexcept>
2424

25+
#include "array_of_strings_sketch.hpp"
2526
#include "common_defs.hpp"
2627
#include "third_party/utf8cpp/utf8.h"
2728

2829
namespace datasketches {
2930

30-
template<typename Allocator>
31-
default_array_of_strings_update_policy<Allocator>::default_array_of_strings_update_policy(const Allocator& allocator):
32-
allocator_(allocator) {}
33-
34-
template<typename Allocator>
35-
auto default_array_of_strings_update_policy<Allocator>::create() const -> array_of_strings {
36-
return array_of_strings(0, "", allocator_);
31+
inline array_of_strings default_array_of_strings_update_policy::create() const {
32+
return array_of_strings(0, "");
3733
}
3834

39-
template<typename Allocator>
40-
void default_array_of_strings_update_policy<Allocator>::update(
35+
inline void default_array_of_strings_update_policy::update(
4136
array_of_strings& array, const array_of_strings& input
4237
) const {
4338
const auto length = static_cast<size_t>(input.size());
44-
array = array_of_strings(static_cast<uint8_t>(length), "", allocator_);
39+
array = array_of_strings(static_cast<uint8_t>(length), "");
4540
for (size_t i = 0; i < length; ++i) array[i] = input[i];
4641
}
4742

48-
template<typename Allocator>
49-
void default_array_of_strings_update_policy<Allocator>::update(
43+
inline void default_array_of_strings_update_policy::update(
5044
array_of_strings& array, const array_of_strings* input
5145
) const {
5246
if (input == nullptr) {
53-
array = array_of_strings(0, "", allocator_);
47+
array = array_of_strings(0, "");
5448
return;
5549
}
5650
const auto length = static_cast<size_t>(input->size());
57-
array = array_of_strings(static_cast<uint8_t>(length), "", allocator_);
51+
array = array_of_strings(static_cast<uint8_t>(length), "");
5852
for (size_t i = 0; i < length; ++i) array[i] = (*input)[i];
5953
}
6054

61-
template<typename Allocator>
62-
uint64_t hash_array_of_strings_key(const array<std::string, Allocator>& key) {
55+
inline uint64_t hash_array_of_strings_key(const array_of_strings& key) {
6356
// Matches Java Util.PRIME for ArrayOfStrings key hashing.
6457
static constexpr uint64_t STRING_ARR_HASH_SEED = 0x7A3CCA71ULL;
6558
XXHash64 hasher(STRING_ARR_HASH_SEED);
@@ -95,8 +88,7 @@ template<typename SerDe>
9588
auto compact_array_of_strings_tuple_sketch<Allocator>::deserialize(
9689
std::istream& is, uint64_t seed, const SerDe& sd, const Allocator& allocator
9790
) -> compact_array_of_strings_tuple_sketch {
98-
summary_allocator alloc(allocator);
99-
auto base = Base::deserialize(is, seed, sd, alloc);
91+
auto base = Base::deserialize(is, seed, sd, allocator);
10092
return compact_array_of_strings_tuple_sketch(std::move(base));
10193
}
10294

@@ -105,14 +97,12 @@ template<typename SerDe>
10597
auto compact_array_of_strings_tuple_sketch<Allocator>::deserialize(
10698
const void* bytes, size_t size, uint64_t seed, const SerDe& sd, const Allocator& allocator
10799
) -> compact_array_of_strings_tuple_sketch {
108-
summary_allocator alloc(allocator);
109-
auto base = Base::deserialize(bytes, size, seed, sd, alloc);
100+
auto base = Base::deserialize(bytes, size, seed, sd, allocator);
110101
return compact_array_of_strings_tuple_sketch(std::move(base));
111102
}
112103

113104
template<typename Allocator>
114105
default_array_of_strings_serde<Allocator>::default_array_of_strings_serde(const Allocator& allocator):
115-
allocator_(allocator),
116106
summary_allocator_(allocator) {}
117107

118108
template<typename Allocator>
@@ -144,7 +134,7 @@ void default_array_of_strings_serde<Allocator>::deserialize(
144134
const uint8_t num_nodes = read<uint8_t>(is);
145135
if (!is) throw std::runtime_error("array_of_strings stream read failed");
146136
check_num_nodes(num_nodes);
147-
array_of_strings array(num_nodes, "", allocator_);
137+
array_of_strings array(num_nodes, "");
148138
for (uint8_t j = 0; j < num_nodes; ++j) {
149139
const uint32_t length = read<uint32_t>(is);
150140
if (!is) throw std::runtime_error("array_of_strings stream read failed");
@@ -202,7 +192,7 @@ size_t default_array_of_strings_serde<Allocator>::deserialize(
202192
uint8_t num_nodes;
203193
bytes_read += copy_from_mem(ptr8 + bytes_read, num_nodes);
204194
check_num_nodes(num_nodes);
205-
array_of_strings array(num_nodes, "", allocator_);
195+
array_of_strings array(num_nodes, "");
206196
for (uint8_t j = 0; j < num_nodes; ++j) {
207197
uint32_t length;
208198
bytes_read += copy_from_mem(ptr8 + bytes_read, length);
@@ -252,4 +242,4 @@ void default_array_of_strings_serde<Allocator>::check_utf8(const std::string& va
252242

253243
} /* namespace datasketches */
254244

255-
#endif
245+
#endif

tuple/test/array_of_strings_sketch_test.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@
2929

3030
namespace datasketches {
3131

32-
using array_of_strings = array<std::string>;
33-
3432
TEST_CASE("aos update policy", "[tuple_sketch]") {
35-
default_array_of_strings_update_policy<> policy;
33+
default_array_of_strings_update_policy policy;
3634

3735
SECTION("create empty") {
3836
auto values = policy.create();

0 commit comments

Comments
 (0)