datasketches 0.5.0

A software library of stochastic streaming algorithms (a.k.a. sketches)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

//! Tuple sketch types.
//!
//! This module provides [`TupleSketch`] (mutable) and [`CompactTupleSketch`] (immutable),
//! the Tuple sketch analogues of the Theta sketch. Each retained key carries a user-defined summary
//! created by a [`SummaryPolicy`] and updated through one or more [`SummaryUpdatePolicy`]
//! implementations.

use std::hash::Hash;
use std::slice;

use crate::codec::SketchBytes;
use crate::codec::SketchSlice;
use crate::codec::assert::ensure_preamble_longs_in_range;
use crate::codec::assert::insufficient_data;
use crate::codec::family::Family;
use crate::common::NumStdDev;
use crate::common::ResizeFactor;
use crate::error::Error;
use crate::error::ErrorKind;
use crate::hash::DEFAULT_UPDATE_SEED;
use crate::hash::check_seed_hash;
use crate::hash::compute_seed_hash;
use crate::thetacommon::EntrySketch;
use crate::thetacommon::KeySketch;
use crate::thetacommon::binomial_bounds;
use crate::thetacommon::constants::DEFAULT_LG_K;
use crate::thetacommon::constants::FLAGS_IS_COMPACT;
use crate::thetacommon::constants::FLAGS_IS_EMPTY;
use crate::thetacommon::constants::FLAGS_IS_ORDERED;
use crate::thetacommon::constants::FLAGS_IS_READ_ONLY;
use crate::thetacommon::constants::MAX_THETA;
use crate::thetacommon::hash_table::SketchHashTableIter;
use crate::thetacommon::sketch_state::CompactSketchState;
use crate::thetacommon::sketch_state::ThetaFamilySketchMetadata;
use crate::tuple::hash_table::TupleEntry;
use crate::tuple::hash_table::TupleHashTable;
use crate::tuple::policy::SummaryPolicy;
use crate::tuple::policy::SummaryUpdatePolicy;
use crate::tuple::serialization::SERIAL_VERSION;
use crate::tuple::serialization::SERIAL_VERSION_LEGACY;
use crate::tuple::serialization::SKETCH_TYPE;
use crate::tuple::serialization::SKETCH_TYPE_LEGACY;
use crate::tuple::serialization::TupleSummaryValue;

/// Read-only view of a mutable or compact Tuple sketch.
///
/// The view borrows the sketch without exposing its update policy. It can inspect keys without
/// requiring `S: Clone`; set operations that retain summaries require `S: Clone` when invoked.
///
/// # Examples
///
/// ```
/// use datasketches::tuple::DefaultUpdatePolicy;
/// use datasketches::tuple::TupleSketchBuilder;
///
/// let mut sketch = TupleSketchBuilder::new(DefaultUpdatePolicy::<u64>::default())
///     .build()
///     .unwrap();
/// sketch.update("apple", 1);
/// let view = sketch.as_view();
/// assert_eq!(view.iter().next().unwrap().summary(), &1);
/// ```
#[derive(Debug)]
pub struct TupleSketchView<'a, S>(TupleSketchViewState<'a, S>);

#[derive(Debug)]
enum TupleSketchViewState<'a, S> {
    Mutable {
        table: &'a TupleHashTable<S>,
        is_empty: bool,
    },
    Compact(&'a CompactTupleSketch<S>),
}

enum TupleSketchIter<'a, S> {
    Mutable(SketchHashTableIter<'a, TupleEntry<S>>),
    Compact(slice::Iter<'a, TupleEntry<S>>),
}

impl<'a, S> Iterator for TupleSketchIter<'a, S> {
    type Item = &'a TupleEntry<S>;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::Mutable(iter) => iter.next(),
            Self::Compact(iter) => iter.next(),
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        match self {
            Self::Mutable(iter) => iter.size_hint(),
            Self::Compact(iter) => iter.size_hint(),
        }
    }
}

impl<S> Clone for TupleSketchView<'_, S> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<S> Copy for TupleSketchView<'_, S> {}

