20.03.2013 Views

II. Notes on Data Structuring * - Cornell University

II. Notes on Data Structuring * - Cornell University

II. Notes on Data Structuring * - Cornell University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

NOTES ON DATA STRUCTURING 109<br />

The minimal representati<strong>on</strong> is not often used for data storage, since the<br />

small amount of extra space it saves (always less than <strong>on</strong>e bit per comp<strong>on</strong>ent)<br />

is usually more than outweighed by the extra time taken by multiplying and<br />

dividing <strong>on</strong> every access to the comp<strong>on</strong>ents, as compared with the more<br />

usual shifting and masking. However, the technique can be useful, possibly<br />

in c<strong>on</strong>juncti<strong>on</strong> with more c<strong>on</strong>venti<strong>on</strong>al packing, if there is no other way of<br />

fitting the value within c<strong>on</strong>venient word boundaries. Also, if the value is to<br />

be used solely or primarily as an index to a multi-dimensi<strong>on</strong>al array, the<br />

minimal representati<strong>on</strong> is to be preferred; since this will save a significant<br />

amount of space in the representati<strong>on</strong> of the array (see Secti<strong>on</strong> 6.2).<br />

In representing the with c<strong>on</strong>structi<strong>on</strong> in machine code, it is sometimes<br />

c<strong>on</strong>venient to compute the address of the structure being referenced and<br />

store it in a register; this may achieve shorter and faster code for accessing<br />

the comp<strong>on</strong>ents. If the comp<strong>on</strong>ents have been packed, it may pay to unpack<br />

them into separate words before starting to process them, so that they can<br />

be easily referenced or updated; and if they have been updated, they must be<br />

packed up again and stored in the structure when the processing is complete.<br />

On some machines, it is more ec<strong>on</strong>omic to pack and unpack a whole structure<br />

at the same time, rather than to perform these operati<strong>on</strong>s <strong>on</strong>e at a time <strong>on</strong><br />

the comp<strong>on</strong>ents.<br />

Exercise<br />

Given a variable<br />

today: date;<br />

write a program to assign the value of the next following date to the variable<br />

tomorrow:date. Translate this program into the machine code of your<br />

choice using a tightly packed representati<strong>on</strong>. Rewrite the program using an<br />

unpacked and then a minimal representati<strong>on</strong>. Compare the lengths of the<br />

code involved, and the time taken to execute them.<br />

5. THE DISCRIMINATED UNION<br />

In defining sets of objects, it is often useful to define <strong>on</strong>e set as the uni<strong>on</strong><br />

of two previously known sets. For example, when jokers~ are added to a<br />

standard pack of cards, the extended set may be described as the uni<strong>on</strong> of<br />

the standard set plus the set c<strong>on</strong>sisting of the "wild" cards, joker 1 and<br />

joker 2. A type whose values range over the members of this set may be<br />

declared as the uni<strong>on</strong> of two alternatives, the card type, and an enumerati<strong>on</strong><br />

type with two distinct values:<br />

type pokercard = (normal: (s: suit; r: rank),<br />

wild : (joker 1, joker 2)).

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

Saved successfully!

Ooh no, something went wrong!