|
Data Structures and Algorithms
with Object-Oriented Design Patterns in Python |
As shown in Program
,
objects that implement the Cursor interface
can be used to access, insert, and delete objects in an ordered list.
Program
defines the nested class
called OrderedListAsArray.Cursor
that extends the abstract Cursor class.
The idea is that instances of this class
are used by the OrderedListAsArray class to represent
the abstraction of a position in an ordered list.

Program: OrderedListAsArray.Cursor class __init__ and getDatum methods.
The Cursor class has two instance attributes,
_list and _offset.
The _list instance attribute refers
to an OrderedListAsArray instance
and the _offset instance attribute
records an offset in that list's array of objects.
The __init__ method simply assigns the given values
to the _list and _offset instance attributes.
Program
also defines the getDatum method
of the Cursor class.
This method returns the item in the array
at the position record in the _offset instance attribute,
provided that position is valid.
The running time of the accessor is simply O(1).