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.

140 c. A, R. HOARE<br />

mark-collect garbage collecti<strong>on</strong>. Although it appears to relieve the pro-<br />

grammer of the resp<strong>on</strong>sibility of explicit c<strong>on</strong>trol of main store allocati<strong>on</strong> and<br />

deallocati<strong>on</strong>, this can be dangerous in n<strong>on</strong>-trivial computer applicati<strong>on</strong>s<br />

where the resp<strong>on</strong>sibility is <strong>on</strong>e that cannot so lightly be evaded.<br />

In the case of a deque, when reading is required in both directi<strong>on</strong>s, a single-<br />

linked chain is no l<strong>on</strong>ger adequate; and the usual soluti<strong>on</strong> is to adjoin two<br />

pointers to each item in the chain, <strong>on</strong>e pointing to the previous item and <strong>on</strong>e<br />

pointing to the following item. In fact these two pointers can be compressed<br />

into a single word c<strong>on</strong>taining <strong>on</strong>ly the difference between them. Since in<br />

the first and last items <strong>on</strong>e of the pointers is a standard null value, the value<br />

of the other pointer from these items can always be obtained by subtracti<strong>on</strong>.<br />

On reading or writing, the value of the link locati<strong>on</strong> for the new first or last<br />

item can be readily adjusted, since at this stage the address of the previous<br />

first or last item is still known. The detailed working out of this scheme is<br />

left as an exercise.<br />

An alternative method of linking the items of a chain is to collect all links<br />

together in a single c<strong>on</strong>tiguous table, preferably of fixed length. This giwes a<br />

form of tree representati<strong>on</strong> for the sequence, and permits ready scanning<br />

in both directi<strong>on</strong>s. But it places an upper bound <strong>on</strong> the number of items in<br />

the sequence; and it means that the locati<strong>on</strong>s used for links must be per-<br />

manently allocated, even at times when the sequence is relatively short.<br />

This problem can be mitigated by the use of blocking.<br />

8.2.3 Blocked Representati<strong>on</strong><br />

One disadvantage of chaining is the amount of extra storage required to hold<br />

the links, and the time taken to administer the free store chain <strong>on</strong> each<br />

operati<strong>on</strong>. These problems are particularly severe when the size of the<br />

individual items of the sequence are small and the sequence is l<strong>on</strong>g. The<br />

method of solving this problem is to use blocking; that is, a combinati<strong>on</strong> of<br />

the c<strong>on</strong>tiguous and chained techniques.<br />

In this technique, a fixed-length block of storage is allocated, sufficient to<br />

hold perhaps between ten and a hundred items. When this block is filled, a<br />

new block is chained to it, using any of the methods described in the previous<br />

secti<strong>on</strong>. On input, a block is not released to free store until all the items it<br />

c<strong>on</strong>tains have been scanned. Thus the amount of store used <strong>on</strong> links can be<br />

reduced to negligible proporti<strong>on</strong>s. This can be of particular benefit in the<br />

tree representati<strong>on</strong> of the chain.<br />

As menti<strong>on</strong>ed above, the use of blocking can also avoid the<br />

problems arising from variable-length dynamic storage allocati<strong>on</strong>, since the<br />

size of the block may be held c<strong>on</strong>stant for all sequences, independent of the<br />

size of their items. Furthermore, in cases where part or all of the sequence<br />

is to be held <strong>on</strong> backing store, the use of blocking is almost universally

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

Saved successfully!

Ooh no, something went wrong!