Pixie
Loading...
Searching...
No Matches
split_span.h
Go to the documentation of this file.
1#pragma once
2
7
8#include <array>
9#include <cstddef>
10#include <span>
11
12namespace pixie {
13
24template <class T>
25class SplitSpan {
26 public:
27 using span_type = std::span<T>;
28 using const_iterator = typename std::array<span_type, 2>::const_iterator;
29
31 constexpr SplitSpan() = default;
32
34 constexpr explicit SplitSpan(span_type segment)
35 : segments_{segment, {}}, segment_count_(segment.empty() ? 0 : 1) {}
36
38 constexpr SplitSpan(span_type first, span_type second) {
39 if (first.empty()) {
40 first = second;
41 second = {};
42 }
43 segments_ = {first, second};
44 segment_count_ = static_cast<std::size_t>(!first.empty()) +
45 static_cast<std::size_t>(!second.empty());
46 }
47
49 constexpr std::size_t size() const noexcept {
50 return segments_[0].size() + segments_[1].size();
51 }
52
54 constexpr bool empty() const noexcept { return segment_count_ == 0; }
55
57 constexpr std::size_t segment_count() const noexcept {
58 return segment_count_;
59 }
60
62 constexpr const_iterator begin() const noexcept { return segments_.begin(); }
63
65 constexpr const_iterator end() const noexcept {
66 return segments_.begin() + static_cast<std::ptrdiff_t>(segment_count_);
67 }
68
69 private:
70 std::array<span_type, 2> segments_{};
71 std::size_t segment_count_ = 0;
72};
73
74} // namespace pixie
constexpr const_iterator begin() const noexcept
Iterate over the non-empty physical segments in logical order.
Definition split_span.h:62
constexpr std::size_t segment_count() const noexcept
Return the number of non-empty physical segments.
Definition split_span.h:57
constexpr const_iterator end() const noexcept
Return the end iterator for the non-empty physical segments.
Definition split_span.h:65
constexpr std::size_t size() const noexcept
Return the total number of logical elements.
Definition split_span.h:49
constexpr SplitSpan(span_type first, span_type second)
Construct a range stored in up to two physical spans.
Definition split_span.h:38
constexpr SplitSpan()=default
Construct an empty logical range.
constexpr SplitSpan(span_type segment)
Construct a range stored in one physical span.
Definition split_span.h:34
constexpr bool empty() const noexcept
Return whether the logical range is empty.
Definition split_span.h:54