impl<S> Clone for TupleSketchViewState<'_, S> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<S> Copy for TupleSketchViewState<'_, S> {}

impl<'a, S> TupleSketchView<'a, S> {
    /// Returns the 16-bit seed hash.
    pub fn seed_hash(&self) -> u16 {
        match self.0 {
            TupleSketchViewState::Mutable { table, .. } => table.seed_hash(),
            TupleSketchViewState::Compact(sketch) => sketch.seed_hash(),
        }
    }

    /// Returns theta as a `u64` threshold.
    pub fn theta64(&self) -> u64 {
        match self.0 {
            TupleSketchViewState::Mutable { table, is_empty } => {
                if is_empty {
                    MAX_THETA
                } else {
                    table.retention_theta()
                }
            }
            TupleSketchViewState::Compact(sketch) => sketch.theta64(),
        }
    }

    /// Returns `true` if the viewed sketch is empty.
    pub fn is_empty(&self) -> bool {
        match self.0 {
            TupleSketchViewState::Mutable { is_empty, .. } => is_empty,
            TupleSketchViewState::Compact(sketch) => sketch.is_empty(),
        }
    }

    /// Returns whether retained entries are ordered by ascending hash.
    pub fn is_ordered(&self) -> bool {
        match self.0 {
            TupleSketchViewState::Mutable { .. } => false,
            TupleSketchViewState::Compact(sketch) => sketch.is_ordered(),
        }
    }

    /// Returns an iterator over retained entries.
    pub fn iter(self) -> impl Iterator<Item = &'a TupleEntry<S>> + 'a {
        match self.0 {
            TupleSketchViewState::Mutable { table, .. } => {
                TupleSketchIter::Mutable(table.iter_entries())
            }
            TupleSketchViewState::Compact(sketch) => {
                TupleSketchIter::Compact(sketch.compact_state.retained_entries().iter())
            }
        }
    }

    /// Returns the number of retained entries.
    pub fn num_retained(&self) -> usize {
        match self.0 {
            TupleSketchViewState::Mutable { table, .. } => table.num_retained(),
            TupleSketchViewState::Compact(sketch) => sketch.num_retained(),
        }
    }
}

impl<S> KeySketch for TupleSketchView<'_, S> {
    fn metadata(self) -> ThetaFamilySketchMetadata {
        if self.is_empty() {
            ThetaFamilySketchMetadata::Empty {
                seed_hash: self.seed_hash(),
            }
        } else {
            ThetaFamilySketchMetadata::NonEmpty {
                seed_hash: self.seed_hash(),
                theta: self.theta64(),
                ordered: self.is_ordered(),
                num_retained: self.num_retained(),
            }
        }
    }

    fn hashes(self) -> impl Iterator<Item = u64> {
        self.iter().map(TupleEntry::hash)
    }
}

impl<'a, S> EntrySketch for TupleSketchView<'a, S>
where
    S: Clone + 'a,
{
    type Entry = TupleEntry<S>;

    fn entries(self) -> impl Iterator<Item = Self::Entry> {
        self.iter().cloned()
    }
}

impl<'a, P> From<&'a TupleSketch<P>> for TupleSketchView<'a, P::Summary>
where
    P: SummaryPolicy,
{
    fn from(sketch: &'a TupleSketch<P>) -> Self {
        Self(TupleSketchViewState::Mutable {
            table: &sketch.table,
            is_empty: sketch.is_empty,
        })
    }
}

impl<'a, S> From<&'a CompactTupleSketch<S>> for TupleSketchView<'a, S> {
    fn from(sketch: &'a CompactTupleSketch<S>) -> Self {
        Self(TupleSketchViewState::Compact(sketch))
    }
}

