Skip to content

Commit

Permalink
Make iterator bounds behave as expected
Browse files Browse the repository at this point in the history
meaning ++ from an end() iterator results in an end() iterator and --
from a begin() iterator results in a begin() iterator
  • Loading branch information
chrisvasz committed Jan 1, 2012
1 parent 433a652 commit 591f089
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
22 changes: 9 additions & 13 deletions src/array_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

// TODO backwards iteration

#ifndef ARRAY_HASH_H
#define ARRAY_HASH_H

Expand All @@ -29,10 +27,6 @@
#include <utility>
#include <iterator>

// TODO
#include <iostream>
using namespace std;

namespace stx {

/**
Expand Down Expand Up @@ -123,15 +117,15 @@ class array_hash_traits
};
*/

template<class T>
template <class T>
class array_hash
{
};

/**
* Hash table container for unsorted strings.
*/
template<>
template <>
class array_hash<std::string>
{
private:
Expand Down Expand Up @@ -162,7 +156,7 @@ class array_hash<std::string>
/**
* Iterator range constructor.
*/
template<class Iterator>
template <class Iterator>
array_hash(Iterator first, const Iterator& last,
const array_hash_traits& traits = array_hash_traits()) :
_traits(traits)
Expand Down Expand Up @@ -506,6 +500,8 @@ class array_hash<std::string>
/**
* Move this iterator forward to the next element in the table.
*
* Calling this function on an end() iterator does nothing.
*
* @return self-reference
*/
iterator& operator++()
Expand Down Expand Up @@ -535,6 +531,8 @@ class array_hash<std::string>
/**
* Move this iterator backward to the previous element in the table.
*
* Calling this function on a begin iterator does nothing.
*
* @return self-reference
*/
iterator& operator--()
Expand All @@ -553,6 +551,7 @@ class array_hash<std::string>
_p = prev;
} else {
// Move back to the previous occupied slot
int tmp = _slot;
--_slot;
while (_slot >= 0 && _data[_slot] == NULL) {
--_slot;
Expand All @@ -561,10 +560,7 @@ class array_hash<std::string>
if (_slot < 0) {
// We are at the beginning. Make this a begin
// iterator
while (_data[_slot] == NULL) {
++_slot;
}
_p = _data[_slot] + sizeof(size_type);
_slot = tmp;
return *this;
}
}
Expand Down
14 changes: 12 additions & 2 deletions test/array_hash_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,9 @@ CASE(testInsert)

CASE(testReverseIteration)
{
stack<string> st;

// Initialize the stack
array_hash<string> ah(data.begin(), data.end());
stack<string> st;
for (array_hash<string>::iterator it = ah.begin(); it != ah.end(); ++it) {
st.push(*it);
}
Expand All @@ -188,6 +187,17 @@ CASE(testReverseIteration)
}
}

CASE(testIteratorBounds)
{
array_hash<string> ah(data.begin(), data.end());
array_hash<string>::const_iterator it = ah.begin();
--it;
BOOST_CHECK(it == ah.begin());
it = ah.end();
++it;
BOOST_CHECK(it == ah.end());
}

CASE(testEquality)
{
array_hash<string> a(data.begin(), data.end());
Expand Down

0 comments on commit 591f089

Please sign in to comment.