@@ -46,6 +46,10 @@ use crate::theta::serialization::V2_PREAMBLE_PRECISE;
4646use crate :: thetacommon:: RawThetaSketchView ;
4747use crate :: thetacommon:: binomial_bounds;
4848use crate :: thetacommon:: constants:: DEFAULT_LG_K ;
49+ use crate :: thetacommon:: constants:: FLAGS_IS_COMPACT ;
50+ use crate :: thetacommon:: constants:: FLAGS_IS_EMPTY ;
51+ use crate :: thetacommon:: constants:: FLAGS_IS_ORDERED ;
52+ use crate :: thetacommon:: constants:: FLAGS_IS_READ_ONLY ;
4953use crate :: thetacommon:: constants:: MAX_LG_K ;
5054use crate :: thetacommon:: constants:: MAX_THETA ;
5155use crate :: thetacommon:: constants:: MIN_LG_K ;
@@ -220,25 +224,18 @@ impl ThetaSketch {
220224 /// assert_eq!(compact.num_retained(), 1);
221225 /// ```
222226 pub fn compact ( & self , ordered : bool ) -> CompactThetaSketch {
223- let mut entries: Vec < u64 > = self . iter ( ) . map ( |entry| entry. hash ( ) ) . collect ( ) ;
224-
225- let empty = self . is_empty ( ) ;
226- let theta = if empty {
227- // Match Java's correctThetaOnCompact() behavior for never-updated sketches
228- // initialized with p < 1.0.
229- MAX_THETA
230- } else {
231- self . table . theta ( )
232- } ;
233- let is_single = entries. len ( ) == 1 && theta == MAX_THETA ;
234- // Empty or Single-item sketches are always ordered (Java compatibility)
235- let ordered = ordered || empty || is_single;
236-
237- if ordered && entries. len ( ) > 1 {
238- entries. sort_unstable ( ) ;
239- }
240-
241- CompactThetaSketch :: from_parts ( entries, theta, self . table . seed_hash ( ) , ordered, empty)
227+ let parts = self . table . to_compact_parts ( ordered) ;
228+ CompactThetaSketch :: from_parts (
229+ parts
230+ . entries
231+ . into_iter ( )
232+ . map ( |entry| entry. hash ( ) )
233+ . collect ( ) ,
234+ parts. theta ,
235+ parts. seed_hash ,
236+ parts. ordered ,
237+ parts. empty ,
238+ )
242239 }
243240
244241 /// Returns the approximate lower error bound given the specified number of Standard Deviations.
@@ -467,13 +464,13 @@ impl CompactThetaSketch {
467464 bytes. write_u16_be ( 0 ) ; // unused for compact
468465
469466 let mut flags = 0u8 ;
470- flags |= serialization :: FLAGS_IS_READ_ONLY ;
471- flags |= serialization :: FLAGS_IS_COMPACT ;
467+ flags |= FLAGS_IS_READ_ONLY ;
468+ flags |= FLAGS_IS_COMPACT ;
472469 if self . is_empty ( ) {
473- flags |= serialization :: FLAGS_IS_EMPTY ;
470+ flags |= FLAGS_IS_EMPTY ;
474471 }
475472 if self . is_ordered ( ) {
476- flags |= serialization :: FLAGS_IS_ORDERED ;
473+ flags |= FLAGS_IS_ORDERED ;
477474 }
478475 bytes. write_u8 ( flags) ;
479476
@@ -510,9 +507,9 @@ impl CompactThetaSketch {
510507 bytes. write_u8 ( num_entries_bytes) ;
511508
512509 let mut flags = 0u8 ;
513- flags |= serialization :: FLAGS_IS_READ_ONLY ;
514- flags |= serialization :: FLAGS_IS_COMPACT ;
515- flags |= serialization :: FLAGS_IS_ORDERED ;
510+ flags |= FLAGS_IS_READ_ONLY ;
511+ flags |= FLAGS_IS_COMPACT ;
512+ flags |= FLAGS_IS_ORDERED ;
516513 bytes. write_u8 ( flags) ;
517514
518515 bytes. write_u16_le ( self . seed_hash ) ;
@@ -748,7 +745,7 @@ impl CompactThetaSketch {
748745 . read_u16_le ( )
749746 . map_err ( insufficient_data ( "seed_hash" ) ) ?;
750747
751- let empty = ( flags & serialization :: FLAGS_IS_EMPTY ) != 0 ;
748+ let empty = ( flags & FLAGS_IS_EMPTY ) != 0 ;
752749 let mut theta = MAX_THETA ;
753750 let num_entries;
754751 let mut entries = vec ! [ ] ;
@@ -776,7 +773,7 @@ impl CompactThetaSketch {
776773 }
777774 entries = Self :: read_entries ( & mut cursor, num_entries as usize , theta) ?;
778775 }
779- let ordered = ( flags & serialization :: FLAGS_IS_ORDERED ) != 0 ;
776+ let ordered = ( flags & FLAGS_IS_ORDERED ) != 0 ;
780777 Ok ( Self {
781778 entries,
782779 theta,
@@ -797,7 +794,7 @@ impl CompactThetaSketch {
797794 let seed_hash = cursor
798795 . read_u16_le ( )
799796 . map_err ( insufficient_data ( "seed_hash" ) ) ?;
800- let empty = ( flags & serialization :: FLAGS_IS_EMPTY ) != 0 ;
797+ let empty = ( flags & FLAGS_IS_EMPTY ) != 0 ;
801798 if !empty {
802799 let expected_seed_hash = compute_seed_hash ( expected_seed) ;
803800 if seed_hash != expected_seed_hash {
@@ -861,7 +858,7 @@ impl CompactThetaSketch {
861858 }
862859 }
863860
864- let ordered = ( flags & serialization :: FLAGS_IS_ORDERED ) != 0 ;
861+ let ordered = ( flags & FLAGS_IS_ORDERED ) != 0 ;
865862
866863 Ok ( Self {
867864 entries,
0 commit comments