Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
Remove const_iterator (again)
Browse files Browse the repository at this point in the history
Having both begin() and cbegin() that return different
types while being both const breaks certain generic
code that assumes that begin() will return a
const_iterator if the container is const.
  • Loading branch information
clechasseur committed Nov 21, 2017
1 parent 28f2282 commit 46982ee
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 70 deletions.
65 changes: 1 addition & 64 deletions lib/coveo/enumerable/enumerable.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,15 @@ class enumerable
typedef typename detail::seq_element_traits<T>::pointer pointer; // Pointer to a sequence element.
typedef typename detail::seq_element_traits<T>::reference reference; // Reference to a sequence element.

typedef typename detail::seq_element_traits<T>::const_value_type const_value_type; // Const version of /value_type/.
typedef typename detail::seq_element_traits<T>::const_pointer const_pointer; // Const version of /pointer/.
typedef typename detail::seq_element_traits<T>::const_reference const_reference; // Const version of /reference/.

// Delegate that returns next element in sequence, or nullptr when done.
// Receives a stable unique_ptr<raw_value_type> each time that can be used to store next value.
typedef std::function<pointer(std::unique_ptr<raw_value_type>&)> next_delegate;

// Delegate that returns number of elements in sequence.
typedef std::function<std::size_t()> size_delegate;

// Forward declaration of iterator classes.
// Forward declaration of iterator class.
class iterator;
class const_iterator;

private:
next_delegate zero_; // Next delegate which we will clone to iterate sequence.
Expand Down Expand Up @@ -110,16 +105,9 @@ class enumerable
iterator begin() const {
return iterator(*this, false);
}
const_iterator cbegin() const {
return const_iterator(*this, false);
}

iterator end() const {
return iterator(*this, true);
}
const_iterator cend() const {
return const_iterator(*this, true);
}

// Access to size of sequence
bool has_fast_size() const {
Expand Down Expand Up @@ -272,57 +260,6 @@ class enumerable
}
};

// Iterator for the elements in an enumerable's sequence, but returning const references.
class const_iterator
{
public:
// Standard iterator typedefs, plus a few more
typedef std::forward_iterator_tag iterator_category;
typedef typename enumerable<T>::const_value_type value_type;
typedef typename enumerable<T>::raw_value_type raw_value_type; // Non-standard
typedef std::ptrdiff_t difference_type;
typedef typename enumerable<T>::const_pointer pointer;
typedef typename enumerable<T>::const_reference reference;

private:
iterator it_; // Iterator implementation we're simply wrapping.

public:
// Default constructor
const_iterator() = default;

// Constructor from enumerable
const_iterator(const enumerable<T>& parent, bool is_end)
: it_(parent, is_end) { }

// Element access
reference operator*() const {
return *it_;
}
pointer operator->() const {
return it_.operator->();
}

// Move to next element (pre/post versions)
const_iterator& operator++() {
++it_;
return *this;
}
const_iterator operator++(int) {
const_iterator it(*this);
++*this;
return it;
}

// Iterator comparison
friend bool operator==(const const_iterator& left, const const_iterator& right) {
return left.it_ == right.it_;
}
friend bool operator!=(const const_iterator& left, const const_iterator& right) {
return !(left == right);
}
};

public:
// Helper static methods

Expand Down
6 changes: 0 additions & 6 deletions tests/coveo/enumerable/enumerable_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ void validate_sequence(const coveo::enumerable<T>& seq, const C& expected, bool
COVEO_ASSERT(obj == *eit++);
}
COVEO_ASSERT(eit == eend);
eit = std::begin(expected);
for (auto sit = seq.cbegin(), send = seq.cend(); sit != send; ++sit) {
COVEO_ASSERT(eit != eend);
COVEO_ASSERT(*sit == *eit++);
}
COVEO_ASSERT(eit == eend);
COVEO_ASSERT(seq.has_fast_size() == fast_size);
COVEO_ASSERT(seq.size() == expected.size());
}
Expand Down

0 comments on commit 46982ee

Please sign in to comment.