/// Mutable Tuple sketch for building from input data.
///
/// `P` defines how summaries are created. The summary retained alongside each key is
/// [`P::Summary`](SummaryPolicy::Summary), while each accepted update type is selected by a
/// [`SummaryUpdatePolicy<U>`] implementation.
///
/// # Examples
///
/// ```
/// use datasketches::tuple::DefaultUpdatePolicy;
/// use datasketches::tuple::TupleSketchBuilder;
///
/// let policy = DefaultUpdatePolicy::<u64>::default();
/// let mut sketch = TupleSketchBuilder::new(policy).build().unwrap();
/// sketch.update("apple", 1);
/// sketch.update("apple", 1);
/// assert!(sketch.estimate() >= 1.0);
/// assert_eq!(sketch.num_retained(), 1);
/// ```
#[derive(Debug)]
pub struct TupleSketch<P>
where
    P: SummaryPolicy,
{
    table: TupleHashTable<P::Summary>,
    // Public emptiness tracks update calls, not retained entries: theta may screen every update.
    is_empty: bool,
    policy: P,
}

impl<P> TupleSketch<P>
where
    P: SummaryPolicy,
{
    /// Returns a read-only view accepted by Tuple set operations.
    pub fn as_view(&self) -> TupleSketchView<'_, P::Summary> {
        self.into()
    }

    /// Updates the sketch with a key and a value accepted by the policy.
    ///
    /// If the key is new, the policy creates a summary and folds in `value`; if the key already
    /// exists, `value` is folded into the retained summary. Updates screened out by theta do not
    /// change any summary.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tuple::DefaultUpdatePolicy;
    /// use datasketches::tuple::TupleSketchBuilder;
    ///
    /// let policy = DefaultUpdatePolicy::<u64>::default();
    /// let mut sketch = TupleSketchBuilder::new(policy).build().unwrap();
    /// sketch.update(42, 5);
    /// ```
    pub fn update<U>(&mut self, key: impl Hash, value: U)
    where
        P: SummaryUpdatePolicy<U>,
    {
        self.is_empty = false;
        let policy = &self.policy;
        self.table.try_insert(key, |existing| match existing {
            Some(summary) => {
                policy.update(summary, value);
                None
            }
            None => {
                let mut summary = policy.create();
                policy.update(&mut summary, value);
                Some(summary)
            }
        });
    }

    /// Returns the cardinality (distinct key count) estimate.
    pub fn estimate(&self) -> f64 {
        if self.is_empty() {
            return 0.0;
        }
        let num_retained = self.table.num_retained() as f64;
        let theta = self.theta64() as f64 / MAX_THETA as f64;
        num_retained / theta
    }

    /// Returns theta as a fraction in `[0.0, 1.0]`.
    pub fn theta(&self) -> f64 {
        self.theta64() as f64 / MAX_THETA as f64
    }

    /// Returns theta as a `u64`.
    ///
    /// An empty sketch reports `MAX_THETA` even when it was built with a sampling probability
    /// below `1.0`, matching the other DataSketches implementations.
    pub fn theta64(&self) -> u64 {
        if self.is_empty {
            MAX_THETA
        } else {
            self.table.retention_theta()
        }
    }

    /// Returns the 16-bit seed hash.
    pub fn seed_hash(&self) -> u16 {
        self.table.seed_hash()
    }

    /// Returns `true` if the sketch is empty.
    pub fn is_empty(&self) -> bool {
        self.is_empty
    }

    /// Returns `true` if the sketch is in estimation mode.
    pub fn is_estimation_mode(&self) -> bool {
        !self.is_empty && self.table.retention_theta() < MAX_THETA
    }

    /// Returns the number of retained entries.
    pub fn num_retained(&self) -> usize {
        self.table.num_retained()
    }

    /// Returns the configured `lg_k`.
    pub fn lg_k(&self) -> u8 {
        self.table.lg_nom_size()
    }

    /// Trims the sketch to the capacity configured by `lg_k`.
    pub fn trim(&mut self) {
        self.table.trim();
    }

    /// Resets the sketch to the empty state.
    pub fn reset(&mut self) {
        self.table.reset();
        self.is_empty = true;
    }

    /// Returns an iterator over retained entries.
    pub fn iter(&self) -> impl Iterator<Item = &TupleEntry<P::Summary>> + '_ {
        self.table.iter()
    }

    /// Returns the approximate lower error bound given the number of standard deviations.
    pub fn lower_bound(&self, num_std_dev: NumStdDev) -> f64 {
        if !self.is_estimation_mode() {
            return self.num_retained() as f64;
        }
        binomial_bounds::lower_bound(self.num_retained() as u64, self.theta(), num_std_dev)
            .expect("theta should always be valid")
    }

    /// Returns the approximate upper error bound given the number of standard deviations.
    pub fn upper_bound(&self, num_std_dev: NumStdDev) -> f64 {
        if !self.is_estimation_mode() {
            return self.num_retained() as f64;
        }
        binomial_bounds::upper_bound(
            self.num_retained() as u64,
            self.theta(),
            num_std_dev,
            self.is_empty(),
        )
        .expect("theta should always be valid")
    }

    /// Returns the estimated size of the sketch in bytes.
    pub fn estimated_size(&self) -> usize {
        size_of::<Self>() + self.table.estimated_size()
    }
}

