CPUs z Example data compressor 2008 Wayne Wolf

  • Slides: 21
Download presentation
CPUs z. Example: data compressor. © 2008 Wayne Wolf Overheads for Computers as Components

CPUs z. Example: data compressor. © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed.

Goals z. Compress data transmitted over serial line. y. Receives byte-size input symbols. y.

Goals z. Compress data transmitted over serial line. y. Receives byte-size input symbols. y. Produces output symbols packed into bytes. z. Will build software module only here. © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed.

Collaboration diagram for compressor : input 1. . m: packed 1. . n: input

Collaboration diagram for compressor : input 1. . m: packed 1. . n: input output symbols : data compressor : output © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed.

Huffman coding z Early statistical text compression algorithm. z Select non-uniform size codes. y.

Huffman coding z Early statistical text compression algorithm. z Select non-uniform size codes. y. Use shorter codes for more common symbols. y. Use longer codes for less common symbols. z To allow decoding, codes must have unique prefixes. y. No code can be a prefix of a longer valid code. © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed.

Huffman example character a b c d e f v P. 45. 24. 11.

Huffman example character a b c d e f v P. 45. 24. 11. 08. 07. 05 P=1 P=. 55 P=. 19 P=. 31 P=. 12 Overheads for Computers as Components 2 nd ed.

Example Huffman code z. Read code from root to leaves: a 1 b 01

Example Huffman code z. Read code from root to leaves: a 1 b 01 c 0000 d 0001 e 0010 f 0011 © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed.

Huffman coder requirements table © 2008 Wayne Wolf Overheads for Computers as Components 2

Huffman coder requirements table © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed.

Building a specification z. Collaboration diagram shows only steadystate input/output. z. A real system

Building a specification z. Collaboration diagram shows only steadystate input/output. z. A real system must: y. Accept an encoding table. y. Allow a system reset that flushes the compression buffer. © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed.

data-compressor class data-compressor buffer: data-buffer table: symbol-table current-bit: integer encode(): boolean, data-buffer flush() new-symbol-table()

data-compressor class data-compressor buffer: data-buffer table: symbol-table current-bit: integer encode(): boolean, data-buffer flush() new-symbol-table() © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed.

data-compressor behaviors zencode: Takes one-byte input, generates packed encoded symbols and a Boolean indicating

data-compressor behaviors zencode: Takes one-byte input, generates packed encoded symbols and a Boolean indicating whether the buffer is full. znew-symbol-table: installs new symbol table in object, throws away old table. zflush: returns current state of buffer, including number of valid bits in buffer. © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed.

Auxiliary classes data-buffer symbol-table databuf[databuflen] : character len : integer symbols[nsymbols] : data-buffer len

Auxiliary classes data-buffer symbol-table databuf[databuflen] : character len : integer symbols[nsymbols] : data-buffer len : integer insert() length() : integer value() : symbol load() © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed.

Auxiliary class roles zdata-buffer holds both packed and unpacked symbols. y. Longest Huffman code

Auxiliary class roles zdata-buffer holds both packed and unpacked symbols. y. Longest Huffman code for 8 -bit inputs is 256 bits. zsymbol-table indexes encoded verison of each symbol. yload() puts data in a new symbol table. © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed.

Class relationships 1 data-compressor 1 1 data-buffer © 2008 Wayne Wolf 1 symbol-table Overheads

Class relationships 1 data-compressor 1 1 data-buffer © 2008 Wayne Wolf 1 symbol-table Overheads for Computers as Components 2 nd ed.

Encode behavior input symbol T encode return true buffer filled? F © 2008 Wayne

Encode behavior input symbol T encode return true buffer filled? F © 2008 Wayne Wolf create new buffer add to buffers add to buffer Overheads for Computers as Components 2 nd ed. return false

Insert behavior input symbol T pack into this buffer fills buffer? F © 2008

Insert behavior input symbol T pack into this buffer fills buffer? F © 2008 Wayne Wolf pack bottom bits into this buffer, top bits into overflow buffer Overheads for Computers as Components 2 nd ed. update length

Program design z. In an object-oriented language, we can reflect the UML specification in

Program design z. In an object-oriented language, we can reflect the UML specification in the code more directly. z. In a non-object-oriented language, we must either: yadd code to provide object-oriented features; ydiverge from the specification structure. © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed.

C++ classes Class data_buffer { char databuf[databuflen]; int length_in_chars() { return len/bitsperbyte; } public:

C++ classes Class data_buffer { char databuf[databuflen]; int length_in_chars() { return len/bitsperbyte; } public: void insert(data_buffer, data_buffer&); int length() { return len; } int length_in_bytes() { return (int)ceil(len/8. 0); } int initialize(); . . . © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed.

C++ classes, cont’d. class data_compressor { data_buffer; int current_bit; symbol_table; public: boolean encode(char, data_buffer&);

C++ classes, cont’d. class data_compressor { data_buffer; int current_bit; symbol_table; public: boolean encode(char, data_buffer&); void new_symbol_table(symbol_table); int flush(data_buffer&); data_compressor(); ~data_compressor(); } © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed.

C code struct data_compressor_struct { data_buffer; int current_bit; sym_table; } typedef struct data_compressor_struct data_compressor,

C code struct data_compressor_struct { data_buffer; int current_bit; sym_table; } typedef struct data_compressor_struct data_compressor, *data_compressor_ptr; boolean data_compressor_encode(data_compressor_ptr mycmptrs, char isymbol, data_buffer *fullbuf). . . © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed.

Testing z. Test by encoding, then decoding: symbol table input symbols encoder decoder compare

Testing z. Test by encoding, then decoding: symbol table input symbols encoder decoder compare © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed. result

Code inspection tests z. Look at the code for potential problems: y. Can we

Code inspection tests z. Look at the code for potential problems: y. Can we run past end of symbol table? y. What happens when the next symbol does not fill the buffer? Does fill it? y. Do very long encoded symbols work properly? Very short symbols? y. Does flush() work properly? © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed.