Skip to content

Commit 0004d5f

Browse files
committed
typed_array_element_type -> typed_array_values
1 parent 7c30af8 commit 0004d5f

9 files changed

Lines changed: 252 additions & 63 deletions

File tree

doc/ref/corelib/basic_json_visitor.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ Member type |Definition
178178
semantic_tag tag = semantic_tag::none,
179179
const ser_context& context = ser_context()); (35)
180180
181-
bool begin_multi_dim(const span<const size_t>& shape,
181+
bool begin_multi_dim(const span<const size_t>& extents,
182182
semantic_tag tag,
183183
const ser_context& context); (36)
184184

include/jsoncons/detail/mdspan.hpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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_MDSPAN_HPP
8+
#define JSONCONS_DETAIL_MDSPAN_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+
template< class IndexType, std::size_t Rank >
23+
class dextents
24+
{
25+
public:
26+
using index_type = IndexType;
27+
std::size_t rank = Rank;
28+
};
29+
30+
struct layout_left
31+
{
32+
};
33+
34+
struct layout_right
35+
{
36+
};
37+
38+
template< typename T, typename Extents, typename LayoutPolicy = layout_right>
39+
class mdspan
40+
{
41+
};
42+
43+
} // namespace detail
44+
} // namespace jsoncons
45+
46+
#endif // JSONCONS_DETAIL_MDSPAN_HPP

include/jsoncons/json_visitor.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -714,14 +714,14 @@ namespace jsoncons {
714714
JSONCONS_VISITOR_RETURN;
715715
}
716716

