Go to the previous, next section.
Many useful classes operate as containers of elements. Techniques for
accessing these elements from a container differ from class to class.
In the GNU C++ library, access methods have been partially standardized
across different classes via the use of pseudo-indexes called
Pixes
. A Pix
acts in some ways like an index, and in some
ways like a pointer. (Their underlying representations are just
void*
pointers). A Pix
is a kind of "key" that is
translated into an element access by the class. In virtually all cases,
Pixes
are pointers to some kind internal storage cells. The
containers use these pointers to extract items.
Pixes
support traversal and inspection of elements in a
collection using analogs of array indexing. However, they are
pointer-like in that 0
is treated as an invalid Pix
, and
unsafe insofar as programmers can attempt to access nonexistent elements
via dangling or otherwise invalid Pixes
without first checking
for their validity.
In general it is a very bad idea to perform traversals in the the midst of destructive modifications to containers.
Typical applications might include code using the idiom
for (Pix i = a.first(); i != 0; a.next(i)) use(a(i));for some container
a
and function use
.
Classes supporting the use of Pixes
always contain the following
methods, assuming a container a
of element types of Base
.
Pix i = a.first()
a.next(i)
Base x = a(i); a(i) = x;
int present = a.owns(i)
Some container classes also support backwards traversal via
Pix i = a.last()
a.prev(i)
Collections supporting elements with an equality operation possess
Pix j = a.seek(x)
Bag classes possess
Pix j = a.seek(x, Pix from = 0)
Set, Bag, and PQ classes possess
Pix j = a.add(x) (or a.enq(x) for priority queues)
Go to the previous, next section.