/src/jsoncons/include/jsoncons/detail/span.hpp
Line | Count | Source |
1 | | // Copyright 2013-2026 Daniel Parker |
2 | | // Distributed under the Boost license, Version 1.0. |
3 | | // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
4 | | |
5 | | // See https://github.com/danielaparker/jsoncons for latest version |
6 | | |
7 | | #ifndef JSONCONS_DETAIL_SPAN_HPP |
8 | | #define JSONCONS_DETAIL_SPAN_HPP |
9 | | |
10 | | #include <array> |
11 | | #include <cstddef> |
12 | | #include <iterator> |
13 | | #include <limits> |
14 | | #include <memory> // std::addressof |
15 | | #include <type_traits> // std::enable_if, std::true_type, std::false_type |
16 | | |
17 | | #include <jsoncons/utility/more_type_traits.hpp> |
18 | | |
19 | | namespace jsoncons { |
20 | | namespace detail { |
21 | | |
22 | | constexpr std::size_t dynamic_extent = (std::numeric_limits<std::size_t>::max)(); |
23 | | |
24 | | template< typename T, std::size_t Extent = dynamic_extent> |
25 | | class span; |
26 | | |
27 | | template <typename T> |
28 | | struct is_span : std::false_type{}; |
29 | | |
30 | | template< typename T> |
31 | | struct is_span<span<T>> : std::true_type{}; |
32 | | |
33 | | template< |
34 | | typename T, |
35 | | std::size_t Extent |
36 | | > class span |
37 | | { |
38 | | public: |
39 | | using element_type = T; |
40 | | using value_type = typename std::remove_const<T>::type; |
41 | | using size_type = std::size_t; |
42 | | using difference_type = std::ptrdiff_t; |
43 | | using pointer = T*; |
44 | | using const_pointer = const T*; |
45 | | using reference = T&; |
46 | | using const_reference = const T&; |
47 | | using iterator = pointer; |
48 | | using const_iterator = const_pointer; |
49 | | using reverse_iterator = std::reverse_iterator<iterator>; |
50 | | using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
51 | | private: |
52 | | pointer data_{nullptr}; |
53 | | size_type size_{0}; |
54 | | public: |
55 | | static constexpr std::size_t extent = Extent; |
56 | | |
57 | | constexpr span() noexcept |
58 | 30.8k | { |
59 | 30.8k | } jsoncons::detail::span<char const, 18446744073709551615ul>::span() Line | Count | Source | 58 | 27.5k | { | 59 | 27.5k | } |
jsoncons::detail::span<unsigned long const, 18446744073709551615ul>::span() Line | Count | Source | 58 | 3.31k | { | 59 | 3.31k | } |
Unexecuted instantiation: jsoncons::detail::span<unsigned char, 18446744073709551615ul>::span() |
60 | | constexpr span(pointer data, size_type size) |
61 | 2.43M | : data_(data), size_(size) |
62 | 2.43M | { |
63 | 2.43M | } jsoncons::detail::span<unsigned char const, 18446744073709551615ul>::span(unsigned char const*, unsigned long) Line | Count | Source | 61 | 2.33M | : data_(data), size_(size) | 62 | 2.33M | { | 63 | 2.33M | } |
jsoncons::detail::span<char const, 18446744073709551615ul>::span(char const*, unsigned long) Line | Count | Source | 61 | 56.9k | : data_(data), size_(size) | 62 | 56.9k | { | 63 | 56.9k | } |
jsoncons::detail::span<unsigned short, 18446744073709551615ul>::span(unsigned short*, unsigned long) Line | Count | Source | 61 | 12.2k | : data_(data), size_(size) | 62 | 12.2k | { | 63 | 12.2k | } |
jsoncons::detail::span<unsigned int, 18446744073709551615ul>::span(unsigned int*, unsigned long) Line | Count | Source | 61 | 2.88k | : data_(data), size_(size) | 62 | 2.88k | { | 63 | 2.88k | } |
jsoncons::detail::span<unsigned long, 18446744073709551615ul>::span(unsigned long*, unsigned long) Line | Count | Source | 61 | 8.08k | : data_(data), size_(size) | 62 | 8.08k | { | 63 | 8.08k | } |
jsoncons::detail::span<signed char, 18446744073709551615ul>::span(signed char*, unsigned long) Line | Count | Source | 61 | 3.27k | : data_(data), size_(size) | 62 | 3.27k | { | 63 | 3.27k | } |
jsoncons::detail::span<short, 18446744073709551615ul>::span(short*, unsigned long) Line | Count | Source | 61 | 3.97k | : data_(data), size_(size) | 62 | 3.97k | { | 63 | 3.97k | } |
jsoncons::detail::span<int, 18446744073709551615ul>::span(int*, unsigned long) Line | Count | Source | 61 | 4.01k | : data_(data), size_(size) | 62 | 4.01k | { | 63 | 4.01k | } |
jsoncons::detail::span<long, 18446744073709551615ul>::span(long*, unsigned long) Line | Count | Source | 61 | 3.57k | : data_(data), size_(size) | 62 | 3.57k | { | 63 | 3.57k | } |
jsoncons::detail::span<float, 18446744073709551615ul>::span(float*, unsigned long) Line | Count | Source | 61 | 4.67k | : data_(data), size_(size) | 62 | 4.67k | { | 63 | 4.67k | } |
jsoncons::detail::span<double, 18446744073709551615ul>::span(double*, unsigned long) Line | Count | Source | 61 | 3.06k | : data_(data), size_(size) | 62 | 3.06k | { | 63 | 3.06k | } |
|
64 | | |
65 | | template <typename C> |
66 | | constexpr span(C& c, |
67 | | typename std::enable_if<!is_span<C>::value && !ext_traits::is_std_array<C>::value && ext_traits::is_compatible_element<C,element_type>::value && ext_traits::has_data_and_size<C>::value>::type* = 0) |
68 | 49.2k | : data_(c.data()), size_(c.size()) |
69 | 49.2k | { |
70 | 49.2k | } _ZN8jsoncons6detail4spanIhLm18446744073709551615EEC2INSt3__16vectorIhNS4_9allocatorIhEEEEEERT_PNS4_9enable_ifIXaaaaaantsr7is_spanIS9_EE5valuentsr10ext_traits12is_std_arrayIS9_EE5valuesr10ext_traits21is_compatible_elementIS9_hEE5valuesr10ext_traits17has_data_and_sizeIS9_EE5valueEvE4typeE Line | Count | Source | 68 | 49.0k | : data_(c.data()), size_(c.size()) | 69 | 49.0k | { | 70 | 49.0k | } |
_ZN8jsoncons6detail4spanIKmLm18446744073709551615EEC2INSt3__16vectorImNS5_9allocatorImEEEEEERT_PNS5_9enable_ifIXaaaaaantsr7is_spanISA_EE5valuentsr10ext_traits12is_std_arrayISA_EE5valuesr10ext_traits21is_compatible_elementISA_S2_EE5valuesr10ext_traits17has_data_and_sizeISA_EE5valueEvE4typeE Line | Count | Source | 68 | 218 | : data_(c.data()), size_(c.size()) | 69 | 218 | { | 70 | 218 | } |
|
71 | | |
72 | | template <std::size_t N> |
73 | | span(element_type (&arr)[N]) noexcept |
74 | | : data_(std::addressof(arr[0])), size_(N) |
75 | | { |
76 | | } |
77 | | |
78 | | template <std::size_t N> |
79 | | span(std::array<value_type, N>& arr, |
80 | | typename std::enable_if<(extent == dynamic_extent || extent == N)>::type* = 0) noexcept |
81 | | : data_(arr.data()), size_(arr.size()) |
82 | | { |
83 | | } |
84 | | |
85 | | template <std::size_t N> |
86 | | span(const std::array<value_type, N>& arr, |
87 | | typename std::enable_if<(extent == dynamic_extent || extent == N)>::type* = 0) noexcept |
88 | | : data_(arr.data()), size_(arr.size()) |
89 | | { |
90 | | } |
91 | | |
92 | | template <typename C> |
93 | | constexpr span(const C& c, |
94 | | typename std::enable_if<!is_span<C>::value && !ext_traits::is_std_array<C>::value && ext_traits::is_compatible_element<C,element_type>::value && ext_traits::has_data_and_size<C>::value>::type* = 0) |
95 | | : data_(c.data()), size_(c.size()) |
96 | | { |
97 | | } |
98 | | |
99 | | template <typename U, std::size_t N> |
100 | | constexpr span(const span<U, N>& s, |
101 | | typename std::enable_if<(N == dynamic_extent || N == extent) && std::is_convertible<U(*)[], element_type(*)[]>::value>::type* = 0) noexcept |
102 | | : data_(s.data()), size_(s.size()) |
103 | | { |
104 | | } |
105 | | |
106 | | constexpr span(const span& other) = default; |
107 | | |
108 | | span& operator=( const span& other ) = default; |
109 | | |
110 | | constexpr pointer data() const noexcept |
111 | 2.43M | { |
112 | 2.43M | return data_; |
113 | 2.43M | } jsoncons::detail::span<unsigned char const, 18446744073709551615ul>::data() const Line | Count | Source | 111 | 2.33M | { | 112 | 2.33M | return data_; | 113 | 2.33M | } |
jsoncons::detail::span<char const, 18446744073709551615ul>::data() const Line | Count | Source | 111 | 56.7k | { | 112 | 56.7k | return data_; | 113 | 56.7k | } |
Unexecuted instantiation: jsoncons::detail::span<unsigned short const, 18446744073709551615ul>::data() const Unexecuted instantiation: jsoncons::detail::span<unsigned int const, 18446744073709551615ul>::data() const Unexecuted instantiation: jsoncons::detail::span<unsigned long const, 18446744073709551615ul>::data() const Unexecuted instantiation: jsoncons::detail::span<signed char const, 18446744073709551615ul>::data() const Unexecuted instantiation: jsoncons::detail::span<short const, 18446744073709551615ul>::data() const Unexecuted instantiation: jsoncons::detail::span<int const, 18446744073709551615ul>::data() const Unexecuted instantiation: jsoncons::detail::span<long const, 18446744073709551615ul>::data() const Unexecuted instantiation: jsoncons::detail::span<float const, 18446744073709551615ul>::data() const Unexecuted instantiation: jsoncons::detail::span<double const, 18446744073709551615ul>::data() const jsoncons::detail::span<unsigned char, 18446744073709551615ul>::data() const Line | Count | Source | 111 | 49.0k | { | 112 | 49.0k | return data_; | 113 | 49.0k | } |
|
114 | | |
115 | | constexpr size_type size() const noexcept |
116 | 42.2M | { |
117 | 42.2M | return size_; |
118 | 42.2M | } jsoncons::detail::span<unsigned char const, 18446744073709551615ul>::size() const Line | Count | Source | 116 | 7.11M | { | 117 | 7.11M | return size_; | 118 | 7.11M | } |
Unexecuted instantiation: jsoncons::detail::span<unsigned short const, 18446744073709551615ul>::size() const Unexecuted instantiation: jsoncons::detail::span<unsigned int const, 18446744073709551615ul>::size() const jsoncons::detail::span<unsigned long const, 18446744073709551615ul>::size() const Line | Count | Source | 116 | 330 | { | 117 | 330 | return size_; | 118 | 330 | } |
Unexecuted instantiation: jsoncons::detail::span<signed char const, 18446744073709551615ul>::size() const Unexecuted instantiation: jsoncons::detail::span<short const, 18446744073709551615ul>::size() const Unexecuted instantiation: jsoncons::detail::span<int const, 18446744073709551615ul>::size() const Unexecuted instantiation: jsoncons::detail::span<long const, 18446744073709551615ul>::size() const Unexecuted instantiation: jsoncons::detail::span<float const, 18446744073709551615ul>::size() const Unexecuted instantiation: jsoncons::detail::span<double const, 18446744073709551615ul>::size() const jsoncons::detail::span<char const, 18446744073709551615ul>::size() const Line | Count | Source | 116 | 112k | { | 117 | 112k | return size_; | 118 | 112k | } |
jsoncons::detail::span<unsigned char, 18446744073709551615ul>::size() const Line | Count | Source | 116 | 49.0k | { | 117 | 49.0k | return size_; | 118 | 49.0k | } |
jsoncons::detail::span<unsigned short, 18446744073709551615ul>::size() const Line | Count | Source | 116 | 12.1M | { | 117 | 12.1M | return size_; | 118 | 12.1M | } |
jsoncons::detail::span<unsigned int, 18446744073709551615ul>::size() const Line | Count | Source | 116 | 8.77M | { | 117 | 8.77M | return size_; | 118 | 8.77M | } |
jsoncons::detail::span<unsigned long, 18446744073709551615ul>::size() const Line | Count | Source | 116 | 1.37M | { | 117 | 1.37M | return size_; | 118 | 1.37M | } |
jsoncons::detail::span<signed char, 18446744073709551615ul>::size() const Line | Count | Source | 116 | 624k | { | 117 | 624k | return size_; | 118 | 624k | } |
jsoncons::detail::span<short, 18446744073709551615ul>::size() const Line | Count | Source | 116 | 3.57M | { | 117 | 3.57M | return size_; | 118 | 3.57M | } |
jsoncons::detail::span<int, 18446744073709551615ul>::size() const Line | Count | Source | 116 | 4.99M | { | 117 | 4.99M | return size_; | 118 | 4.99M | } |
jsoncons::detail::span<long, 18446744073709551615ul>::size() const Line | Count | Source | 116 | 1.29M | { | 117 | 1.29M | return size_; | 118 | 1.29M | } |
jsoncons::detail::span<float, 18446744073709551615ul>::size() const Line | Count | Source | 116 | 1.47M | { | 117 | 1.47M | return size_; | 118 | 1.47M | } |
jsoncons::detail::span<double, 18446744073709551615ul>::size() const Line | Count | Source | 116 | 656k | { | 117 | 656k | return size_; | 118 | 656k | } |
|
119 | | |
120 | | constexpr bool empty() const noexcept |
121 | 203 | { |
122 | 203 | return size_ == 0; |
123 | 203 | } |
124 | | |
125 | | constexpr reference operator[](size_type index) const |
126 | 52.9M | { |
127 | 52.9M | return data_[index]; |
128 | 52.9M | } jsoncons::detail::span<unsigned long const, 18446744073709551615ul>::operator[](unsigned long) const Line | Count | Source | 126 | 801 | { | 127 | 801 | return data_[index]; | 128 | 801 | } |
jsoncons::detail::span<unsigned char const, 18446744073709551615ul>::operator[](unsigned long) const Line | Count | Source | 126 | 4.77M | { | 127 | 4.77M | return data_[index]; | 128 | 4.77M | } |
jsoncons::detail::span<unsigned short, 18446744073709551615ul>::operator[](unsigned long) const Line | Count | Source | 126 | 14.8M | { | 127 | 14.8M | return data_[index]; | 128 | 14.8M | } |
jsoncons::detail::span<unsigned int, 18446744073709551615ul>::operator[](unsigned long) const Line | Count | Source | 126 | 13.1M | { | 127 | 13.1M | return data_[index]; | 128 | 13.1M | } |
jsoncons::detail::span<unsigned long, 18446744073709551615ul>::operator[](unsigned long) const Line | Count | Source | 126 | 2.01M | { | 127 | 2.01M | return data_[index]; | 128 | 2.01M | } |
jsoncons::detail::span<signed char, 18446744073709551615ul>::operator[](unsigned long) const Line | Count | Source | 126 | 614k | { | 127 | 614k | return data_[index]; | 128 | 614k | } |
jsoncons::detail::span<short, 18446744073709551615ul>::operator[](unsigned long) const Line | Count | Source | 126 | 5.00M | { | 127 | 5.00M | return data_[index]; | 128 | 5.00M | } |
jsoncons::detail::span<int, 18446744073709551615ul>::operator[](unsigned long) const Line | Count | Source | 126 | 7.47M | { | 127 | 7.47M | return data_[index]; | 128 | 7.47M | } |
jsoncons::detail::span<long, 18446744073709551615ul>::operator[](unsigned long) const Line | Count | Source | 126 | 1.91M | { | 127 | 1.91M | return data_[index]; | 128 | 1.91M | } |
jsoncons::detail::span<float, 18446744073709551615ul>::operator[](unsigned long) const Line | Count | Source | 126 | 2.14M | { | 127 | 2.14M | return data_[index]; | 128 | 2.14M | } |
jsoncons::detail::span<double, 18446744073709551615ul>::operator[](unsigned long) const Line | Count | Source | 126 | 941k | { | 127 | 941k | return data_[index]; | 128 | 941k | } |
|
129 | | |
130 | | // iterator support |
131 | | const_iterator begin() const noexcept |
132 | 0 | { |
133 | 0 | return data_; |
134 | 0 | } Unexecuted instantiation: jsoncons::detail::span<unsigned char const, 18446744073709551615ul>::begin() const Unexecuted instantiation: jsoncons::detail::span<unsigned short const, 18446744073709551615ul>::begin() const Unexecuted instantiation: jsoncons::detail::span<unsigned int const, 18446744073709551615ul>::begin() const Unexecuted instantiation: jsoncons::detail::span<unsigned long const, 18446744073709551615ul>::begin() const Unexecuted instantiation: jsoncons::detail::span<signed char const, 18446744073709551615ul>::begin() const Unexecuted instantiation: jsoncons::detail::span<short const, 18446744073709551615ul>::begin() const Unexecuted instantiation: jsoncons::detail::span<int const, 18446744073709551615ul>::begin() const Unexecuted instantiation: jsoncons::detail::span<long const, 18446744073709551615ul>::begin() const Unexecuted instantiation: jsoncons::detail::span<float const, 18446744073709551615ul>::begin() const Unexecuted instantiation: jsoncons::detail::span<double const, 18446744073709551615ul>::begin() const |
135 | | const_iterator end() const noexcept |
136 | 0 | { |
137 | 0 | return data_ + size_; |
138 | 0 | } Unexecuted instantiation: jsoncons::detail::span<unsigned char const, 18446744073709551615ul>::end() const Unexecuted instantiation: jsoncons::detail::span<unsigned short const, 18446744073709551615ul>::end() const Unexecuted instantiation: jsoncons::detail::span<unsigned int const, 18446744073709551615ul>::end() const Unexecuted instantiation: jsoncons::detail::span<unsigned long const, 18446744073709551615ul>::end() const Unexecuted instantiation: jsoncons::detail::span<signed char const, 18446744073709551615ul>::end() const Unexecuted instantiation: jsoncons::detail::span<short const, 18446744073709551615ul>::end() const Unexecuted instantiation: jsoncons::detail::span<int const, 18446744073709551615ul>::end() const Unexecuted instantiation: jsoncons::detail::span<long const, 18446744073709551615ul>::end() const Unexecuted instantiation: jsoncons::detail::span<float const, 18446744073709551615ul>::end() const Unexecuted instantiation: jsoncons::detail::span<double const, 18446744073709551615ul>::end() const |
139 | | const_iterator cbegin() const noexcept |
140 | | { |
141 | | return data_; |
142 | | } |
143 | | const_iterator cend() const noexcept |
144 | | { |
145 | | return data_ + size_; |
146 | | } |
147 | | const_reverse_iterator rbegin() const noexcept |
148 | | { |
149 | | return const_reverse_iterator(end()); |
150 | | } |
151 | | const_reverse_iterator rend() const noexcept |
152 | | { |
153 | | return const_reverse_iterator(begin()); |
154 | | } |
155 | | const_reverse_iterator crbegin() const noexcept |
156 | | { |
157 | | return const_reverse_iterator(end()); |
158 | | } |
159 | | const_reverse_iterator crend() const noexcept |
160 | | { |
161 | | return const_reverse_iterator(begin()); |
162 | | } |
163 | | |
164 | | span<element_type, dynamic_extent> |
165 | | first(std::size_t count) const |
166 | | { |
167 | | JSONCONS_ASSERT(count <= size()); |
168 | | |
169 | | return span< element_type, dynamic_extent >( data(), count ); |
170 | | } |
171 | | |
172 | | span<element_type, dynamic_extent> |
173 | | last(std::size_t count) const |
174 | | { |
175 | | JSONCONS_ASSERT(count <= size()); |
176 | | |
177 | | return span<element_type, dynamic_extent>(data() + ( size() - count ), count); |
178 | | } |
179 | | |
180 | | span<element_type, dynamic_extent> |
181 | | subspan(std::size_t offset, std::size_t count = dynamic_extent) const |
182 | | { |
183 | | //JSONCONS_ASSERT((offset <= size() && (count == dynamic_extent || (offset + count <= size())))); |
184 | | |
185 | | return span<element_type, dynamic_extent>( |
186 | | data() + offset, count == dynamic_extent ? size() - offset : count ); |
187 | | } |
188 | | }; |
189 | | |
190 | | } // namespace detail |
191 | | } // namespace jsoncons |
192 | | |
193 | | #endif // JSONCONS_DETAIL_SPAN_HPP |