717-
virtual JSONCONS_VISITOR_RETURN_TYPE visit_begin_multi_dim(const jsoncons::span<const size_t>& shape,
717+
virtual JSONCONS_VISITOR_RETURN_TYPE visit_begin_multi_dim(const jsoncons::span<const size_t>& extents,
718718
semantic_tag tag,
719719
const ser_context& context,
720720
std::error_code& ec)
721721
{
722722
visit_begin_array(2, tag, context, ec);
723-
visit_begin_array(shape.size(), tag, context, ec);
724-
for (auto it = shape.begin(); it != shape.end(); ++it)
723+
visit_begin_array(extents.size(), tag, context, ec);
724+
for (auto it = extents.begin(); it != extents.end(); ++it)
725725
{
726726
visit_uint64(*it, semantic_tag::none, context, ec);
727727
}

include/jsoncons/reflect/decode_traits.hpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ namespace reflect {
3838

3939
template <typename T>
4040
typename std::enable_if<ext_traits::is_back_insertable<T>::value,void>::type
41-
read_typed_array(jsoncons::span<uint8_t> typed_array, typed_array_element_type element_type, T& v)
41+
read_typed_array(jsoncons::span<uint8_t> typed_array, typed_array_values element_type, T& v)
4242
{
4343
using value_type = typename T::value_type;
4444

4545
switch (element_type)
4646
{
47-
case typed_array_element_type::uint8:
47+
case typed_array_values::uint8_value:
4848
{
4949
auto ta = typed_array_cast<const uint8_t>(typed_array);
5050
for (auto item : ta)
@@ -53,7 +53,7 @@ read_typed_array(jsoncons::span<uint8_t> typed_array, typed_array_element_type e
5353
}
5454
break;
5555
}
56-
case typed_array_element_type::uint16:
56+
case typed_array_values::uint16_value:
5757
{
5858
auto ta = typed_array_cast<const uint16_t>(typed_array);
5959
for (auto item : ta)
@@ -62,7 +62,7 @@ read_typed_array(jsoncons::span<uint8_t> typed_array, typed_array_element_type e
6262
}
6363
break;
6464
}
65-
case typed_array_element_type::uint32:
65+
case typed_array_values::uint32_value:
6666
{
6767
auto ta = typed_array_cast<const uint32_t>(typed_array);
6868
for (auto item : ta)
@@ -71,7 +71,7 @@ read_typed_array(jsoncons::span<uint8_t> typed_array, typed_array_element_type e
7171
}
7272
break;
7373
}
74-
case typed_array_element_type::uint64:
74+
case typed_array_values::uint64_value:
7575
{
7676
auto ta = typed_array_cast<const uint64_t>(typed_array);
7777
for (auto item : ta)
@@ -80,7 +80,7 @@ read_typed_array(jsoncons::span<uint8_t> typed_array, typed_array_element_type e
8080
}
8181
break;
8282
}
83-
case typed_array_element_type::int8:
83+
case typed_array_values::int8_value:
8484
{
8585
auto ta = typed_array_cast<const int8_t>(typed_array);
8686
for (auto item : ta)
@@ -89,7 +89,7 @@ read_typed_array(jsoncons::span<uint8_t> typed_array, typed_array_element_type e
8989
}
9090
break;
9191
}
92-
case typed_array_element_type::int16:
92+
case typed_array_values::int16_value:
9393
{
9494
auto ta = typed_array_cast<const int16_t>(typed_array);
9595
for (auto item : ta)
@@ -98,7 +98,7 @@ read_typed_array(jsoncons::span<uint8_t> typed_array, typed_array_element_type e
9898
}
9999
break;
100100
}
101-
case typed_array_element_type::int32:
101+
case typed_array_values::int32_value:
102102
{
103103
auto ta = typed_array_cast<const int32_t>(typed_array);
104104
for (auto item : ta)
@@ -107,7 +107,7 @@ read_typed_array(jsoncons::span<uint8_t> typed_array, typed_array_element_type e
107107
}
108108
break;
109109
}
110-
case typed_array_element_type::int64:
110+
case typed_array_values::int64_value:
111111
{
112112
auto ta = typed_array_cast<const int64_t>(typed_array);
113113
for (auto item : ta)
@@ -116,7 +116,7 @@ read_typed_array(jsoncons::span<uint8_t> typed_array, typed_array_element_type e
116116
}
117117
break;
118118
}
119-
case typed_array_element_type::half_float:
119+
case typed_array_values::half_value:
120120
{
121121
auto ta = typed_array_cast<const int16_t>(typed_array);
122122
for (auto item : ta)
@@ -125,7 +125,7 @@ read_typed_array(jsoncons::span<uint8_t> typed_array, typed_array_element_type e
125125
}
126126
break;
127127
}
128-
case typed_array_element_type::float32:
128+
case typed_array_values::float_value:
129129
{
130130
auto ta = typed_array_cast<const float>(typed_array);
131131
for (auto item : ta)
@@ -134,7 +134,7 @@ read_typed_array(jsoncons::span<uint8_t> typed_array, typed_array_element_type e
134134
}
135135
break;
136136
}
137-
case typed_array_element_type::float64:
137+
case typed_array_values::double_value:
138138
{
139139
auto ta = typed_array_cast<const double>(typed_array);
140140
for (auto item : ta)
@@ -424,7 +424,7 @@ struct decode_traits<T,
424424
if (cursor.is_typed_array())
425425
{
426426
read_typed_array(cursor.typed_array(), cursor.element_type(), v);
427-
cursor.clear_typed_array();
427+
cursor.to_end_array();
428428
}
429429
else
430430
{
@@ -493,7 +493,7 @@ struct decode_traits<T,
493493
if (cursor.is_typed_array())
494494
{
495495
read_typed_array(cursor.typed_array(), cursor.element_type(), v);
496-
cursor.clear_typed_array();
496+
cursor.to_end_array();
497497
}
498498
else
499499
{

include/jsoncons/staj_cursor.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,17 +310,17 @@ class basic_staj_cursor
310310
return false;
311311
}
312312

313-
virtual typed_array_element_type element_type() const
313+
virtual typed_array_values element_type() const
314314
{
315-
return typed_array_element_type{};
315+
return typed_array_values{};
316316
}
317317

318318
virtual jsoncons::span<uint8_t> typed_array()
319319
{
320320
return jsoncons::span<uint8_t>{};
321321
}
322322

323-
virtual void clear_typed_array()
323+
virtual void to_end_array()
324324
{
325325
}
326326
};

include/jsoncons/typed_array.hpp

Lines changed: 127 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,19 @@
2424

2525
namespace jsoncons {
2626

27-
enum class typed_array_element_type
27+
enum class typed_array_values
2828
{
29-
uint8=1,uint16,uint32,uint64,
30-
int8,int16,int32,int64,
31-
half_float, float32, float64
29+
uint8_value = 1,
30+
uint16_value = 2,
31+
uint32_value = 4,
32+
uint64_value = 8,
33+
int8_value = 16,
34+
int16_value = 32,
35+
int32_value = 64,
36+
int64_value = 128,
37+
half_value = 256,
38+
float_value = 512,
39+
double_value = 1024
3240
};
3341

3442
template <typename T>
@@ -154,6 +162,121 @@ jsoncons::span<const double> typed_array_cast<const double>(jsoncons::span<uint8
154162
return jsoncons::span<const double>{reinterpret_cast<const double*>(bytes.data()), bytes.size()/sizeof(double)};
155163
}
156164

165+
class slice
166+
{
167+
static constexpr size_t npos = size_t(-1);
168+
169+
size_t start_;
170+
size_t stop_;
171+
size_t step_;
172+
public:
173+
constexpr explicit slice(size_t start = npos, size_t stop = npos, size_t step=1)
174+
: start_(start), stop_(stop), step_(step)
175+
{
176+
}
177+
178+
slice(const slice& other) = default;
179+
slice(slice&& other) = default;
180+
181+
slice& operator=(const slice& other) = default;
182+
slice& operator=(slice&& other) = default;
183+
184+
size_t start(size_t origin) const
185+
{
186+
return start_ == npos ? origin : start_;
187+
}
188+
189+
size_t stop(size_t origin, size_t n) const
190+
{
191+
return stop_ == npos ? (origin+n) : stop_;
192+
}
193+
size_t step() const
194+
{
195+
return step_;
196+
}
197+
198+
size_t length(size_t origin, size_t n) const
199+
{
200+
size_t y = stop(origin, n);
201+
size_t x = start(origin);
202+
203+
assert(y >= x);
204+
size_t w = y - x;
205+
return w/step_ + (w % step_ != 0);
206+
}
207+
};
208+
209+
#if 0
210+
struct row_major
211+
{
212+
static void calculate_strides(jsoncons::span<const std::size_t> extents,
213+
std::vector<std::size_t>& strides, size_t& size)
214+
{
215+
size = 1;
216+
const size_t num_extents = extents.size();
217+
for (size_t i = 0; i < num_extents; ++i)
218+
{
219+
strides[num_extents-i-1] = size;
220+
size *= extents[num_extents-i-1];
221+
}
222+
}
223+
/*
224+
template <size_t N>
225+
static void update_offsets(size_t rel, indices_t<N>& offsets)
226+
{
227+
offsets[N-1] += rel;
228+
}
229+
*/
230+
template <size_t N>
231+
static std::vector<std::size_t> calculate_offsets(size_t rel,
232+
jsoncons::span<const std::size_t> strides,
233+
jsoncons::span<const std::size_t> slices)
234+
{
235+
indices_t<N> offsets;
236+
offsets[N-1] = rel + slices[N-1].start(Base::origin()) * strides[N-1];
237+
for (size_t i = 0; i+1 < N; ++i)
238+
{
239+
offsets[i] = slices[i].start(Base::origin()) * strides[i];
240+
}
241+
return offsets;
242+
}
243+
};
244+
245+
struct column_major
246+
{
247+
template <size_t N>
248+
static void calculate_strides(const extents_t<N>& extents, indices_t<N>& strides, size_t& size)
249+
{
250+
size = 1;
251+
for (size_t i = 0; i < N; ++i)
252+
{
253+
strides[i] = size;
254+
size *= extents[i];
255+
}
256+
}
257+
/*
258+
template <size_t N>
259+
static void update_offsets(size_t rel, indices_t<N>& offsets)
260+
{
261+
offsets[0] += rel;
262+
}
263+
*/
264+
template <size_t N, typename Base>
265+
static indices_t<N> calculate_offsets(size_t rel,
266+
const indices_t<N>& strides,
267+
const std::array<slice,N>& slices)
268+
{
269+
indices_t<N> offsets;
270+
offsets[0] = rel + Base::rebase_to_zero(slices[0].start(Base::origin())) * strides[0];
271+
for (size_t i = 1; i < N; ++i)
272+
{
273+
offsets[i] = Base::rebase_to_zero(slices[i].start(Base::origin())) * strides[i];
274+
}
275+
return offsets;
276+
}
277+
};
278+
#endif
279+
157280
} // namespace jsoncons
158281

159282
#endif // JSONCONS_TYPED_ARRAY_HPP

include/jsoncons_ext/cbor/cbor_cursor.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class basic_cbor_cursor : public basic_staj_cursor<char>, private virtual ser_co
168168
return parser_.is_typed_array();
169169
}
170170

171-
typed_array_element_type element_type() const final
171+
typed_array_values element_type() const final
172172
{
173173
return parser_.element_type();
174174
}
@@ -178,9 +178,9 @@ class basic_cbor_cursor : public basic_staj_cursor<char>, private virtual ser_co
178178
return parser_.typed_array();
179179
}
180180

181-
void clear_typed_array() final
181+
void to_end_array() final
182182
{
183-
parser_.clear_typed_array();
183+
parser_.to_end_array();
184184
}
185185

186186
const staj_event& current() const override

0 commit comments

Comments
 (0)