impl<P> TupleSketch<P>
where
    P: SummaryPolicy,
    P::Summary: Clone,
{
    /// Returns this sketch in compact, immutable form.
    ///
    /// If `ordered` is `true`, retained entries are sorted by hash in ascending order.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tuple::DefaultUpdatePolicy;
    /// use datasketches::tuple::TupleSketchBuilder;
    ///
    /// let policy = DefaultUpdatePolicy::<u64>::default();
    /// let mut sketch = TupleSketchBuilder::new(policy).build().unwrap();
    /// sketch.update("apple", 1);
    /// let compact = sketch.compact(true);
    /// assert_eq!(compact.num_retained(), 1);
    /// ```
    pub fn compact(&self, ordered: bool) -> CompactTupleSketch<P::Summary> {
        let compact_state = if self.is_empty() {
            debug_assert_eq!(self.num_retained(), 0);
            CompactSketchState::empty(self.seed_hash())
        } else {
            self.table.to_non_empty_compact_state(ordered)
        };
        CompactTupleSketch::from_compact_state(compact_state)
    }
}

/// Compact (immutable) Tuple sketch.
///
/// This is the serialization-friendly form: a compact array of retained hash-summary pairs plus
/// theta and a 16-bit seed hash. It can be ordered (sorted ascending by hash) or unordered.
#[derive(Clone, Debug)]
pub struct CompactTupleSketch<S> {
    compact_state: CompactSketchState<TupleEntry<S>>,
}

impl<S> CompactTupleSketch<S> {
    pub(super) fn from_compact_state(compact_state: CompactSketchState<TupleEntry<S>>) -> Self {
        Self { compact_state }
    }

