25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

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

A sequence is generated by appending elements at its end after having placed a rider on the file.<br />

Assuming the declaration<br />

VAR r: Rider<br />

we position the rider r on the file f by the statement<br />

Set(r, f, pos)<br />

where pos = 0 designates the beginning of the file (sequence). A typical pattern for generating the<br />

sequence is:<br />

WHILE more DO compute next element x; Write(r, x) END<br />

A sequence is inspected by first positioning a rider as shown above, <strong>and</strong> then proceeding from element to<br />

element. A typical pattern for reading a sequence is:<br />

Read(r, x);<br />

WHILE ~r.eof DO process element x; Read(r, x) END<br />

Evidently, a certain position is always associated with every rider. It is denoted by r.pos. Furthermore,<br />

we postulate that a rider contain a predicate (flag) r.eof indicating whether a preceding read operation had<br />

reached the sequence's end. We can now postulate <strong>and</strong> describe informally the following set of primitive<br />

operators:<br />

1a. New(f, name) defines f to be the empty sequence.<br />

1b. Old(f, name) defines f to be the sequence persistently stored with given name.<br />

2. Set(r, f, pos) associate rider r with sequence f, <strong>and</strong> place it at position pos.<br />

3. Write(r, x) place element with value x in the sequence designated by rider r, <strong>and</strong> advance.<br />

4. Read(r, x) assign to x the value of the element designated by rider r, <strong>and</strong> advance.<br />

5. Close(f) registers the written file f in the persistent store (flush buffers).<br />

Note. Writing an element in a sequence is often a complex operation. However, mostly, files are created<br />

by appending elements at the end.<br />

Translator's note. The program examples in the book use two more comm<strong>and</strong>s:<br />

6. WriteInt(r, n) place the integer n in the sequence designated by rider r, <strong>and</strong> advance.<br />

7. ReadInt(r, n) assign to n the integer value designated by rider r, <strong>and</strong> advance.<br />

In order to convey a more precise underst<strong>and</strong>ing of the sequencing operators, the following example of<br />

an implementation is provided. It shows how they might be expressed if sequences were represented by<br />

arrays. This example of an implementation intentionally builds upon concepts introduced <strong>and</strong> discussed<br />

earlier, <strong>and</strong> it does not involve either buffering or sequential stores which, as mentioned above, make the<br />

sequence concept truly necessary <strong>and</strong> attractive. Nevertheless, this example exhibits all the essential<br />

characteristics of the primitive sequence operators, independently of how the sequences are represented in<br />

store.<br />

The operators are presented in terms of conventional procedures. This collection of definitions of types,<br />

variables, <strong>and</strong> procedure headings (signatures) is called a definition. We assume that we are to deal with<br />

sequences of characters, i.e. text files whose elements are of type CHAR. The declarations of File <strong>and</strong> Rider<br />

are good examples of an application of record structures because, in addition to the field denoting the array<br />

which represents the data, further fields are required to denote the current length <strong>and</strong> position, i.e. the state<br />

of the rider.

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

Saved successfully!

Ooh no, something went wrong!