Skip to content

Commit

Permalink
[ADT] Clean up zip iterators. NFC.
Browse files Browse the repository at this point in the history
*  Use inheriting constructors declarations to avoid introducing the
   `Base` typedef and duplicate constructor definitions. This should make
   things cleaner, especially since `zip_common` also exposes a `Base`
   typedef.
*  Drop unnecessary template parameters.
*  Avoid double negation in `zip_shortest`'s `operator==` and rename the
   comparison function for better readability.

Reviewed By: zero9178

Differential Revision: https://reviews.llvm.org/D145332
  • Loading branch information
kuhar committed Mar 6, 2023
1 parent 7b54a29 commit e969c80
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions llvm/include/llvm/ADT/STLExtras.h
Expand Up @@ -827,32 +827,28 @@ struct zip_common : public zip_traits<ZipType, Iters...> {
};

template <typename... Iters>
struct zip_first : public zip_common<zip_first<Iters...>, Iters...> {
using Base = zip_common<zip_first<Iters...>, Iters...>;
struct zip_first : zip_common<zip_first<Iters...>, Iters...> {
using zip_common<zip_first, Iters...>::zip_common;

bool operator==(const zip_first<Iters...> &other) const {
bool operator==(const zip_first &other) const {
return std::get<0>(this->iterators) == std::get<0>(other.iterators);
}

zip_first(Iters &&... ts) : Base(std::forward<Iters>(ts)...) {}
};

template <typename... Iters>
class zip_shortest : public zip_common<zip_shortest<Iters...>, Iters...> {
template <size_t... Ns>
bool test(const zip_shortest<Iters...> &other,
std::index_sequence<Ns...>) const {
return ((std::get<Ns>(this->iterators) != std::get<Ns>(other.iterators)) &&
...);
}

public:
using Base = zip_common<zip_shortest<Iters...>, Iters...>;
struct zip_shortest : zip_common<zip_shortest<Iters...>, Iters...> {
using zip_common<zip_shortest, Iters...>::zip_common;

zip_shortest(Iters &&... ts) : Base(std::forward<Iters>(ts)...) {}
bool operator==(const zip_shortest &other) const {
return any_iterator_equals(other, std::index_sequence_for<Iters...>{});
}

bool operator==(const zip_shortest<Iters...> &other) const {
return !test(other, std::index_sequence_for<Iters...>{});
private:
template <size_t... Ns>
bool any_iterator_equals(const zip_shortest &other,
std::index_sequence<Ns...>) const {
return ((std::get<Ns>(this->iterators) == std::get<Ns>(other.iterators)) ||
...);
}
};

Expand Down

0 comments on commit e969c80

Please sign in to comment.