/usr/include/range/v3/span.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 | /// \file
// Range v3 library
//
// Copyright Casey Carter 2016-2017
//
// 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_SPAN_HPP
#define RANGES_V3_SPAN_HPP
#include <cstddef>
#include <meta/meta.hpp>
#include <range/v3/begin_end.hpp>
#include <range/v3/data.hpp>
#include <range/v3/range_concepts.hpp>
#include <range/v3/range_fwd.hpp>
#include <range/v3/range_traits.hpp>
#include <range/v3/size.hpp>
#include <range/v3/view_interface.hpp>
#include <range/v3/algorithm/equal.hpp>
#include <range/v3/algorithm/lexicographical_compare.hpp>
#include <range/v3/utility/iterator.hpp>
namespace ranges
{
inline namespace v3
{
/// \cond
namespace detail
{
using span_index_t = std::ptrdiff_t;
} // namespace detail
/// \endcond
constexpr detail::span_index_t dynamic_extent = -1;
/// \cond
namespace detail
{
template<typename To, typename From,
CONCEPT_REQUIRES_(Integral<To>() && Integral<From>())>
constexpr To narrow_cast(From from) noexcept
{
using C = common_type_t<To, From>;
return RANGES_EXPECT((from > 0) == (static_cast<To>(from) > 0)),
RANGES_EXPECT(static_cast<C>(from) == static_cast<C>(static_cast<To>(from))),
static_cast<To>(from);
}
template<typename T>
constexpr span_index_t byte_size(span_index_t n) noexcept
{
return n == dynamic_extent ? dynamic_extent
: (RANGES_EXPECT(n >= 0),
RANGES_EXPECT(narrow_cast<std::size_t>(n) <= PTRDIFF_MAX / sizeof(T)),
n * narrow_cast<span_index_t>(sizeof(T)));
}
template<span_index_t N>
struct span_extent
{
CONCEPT_ASSERT(N >= 0);
constexpr span_extent() noexcept = default;
constexpr span_extent(span_index_t size) noexcept
// this constructor does nothing, the delegation exists only
// to provide a place for the contract check expression.
: span_extent{(RANGES_EXPECT(size == N), tag{})}
{}
constexpr span_index_t size() const noexcept
{
return N;
}
private:
struct tag {};
constexpr span_extent(tag) noexcept
{}
};
template<>
struct span_extent<dynamic_extent>
{
span_extent() = default;
constexpr span_extent(span_index_t size) noexcept
: size_{((void)RANGES_EXPECT(size >= 0), size)}
{}
constexpr span_index_t size() const noexcept
{
return size_;
}
private:
span_index_t size_ = 0;
};
} // namespace detail
/// \endcond
template<typename T, detail::span_index_t N = dynamic_extent>
struct span
: public view_interface<span<T, N>,
(N == dynamic_extent ? finite : static_cast<cardinality>(N))>,
public detail::span_extent<N>
{
CONCEPT_ASSERT(std::is_object<T>::value);
using element_type = T;
using value_type = meta::_t<std::remove_cv<T>>;
using index_type = detail::span_index_t;
using difference_type = index_type;
using pointer = T *;
using reference = T &;
using iterator = T *;
using reverse_iterator = ranges::reverse_iterator<iterator>;
static constexpr index_type extent = N;
constexpr span() noexcept = default;
constexpr span(pointer ptr, index_type count) noexcept
: detail::span_extent<N>{(RANGES_EXPECT(count >= 0), count)}
, data_{(RANGES_EXPECT(0 == count || ptr != nullptr), ptr)}
{}
template<typename = void> // Artificially templatize so that the other
// constructor is preferred for {ptr, 0}
constexpr span(pointer first, pointer last) noexcept
: span{first, last - first}
{}
template<typename Rng>
using CompatibleRange =
meta::and_<meta::bool_<!Same<span, uncvref_t<Rng>>()>,
SizedRange<Rng>, ContiguousRange<Rng>,
std::is_convertible<concepts::ContiguousRange::element_t<Rng>(*)[], T(*)[]>>;
template<typename Rng>
using DynamicConversion = meta::bool_<
N == dynamic_extent || (range_cardinality<Rng>::value < cardinality{})>;
template<typename Rng,
// This multiple-CONCEPT_REQUIRES_ form works around a gcc 4.9 bug.
CONCEPT_REQUIRES_(CompatibleRange<Rng>()),
CONCEPT_REQUIRES_(DynamicConversion<Rng>())>
constexpr span(Rng &&rng)
noexcept(noexcept(ranges::data(rng), ranges::size(rng)))
: span{ranges::data(rng), detail::narrow_cast<index_type>(ranges::size(rng))}
{}
template<typename Rng>
using StaticConversion = meta::bool_<
N != dynamic_extent && range_cardinality<Rng>::value == N>;
template<typename Rng,
CONCEPT_REQUIRES_(CompatibleRange<Rng>()),
CONCEPT_REQUIRES_(StaticConversion<Rng>())>
constexpr span(Rng &&rng)
noexcept(noexcept(ranges::data(rng)))
: span{ranges::data(rng), N}
{}
template<index_type Count>
constexpr span<T, Count> first() const noexcept
{
static_assert(Count >= 0,
"Count of elements to extract cannot be negative.");
static_assert(N == dynamic_extent || Count <= N,
"Count of elements to extract must be less than the static span extent.");
return RANGES_EXPECT(Count <= size()),
RANGES_EXPECT(Count == 0 || data_ != nullptr),
span<T, Count>{data_, Count};
}
constexpr span<T> first(index_type count) const noexcept
{
return RANGES_EXPECT(count >= 0 && count <= size()),
RANGES_EXPECT(count == 0 || data_ != nullptr),
span<T>{data_, count};
}
template<index_type Count>
constexpr span<T, Count> last() const noexcept
{
static_assert(Count >= 0,
"Count of elements to extract cannot be negative.");
static_assert(N == dynamic_extent || Count <= N,
"Count of elements to extract must be less than the static span extent.");
return RANGES_EXPECT(Count <= size()),
RANGES_EXPECT((Count == 0 && size() == 0) || data_ != nullptr),
span<T, Count>{data_ + size() - Count, Count};
}
constexpr span<T> last(index_type count) const noexcept
{
return RANGES_EXPECT(count >= 0 && count <= size()),
RANGES_EXPECT((count == 0 && size() == 0) || data_ != nullptr),
span<T>{data_ + size() - count, count};
}
template<index_type Offset, index_type Count>
constexpr span<T, Count> subspan() const noexcept
{
static_assert(Offset >= 0,
"Offset of first element to extract cannot be negative.");
static_assert(Count >= 0,
"Count of elements to extract cannot be negative.");
static_assert(N == dynamic_extent || N >= Offset + Count,
"Sequence of elements to extract must be within the static span extent.");
return RANGES_EXPECT(size() >= Offset + Count),
RANGES_EXPECT((Offset == 0 && Count == 0) || data_ != nullptr),
span<T, Count>{data_ + Offset, Count};
}
template<index_type Offset>
constexpr span<T, N >= Offset ? N - Offset : dynamic_extent> subspan() const noexcept
{
static_assert(Offset >= 0,
"Offset of first element to extract cannot be negative.");
static_assert(N == dynamic_extent || Offset <= N,
"Offset of first element to extract must be less than the static span extent.");
return RANGES_EXPECT(size() >= Offset),
RANGES_EXPECT((Offset == 0 && size() == 0) || data_ != nullptr),
span<T, N >= Offset ? N - Offset : dynamic_extent>{
data_ + Offset, size() - Offset};
}
constexpr span<T, dynamic_extent> subspan(index_type offset) const noexcept
{
return RANGES_EXPECT(offset >= 0),
RANGES_EXPECT(size() >= offset),
RANGES_EXPECT((offset == 0 && size() == 0) || data_ != nullptr),
span<T, dynamic_extent>{data_ + offset, size() - offset};
}
constexpr span<T, dynamic_extent> subspan(
index_type offset, index_type count) const noexcept
{
return RANGES_EXPECT(offset >= 0),
RANGES_EXPECT(count >= 0),
RANGES_EXPECT(size() >= offset + count),
RANGES_EXPECT((offset == 0 && count == 0) || data_ != nullptr),
span<T, dynamic_extent>{data_ + offset, count};
}
constexpr pointer data() const noexcept { return data_; }
using detail::span_extent<N>::size;
constexpr index_type size_bytes() const noexcept
{
return detail::byte_size<T>(size());
}
constexpr bool empty() const noexcept { return size() == 0; }
constexpr reference operator[](index_type idx) const noexcept
{
return RANGES_EXPECT(idx >= 0),
RANGES_EXPECT(idx < size()),
RANGES_EXPECT(data_),
data_[idx];
}
constexpr iterator begin() const noexcept
{
return RANGES_EXPECT(!size() || data_),
data_;
}
constexpr iterator end() const noexcept
{
return RANGES_EXPECT(!size() || data_),
data_ + size();
}
constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; }
constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; }
template<typename U, index_type M,
CONCEPT_REQUIRES_(EqualityComparable<T, U>())>
bool operator==(span<U, M> const &that) const
{
RANGES_EXPECT(!size() || data());
RANGES_EXPECT(!that.size() || that.data());
return ranges::equal(*this, that);
}
template<typename U, index_type M,
CONCEPT_REQUIRES_(EqualityComparable<T, U>())>
bool operator!=(span<U, M> const &that) const
{
return !(*this == that);
}
template<typename U, index_type M,
CONCEPT_REQUIRES_(TotallyOrdered<T, U>())>
bool operator<(span<U, M> const &that) const
{
RANGES_EXPECT(!size() || data());
RANGES_EXPECT(!that.size() || that.data());
return ranges::lexicographical_compare(*this, that);
}
template<typename U, index_type M,
CONCEPT_REQUIRES_(TotallyOrdered<T, U>())>
bool operator>(span<U, M> const &that) const
{
return that < *this;
}
template<typename U, index_type M,
CONCEPT_REQUIRES_(TotallyOrdered<T, U>())>
bool operator<=(span<U, M> const &that) const
{
return !(that < *this);
}
template<typename U, index_type M,
CONCEPT_REQUIRES_(TotallyOrdered<T, U>())>
bool operator>=(span<U, M> const &that) const
{
return !(*this < that);
}
private:
T *data_ = nullptr;
};
template<typename T, detail::span_index_t N>
constexpr detail::span_index_t span<T, N>::extent;
#if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
template<typename Rng,
CONCEPT_REQUIRES_(ContiguousRange<Rng>() &&
range_cardinality<Rng>::value < cardinality{})>
span(Rng &&rng) ->
span<concepts::ContiguousRange::element_t<Rng>>;
template<typename Rng,
CONCEPT_REQUIRES_(ContiguousRange<Rng>() &&
range_cardinality<Rng>::value >= cardinality{})>
span(Rng &&rng) ->
span<concepts::ContiguousRange::element_t<Rng>,
static_cast<detail::span_index_t>(range_cardinality<Rng>::value)>;
#endif
template<typename T, detail::span_index_t N>
span<unsigned char const, detail::byte_size<T>(N)>
as_bytes(span<T, N> s) noexcept
{
return {reinterpret_cast<unsigned char const *>(s.data()), s.size_bytes()};
}
template<typename T, detail::span_index_t N>
span<unsigned char, detail::byte_size<T>(N)>
as_writeable_bytes(span<T, N> s) noexcept
{
return {reinterpret_cast<unsigned char *>(s.data()), s.size_bytes()};
}
template<typename ElementType>
constexpr span<ElementType>
make_span(ElementType *ptr, detail::span_index_t count) noexcept
{
return span<ElementType>{ptr, count};
}
template<typename ElementType>
constexpr span<ElementType>
make_span(ElementType *first, ElementType *last) noexcept
{
return span<ElementType>{first, last};
}
template<typename Rng,
CONCEPT_REQUIRES_(ContiguousRange<Rng>() &&
range_cardinality<Rng>::value < cardinality{})>
constexpr span<concepts::ContiguousRange::element_t<Rng>>
make_span(Rng &&rng)
noexcept(noexcept(ranges::data(rng), ranges::size(rng)))
{
return {ranges::data(rng),
detail::narrow_cast<detail::span_index_t>(ranges::size(rng))};
}
template<typename Rng,
CONCEPT_REQUIRES_(ContiguousRange<Rng>() &&
range_cardinality<Rng>::value >= cardinality{})>
constexpr span<concepts::ContiguousRange::element_t<Rng>,
static_cast<detail::span_index_t>(range_cardinality<Rng>::value)>
make_span(Rng &&rng)
noexcept(noexcept(ranges::data(rng)))
{
return {ranges::data(rng), range_cardinality<Rng>::value};
}
} // namespace v3
} // namespace ranges
#endif // RANGES_V3_SPAN_HPP
|