File: contiguous_iterator.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (113 lines) | stat: -rw-r--r-- 4,526 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_CONTAINERS_CONTIGUOUS_ITERATOR_H_
#define BASE_CONTAINERS_CONTIGUOUS_ITERATOR_H_

#include <array>
#include <iterator>
#include <string>
#include <type_traits>
#include <vector>

#include "base/containers/checked_iterators.h"

namespace base {

namespace internal {

template <typename T>
struct PointsToObject : std::true_type {};
// std::iter_value_t is not defined for `T*` where T is not an object type.
template <typename T>
struct PointsToObject<T*> : std::is_object<T> {};

// A pointer is a contiguous iterator.
// Reference: https://wg21.link/iterator.traits#5
template <typename T>
struct IsPointer : std::is_pointer<T> {};

template <typename T, typename StringT = std::basic_string<iter_value_t<T>>>
struct IsStringIterImpl
    : std::disjunction<std::is_same<T, typename StringT::const_iterator>,
                       std::is_same<T, typename StringT::iterator>> {};

// An iterator to std::basic_string is contiguous.
// Reference: https://wg21.link/basic.string.general#2
//
// Note: Requires indirection via `IsStringIterImpl` to avoid triggering a
// `static_assert(is_trivial_v<value_type>)` inside libc++'s std::basic_string.
template <typename T>
struct IsStringIter
    : std::conjunction<
          std::disjunction<std::is_same<iter_value_t<T>, char>,
                           std::is_same<iter_value_t<T>, wchar_t>,
                           std::is_same<iter_value_t<T>, char8_t>,
                           std::is_same<iter_value_t<T>, char16_t>,
                           std::is_same<iter_value_t<T>, char32_t>>,
          IsStringIterImpl<T>> {};

// An iterator to std::array is contiguous.
// Reference: https://wg21.link/array.overview#1
template <typename T, typename ArrayT = std::array<iter_value_t<T>, 1>>
struct IsArrayIter
    : std::disjunction<std::is_same<T, typename ArrayT::const_iterator>,
                       std::is_same<T, typename ArrayT::iterator>> {};

// An iterator to a non-bool std::vector is contiguous.
// Reference: https://wg21.link/vector.overview#2
template <typename T, typename VectorT = std::vector<iter_value_t<T>>>
struct IsVectorIter
    : std::conjunction<
          std::negation<std::is_same<iter_value_t<T>, bool>>,
          std::disjunction<std::is_same<T, typename VectorT::const_iterator>,
                           std::is_same<T, typename VectorT::iterator>>> {};

// The result of passing a std::valarray to std::begin is a contiguous iterator.
// Note: Since all common standard library implementations (i.e. libc++,
// stdlibc++ and MSVC's STL) just use a pointer here, we perform a similar
// optimization. The corresponding unittest still ensures that this is working
// as intended.
// Reference: https://wg21.link/valarray.range#1
template <typename T>
struct IsValueArrayIter : std::is_pointer<T> {};

// base's CheckedContiguousIterator is a contiguous iterator.
template <typename T, typename ValueT = iter_value_t<T>>
struct IsCheckedContiguousIter
    : std::disjunction<
          std::is_same<T, base::CheckedContiguousConstIterator<ValueT>>,
          std::is_same<T, base::CheckedContiguousIterator<ValueT>>> {};

// Check that the iterator points to an actual object, and is one of the
// iterator types mentioned above.
template <typename T, bool B = PointsToObject<T>::value>
struct IsContiguousIteratorImpl : std::false_type {};
template <typename T>
struct IsContiguousIteratorImpl<T, true>
    : std::disjunction<IsPointer<T>,
                       IsStringIter<T>,
                       IsArrayIter<T>,
                       IsVectorIter<T>,
                       IsValueArrayIter<T>,
                       IsCheckedContiguousIter<T>> {};

}  // namespace internal

// IsContiguousIterator is a type trait that determines whether a given type is
// a contiguous iterator. It is similar to C++20's contiguous_iterator concept,
// but due to a lack of the corresponding contiguous_iterator_tag relies on
// explicitly instantiating the type with iterators that are supposed to be
// contiguous iterators.
// References:
// - https://wg21.link/iterator.concept.contiguous
// - https://wg21.link/std.iterator.tags#lib:contiguous_iterator_tag
// - https://wg21.link/n4284
template <typename T>
struct IsContiguousIterator
    : internal::IsContiguousIteratorImpl<remove_cvref_t<T>> {};

}  // namespace base

#endif  // BASE_CONTAINERS_CONTIGUOUS_ITERATOR_H_