File: key_value_iterator.h

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (176 lines) | stat: -rw-r--r-- 6,180 bytes parent folder | download | duplicates (9)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP___FLAT_MAP_KEY_VALUE_ITERATOR_H
#define _LIBCPP___FLAT_MAP_KEY_VALUE_ITERATOR_H

#include <__compare/three_way_comparable.h>
#include <__concepts/convertible_to.h>
#include <__config>
#include <__iterator/iterator_traits.h>
#include <__memory/addressof.h>
#include <__type_traits/conditional.h>
#include <__utility/move.h>
#include <__utility/pair.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#  pragma GCC system_header
#endif

_LIBCPP_PUSH_MACROS
#include <__undef_macros>

#if _LIBCPP_STD_VER >= 23

_LIBCPP_BEGIN_NAMESPACE_STD

/**
 * __key_value_iterator is a proxy iterator which zips the underlying
 * _KeyContainer::iterator and the underlying _MappedContainer::iterator.
 * The two underlying iterators will be incremented/decremented together.
 * And the reference is a pair of the const key reference and the value reference.
 */
template <class _Owner, class _KeyContainer, class _MappedContainer, bool _Const>
struct __key_value_iterator {
private:
  using __key_iterator _LIBCPP_NODEBUG = typename _KeyContainer::const_iterator;
  using __mapped_iterator _LIBCPP_NODEBUG =
      _If<_Const, typename _MappedContainer::const_iterator, typename _MappedContainer::iterator>;
  using __reference _LIBCPP_NODEBUG = _If<_Const, typename _Owner::const_reference, typename _Owner::reference>;

  struct __arrow_proxy {
    __reference __ref_;
    _LIBCPP_HIDE_FROM_ABI __reference* operator->() { return std::addressof(__ref_); }
  };

  __key_iterator __key_iter_;
  __mapped_iterator __mapped_iter_;

  friend _Owner;

  template <class, class, class, bool>
  friend struct __key_value_iterator;

public:
  using iterator_concept = random_access_iterator_tag;
  // `__key_value_iterator` only satisfy "Cpp17InputIterator" named requirements, because
  // its `reference` is not a reference type.
  // However, to avoid surprising runtime behaviour when it is used with the
  // Cpp17 algorithms or operations, iterator_category is set to random_access_iterator_tag.
  using iterator_category = random_access_iterator_tag;
  using value_type        = typename _Owner::value_type;
  using difference_type   = typename _Owner::difference_type;

  _LIBCPP_HIDE_FROM_ABI __key_value_iterator() = default;

  _LIBCPP_HIDE_FROM_ABI __key_value_iterator(__key_value_iterator<_Owner, _KeyContainer, _MappedContainer, !_Const> __i)
    requires _Const && convertible_to<typename _KeyContainer::iterator, __key_iterator> &&
                 convertible_to<typename _MappedContainer::iterator, __mapped_iterator>
      : __key_iter_(std::move(__i.__key_iter_)), __mapped_iter_(std::move(__i.__mapped_iter_)) {}

  _LIBCPP_HIDE_FROM_ABI __key_value_iterator(__key_iterator __key_iter, __mapped_iterator __mapped_iter)
      : __key_iter_(std::move(__key_iter)), __mapped_iter_(std::move(__mapped_iter)) {}

  _LIBCPP_HIDE_FROM_ABI __reference operator*() const { return __reference(*__key_iter_, *__mapped_iter_); }
  _LIBCPP_HIDE_FROM_ABI __arrow_proxy operator->() const { return __arrow_proxy{**this}; }

  _LIBCPP_HIDE_FROM_ABI __key_value_iterator& operator++() {
    ++__key_iter_;
    ++__mapped_iter_;
    return *this;
  }

  _LIBCPP_HIDE_FROM_ABI __key_value_iterator operator++(int) {
    __key_value_iterator __tmp(*this);
    ++*this;
    return __tmp;
  }

  _LIBCPP_HIDE_FROM_ABI __key_value_iterator& operator--() {
    --__key_iter_;
    --__mapped_iter_;
    return *this;
  }

  _LIBCPP_HIDE_FROM_ABI __key_value_iterator operator--(int) {
    __key_value_iterator __tmp(*this);
    --*this;
    return __tmp;
  }

  _LIBCPP_HIDE_FROM_ABI __key_value_iterator& operator+=(difference_type __x) {
    __key_iter_ += __x;
    __mapped_iter_ += __x;
    return *this;
  }

  _LIBCPP_HIDE_FROM_ABI __key_value_iterator& operator-=(difference_type __x) {
    __key_iter_ -= __x;
    __mapped_iter_ -= __x;
    return *this;
  }

  _LIBCPP_HIDE_FROM_ABI __reference operator[](difference_type __n) const { return *(*this + __n); }

  _LIBCPP_HIDE_FROM_ABI friend constexpr bool
  operator==(const __key_value_iterator& __x, const __key_value_iterator& __y) {
    return __x.__key_iter_ == __y.__key_iter_;
  }

  _LIBCPP_HIDE_FROM_ABI friend bool operator<(const __key_value_iterator& __x, const __key_value_iterator& __y) {
    return __x.__key_iter_ < __y.__key_iter_;
  }

  _LIBCPP_HIDE_FROM_ABI friend bool operator>(const __key_value_iterator& __x, const __key_value_iterator& __y) {
    return __y < __x;
  }

  _LIBCPP_HIDE_FROM_ABI friend bool operator<=(const __key_value_iterator& __x, const __key_value_iterator& __y) {
    return !(__y < __x);
  }

  _LIBCPP_HIDE_FROM_ABI friend bool operator>=(const __key_value_iterator& __x, const __key_value_iterator& __y) {
    return !(__x < __y);
  }

  _LIBCPP_HIDE_FROM_ABI friend auto operator<=>(const __key_value_iterator& __x, const __key_value_iterator& __y)
    requires three_way_comparable<__key_iterator>
  {
    return __x.__key_iter_ <=> __y.__key_iter_;
  }

  _LIBCPP_HIDE_FROM_ABI friend __key_value_iterator operator+(const __key_value_iterator& __i, difference_type __n) {
    auto __tmp = __i;
    __tmp += __n;
    return __tmp;
  }

  _LIBCPP_HIDE_FROM_ABI friend __key_value_iterator operator+(difference_type __n, const __key_value_iterator& __i) {
    return __i + __n;
  }

  _LIBCPP_HIDE_FROM_ABI friend __key_value_iterator operator-(const __key_value_iterator& __i, difference_type __n) {
    auto __tmp = __i;
    __tmp -= __n;
    return __tmp;
  }

  _LIBCPP_HIDE_FROM_ABI friend difference_type
  operator-(const __key_value_iterator& __x, const __key_value_iterator& __y) {
    return difference_type(__x.__key_iter_ - __y.__key_iter_);
  }
};

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP_STD_VER >= 23

_LIBCPP_POP_MACROS

#endif // _LIBCPP___FLAT_MAP_KEY_VALUE_ITERATOR_H