leerho opened a new pull request, #529: URL: https://github.com/apache/datasketches-cpp/pull/529
Follow-up to #527, which made `is_empty()` mean `total_weight == 0`. A sketch whose purges cleared every counter is non-empty and now serializes with the full preamble and no items. This PR makes the read side and the API consistent with that. ### Changes - **`reset()`**, paralleling Java's `reset()`. Returns the sketch to the empty state: `total_weight` and `offset` are zeroed and the map restarts at `LG_MIN_MAP_SIZE`. The maximum map size, equality operator and allocator are retained. Adds a `get_equal()` accessor to `reverse_purge_hash_map` for this. - **Emptiness on read is decided by preamble longs.** Preamble longs must be 1 (empty) or 4 (non-empty). The flags byte is only cross-checked against it; either legacy empty bit (0x01 or 0x04) is still accepted, and a mismatch is rejected as corruption, as before. - **Reject a non-empty image with non-positive total weight.** No valid writer produces one, and it would otherwise load as an empty sketch and be rewritten in the 8-byte form. Written as `!(total_weight > 0)` so NaN is rejected for floating-point `W`. - **Documented the flags byte**: preamble longs decide emptiness, both empty bits are written only for historical compatibility, and no other flag bits are defined. No change to the serialized format. Every image that deserialized before still deserializes, except the non-empty-with-zero-weight case above. ### Tests - Purged-to-zero sketch: 32-byte image, preamble longs 4, flags 0, stream round trip (#527 covered the byte path). - `reset()` after purges: empty, 8-byte image, max map size retained, and subsequent updates serialize identically to a newly constructed sketch. - Empty image accepted with flags 0x01, 0x04 and 0x05. - Corrupt images rejected: invalid preamble longs, empty flag disagreeing with preamble longs in either direction, zero total weight (bytes and stream). All `fi_test` cases pass, including `[serde_compat]` against the current Java-generated images. ### Related A parallel Java PR will follow: `isEmpty()` as `streamWeight == 0`, the same read-side checks, and fixes to the `FrequentLongsSketch` string format. 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
