Next: Comparing sequence data
Up: Sequence Interfaces
Previous: The Sequence Abstract
How would this translate into C?
First some type definitions:
-
typedef ReturnCode int;
/* 1 for success, zero or negative for failure */
-
typedef Position int; /* a position in the sequence */
/* the first item in the sequence is at position 0 */
-
typedef Item unsigned char;
/* they are sequences of eight bit bytes */
-
typedef struct {
/* To be determined */
/* Whatever information we need for the data structures we choose */
} Sequence;
In this interface the only operations that change
the Sequence are Insert and Delete.
-
Sequence Empty( );
-
ReturnCode Insert( Sequence *sequence, Position position,
Item ch );
-
ReturnCode Delete( Sequence *sequence, Position position );
-
Item ItemAt( Sequence *sequence, Position position ); --
This does not actually require a pointer to a Sequence
since no change to the sequence
is being made but we expect that they will be large structures and
should not be passing them around.
I am ignoring error returns (e.g., position out of range)
for the purposes of this discussion.
These are easily added if desired.
-
ReturnCode Close( Sequence *sequence );
Many variations are possible.
The next few paragraphs discuss some of them.
Any practical interface would allow the sequence to be
initialized with the contexts of a file.
In theory this is just the Empty operation followed
by an Insert operation for each character in the
initializing file.
Of course, this is too inefficient for a real text editor.
Instead we would have a NewSequence operation:
-
Sequence NewSequence( char * file_name ); --
The sequence is initialized with the contents of
the file whose name is contained in `file_name'.
Usually the Delete operation will delete any logically
contiguous subsequence
-
ReturnCode Delete( Sequence *sequence, Position beginPosition,
Position endPosition );
Sometimes the Insert operation will insert a subsequence
instead of just a single character.
-
ReturnCode Insert( Sequence *sequence, Position position,
Sequence sequenceToInsert );
Sometimes Copy and Move are separate operations
(instead of being composed of Inserts and Deletes).
-
ReturnCode Copy( Sequence *sequence, Position fromBegin,
Position fromEnd, Position toPosition );
-
ReturnCode Move( Sequence *sequence, Position fromBegin,
Position fromEnd, Position toPosition );
A Replace operation that subsumes Insert and Delete
in another possibility.
-
ReturnCode Replace( Sequence *sequence, Position fromBegin,
Position fromEnd, Sequence sequenceToReplaceItWith );
Finally the ItemAt procedure could retrieve a subsequence.
-
ReturnCode SequenceAt( Sequence *sequence, Position fromBegin,
Position fromEnd, Sequence *returnedSequence );
These variations will not affect the basic character of the
data structure used to implement the sequence or the comparisons
between them that follow.
Therefore I will assume the first interface
(Empty, Insert, Delete, IntemAt, and Close).
Next: Comparing sequence data
Up: Sequence Interfaces
Previous: The Sequence Abstract
Charlie Crowley
Thu Jun 27 15:36:10 MDT 1996