From cacac82070e592afb169208ce27f35abea0b18a5 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Fri, 22 Aug 2025 07:56:00 +0300 Subject: [PATCH] mu-query-results: add decrement operators So we can easily iterator from end->begin --- lib/mu-query-results.hh | 51 ++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/lib/mu-query-results.hh b/lib/mu-query-results.hh index 0123ab40..4ebb0c2a 100644 --- a/lib/mu-query-results.hh +++ b/lib/mu-query-results.hh @@ -163,7 +163,7 @@ using QueryMatches = std::unordered_map; /// class QueryResultsIterator { public: - using iterator_category = std::output_iterator_tag; + using iterator_category = std::bidirectional_iterator_tag; using value_type = Message; using difference_type = void; using pointer = void; @@ -174,9 +174,9 @@ public: } /** - * Increment the iterator (we don't support post-increment) + * Increment the iterator * - * @return an updated iterator, or end() if we were already at end() + * @return an updated iterator */ QueryResultsIterator& operator++() { ++mset_it_; @@ -184,6 +184,39 @@ public: return *this; } + /** + * Increment the iterator (postfix) + * + * @return the iterator before updating + */ + QueryResultsIterator operator++(int) { + auto old{mset_it_}; + ++mset_it_; + return QueryResultsIterator{old, query_matches_}; + } + + /** + * Decrement the iterator + * + * @return an updated iterator + */ + QueryResultsIterator& operator--() { + --mset_it_; + mdoc_ = Nothing; + return *this; + } + + /** + * Decrement the iterator (postfix) + * + * @return the iterator before updating + */ + QueryResultsIterator operator--(int) { + auto old{mset_it_}; + --mset_it_; + return QueryResultsIterator{old, query_matches_}; + } + /** * (Non)Equivalence operators * @@ -371,9 +404,7 @@ public: * @param mset an Xapian::MSet with matches */ QueryResults(const Xapian::MSet& mset, QueryMatches&& query_matches) - : mset_{mset}, query_matches_{std::move(query_matches)} - { - } + : mset_{mset}, query_matches_{std::move(query_matches)} {} /** * Is this QueryResults object empty (ie., no matches)? * @@ -393,14 +424,18 @@ public: * * @return iterator */ - const iterator begin() const { return QueryResultsIterator(mset_.begin(), query_matches_); } + const_iterator begin() const { + return QueryResultsIterator(mset_.begin(), query_matches_); + } /** * Get the end iterator to the results. * * @return iterator */ - const_iterator end() const { return QueryResultsIterator(mset_.end(), query_matches_); } + const_iterator end() const { + return QueryResultsIterator(mset_.end(), query_matches_); + } /** * Get the query-matches for these QueryResults. The non-const