/usr/include/range/v3/view/slice.hpp is in librange-v3-dev 0.3.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /// \file
// Range v3 library
//
// Copyright Eric Niebler 2013-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_VIEW_SLICE_HPP
#define RANGES_V3_VIEW_SLICE_HPP
#include <type_traits>
#include <meta/meta.hpp>
#include <range/v3/detail/satisfy_boost_range.hpp>
#include <range/v3/range_fwd.hpp>
#include <range/v3/range_traits.hpp>
#include <range/v3/range_concepts.hpp>
#include <range/v3/view_interface.hpp>
#include <range/v3/iterator_range.hpp>
#include <range/v3/utility/counted_iterator.hpp>
#include <range/v3/utility/functional.hpp>
#include <range/v3/utility/iterator_traits.hpp>
#include <range/v3/utility/optional.hpp>
#include <range/v3/utility/static_const.hpp>
#include <range/v3/view/all.hpp>
#include <range/v3/view/drop_exactly.hpp>
#include <range/v3/view/view.hpp>
namespace ranges
{
inline namespace v3
{
/// \cond
namespace detail
{
template<typename Rng, typename Int>
iterator_t<Rng> pos_at_(Rng && rng, Int i, concepts::InputRange *,
std::true_type)
{
RANGES_EXPECT(0 <= i);
return next(ranges::begin(rng), i);
}
template<typename Rng, typename Int>
iterator_t<Rng> pos_at_(Rng && rng, Int i, concepts::BidirectionalRange *,
std::false_type)
{
if(0 > i)
{
// If it's not bounded and we know the size, faster to count from the front
if(SizedRange<Rng>() && !BoundedRange<Rng>())
return next(ranges::begin(rng), distance(rng) + i);
// Otherwise, probably faster to count from the back.
return next(ranges::next(ranges::begin(rng), ranges::end(rng)), i);
}
return next(ranges::begin(rng), i);
}
template<typename Rng, typename Int>
iterator_t<Rng> pos_at_(Rng && rng, Int i, concepts::InputRange *,
std::false_type)
{
RANGES_EXPECT(i >= 0 || SizedRange<Rng>() || ForwardRange<Rng>());
if(0 > i)
return next(ranges::begin(rng), distance(rng) + i);
return next(ranges::begin(rng), i);
}
template<typename Rng, bool IsRandomAccess = RandomAccessRange<Rng>()>
struct slice_view_
: view_facade<slice_view<Rng>, finite>
{
private:
friend range_access;
using difference_type_ = range_difference_type_t<Rng>;
Rng rng_;
difference_type_ from_, count_;
detail::non_propagating_cache<iterator_t<Rng>> begin_;
iterator_t<Rng> get_begin_()
{
if(!begin_)
begin_ = detail::pos_at_(rng_, from_, range_concept<Rng>{},
is_infinite<Rng>{});
return *begin_;
}
public:
slice_view_() = default;
slice_view_(Rng rng, difference_type_ from, difference_type_ count)
: rng_(std::move(rng)), from_(from), count_(count)
{}
counted_iterator<iterator_t<Rng>> begin()
{
return make_counted_iterator(get_begin_(), count_);
}
default_sentinel end()
{
return {};
}
range_size_type_t<Rng> size() const
{
return static_cast<range_size_type_t<Rng>>(count_);
}
Rng & base()
{
return rng_;
}
Rng const & base() const
{
return rng_;
}
};
template<typename Rng>
struct slice_view_<Rng, true>
: view_interface<slice_view<Rng>, finite>
{
private:
using difference_type_ = range_difference_type_t<Rng>;
Rng rng_;
difference_type_ from_, count_;
public:
slice_view_() = default;
slice_view_(Rng rng, difference_type_ from, difference_type_ count)
: rng_(std::move(rng)), from_(from), count_(count)
{
RANGES_EXPECT(0 <= count_);
}
iterator_t<Rng> begin()
{
return detail::pos_at_(rng_, from_, range_concept<Rng>{},
is_infinite<Rng>{});
}
iterator_t<Rng> end()
{
return detail::pos_at_(rng_, from_, range_concept<Rng>{},
is_infinite<Rng>{}) + count_;
}
template<typename BaseRng = Rng,
CONCEPT_REQUIRES_(Range<BaseRng const>())>
iterator_t<BaseRng const> begin() const
{
return detail::pos_at_(rng_, from_, range_concept<Rng>{},
is_infinite<Rng>{});
}
template<typename BaseRng = Rng,
CONCEPT_REQUIRES_(Range<BaseRng const>())>
iterator_t<BaseRng const> end() const
{
return detail::pos_at_(rng_, from_, range_concept<Rng>{},
is_infinite<Rng>{}) + count_;
}
range_size_type_t<Rng> size() const
{
return static_cast<range_size_type_t<Rng>>(count_);
}
Rng & base()
{
return rng_;
}
Rng const & base() const
{
return rng_;
}
};
}
/// \endcond
/// \cond
namespace _end_
{
template<typename Int, CONCEPT_REQUIRES_(Integral<Int>())>
detail::from_end_<meta::_t<std::make_signed<Int>>> operator-(end_fn, Int dist)
{
RANGES_EXPECT(0 <= static_cast<meta::_t<std::make_signed<Int>>>(dist));
return {-static_cast<meta::_t<std::make_signed<Int>>>(dist)};
}
}
/// \endcond
/// \addtogroup group-views
/// @{
template<typename Rng>
struct slice_view
: detail::slice_view_<Rng>
{
using detail::slice_view_<Rng>::slice_view_;
};
namespace view
{
struct slice_fn
{
private:
friend view_access;
template<typename Rng>
static slice_view<all_t<Rng>>
invoke_(Rng && rng, range_difference_type_t<Rng> from, range_difference_type_t<Rng> count,
concepts::InputRange *, concepts::Range * = nullptr)
{
return {all(static_cast<Rng&&>(rng)), from, count};
}
template<typename Rng,
CONCEPT_REQUIRES_(!View<uncvref_t<Rng>>() && std::is_lvalue_reference<Rng>())>
static iterator_range<iterator_t<Rng>>
invoke_(Rng && rng, range_difference_type_t<Rng> from, range_difference_type_t<Rng> count,
concepts::RandomAccessRange *, concepts::BoundedRange * = nullptr)
{
auto it = detail::pos_at_(rng, from, range_concept<Rng>{}, is_infinite<Rng>{});
return {it, it + count};
}
// Overloads for the pipe syntax: rng | view::slice(from,to)
template<typename Int, CONCEPT_REQUIRES_(Integral<Int>())>
static auto bind(slice_fn slice, Int from, Int to)
RANGES_DECLTYPE_AUTO_RETURN
(
make_pipeable(std::bind(slice, std::placeholders::_1, from, to))
)
template<typename Int, CONCEPT_REQUIRES_(Integral<Int>())>
static auto bind(slice_fn slice, Int from, detail::from_end_<Int> to)
RANGES_DECLTYPE_AUTO_RETURN
(
make_pipeable(std::bind(slice, std::placeholders::_1, from, to))
)
template<typename Int, CONCEPT_REQUIRES_(Integral<Int>())>
static auto bind(slice_fn slice, detail::from_end_<Int> from, detail::from_end_<Int> to)
RANGES_DECLTYPE_AUTO_RETURN
(
make_pipeable(std::bind(slice, std::placeholders::_1, from, to))
)
template<typename Int, CONCEPT_REQUIRES_(Integral<Int>())>
static auto bind(slice_fn, Int from, end_fn)
RANGES_DECLTYPE_AUTO_RETURN
(
make_pipeable(std::bind(ranges::view::drop_exactly, std::placeholders::_1, from))
)
template<typename Int, CONCEPT_REQUIRES_(Integral<Int>())>
static auto bind(slice_fn slice, detail::from_end_<Int> from, end_fn to)
RANGES_DECLTYPE_AUTO_RETURN
(
make_pipeable(std::bind(slice, std::placeholders::_1, from, to))
)
public:
// slice(rng, 2, 4)
template<typename Rng,
CONCEPT_REQUIRES_(InputRange<Rng>())>
auto operator()(Rng && rng, range_difference_type_t<Rng> from,
range_difference_type_t<Rng> to) const ->
decltype(slice_fn::invoke_(static_cast<Rng&&>(rng), from, to - from,
range_concept<Rng>{}))
{
RANGES_EXPECT(0 <= from && from <= to);
return slice_fn::invoke_(static_cast<Rng&&>(rng), from, to - from,
range_concept<Rng>{});
}
// slice(rng, 4, end-2)
// TODO Support Forward, non-Sized ranges by returning a range that
// doesn't know it's size?
template<typename Rng,
CONCEPT_REQUIRES_(InputRange<Rng>() && SizedRange<Rng>())>
auto operator()(Rng && rng, range_difference_type_t<Rng> from,
detail::from_end_<range_difference_type_t<Rng>> to) const ->
decltype(slice_fn::invoke_(static_cast<Rng&&>(rng), from,
distance(rng) + to.dist_ - from, range_concept<Rng>{}))
{
static_assert(!is_infinite<Rng>(),
"Can't index from the end of an infinite range!");
RANGES_EXPECT(0 <= from);
RANGES_ASSERT(from <= distance(rng) + to.dist_);
return slice_fn::invoke_(static_cast<Rng&&>(rng), from,
distance(rng) + to.dist_ - from, range_concept<Rng>{});
}
// slice(rng, end-4, end-2)
template<typename Rng,
CONCEPT_REQUIRES_((InputRange<Rng>() && SizedRange<Rng>()) ||
ForwardRange<Rng>())>
auto operator()(Rng && rng, detail::from_end_<range_difference_type_t<Rng>> from,
detail::from_end_<range_difference_type_t<Rng>> to) const ->
decltype(slice_fn::invoke_(static_cast<Rng&&>(rng), from.dist_,
to.dist_ - from.dist_, range_concept<Rng>{},
bounded_range_concept<Rng>{}()))
{
static_assert(!is_infinite<Rng>(),
"Can't index from the end of an infinite range!");
RANGES_EXPECT(from.dist_ <= to.dist_);
return slice_fn::invoke_(static_cast<Rng&&>(rng), from.dist_,
to.dist_ - from.dist_, range_concept<Rng>{},
bounded_range_concept<Rng>{}());
}
// slice(rng, 4, end)
template<typename Rng,
CONCEPT_REQUIRES_(InputRange<Rng>())>
auto operator()(Rng && rng, range_difference_type_t<Rng> from, end_fn) const ->
decltype(ranges::view::drop_exactly(static_cast<Rng&&>(rng), from))
{
RANGES_EXPECT(0 <= from);
return ranges::view::drop_exactly(static_cast<Rng&&>(rng), from);
}
// slice(rng, end-4, end)
template<typename Rng,
CONCEPT_REQUIRES_((InputRange<Rng>() && SizedRange<Rng>()) ||
ForwardRange<Rng>())>
auto operator()(Rng && rng, detail::from_end_<range_difference_type_t<Rng>> from,
end_fn) const ->
decltype(slice_fn::invoke_(static_cast<Rng&&>(rng), from.dist_,
-from.dist_, range_concept<Rng>{},
bounded_range_concept<Rng>{}()))
{
static_assert(!is_infinite<Rng>(),
"Can't index from the end of an infinite range!");
return slice_fn::invoke_(static_cast<Rng&&>(rng), from.dist_,
-from.dist_, range_concept<Rng>{},
bounded_range_concept<Rng>{}());
}
#ifndef RANGES_DOXYGEN_INVOKED
//
// These overloads are strictly so that users get better error messages
// when they try to slice things in a way that doesn't support the operation.
//
// slice(rng, 2, 4)
template<typename Rng,
CONCEPT_REQUIRES_(!InputRange<Rng>())>
void operator()(Rng &&, range_difference_type_t<Rng>, range_difference_type_t<Rng>) const
{
CONCEPT_ASSERT_MSG(InputRange<Rng>(),
"The object to be sliced must be a model of the InputRange concept.");
}
// slice(rng, 4, end-2)
template<typename Rng,
CONCEPT_REQUIRES_(!(InputRange<Rng>() && SizedRange<Rng>()))>
void operator()(Rng &&, range_difference_type_t<Rng>,
detail::from_end_<range_difference_type_t<Rng>>) const
{
CONCEPT_ASSERT_MSG(InputRange<Rng>(),
"The object to be sliced must be a model of the InputRange concept.");
CONCEPT_ASSERT_MSG(SizedRange<Rng>(),
"When slicing a range with a positive start offset and a stop offset "
"measured from the end, the range must be a model of the SizedRange "
"concept; that is, its size must be known.");
}
// slice(rng, end-4, end-2)
template<typename Rng,
CONCEPT_REQUIRES_(!((InputRange<Rng>() && SizedRange<Rng>()) ||
ForwardRange<Rng>()))>
void operator()(Rng &&, detail::from_end_<range_difference_type_t<Rng>>,
detail::from_end_<range_difference_type_t<Rng>>) const
{
CONCEPT_ASSERT_MSG(InputRange<Rng>(),
"The object to be sliced must be a model of the InputRange concept.");
CONCEPT_ASSERT_MSG(SizedRange<Rng>() || ForwardRange<Rng>(),
"When slicing a range with a start and stop offset measured from the end, "
"the range must either be a model of the SizedRange concept (its size "
"must be known), or it must be a model of the ForwardRange concept.");
}
// slice(rng, 4, end)
template<typename Rng,
CONCEPT_REQUIRES_(!(InputRange<Rng>()))>
void operator()(Rng &&, range_difference_type_t<Rng>, end_fn) const
{
CONCEPT_ASSERT_MSG(InputRange<Rng>(),
"The object to be sliced must be a model of the InputRange concept.");
}
// slice(rng, end-4, end)
template<typename Rng,
CONCEPT_REQUIRES_(!((InputRange<Rng>() && SizedRange<Rng>()) ||
ForwardRange<Rng>()))>
void operator()(Rng &&, detail::from_end_<range_difference_type_t<Rng>>, end_fn) const
{
CONCEPT_ASSERT_MSG(InputRange<Rng>(),
"The object to be sliced must be a model of the InputRange concept.");
CONCEPT_ASSERT_MSG(SizedRange<Rng>() || ForwardRange<Rng>(),
"When slicing a range with a start and stop offset measured from the end, "
"the range must either be a model of the SizedRange concept (its size "
"must be known), or it must be a model of the ForwardRange concept.");
}
#endif
};
/// \relates slice_fn
/// \ingroup group-views
RANGES_INLINE_VARIABLE(view<slice_fn>, slice)
}
/// @}
}
}
RANGES_SATISFY_BOOST_RANGE(::ranges::v3::slice_view)
#endif
|