    /// Returns a read-only view accepted by Tuple set operations.
    pub fn as_view(&self) -> TupleSketchView<'_, S> {
        self.into()
    }

    /// Returns the cardinality (distinct key count) estimate.
    pub fn estimate(&self) -> f64 {
        if self.is_empty() {
            return 0.0;
        }
        let num_retained = self.num_retained() as f64;
        if self.theta64() == MAX_THETA {
            return num_retained;
        }
        let theta = self.theta();
        num_retained / theta
    }

    /// Returns theta as a fraction (0.0 to 1.0).
    pub fn theta(&self) -> f64 {
        self.theta64() as f64 / MAX_THETA as f64
    }

    /// Returns theta as `u64`.
    pub fn theta64(&self) -> u64 {
        self.compact_state.theta()
    }

    /// Returns `true` if the sketch is empty.
    pub fn is_empty(&self) -> bool {
        self.compact_state.is_empty()
    }

    /// Returns `true` if the sketch is in estimation mode.
    pub fn is_estimation_mode(&self) -> bool {
        self.compact_state.is_estimation_mode()
    }

    /// Returns the number of retained entries.
    pub fn num_retained(&self) -> usize {
        self.retained_entries().len()
    }

    /// Returns `true` if retained entries are ordered (sorted ascending by hash).
    pub fn is_ordered(&self) -> bool {
        self.compact_state.is_ordered()
    }

    /// Returns the 16-bit seed hash.
    pub fn seed_hash(&self) -> u16 {
        self.compact_state.seed_hash()
    }

    /// Returns an iterator over retained entries.
    pub fn iter(&self) -> impl Iterator<Item = &TupleEntry<S>> + '_ {
        self.retained_entries().iter()
    }

    fn retained_entries(&self) -> &[TupleEntry<S>] {
        self.compact_state.retained_entries()
    }

    /// Returns the approximate lower error bound given the number of standard deviations.
    pub fn lower_bound(&self, num_std_dev: NumStdDev) -> f64 {
        if !self.is_estimation_mode() {
            return self.num_retained() as f64;
        }
        binomial_bounds::lower_bound(self.num_retained() as u64, self.theta(), num_std_dev)
            .expect("compact theta should always be valid")
    }

    /// Returns the approximate upper error bound given the number of standard deviations.
    pub fn upper_bound(&self, num_std_dev: NumStdDev) -> f64 {
        if !self.is_estimation_mode() {
            return self.num_retained() as f64;
        }
        binomial_bounds::upper_bound(
            self.num_retained() as u64,
            self.theta(),
            num_std_dev,
            self.is_empty(),
        )
        .expect("compact theta should always be valid")
    }

    /// Returns the estimated size of the sketch in bytes.
    pub fn estimated_size(&self) -> usize {
        size_of::<Self>()
            + self.compact_state.retained_entries_capacity() * size_of::<TupleEntry<S>>()
    }

    fn preamble_longs(&self) -> u8 {
        if self.is_estimation_mode() {
            3
        } else if self.is_empty() || self.num_retained() == 1 {
            1
        } else {
            2
        }
    }

    /// Serializes this sketch into the compact Tuple binary format.
    ///
    /// Each summary is encoded by its [`TupleSummaryValue`] implementation. The layout matches the
    /// Java/C++ Tuple sketches, so the output can be read by those implementations given a
    /// compatible summary encoding.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tuple::DefaultUpdatePolicy;
    /// use datasketches::tuple::TupleSketchBuilder;
    ///
    /// let policy = DefaultUpdatePolicy::<u64>::default();
    /// let mut sketch = TupleSketchBuilder::new(policy).build().unwrap();
    /// sketch.update("apple", 1);
    /// let bytes = sketch.compact(true).serialize();
    /// assert!(!bytes.is_empty());
    /// ```
    pub fn serialize(&self) -> Vec<u8>
    where
        S: TupleSummaryValue,
    {
        let retained_entries = self.retained_entries();
        let pre_longs = self.preamble_longs();
        let entries_size: usize = retained_entries
            .iter()
            .map(|entry| 8 + entry.summary().serialize_size())
            .sum();
        let mut bytes = SketchBytes::with_capacity(8 * pre_longs as usize + entries_size);

        bytes.write_u8(pre_longs);
        bytes.write_u8(SERIAL_VERSION);
        bytes.write_u8(Family::TUPLE.id);
        bytes.write_u8(SKETCH_TYPE);
        bytes.write_u8(0); // unused

        let mut flags = FLAGS_IS_READ_ONLY | FLAGS_IS_COMPACT;
        if self.is_empty() {
            flags |= FLAGS_IS_EMPTY;
        }
        if self.is_ordered() {
            flags |= FLAGS_IS_ORDERED;
        }
        bytes.write_u8(flags);
        bytes.write_u16_le(self.seed_hash());

        if pre_longs > 1 {
            bytes.write_u32_le(retained_entries.len() as u32);
            bytes.write_u32_le(0); // unused
        }
        if self.is_estimation_mode() {
            bytes.write_u64_le(self.theta64());
        }

        for entry in retained_entries {
            bytes.write_u64_le(entry.hash());
            entry.summary().serialize_value(&mut bytes);
        }
        bytes.into_bytes()
    }

    /// Deserializes a compact Tuple sketch using the default seed.
    ///
    /// # Errors
    ///
    /// Returns `InvalidData` if the image is malformed, its seed hash does not match the default
    /// seed, or a summary cannot be decoded by `S`.
    pub fn deserialize(bytes: &[u8]) -> Result<Self, Error>
    where
        S: TupleSummaryValue,
    {
        Self::deserialize_with_seed(bytes, DEFAULT_UPDATE_SEED)
    }

    /// Deserializes a compact Tuple sketch using the provided expected `seed`.
    ///
    /// # Errors
    ///
    /// Returns `InvalidData` if the bytes are truncated, the family/serial version/sketch type are
    /// unexpected, the seed hash does not match, the supplied seed computes to the reserved zero
    /// seed hash, or an entry is corrupted.
    pub fn deserialize_with_seed(bytes: &[u8], seed: u64) -> Result<Self, Error>
    where
        S: TupleSummaryValue,
    {
        let expected_seed_hash = compute_seed_hash(seed, ErrorKind::InvalidData)?;
        let mut cursor = SketchSlice::new(bytes);
        let pre_longs = cursor
            .read_u8()
            .map_err(insufficient_data("preamble_longs"))?;
        let ser_ver = cursor
            .read_u8()
            .map_err(insufficient_data("serial_version"))?;
        let family_id = cursor.read_u8().map_err(insufficient_data("family_id"))?;
        let sketch_type = cursor.read_u8().map_err(insufficient_data("sketch_type"))?;
        cursor.read_u8().map_err(insufficient_data("<unused>"))?;
        let flags = cursor.read_u8().map_err(insufficient_data("flags"))?;
        let seed_hash = cursor
            .read_u16_le()
            .map_err(insufficient_data("seed_hash"))?;

        Family::TUPLE.validate_id(family_id)?;
        ensure_preamble_longs_in_range(
            Family::TUPLE.min_pre_longs..=Family::TUPLE.max_pre_longs,
            pre_longs,
        )?;
        if ser_ver != SERIAL_VERSION && ser_ver != SERIAL_VERSION_LEGACY {
            return Err(Error::deserial(format!(
                "unsupported serial version: expected {} or {}, got {ser_ver}",
                SERIAL_VERSION, SERIAL_VERSION_LEGACY,
            )));
        }
        if sketch_type != SKETCH_TYPE && sketch_type != SKETCH_TYPE_LEGACY {
            return Err(Error::deserial(format!(
                "unsupported sketch type: expected {} or {}, got {sketch_type}",
                SKETCH_TYPE, SKETCH_TYPE_LEGACY,
            )));
        }

        let empty = (flags & FLAGS_IS_EMPTY) != 0;
        let ordered = (flags & FLAGS_IS_ORDERED) != 0;

        if empty {
            return Ok(Self::from_compact_state(CompactSketchState::empty(
                seed_hash,
            )));
        }

        check_seed_hash(
            expected_seed_hash,
            seed_hash,
            "deserialized CompactTupleSketch",
            ErrorKind::InvalidData,
        )?;

        let mut theta = MAX_THETA;
        let num_entries = if pre_longs == 1 {
            1
        } else {
            let n = cursor
                .read_u32_le()
                .map_err(insufficient_data("num_entries"))? as usize;
            cursor
                .read_u32_le()
                .map_err(insufficient_data("<unused_u32>"))?;
            if pre_longs > 2 {
                let value = cursor.read_u64_le().map_err(insufficient_data("theta"))?;
                if !(1..=MAX_THETA).contains(&value) {
                    return Err(Error::deserial(format!(
                        "corrupted: theta must be in [1, {MAX_THETA}], got {value}"
                    )));
                }
                theta = value;
            }
            n
        };

        let required_hash_bytes = num_entries
            .checked_mul(size_of::<u64>())
            .ok_or_else(|| Error::deserial("Tuple entry payload length overflows"))?;
        if required_hash_bytes > cursor.remaining().len() {
            return Err(Error::insufficient_data(format!(
                "Tuple entry hashes require at least {required_hash_bytes} bytes, got {}",
                cursor.remaining().len()
            )));
        }
        let mut retained_entries = Vec::with_capacity(num_entries);
        for _ in 0..num_entries {
            let hash = cursor
                .read_u64_le()
                .map_err(insufficient_data("entry_hash"))?;
            if hash == 0 || hash >= theta {
                return Err(Error::deserial("corrupted: invalid retained hash value"));
            }
            let summary = S::deserialize_value(&mut cursor)?;
            retained_entries.push(TupleEntry::new(hash, summary));
        }

        Ok(Self::from_compact_state(CompactSketchState::non_empty(
            retained_entries,
            theta,
            seed_hash,
            ordered,
        )))
    }
}

