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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
|
/// \file
// Range v3 library
//
// Copyright Eric Niebler 2014-present
//
// Use, modification and distribution is subject to the
// Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ericniebler/range-v3
//
#ifndef RANGES_V3_ITERATOR_COUNTED_ITERATOR_HPP
#define RANGES_V3_ITERATOR_COUNTED_ITERATOR_HPP
#include <utility>
#include <meta/meta.hpp>
#include <range/v3/range_fwd.hpp>
#include <range/v3/iterator/concepts.hpp>
#include <range/v3/iterator/default_sentinel.hpp>
#include <range/v3/iterator/operations.hpp>
#include <range/v3/iterator/traits.hpp>
#include <range/v3/detail/prologue.hpp>
namespace ranges
{
/// \addtogroup group-iterator
/// @{
/// \cond
namespace _counted_iterator_
{
struct access
{
template<typename I>
static constexpr iter_difference_t<counted_iterator<I>> & count(
counted_iterator<I> & ci) noexcept
{
return ci.cnt_;
}
template<typename I>
static constexpr I & current(counted_iterator<I> & ci) noexcept
{
return ci.current_;
}
template<typename I>
static constexpr I const & current(counted_iterator<I> const & ci) noexcept
{
return ci.current_;
}
};
template<bool>
struct contiguous_iterator_concept_base
{};
template<>
struct contiguous_iterator_concept_base<true>
{
using iterator_concept = ranges::contiguous_iterator_tag;
};
} // namespace _counted_iterator_
/// \endcond
template<typename I>
// requires input_or_output_iterator<I>
struct counted_iterator
: _counted_iterator_::contiguous_iterator_concept_base<(bool)contiguous_iterator<I>>
{
private:
friend advance_fn;
CPP_assert(input_or_output_iterator<I>);
friend _counted_iterator_::access;
I current_{};
iter_difference_t<I> cnt_{0};
constexpr void post_increment_(std::true_type)
{
CPP_assert(std::is_void<decltype(current_++)>());
++current_;
--cnt_;
}
constexpr auto post_increment_(std::false_type) -> decltype(current_++)
{
CPP_assert(!std::is_void<decltype(current_++)>());
auto && tmp = current_++;
--cnt_;
return static_cast<decltype(tmp) &&>(tmp);
}
public:
using iterator_type = I;
using difference_type = iter_difference_t<I>;
counted_iterator() = default;
constexpr counted_iterator(I x, iter_difference_t<I> n)
: current_(std::move(x))
, cnt_(n)
{
RANGES_EXPECT(n >= 0);
}
template(typename I2)(
requires convertible_to<I2, I>)
constexpr counted_iterator(counted_iterator<I2> const & i)
: current_(_counted_iterator_::access::current(i))
, cnt_(i.count())
{}
template(typename I2)(
requires convertible_to<I2, I>)
constexpr counted_iterator & operator=(counted_iterator<I2> const & i)
{
current_ = _counted_iterator_::access::current(i);
cnt_ = i.count();
}
constexpr I base() const
{
return current_;
}
constexpr iter_difference_t<I> count() const
{
return cnt_;
}
constexpr iter_reference_t<I> operator*() noexcept(
noexcept(iter_reference_t<I>(*current_)))
{
RANGES_EXPECT(cnt_ > 0);
return *current_;
}
template(typename I2 = I)(
requires indirectly_readable<I2 const>)
constexpr iter_reference_t<I2> operator*() const //
noexcept(noexcept(iter_reference_t<I>(*current_)))
{
RANGES_EXPECT(cnt_ > 0);
return *current_;
}
constexpr counted_iterator & operator++()
{
RANGES_EXPECT(cnt_ > 0);
++current_;
--cnt_;
return *this;
}
#ifdef RANGES_WORKAROUND_MSVC_677925
template(typename I2 = I)(
requires (!forward_iterator<I2>)) //
constexpr auto operator++(int) -> decltype(std::declval<I2 &>()++)
#else // ^^^ workaround ^^^ / vvv no workaround vvv
CPP_member
constexpr auto operator++(int) //
-> CPP_ret(decltype(std::declval<I &>()++))(
requires (!forward_iterator<I>))
#endif // RANGES_WORKAROUND_MSVC_677925
{
RANGES_EXPECT(cnt_ > 0);
return post_increment_(std::is_void<decltype(current_++)>());
}
CPP_member
constexpr auto operator++(int) //
-> CPP_ret(counted_iterator)(
requires forward_iterator<I>)
{
auto tmp(*this);
++*this;
return tmp;
}
CPP_member
constexpr auto operator--() //
-> CPP_ret(counted_iterator &)(
requires bidirectional_iterator<I>)
{
--current_;
++cnt_;
return *this;
}
CPP_member
constexpr auto operator--(int) //
-> CPP_ret(counted_iterator)(
requires bidirectional_iterator<I>)
{
auto tmp(*this);
--*this;
return tmp;
}
CPP_member
constexpr auto operator+=(difference_type n) //
-> CPP_ret(counted_iterator &)(
requires random_access_iterator<I>)
{
RANGES_EXPECT(cnt_ >= n);
current_ += n;
cnt_ -= n;
return *this;
}
CPP_member
constexpr auto operator+(difference_type n) const //
-> CPP_ret(counted_iterator)(
requires random_access_iterator<I>)
{
auto tmp(*this);
tmp += n;
return tmp;
}
CPP_member
constexpr auto operator-=(difference_type n) //
-> CPP_ret(counted_iterator &)(
requires random_access_iterator<I>)
{
RANGES_EXPECT(cnt_ >= -n);
current_ -= n;
cnt_ += n;
return *this;
}
CPP_member
constexpr auto operator-(difference_type n) const //
-> CPP_ret(counted_iterator)(
requires random_access_iterator<I>)
{
auto tmp(*this);
tmp -= n;
return tmp;
}
CPP_member
constexpr auto operator[](difference_type n) const //
-> CPP_ret(iter_reference_t<I>)(
requires random_access_iterator<I>)
{
RANGES_EXPECT(cnt_ >= n);
return current_[n];
}
#if !RANGES_BROKEN_CPO_LOOKUP
CPP_broken_friend_member
friend constexpr auto iter_move(counted_iterator const & i) //
noexcept(detail::has_nothrow_iter_move_v<I>)
-> CPP_broken_friend_ret(iter_rvalue_reference_t<I>)(
requires input_iterator<I>)
{
return ranges::iter_move(i.current_);
}
template<typename I2, typename S2>
friend constexpr auto iter_swap(counted_iterator const & x,
counted_iterator<I2> const & y) //
noexcept(is_nothrow_indirectly_swappable<I, I2>::value)
-> CPP_broken_friend_ret(void)(
requires indirectly_swappable<I2, I>)
{
return ranges::iter_swap(x.current_, _counted_iterator_::access::current(y));
}
#endif
};
/// \cond
#if RANGES_BROKEN_CPO_LOOKUP
namespace _counted_iterator_
{
template<typename I>
constexpr auto iter_move(counted_iterator<I> const & i) noexcept(
detail::has_nothrow_iter_move_v<I>)
-> CPP_broken_friend_ret(iter_rvalue_reference_t<I>)(
requires input_iterator<I>)
{
return ranges::iter_move(_counted_iterator_::access::current(i));
}
template<typename I1, typename I2>
constexpr auto iter_swap(
counted_iterator<I1> const & x,
counted_iterator<I2> const &
y) noexcept(is_nothrow_indirectly_swappable<I1, I2>::value)
-> CPP_broken_friend_ret(void)(
requires indirectly_swappable<I2, I1>)
{
return ranges::iter_swap(_counted_iterator_::access::current(x),
_counted_iterator_::access::current(y));
}
} // namespace _counted_iterator_
#endif
/// endcond
template(typename I1, typename I2)(
requires common_with<I1, I2>)
constexpr bool operator==(counted_iterator<I1> const & x,
counted_iterator<I2> const & y)
{
return x.count() == y.count();
}
template<typename I>
constexpr bool operator==(counted_iterator<I> const & x, default_sentinel_t)
{
return x.count() == 0;
}
template<typename I>
constexpr bool operator==(default_sentinel_t, counted_iterator<I> const & x)
{
return x.count() == 0;
}
template(typename I1, typename I2)(
requires common_with<I1, I2>)
constexpr bool operator!=(counted_iterator<I1> const & x,
counted_iterator<I2> const & y)
{
return !(x == y);
}
template<typename I>
constexpr bool operator!=(counted_iterator<I> const & x, default_sentinel_t y)
{
return !(x == y);
}
template<typename I>
constexpr bool operator!=(default_sentinel_t x, counted_iterator<I> const & y)
{
return !(x == y);
}
template(typename I1, typename I2)(
requires common_with<I1, I2>)
constexpr bool operator<(counted_iterator<I1> const & x,
counted_iterator<I2> const & y)
{
return y.count() < x.count();
}
template(typename I1, typename I2)(
requires common_with<I1, I2>)
constexpr bool operator<=(counted_iterator<I1> const & x,
counted_iterator<I2> const & y)
{
return !(y < x);
}
template(typename I1, typename I2)(
requires common_with<I1, I2>)
constexpr bool operator>(counted_iterator<I1> const & x,
counted_iterator<I2> const & y)
{
return y < x;
}
template(typename I1, typename I2)(
requires common_with<I1, I2>)
constexpr bool operator>=(counted_iterator<I1> const & x,
counted_iterator<I2> const & y)
{
return !(x < y);
}
template(typename I1, typename I2)(
requires common_with<I1, I2>)
constexpr iter_difference_t<I2> operator-(counted_iterator<I1> const & x,
counted_iterator<I2> const & y)
{
return y.count() - x.count();
}
template<typename I>
constexpr iter_difference_t<I> operator-(counted_iterator<I> const & x,
default_sentinel_t)
{
return -x.count();
}
template<typename I>
constexpr iter_difference_t<I> operator-(default_sentinel_t,
counted_iterator<I> const & y)
{
return y.count();
}
template(typename I)(
requires random_access_iterator<I>)
constexpr counted_iterator<I> operator+(iter_difference_t<I> n,
counted_iterator<I> const & x)
{
return x + n;
}
template(typename I)(
requires input_or_output_iterator<I>)
constexpr counted_iterator<I> make_counted_iterator(I i, iter_difference_t<I> n)
{
return {std::move(i), n};
}
template<typename I>
struct indirectly_readable_traits<counted_iterator<I>>
: meta::conditional_t<
(bool)indirectly_readable<I>,
indirectly_readable_traits<I>,
meta::nil_>
{};
CPP_template_def(typename I)(
requires input_or_output_iterator<I>)
constexpr void advance_fn::operator()(counted_iterator<I> & i,
iter_difference_t<I> n) const
{
RANGES_EXPECT(n <= i.cnt_);
advance(i.current_, n);
i.cnt_ -= n;
}
namespace cpp20
{
using ranges::counted_iterator;
}
/// @}
} // namespace ranges
/// \cond
namespace ranges
{
namespace _counted_iterator_
{
template<typename I, typename = void>
struct iterator_traits_
{
using difference_type = iter_difference_t<I>;
using value_type = void;
using reference = void;
using pointer = void;
using iterator_category = std::output_iterator_tag;
};
template<typename I>
struct iterator_traits_<I, meta::if_c<input_iterator<I>>>
: std::iterator_traits<I>
{
using pointer = void;
};
} // namespace _counted_iterator_
} // namespace ranges
namespace std
{
template<typename I>
struct iterator_traits<::ranges::counted_iterator<I>>
: ::ranges::_counted_iterator_::iterator_traits_<I>
{};
} // namespace std
/// \endcond
#include <range/v3/detail/epilogue.hpp>
#endif
|