Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Andrew Koenig

Dr. Dobb's Bloggers

Representing Ranges With Element Pointers

June 18, 2014

Last week, we observed that a pointer is not quite the right abstraction to use for representing a range of elements in a sequence. Instead, we imagined a type that we called a boundary. An object of such a type denotes the infinitesimal space between two adjacent elements of a sequence, or immediately before the first or after the last element; given a boundary object, we can access the element either before or after the boundary if such an element exists.

With boundaries defined in this way, every n-element sequence contains n+1 boundaries. Moreover, every element corresponds to two boundaries, namely the ones immediately before and after that element. However, not every boundary has a corresponding element — after all, the sequence might not have any elements at all. Instead, we can think of each boundary as being either the beginning boundary, the end boundary, or an interior boundary. Thought of in this way, every interior boundary has two elements that correspond to it, namely those before and after the boundary. If a boundary is not a beginning boundary, then there is an element before it; if it is not an ending boundary, there is an element after it.

This description leads to some conclusions about the relationship between boundaries and pointers to elements:

  • For every pointer to an element, there are two operations, which we can call before and after, each of which maps the pointer into one of the adjacent boundaries. These two operations are defined on every valid pointer.
  • For every boundary, there are two operations, which we can also call before and after, that map the boundary into one of the adjacent elements. There is no more confusion in using before and after in these two different contexts than there is in other uses of overloading.
  • Unlike the pointer operations, the boundary operations do not always yield a pointer to an element. In particular, before yields a pointer to an element only if its argument is not a beginning boundary; after yields a pointer to an element only if its argument is not an ending boundary.
  • If we use before to transform a pointer to an element into a boundary, we can use after to transform that boundary back into a pointer to the same element. Similarly, we can use after to transform a pointer into a boundary, and then use before to recover the original pointer.

Together, these facts suggest that we can use a pointer to represent a boundary. We simply define a boundary as an abstract data structure that contains a pointer, and decree arbitrarily that such a data structure represents the boundary before the element to which the pointer points. If we adopt this strategy, then every element in an n-element sequence maps into one of the boundaries associated with that sequence, leaving one boundary untouched. This boundary is the one after the last element; we can reach that boundary by mapping the off-the-end pointer for the sequence.

In other words, this strategy yields a one-to-one correspondence between the boundaries associated with a sequence and the pointers to the sequence's elements augmented by the off-the-end pointer. It is then reasonable to say that before is the mapping that we wish to use from pointer to boundary, and after is the corresponding mapping from boundary to pointer.

Once we have defined the mapping between pointers and boundaries (and vice versa), we can think about how to take our earlier code that used boundaries and rewrite it to use pointers. We shall explore how to do so next week.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.