@@ -268,39 +268,55 @@ return _sketch_array.end();
268268
269269template <typename W, typename A>
270270void count_min_sketch<W,A>::serialize(std::ostream& os) const {
271+ serialize_to ([&os](const void * data, size_t size) {
272+ os.write (static_cast <const char *>(data), size);
273+ });
274+ }
275+
276+ template <typename W, typename A>
277+ template <typename WriteBytes>
278+ size_t count_min_sketch<W,A>::serialize_to(WriteBytes&& write_bytes) const {
279+ size_t bytes_written = 0 ;
280+ auto write_raw = [&write_bytes, &bytes_written](const void * data, size_t size) {
281+ write_bytes (data, size);
282+ bytes_written += size;
283+ };
284+ auto write_value = [&write_raw](const auto & value) {
285+ write_raw (&value, sizeof (value));
286+ };
287+
271288 // Long 0
272289 // const uint8_t preamble_longs = is_empty() ? PREAMBLE_LONGS_SHORT : PREAMBLE_LONGS_FULL;
273290 const uint8_t preamble_longs = PREAMBLE_LONGS_SHORT ;
274291 const uint8_t ser_ver = SERIAL_VERSION_1 ;
275292 const uint8_t family_id = FAMILY_ID ;
276293 const uint8_t flags_byte = (is_empty () ? 1 << flags::IS_EMPTY : 0 );
277294 const uint32_t unused32 = NULL_32 ;
278- write (os, preamble_longs);
279- write (os, ser_ver);
280- write (os, family_id);
281- write (os, flags_byte);
282- write (os, unused32);
295+ write_value ( preamble_longs);
296+ write_value ( ser_ver);
297+ write_value ( family_id);
298+ write_value ( flags_byte);
299+ write_value ( unused32);
283300
284301 // Long 1
285302 const uint32_t nbuckets = _num_buckets;
286303 const uint8_t nhashes = _num_hashes;
287304 const uint16_t seed_hash (compute_seed_hash (_seed));
288305 const uint8_t unused8 = NULL_8 ;
289- write (os, nbuckets);
290- write (os, nhashes);
291- write (os, seed_hash);
292- write (os, unused8);
293- if (is_empty ()) return ; // sketch is empty, no need to write further bytes.
306+ write_value ( nbuckets);
307+ write_value ( nhashes);
308+ write_value ( seed_hash);
309+ write_value ( unused8);
310+ if (is_empty ()) return bytes_written ; // sketch is empty, no need to write further bytes.
294311
295312 // Long 2
296- write (os, _total_weight);
313+ const W t_weight = _total_weight;
314+ write_value (t_weight);
297315
298316 // Long 3 onwards: remaining bytes are consumed by writing the weight and the array values.
299- auto it = _sketch_array.begin ();
300- while (it != _sketch_array.end ()) {
301- write (os, *it);
302- ++it;
303- }
317+ for (const auto & value: _sketch_array) write_value (value);
318+
319+ return bytes_written;
304320}
305321
306322template <typename W, typename A>
@@ -351,40 +367,9 @@ template<typename W, typename A>
351367auto count_min_sketch<W,A>::serialize(unsigned header_size_bytes) const -> vector_bytes {
352368 vector_bytes bytes (header_size_bytes + get_serialized_size_bytes (), 0 , _allocator);
353369 uint8_t *ptr = bytes.data () + header_size_bytes;
354-
355- // Long 0
356- const uint8_t preamble_longs = PREAMBLE_LONGS_SHORT ;
357- ptr += copy_to_mem (preamble_longs, ptr);
358- const uint8_t ser_ver = SERIAL_VERSION_1 ;
359- ptr += copy_to_mem (ser_ver, ptr);
360- const uint8_t family_id = FAMILY_ID ;
361- ptr += copy_to_mem (family_id, ptr);
362- const uint8_t flags_byte = (is_empty () ? 1 << flags::IS_EMPTY : 0 );
363- ptr += copy_to_mem (flags_byte, ptr);
364- const uint32_t unused32 = NULL_32 ;
365- ptr += copy_to_mem (unused32, ptr);
366-
367- // Long 1
368- const uint32_t nbuckets = _num_buckets;
369- const uint8_t nhashes = _num_hashes;
370- const uint16_t seed_hash (compute_seed_hash (_seed));
371- const uint8_t null_characters_8 = NULL_8 ;
372- ptr += copy_to_mem (nbuckets, ptr);
373- ptr += copy_to_mem (nhashes, ptr);
374- ptr += copy_to_mem (seed_hash, ptr);
375- ptr += copy_to_mem (null_characters_8, ptr);
376- if (is_empty ()) return bytes; // sketch is empty, no need to write further bytes.
377-
378- // Long 2
379- const W t_weight = _total_weight;
380- ptr += copy_to_mem (t_weight, ptr);
381-
382- // Long 3 onwards: remaining bytes are consumed by writing the weight and the array values.
383- auto it = _sketch_array.begin ();
384- while (it != _sketch_array.end ()) {
385- ptr += copy_to_mem (*it, ptr);
386- ++it;
387- }
370+ serialize_to ([&ptr](const void * data, size_t size) {
371+ ptr += copy_to_mem (data, ptr, size);
372+ });
388373
389374 return bytes;
390375}
0 commit comments