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

Commit

Permalink
Make sequence_equal compatible with order_by
Browse files Browse the repository at this point in the history
order_by's sequence implementation did not have
const begin()/end() methods which broke sequence_equal
and probably order things too.
  • Loading branch information
clechasseur committed Nov 23, 2017
1 parent eaab580 commit 17764bb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
23 changes: 12 additions & 11 deletions lib/coveo/linq/detail/linq_detail.h
Original file line number Diff line number Diff line change
Expand Up @@ -1967,13 +1967,13 @@ class order_by_impl_with_seq
template<typename> friend class order_by_impl;

private:
Seq seq_; // Sequence we're ordering.
std::unique_ptr<Cmp> upcmp_; // Comparator used to order a sequence.
coveo::enumerable<typename seq_traits<Seq>::const_value_type> enum_; // Enumerator of ordered elements.
bool init_flag_; // Whether enum_ and size_ have been initialized.
Seq seq_; // Sequence we're ordering.
std::unique_ptr<Cmp> upcmp_; // Comparator used to order a sequence.
mutable coveo::enumerable<typename seq_traits<Seq>::const_value_type> enum_; // Enumerator of ordered elements.
mutable bool init_flag_; // Whether enum_ and size_ have been initialized.

// Called to initialize enum_ and size_ before using them.
void init() {
void init() const {
std::vector<typename seq_traits<Seq>::raw_value_type> ordered;
try_reserve(ordered, seq_);
ordered.insert(ordered.end(), std::begin(seq_), std::end(seq_));
Expand All @@ -1988,8 +1988,9 @@ class order_by_impl_with_seq
}

public:
// Type of iterator used for the ordered sequence.
typedef typename coveo::enumerable<typename seq_traits<Seq>::const_value_type>::iterator iterator;
// Type of iterators used for the ordered sequence.
typedef typename coveo::enumerable<typename seq_traits<Seq>::const_value_type>::iterator iterator;
typedef typename coveo::enumerable<typename seq_traits<Seq>::const_value_type>::const_iterator const_iterator;

// Constructor called by the impl without sequence.
order_by_impl_with_seq(Seq&& seq, std::unique_ptr<Cmp>&& upcmp)
Expand All @@ -2002,27 +2003,27 @@ class order_by_impl_with_seq
order_by_impl_with_seq& operator=(order_by_impl_with_seq&&) = default;

// Support for ordered sequence.
iterator begin() {
iterator begin() const {
if (!init_flag_) {
init();
}
return enum_.begin();
}
iterator end() {
iterator end() const {
if (!init_flag_) {
init();
}
return enum_.end();
}

// Support for sequence size (a bit like the enumerable API)
bool has_fast_size() {
bool has_fast_size() const {
if (!init_flag_) {
init();
}
return enum_.has_fast_size();
}
std::size_t size() {
std::size_t size() const {
if (!init_flag_) {
init();
}
Expand Down
1 change: 1 addition & 0 deletions tests/coveo/linq/all_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void all_tests()
linq_tests();
chaining_tests();
dangling_ref_tests();
bugs_tests();
}

// Runs all benchmarks for coveo::enumerable and coveo::linq
Expand Down
17 changes: 17 additions & 0 deletions tests/coveo/linq/linq_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,23 @@ void dangling_ref_tests()
}
}

// Runs tests for specific bugs
void bugs_tests()
{
using namespace coveo::linq;

// sequence_equal used not to work with the product of order_by
{
const std::vector<int> v = { 42, 23, 66 };
auto e1 = from(v)
| order_by([](int i) { return i; });
auto e2 = from(v)
| order_by([](int i) { return i; });
COVEO_ASSERT(from(e1)
| sequence_equal(e2));
}
}

// Runs all benchmarks for coveo::linq operators
void linq_benchmarks()
{
Expand Down
1 change: 1 addition & 0 deletions tests/coveo/linq/linq_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace linq {
void linq_tests();
void chaining_tests();
void dangling_ref_tests();
void bugs_tests();

void linq_benchmarks();

Expand Down

0 comments on commit 17764bb

Please sign in to comment.