SeqAn3 3.4.3-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
pairwise_combine.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2026 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2026 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <cmath>
13#include <ranges>
14
20
21namespace seqan3::detail
22{
38template <std::ranges::view underlying_range_type>
39 requires std::ranges::forward_range<underlying_range_type> && std::ranges::common_range<underlying_range_type>
40class pairwise_combine_view : public std::ranges::view_interface<pairwise_combine_view<underlying_range_type>>
41{
42private:
44 template <typename range_type>
45 class basic_iterator;
46
57
58public:
68
85 explicit constexpr pairwise_combine_view(underlying_range_type range) : u_range{std::move(range)}
86 {
87 // Check if range is empty.
88 if (std::ranges::empty(u_range))
89 {
90 back_iterator = std::ranges::end(u_range);
91 }
92 else
93 {
94 if constexpr (std::ranges::bidirectional_range<underlying_range_type>)
95 { // Simply take one before the end. We can do this as we require underlying_range_type to be a common range.
96 back_iterator = std::ranges::prev(std::ranges::end(u_range));
97 }
98 else
99 { // For all other cases we need to set the back_iterator in linear time to the correct position.
101 if constexpr (std::ranges::sized_range<underlying_range_type>)
102 {
103 std::ranges::advance(back_iterator, std::ranges::size(u_range) - 1);
104 }
105 else // We don't have the size, so we need to increment until one before the end in a linear pass.
106 {
107 auto tmp_it = back_iterator;
108 do
109 {
110 back_iterator = tmp_it;
111 }
112 while (++tmp_it != std::ranges::end(u_range));
113 }
114 }
115 }
116 }
117
137 template <typename other_range_t>
138 requires (!std::same_as<std::remove_cvref_t<other_range_t>, pairwise_combine_view>)
139 && std::ranges::viewable_range<other_range_t>
140 && // Must come after self type check to avoid conflicts with the move constructor.
141 std::constructible_from<underlying_range_type,
142 std::ranges::ref_view<std::remove_reference_t<other_range_t>>>
143 //TODO: Investigate: the following expression is equivalent to the one above but raises a weird assertion in
144 // the ranges adaptor suggesting that the pairwise_combine_view is not a viewable_range.
145 // std::constructible_from<underlying_range_type, decltype(std::views::all(std::declval<other_range_t &&>()))>
146 explicit constexpr pairwise_combine_view(other_range_t && range) :
147 pairwise_combine_view{std::views::all(std::forward<other_range_t>(range))}
148 {}
149
166 constexpr iterator begin() noexcept
167 {
168 return {std::ranges::begin(u_range), std::ranges::begin(u_range), std::ranges::end(u_range)};
169 }
170
172 constexpr const_iterator begin() const noexcept
173 requires const_iterable_range<underlying_range_type>
174 {
175 return {std::ranges::begin(u_range), std::ranges::begin(u_range), std::ranges::end(u_range)};
176 }
177
191 constexpr iterator end() noexcept
192 {
193 return {back_iterator, std::ranges::begin(u_range), std::ranges::end(u_range)};
194 }
195
197 constexpr const_iterator end() const noexcept
198 requires const_iterable_range<underlying_range_type>
199 {
200 return {back_iterator, std::ranges::begin(u_range), std::ranges::end(u_range)};
201 }
203
208 constexpr auto size() const noexcept
209 requires std::ranges::sized_range<underlying_range_type>
210 {
211 auto const size = std::ranges::size(u_range);
212 return (size * (size - 1)) / 2;
213 }
215
216private:
218 underlying_range_type u_range;
220 std::ranges::iterator_t<underlying_range_type> back_iterator{};
221};
222
228template <std::ranges::viewable_range other_range_t>
231
245template <std::ranges::view underlying_range_type>
246 requires std::ranges::forward_range<underlying_range_type> && std::ranges::common_range<underlying_range_type>
247template <typename range_type>
248class pairwise_combine_view<underlying_range_type>::basic_iterator :
249 public maybe_iterator_category<std::ranges::iterator_t<range_type>>
250{
251private:
253 template <typename other_range_type>
254 friend class basic_iterator;
255
257 using underlying_iterator_type = std::ranges::iterator_t<range_type>;
262
263public:
275 using pointer = void;
286
290 basic_iterator() = default;
291 basic_iterator(basic_iterator const &) = default;
295 ~basic_iterator() = default;
296
311 underlying_iterator_type end_it) noexcept :
312 first_it{iter},
313 second_it{std::ranges::next(iter, 1, end_it)},
314 begin_it{begin_it},
315 end_it{end_it}
316 {}
317
326 template <typename other_range_type>
327 requires std::convertible_to<other_range_type, range_type &>
328 && std::same_as<std::remove_const_t<other_range_type>, std::remove_const_t<range_type>>
330 basic_iterator{std::move(other.first_it), std::move(other.begin_it), std::move(other.end_it)}
331 {}
333
338 constexpr reference operator*() const noexcept(noexcept(*std::declval<underlying_iterator_type>()))
339 {
340 return reference{*first_it, *second_it};
341 }
342
346 constexpr reference operator[](size_t const index) const
347 noexcept(noexcept(std::declval<basic_iterator &>().from_index(1)))
348 requires std::random_access_iterator<underlying_iterator_type>
349 {
350 return *(*this + index);
351 }
353
358 constexpr basic_iterator & operator++(/*pre-increment*/)
359 noexcept(noexcept(++std::declval<underlying_iterator_type &>()))
360 {
361 if (++second_it == end_it)
362 {
363 ++first_it;
364 second_it = first_it;
365 ++second_it;
366 }
367 return *this;
368 }
369
371 constexpr basic_iterator operator++(int /*post-increment*/)
372 noexcept(noexcept(std::declval<underlying_iterator_type &>()++))
373 {
374 basic_iterator tmp{*this};
375 ++*this;
376 return tmp;
377 }
378
380 constexpr basic_iterator & operator--(/*pre-decrement*/)
381 noexcept(noexcept(--std::declval<underlying_iterator_type &>()))
382 requires std::bidirectional_iterator<underlying_iterator_type>
383 {
384 if (--second_it == first_it)
385 {
386 --first_it;
387 second_it = end_it;
388 --second_it;
389 }
390 return *this;
391 }
392
394 constexpr basic_iterator operator--(int /*post-decrement*/)
395 noexcept(noexcept(std::declval<underlying_iterator_type &>()--))
396 requires std::bidirectional_iterator<underlying_iterator_type>
397 {
398 basic_iterator tmp{*this};
399 --*this;
400 return tmp;
401 }
402
406 noexcept(noexcept(std::declval<basic_iterator &>().from_index(1)))
407 requires std::random_access_iterator<underlying_iterator_type>
408 {
409 from_index(to_index() + offset);
410 return *this;
411 }
412
416 noexcept(noexcept(std::declval<basic_iterator &>() += 1))
417 requires std::random_access_iterator<underlying_iterator_type>
418 {
419 basic_iterator tmp{*this};
420 return (tmp += offset);
421 }
422
426 noexcept(noexcept(std::declval<basic_iterator<range_type> &>().from_index(1)))
427 requires std::random_access_iterator<underlying_iterator_type>
428 {
429 iter.from_index(iter.to_index() + offset);
430 return iter;
431 }
432
436 noexcept(noexcept(std::declval<basic_iterator &>().from_index(1)))
437 requires std::random_access_iterator<underlying_iterator_type>
438 {
439 from_index(to_index() - offset);
440 return *this;
441 }
442
446 noexcept(noexcept(std::declval<basic_iterator &>() -= 1))
447 requires std::random_access_iterator<underlying_iterator_type>
448 {
449 basic_iterator tmp{*this};
450 return (tmp -= offset);
451 }
452
455 template <typename other_range_type>
456 requires std::random_access_iterator<underlying_iterator_type>
457 && std::same_as<std::remove_const_t<range_type>, std::remove_const_t<other_range_type>>
459 noexcept(noexcept(std::declval<basic_iterator &>().to_index()))
460 {
461 return static_cast<difference_type>(to_index() - rhs.to_index());
462 }
464
470 //NOTE: The comparison operators should be implemented as friends, but due to a bug in gcc friend function
471 // cannot yet be constrained. To avoid unexpected errors with the comparison all operators are implemented as
472 // direct members and not as friends.
473
475 template <typename other_range_type>
476 requires std::equality_comparable_with<underlying_iterator_type, std::ranges::iterator_t<other_range_type>>
477 && std::same_as<std::remove_const_t<range_type>, std::remove_const_t<other_range_type>>
478 constexpr bool operator==(basic_iterator<other_range_type> const & rhs) const
479 noexcept(noexcept(std::declval<underlying_iterator_type &>() == std::declval<underlying_iterator_type &>()))
480 {
481 return std::tie(first_it, second_it) == std::tie(rhs.first_it, rhs.second_it);
482 }
483
485 template <typename other_range_type>
486 requires std::equality_comparable_with<underlying_iterator_type, std::ranges::iterator_t<other_range_type>>
487 && std::same_as<std::remove_const_t<range_type>, std::remove_const_t<other_range_type>>
488 constexpr bool operator!=(basic_iterator<other_range_type> const & rhs) const
489 noexcept(noexcept(std::declval<underlying_iterator_type &>() != std::declval<underlying_iterator_type &>()))
490 {
491 return !(*this == rhs);
492 }
493
495 template <typename other_range_type>
496 requires std::totally_ordered_with<underlying_iterator_type, std::ranges::iterator_t<other_range_type>>
497 && std::same_as<std::remove_const_t<range_type>, std::remove_const_t<other_range_type>>
498 constexpr bool operator<(basic_iterator<other_range_type> const & rhs) const
499 noexcept(noexcept(std::declval<underlying_iterator_type &>() < std::declval<underlying_iterator_type &>()))
500 {
501 return std::tie(first_it, second_it) < std::tie(rhs.first_it, rhs.second_it);
502 }
503
505 template <typename other_range_type>
506 requires std::totally_ordered_with<underlying_iterator_type, std::ranges::iterator_t<other_range_type>>
507 && std::same_as<std::remove_const_t<range_type>, std::remove_const_t<other_range_type>>
508 constexpr bool operator>(basic_iterator<other_range_type> const & rhs) const
509 noexcept(noexcept(std::declval<underlying_iterator_type &>() > std::declval<underlying_iterator_type &>()))
510
511 {
512 return std::tie(first_it, second_it) > std::tie(rhs.first_it, rhs.second_it);
513 }
514
516 template <typename other_range_type>
517 requires std::totally_ordered_with<underlying_iterator_type, std::ranges::iterator_t<other_range_type>>
518 && std::same_as<std::remove_const_t<range_type>, std::remove_const_t<other_range_type>>
519 constexpr bool operator<=(basic_iterator<other_range_type> const & rhs) const
520 noexcept(noexcept(std::declval<underlying_iterator_type &>() <= std::declval<underlying_iterator_type &>()))
521 {
522 return std::tie(first_it, second_it) <= std::tie(rhs.first_it, rhs.second_it);
523 }
524
526 template <typename other_range_type>
527 requires std::totally_ordered_with<underlying_iterator_type, std::ranges::iterator_t<other_range_type>>
528 && std::same_as<std::remove_const_t<range_type>, std::remove_const_t<other_range_type>>
529 constexpr bool operator>=(basic_iterator<other_range_type> const & rhs) const
530 noexcept(noexcept(std::declval<underlying_iterator_type &>() >= std::declval<underlying_iterator_type &>()))
531 {
532 return std::tie(first_it, second_it) >= std::tie(rhs.first_it, rhs.second_it);
533 }
535
536private:
549 constexpr size_t to_index() const
550 noexcept(noexcept(std::declval<underlying_iterator_type &>() - std::declval<underlying_iterator_type &>()))
552 {
553 size_t src_size = end_it - begin_it;
554 size_t index_i = first_it - begin_it;
555 size_t index_j = second_it - begin_it;
556 return (src_size * (src_size - 1) / 2) - (src_size - index_i) * ((src_size - index_i) - 1) / 2 + index_j
557 - index_i - 1;
558 }
559
564 constexpr void from_index(size_t const index)
565 noexcept(noexcept(std::declval<underlying_iterator_type &>() - std::declval<underlying_iterator_type &>())
566 && noexcept(std::declval<underlying_iterator_type &>() + 1))
567 requires std::random_access_iterator<underlying_iterator_type>
568 {
569 size_t src_size = end_it - begin_it;
570 size_t index_i =
571 src_size - 2 - std::floor(std::sqrt(-8 * index + 4 * src_size * (src_size - 1) - 7) / 2.0 - 0.5);
572 size_t index_j =
573 index + index_i + 1 - src_size * (src_size - 1) / 2 + (src_size - index_i) * ((src_size - index_i) - 1) / 2;
574 first_it = begin_it + index_i;
575 second_it = begin_it + index_j;
576 }
577
586};
587
588} // namespace seqan3::detail
589
590namespace seqan3::views
591{
655
656} // namespace seqan3::views
Provides seqan3::detail::adaptor_for_view_without_args.
T begin(T... args)
Template for range adaptor closure objects that store no arguments and always delegate to the view co...
Definition adaptor_for_view_without_args.hpp:46
The forward declared iterator type for pairwise_combine_view.
Definition pairwise_combine.hpp:250
constexpr basic_iterator operator++(int) noexcept(noexcept(std::declval< underlying_iterator_type & >()++))
Post-increment operator.
Definition pairwise_combine.hpp:371
constexpr void from_index(size_t const index) noexcept(noexcept(std::declval< underlying_iterator_type & >() - std::declval< underlying_iterator_type & >()) &&noexcept(std::declval< underlying_iterator_type & >()+1))
Sets the iterator to the given index.
Definition pairwise_combine.hpp:564
constexpr basic_iterator & operator-=(difference_type const offset) noexcept(noexcept(std::declval< basic_iterator & >().from_index(1)))
Decrements the iterator by the given offset; underlying_iterator_type must model \ std::random_access...
Definition pairwise_combine.hpp:435
constexpr basic_iterator & operator+=(difference_type const offset) noexcept(noexcept(std::declval< basic_iterator & >().from_index(1)))
Advances the iterator by the given offset; underlying_iterator_type must model \ std::random_access_i...
Definition pairwise_combine.hpp:405
constexpr reference operator[](size_t const index) const noexcept(noexcept(std::declval< basic_iterator & >().from_index(1)))
Access the element at the given index.
Definition pairwise_combine.hpp:346
constexpr reference operator*() const noexcept(noexcept(*std::declval< underlying_iterator_type >()))
Accesses the pointed-to element.
Definition pairwise_combine.hpp:338
constexpr bool operator!=(basic_iterator< other_range_type > const &rhs) const noexcept(noexcept(std::declval< underlying_iterator_type & >() !=std::declval< underlying_iterator_type & >()))
Checks whether *this is not equal to rhs.
Definition pairwise_combine.hpp:488
basic_iterator(basic_iterator const &)=default
Defaulted.
constexpr size_t to_index() const noexcept(noexcept(std::declval< underlying_iterator_type & >() - std::declval< underlying_iterator_type & >()))
Returns the index for the current iterator position.
Definition pairwise_combine.hpp:549
constexpr difference_type operator-(basic_iterator< other_range_type > const &rhs) const noexcept(noexcept(std::declval< basic_iterator & >().to_index()))
Computes the distance between two iterators; underlying_iterator_type must model \ std::random_access...
Definition pairwise_combine.hpp:458
basic_iterator & operator=(basic_iterator const &)=default
Defaulted.
constexpr bool operator==(basic_iterator< other_range_type > const &rhs) const noexcept(noexcept(std::declval< underlying_iterator_type & >()==std::declval< underlying_iterator_type & >()))
Checks whether *this is equal to rhs.
Definition pairwise_combine.hpp:478
constexpr friend basic_iterator operator+(difference_type const offset, basic_iterator iter) noexcept(noexcept(std::declval< basic_iterator< range_type > & >().from_index(1)))
Advances the iterator by the given offset; underlying_iterator_type must model \ std::random_access_i...
Definition pairwise_combine.hpp:425
constexpr basic_iterator & operator++() noexcept(noexcept(++std::declval< underlying_iterator_type & >()))
Pre-increment operator.
Definition pairwise_combine.hpp:358
common_tuple< underlying_ref_t, underlying_ref_t > reference
The reference type.
Definition pairwise_combine.hpp:273
void pointer
The pointer type.
Definition pairwise_combine.hpp:275
constexpr bool operator<=(basic_iterator< other_range_type > const &rhs) const noexcept(noexcept(std::declval< underlying_iterator_type & >()<=std::declval< underlying_iterator_type & >()))
Checks whether *this is less than or equal to rhs.
Definition pairwise_combine.hpp:519
constexpr basic_iterator(underlying_iterator_type iter, underlying_iterator_type begin_it, underlying_iterator_type end_it) noexcept
Constructs the iterator from the current underlying iterator and the end iterator of the underlying r...
Definition pairwise_combine.hpp:309
constexpr basic_iterator operator+(difference_type const offset) const noexcept(noexcept(std::declval< basic_iterator & >()+=1))
Advances the iterator by the given offset; underlying_iterator_type must model \ std::random_access_i...
Definition pairwise_combine.hpp:415
constexpr basic_iterator(basic_iterator< other_range_type > other) noexcept
Constructs const iterator from non-const iterator.
Definition pairwise_combine.hpp:329
constexpr bool operator<(basic_iterator< other_range_type > const &rhs) const noexcept(noexcept(std::declval< underlying_iterator_type & >()< std::declval< underlying_iterator_type & >()))
Checks whether *this is less than rhs.
Definition pairwise_combine.hpp:498
std::ranges::iterator_t< range_type > underlying_iterator_type
Alias type for the iterator over the passed range type.
Definition pairwise_combine.hpp:257
constexpr basic_iterator operator-(difference_type const offset) const noexcept(noexcept(std::declval< basic_iterator & >() -=1))
Decrements the iterator by the given offset; underlying_iterator_type must model \ std::random_access...
Definition pairwise_combine.hpp:445
constexpr bool operator>=(basic_iterator< other_range_type > const &rhs) const noexcept(noexcept(std::declval< underlying_iterator_type & >() >=std::declval< underlying_iterator_type & >()))
Checks whether *this is greater than or equal to rhs.
Definition pairwise_combine.hpp:529
basic_iterator & operator=(basic_iterator &&)=default
Defaulted.
constexpr basic_iterator operator--(int) noexcept(noexcept(std::declval< underlying_iterator_type & >() --))
Post-decrement operator; underlying_iterator_type must model std::bidirectional_iterator.
Definition pairwise_combine.hpp:394
basic_iterator(basic_iterator &&)=default
Defaulted.
constexpr basic_iterator & operator--() noexcept(noexcept(--std::declval< underlying_iterator_type & >()))
Pre-decrement operator; underlying_iterator_type must model std::bidirectional_iterator.
Definition pairwise_combine.hpp:380
constexpr bool operator>(basic_iterator< other_range_type > const &rhs) const noexcept(noexcept(std::declval< underlying_iterator_type & >() > std::declval< underlying_iterator_type & >()))
Checks whether *this is greater than rhs.
Definition pairwise_combine.hpp:508
Generates all pairwise combinations of the elements in the underlying range.
Definition pairwise_combine.hpp:41
underlying_range_type u_range
The underling range.
Definition pairwise_combine.hpp:218
pairwise_combine_view(pairwise_combine_view const &)=default
Defaulted.
constexpr pairwise_combine_view(other_range_t &&range)
Constructs from a view.
Definition pairwise_combine.hpp:146
pairwise_combine_view(pairwise_combine_view &&)=default
Defaulted.
constexpr pairwise_combine_view(underlying_range_type range)
Constructs from a view.
Definition pairwise_combine.hpp:85
constexpr iterator begin() noexcept
Returns an iterator to the first element of the range.
Definition pairwise_combine.hpp:166
pairwise_combine_view()=default
Defaulted.
constexpr auto size() const noexcept
Computes the size based on the size of the underlying range.
Definition pairwise_combine.hpp:208
transformation_trait_or_t< std::type_identity< basic_iterator< underlying_range_type const > >, void > const_iterator
The const iterator type. Evaluates to void if the underlying range is not const iterable.
Definition pairwise_combine.hpp:55
pairwise_combine_view & operator=(pairwise_combine_view &&)=default
Defaulted.
std::ranges::iterator_t< underlying_range_type > back_iterator
The cached iterator pointing to the last element of the underlying range.
Definition pairwise_combine.hpp:220
~pairwise_combine_view()=default
Defaulted.
constexpr const_iterator end() const noexcept
Returns an iterator to the element following the last element of the range.
Definition pairwise_combine.hpp:197
constexpr const_iterator begin() const noexcept
Returns an iterator to the first element of the range.
Definition pairwise_combine.hpp:172
pairwise_combine_view & operator=(pairwise_combine_view const &)=default
Defaulted.
constexpr iterator end() noexcept
Returns an iterator to the element following the last element of the range.
Definition pairwise_combine.hpp:191
A generic random access iterator that delegates most operations to the range.
Definition random_access_iterator.hpp:288
Provides seqan3::common_tuple.
T floor(T... args)
@ offset
Sequence (seqan3::field::seq) relative start position (0-based), unsigned value.
seqan::stl::tuple< t... > common_tuple
A std::tuple implementation that incorporates most changes from C++23's standard library.
Definition common_tuple.hpp:24
typename transformation_trait_or< type_t, default_t >::type transformation_trait_or_t
Helper type of seqan3::detail::transformation_trait_or (transformation_trait shortcut).
Definition transformation_trait_or.hpp:48
constexpr auto pairwise_combine
A view adaptor that generates all pairwise combinations of the elements of the underlying range.
Definition pairwise_combine.hpp:654
Specifies requirements of an input range type for which the const version of that type satisfies the ...
Provides various transformation traits for use on iterators.
The internal SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
The SeqAn namespace for views.
Definition char_strictly_to.hpp:19
SeqAn specific customisations in the standard namespace.
T sqrt(T... args)
Defines iterator_category member if underlying_iterator_t has a valid std::iterator_traits::iterator_...
Definition iterator_traits.hpp:39
T tie(T... args)
Provides seqan3::detail::transformation_trait_or.
Additional non-standard concepts for ranges.
Hide me