/// Builder for [`TupleSketch`].
///
/// Every builder carries a concrete [`SummaryPolicy`]. Use
/// [`DefaultUpdatePolicy`](crate::tuple::DefaultUpdatePolicy) for default-constructed additive
/// summaries, or supply a custom policy.
///
/// Configuration is stored without validation and checked when [`build()`](Self::build) is called.
#[derive(Debug)]
pub struct TupleSketchBuilder<P>
where
    P: SummaryPolicy,
{
    lg_k: u8,
    resize_factor: ResizeFactor,
    sampling_probability: f32,
    seed: u64,
    policy: P,
}

impl<P> TupleSketchBuilder<P>
where
    P: SummaryPolicy,
{
    /// Creates a builder with the given summary policy.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tuple::SummaryPolicy;
    /// use datasketches::tuple::SummaryUpdatePolicy;
    /// use datasketches::tuple::TupleSketchBuilder;
    ///
    /// struct MaxPolicy;
    ///
    /// impl SummaryPolicy for MaxPolicy {
    ///     type Summary = u64;
    ///
    ///     fn create(&self) -> Self::Summary {
    ///         0
    ///     }
    /// }
    ///
    /// impl SummaryUpdatePolicy<u64> for MaxPolicy {
    ///     fn update(&self, summary: &mut Self::Summary, value: u64) {
    ///         *summary = (*summary).max(value);
    ///     }
    /// }
    ///
    /// let mut sketch = TupleSketchBuilder::new(MaxPolicy).build().unwrap();
    /// sketch.update("k", 3);
    /// sketch.update("k", 7);
    /// ```
    pub fn new(policy: P) -> Self {
        Self {
            lg_k: DEFAULT_LG_K,
            resize_factor: ResizeFactor::X8,
            sampling_probability: 1.0,
            seed: DEFAULT_UPDATE_SEED,
            policy,
        }
    }

    /// Sets `lg_k`, the base-2 logarithm of the nominal capacity.
    pub fn lg_k(mut self, lg_k: u8) -> Self {
        self.lg_k = lg_k;
        self
    }

    /// Sets the resize factor.
    pub fn resize_factor(mut self, factor: ResizeFactor) -> Self {
        self.resize_factor = factor;
        self
    }

    /// Sets the sampling probability.
    pub fn sampling_probability(mut self, probability: f32) -> Self {
        self.sampling_probability = probability;
        self
    }

    /// Sets the hash seed.
    pub fn seed(mut self, seed: u64) -> Self {
        self.seed = seed;
        self
    }

    /// Builds a [`TupleSketch`] using the supplied policy.
    ///
    /// # Errors
    ///
    /// Returns an error if `lg_k` is outside `[5, 26]`, `sampling_probability` is outside
    /// `(0.0, 1.0]`, or the computed seed hash is zero.
    pub fn build(self) -> Result<TupleSketch<P>, Error> {
        Ok(TupleSketch {
            table: TupleHashTable::new(
                self.lg_k,
                self.resize_factor,
                self.sampling_probability,
                self.seed,
            )?,
            is_empty: true,
            policy: self.policy,
        })
    }
}