in cpc/cpc_compressed_state.go [278:338]
func (c *CpcCompressedState) uncompressHybridFlavor(src *CpcSketch) error {
// Ensure that the window compression stream is nil and the CSV stream is present.
if c.CwStream != nil {
return fmt.Errorf("uncompressHybridFlavor: expected CwStream to be nil, got %v", c.CwStream)
}
if c.CsvStream == nil {
return fmt.Errorf("uncompressHybridFlavor: CsvStream is nil")
}
// Uncompress the surprising values (i.e. the pairs) from the CSV stream.
pairs, err := uncompressTheSurprisingValues(c)
if err != nil {
return err
}
numPairs := int(c.NumCsv)
// For the hybrid flavor, some pairs belong to the sliding window.
srcLgK := c.LgK
k := 1 << srcLgK
// Allocate a window of k bytes (one byte per row).
window := make([]byte, k)
// Separate out the pairs that belong in the window.
// Pairs with a column index (low 6 bits) less than 8 are moved into the window.
nextTruePair := 0
for i := 0; i < numPairs; i++ {
rowCol := pairs[i]
if rowCol == -1 {
return fmt.Errorf("uncompressHybridFlavor: invalid pair value -1 at index %d", i)
}
col := rowCol & 63
if col < 8 {
row := rowCol >> 6
window[row] |= 1 << col // set the corresponding bit in the window
} else {
// Move the "true" pair down into the pairs array.
pairs[nextTruePair] = rowCol
nextTruePair++
}
}
// The compressed state's window offset should be 0.
if c.getWindowOffset() != 0 {
return fmt.Errorf("uncompressHybridFlavor: expected windowOffset to be 0, got %d", c.getWindowOffset())
}
// Set the target sketch's windowOffset to 0.
src.windowOffset = 0
// Build a new pair table from the true pairs.
table, err := newInstanceFromPairsArray(pairs, nextTruePair, srcLgK)
if err != nil {
return err
}
src.pairTable = table
// Set the sliding window in the target sketch.
src.slidingWindow = window
return nil
}