liveBook · Manning

Compression

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

Compression is the process of representing data with fewer bits by eliminating or encoding redundancy.

Compression encodes data into a smaller representation, while decompression reverses that encoding and restores the original data. It trades processing time for reduced storage or transmission space. section 1.2

A practical compression decision follows three observations:

  • Choose compression when smaller storage or transmission size matters more than immediate execution speed. section 1.2
  • Use a narrower storage type when the possible values already fit within fewer bits; a UInt16 can replace an oversized UInt for values up to 65,535. section 1.2
  • Recognize simple bit-string compression as a special case; broader algorithms usually remove repeated information by finding patterns or structure. section 1.6

Encoding a gene

A gene containing only A, C, G, and T needs two bits per nucleotide: 00, 01, 10, and 11. Store those pairs in a bit vector, retain the original length, and decode each pair to reconstruct the gene. section 1.2

Figure 1.5. Compressing a String representing a gene into a 2-bit-per-nucleotide bit string.

Use the figure to trace how nucleotide characters become a compact two-bit string. section 1.2

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Develops type narrowing and two-bit encoding with compression and decompression, but stops short of advanced pattern-based algorithms.
Examples
Uses oversized unsigned integers, a two-bit gene encoding, bit vectors, and internet file transmission as examples.
What it assumes
Assumes familiarity with data types and introduces bits, binary values, and the relationship between bit width and representable values.
What you can do after
Apply compression by choosing compact numeric types and encoding fixed-alphabet data into bit vectors.
How it connects
Connects compression with decompression, bit-level manipulation, data representation, storage, and bandwidth.

Also explained in

Compare how 11 books explain compression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.