Channels ▼

Andrew Koenig

Dr. Dobb's Bloggers

Asymmetric Bounds, Part 5: Rules of Thumb

July 04, 2012

This series has concentrated more on why than on how. This installment offers concrete advice about how to make programming easier when you use asymmetric bounds.

  1. Think of every bound as the beginning of a range. When you want the end of a range, use the representation for the beginning of what immediately follows the original range. In other words, instead of thinking of the integers from 1 through 5, think about one range of integers that starts at 1 and another that starts at 6, which is the value immediately after the last value of the preceding range. Thinking in this way leads to representing such a range as [1, 6) rather than as [1, 5], which is useful because…
  2. The bounds of an empty asymmetric range are equal to each other. In other words, the range [42, 42) is empty because 42 is equal to 42.
  3. More generally, the number of elements in such a range is equal to the difference between the bounds. It is easier to see that [1, 6) has 6-1 elements than it is to figure out how many elements are in [1, 5].
  4. Two ranges are consecutive if the end of the first range is equal to the beginning of the second. In other words, the ranges [a, b) and [b, c) are contiguous because b is equal to b. Moreover, combining them yields [a, c). The arithmetic here is much easier to follow than it would be for asymmetric ranges.
  5. Use (in)equality comparisons to step through asymmetric ranges. Because the range ends just before the upper bound, there is no need to use order relations. The usual form for stepping through a range [begin, end) is
        pos = begin;
        while (pos != end) {
            process the element at pos;
  6. To step backward through a range, decrement at the beginning. Asymmetric ranges are asymmetric, which means that the beginning and end of the range must be treated differently. This property is one of the few ways in which symmetric ranges are easier to use than asymmetric ranges. The point is that the end of the range does not denote an element, which means that beginning a loop by accessing the element at the end immediately doesn't work: There is no element there to access. Instead, step backward through the range [begin, end) this way:
       pos = end;
        while (pos != begin) {
            process the element at pos;

Again, this technique avoids having to use relational operators. Moreover, it avoids ever having pos refer to the nonexistent position before the beginning of the range, because unlike the case with the off-the-end value, there is no reason to believe that an off-the-beginning value is even possible.

These guidelines make many programming problems simpler. Even though many programmers find asymmetric bounds peculiar at first, one quickly finds that programming problems that involve ranges are usually much easier to follow with asymmetric ranges than they are with symmetric ones.

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.