WPILibC++ 2027.0.0-alpha-2
Loading...
Searching...
No Matches
iterator_range.h
Go to the documentation of this file.
1//===- iterator_range.h - A range adaptor for iterators ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This provides a very simple, boring adaptor for a begin and end iterator
10/// into a range type. This should be used to build range views that work well
11/// with range based for loops and range based constructors.
12///
13/// Note that code here follows more standards-based coding conventions as it
14/// is mirroring proposed interfaces for standardization.
15///
16//===----------------------------------------------------------------------===//
17
18#ifndef WPIUTIL_WPI_ITERATOR_RANGE_H
19#define WPIUTIL_WPI_ITERATOR_RANGE_H
20
21#include "wpi/ADL.h"
22#include <type_traits>
23#include <utility>
24
25namespace wpi {
26
27template <typename From, typename To, typename = void>
28struct explicitly_convertible : std::false_type {};
29
30template <typename From, typename To>
32 From, To,
33 std::void_t<decltype(static_cast<To>(
34 std::declval<std::add_rvalue_reference_t<From>>()))>> : std::true_type {
35};
36
37/// A range adaptor for a pair of iterators.
38///
39/// This just wraps two iterators into a range-compatible interface. Nothing
40/// fancy at all.
41template <typename IteratorT>
43 IteratorT begin_iterator, end_iterator;
44
45public:
46#if defined(__GNUC__) && \
47 (__GNUC__ == 7 || (__GNUC__ == 8 && __GNUC_MINOR__ < 4))
48 // Be careful no to break gcc-7 and gcc-8 < 8.4 on the mlir target.
49 // See https://github.com/llvm/llvm-project/issues/63843
50 template <typename Container>
51#else
52 template <
53 typename Container,
54 std::enable_if_t<explicitly_convertible<
55 wpi::detail::IterOfRange<Container>, IteratorT>::value> * = nullptr>
56#endif
57 iterator_range(Container &&c)
58 : begin_iterator(adl_begin(c)), end_iterator(adl_end(c)) {
59 }
60 iterator_range(IteratorT begin_iterator, IteratorT end_iterator)
61 : begin_iterator(std::move(begin_iterator)),
62 end_iterator(std::move(end_iterator)) {}
63
64 IteratorT begin() const { return begin_iterator; }
65 IteratorT end() const { return end_iterator; }
66 bool empty() const { return begin_iterator == end_iterator; }
67};
68
69template <typename Container>
70iterator_range(Container &&)
72
73/// Convenience function for iterating over sub-ranges.
74///
75/// This provides a bit of syntactic sugar to make using sub-ranges
76/// in for loops a bit easier. Analogous to std::make_pair().
77template <class T> iterator_range<T> make_range(T x, T y) {
78 return iterator_range<T>(std::move(x), std::move(y));
79}
80
81template <typename T> iterator_range<T> make_range(std::pair<T, T> p) {
82 return iterator_range<T>(std::move(p.first), std::move(p.second));
83}
84
85}
86
87#endif
A range adaptor for a pair of iterators.
Definition iterator_range.h:42
iterator_range(Container &&c)
Definition iterator_range.h:57
IteratorT end() const
Definition iterator_range.h:65
iterator_range(IteratorT begin_iterator, IteratorT end_iterator)
Definition iterator_range.h:60
bool empty() const
Definition iterator_range.h:66
IteratorT begin() const
Definition iterator_range.h:64
Definition PointerIntPair.h:280
decltype(adl_begin(std::declval< RangeT & >())) IterOfRange
Definition ADL.h:126
Definition ntcore_cpp.h:26
iterator_range(Container &&) -> iterator_range< wpi::detail::IterOfRange< Container > >
constexpr auto adl_end(RangeT &&range) -> decltype(adl_detail::end_impl(std::forward< RangeT >(range)))
Returns the end iterator to range using std::end and functions found through Argument-Dependent Looku...
Definition ADL.h:86
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition iterator_range.h:77
constexpr auto adl_begin(RangeT &&range) -> decltype(adl_detail::begin_impl(std::forward< RangeT >(range)))
Returns the begin iterator to range using std::begin and function found through Argument-Dependent Lo...
Definition ADL.h:78
Definition iterator_range.h:28
void void_t
Definition base.h:336