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 25<br />

filled. This results in very significantly more effective use of secondary storage. Given sequential access<br />

only, the buffering mechanism is reasonably straightforward for all sequences <strong>and</strong> all media. It can therefore<br />

safely be built into a system for general use, <strong>and</strong> the programmer need not be burdened by incorporating it<br />

in the program. Such a system is usually called a file system, because the high-volume, sequential access<br />

devices are used for permanent storage of (persistent) data, <strong>and</strong> they retain them even when the computer<br />

is switched off. The unit of data on these media is commonly called (sequential) file. Here we will use the<br />

term file as synonym to sequence.<br />

There exist certain storage media in which the sequential access is indeed the only possible one. Among<br />

them are evidently all kinds of tapes. But even on magnetic disks each recording track constitutes a storage<br />

facility allowing only sequential access. Strictly sequential access is the primary characteristic of every<br />

mechanically moving device <strong>and</strong> of some other ones as well.<br />

It follows that it is appropriate to distinguish between the data structure, the sequence, on one h<strong>and</strong>,<br />

<strong>and</strong> the mechanism to access elements on the other h<strong>and</strong>. The former is declared as a data structure, the<br />

latter typically by the introduction of a record with associated operators, or, according to more modern<br />

terminology, by a rider object. The distinction between data <strong>and</strong> mechanism declarations is also useful in<br />

view of the fact that several access points may exist concurrently on one <strong>and</strong> the same sequence, each one<br />

representing a sequential access at a (possibly) different location.<br />

We summarize the essence of the foregoing as follows:<br />

1. Arrays <strong>and</strong> records are r<strong>and</strong>om access structures. They are used when located in primary, r<strong>and</strong>omaccess<br />

store.<br />

2. Sequences are used to access data on secondary, sequential-access stores, such as disks <strong>and</strong> tapes.<br />

3. We distinguish between the declaration of a sequence variable, <strong>and</strong> that of an access mechanism<br />

located at a certain position within the seqence.<br />

1.7.1 Elementary File Operators<br />

The discipline of sequential access can be enforced by providing a set of seqencing operators through<br />

which files can be accessed exclusively. Hence, although we may here refer to the i-th element of a<br />

sequence s by writing s i , this shall not be possible in a program.<br />

Sequences, files, are typically large, dynamic data structures stored on a secondary storage device. Such<br />

a device retains the data even if a program is terminated, or a computer is switched off. Therefore the<br />

introduction of a file variable is a complex operation connecting the data on the external device with the file<br />

variable in the program. We therefore define the type File in a separate module, whose definition specifies<br />

the type together with its operators. We call this module Files <strong>and</strong> postulate that a sequence or file variable<br />

must be explicitly initialized (opened) by calling an appropriate operator or function:<br />

VAR f: File<br />

f := Open(name)<br />

where name identifies the file as recorded on the persistent data carrier. Some systems distinguish between<br />

opening an existing file <strong>and</strong> opening a new file:<br />

f := Old(name)<br />

f := New(name)<br />

The disconnection between secondary storage <strong>and</strong> the file variable then must also be explicitly requested<br />

by, for example, a call of Close(f).<br />

Evidently, the set of operators must contain an operator for generating (writing) <strong>and</strong> one for inspecting<br />

(reading) a sequence. We postulate that these operations apply not to a file directly, but to an object called<br />

a rider, which itself is connected with a file (sequence), <strong>and</strong> which implements a certain access mechanism.<br />

The sequential access discipline is guaranteed by a restrictive set of access operators (procedures).

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

Saved successfully!

Ooh no, something went wrong!