25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

N.Wirth. <strong>Algorithms</strong> <strong>and</strong> <strong>Data</strong> <strong>Structures</strong>. Oberon version 23<br />

data structures. However, it should be possible to indicate the desirability of packing at least in those cases<br />

in which more than one component would fit into a single word, i.e., when a gain of storage economy by a<br />

factor of 2 <strong>and</strong> more could be achieved. We propose the convention to indicate the desirability of packing<br />

by prefixing the symbol ARRAY (or RECORD) in the declaration by the symbol PACKED.<br />

1.6.2 Representation of Records<br />

Records are mapped onto a computer store by simply juxtaposing their components. The address of a<br />

component (field) r i relative to the origin address of the record r is called the field's offset k i . It is computed<br />

as<br />

k i = s 1 + s 2 + ... + s i-1 k 0 = 0<br />

where s j is the size (in words) of the j-th component. We now realize that the fact that all components of an<br />

array are of equal type has the welcome consequence that k i = i × s. The generality of the record<br />

structure does unfortunately not allow such a simple, linear function for offset address computation, <strong>and</strong> it is<br />

therefore the very reason for the requirement that record components be selectable only by fixed<br />

identifiers. This restriction has the desirable benefit that the respective offsets are known at compile time.<br />

The resulting greater efficiency of record field access is well-known.<br />

The technique of packing may be beneficial, if several record components can be fitted into a single<br />

storage word (see Fig. 1.7). Since offsets are computable by the compiler, the offset of a field packed<br />

within a word may also be determined by the compiler. This means that on many computers packing of<br />

records causes a deterioration in access efficiency considerably smaller than that caused by the packing of<br />

arrays.<br />

s 1<br />

s 2 s 3<br />

s 4<br />

padded<br />

s 5<br />

s 6 s 7 s 8<br />

Fig. 1.7. Representation of a packed record<br />

1.6.3 Representation of Sets<br />

A set s is conveniently represented in a computer store by its characteristic function C(s). This is an<br />

array of logical values whose ith component has the meaning "i is present in s". As an example, the set of<br />

small integers s = {2, 3, 5, 7, 11, 13} is represented by the sequence of bits, by a bitstring:<br />

C(s) = (… 0010100010101100)<br />

The representation of sets by their characteristic function has the advantage that the operations of<br />

computing the union, intersection, <strong>and</strong> difference of two sets may be implemented as elementary logical<br />

operations. The following equivalences, which hold for all elements i of the base type of the sets x <strong>and</strong> y,<br />

relate logical operations with operations on sets:<br />

i IN (x+y) = (i IN x) OR (i IN y)<br />

i IN (x*y) = (i IN x) & (i IN y)<br />

i IN (x-y) = (i IN x) & ~(i IN y)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!