Awkward Array: include/awkward/layoutbuilder/LayoutBuilder.h Source File

Loading...
Searching...
No Matches
LayoutBuilder.h
Go to the documentation of this file.
1// BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE
2
3#ifndef AWKWARD_LayoutBuilder_H_
4#define AWKWARD_LayoutBuilder_H_
5
6#include "awkward/common.h"
7#include "awkward/util.h"
8#include "awkward/forth/ForthMachine.h"
9#include "awkward/layoutbuilder/FormBuilder.h"
10
11#include <complex>
12
13namespace awkward {
14
15 using ForthOutputBufferMap = std::map<std::string, std::shared_ptr<ForthOutputBuffer>>;
16
17 const std::string
18 index_form_to_name(const std::string& form_index);
19
20 const std::string
21 index_form_to_vm_format(const std::string& form_index);
22
23 enum class state : std::int32_t {
24 int64 = 0,
25 float64 = 1,
26 begin_list = 2,
27 end_list = 3,
28 boolean = 4,
29 int8 = 5,
30 int16 = 6,
31 int32 = 7,
32 uint8 = 8,
33 uint16 = 9,
34 uint32 = 10,
35 uint64 = 11,
36 float16 = 12,
37 float32 = 13,
38 float128 = 14,
39 complex64 = 15,
40 complex128 = 16,
41 complex256 = 17,
42 null = 18,
43 index = 19,
44 tag = 20,
45 datetime64 = 21,
46 timedelta64 = 22
47 };
48 using utype = std::underlying_type<state>::type;
49
50 const std::string
51 primitive_to_state(const std::string& name);
52
53 const std::string
54 primitive_to_vm_format(const std::string& name);
55
61 template <typename T, typename I>
62 class LIBAWKWARD_EXPORT_SYMBOL LayoutBuilder {
63 public:
71 LayoutBuilder(const std::string& json_form,
72 const int64_t initial,
73 bool vm_init = true);
74
75 const std::string&
76 json_form() const {
77 return json_form_;
78 }
79
82 const std::string
83 to_buffers(BuffersContainer& container) const;
84
86 void
87 connect(const std::shared_ptr<ForthMachineOf<T, I>>& vm);
88
90 void
91 debug_step() const;
92
95 const std::string
96 vm_source() const;
97
99 const std::shared_ptr<ForthMachineOf<T, I>>
100 vm() const;
101
103 int64_t
104 length() const;
105
107 void
108 pre_snapshot() const;
109
111 void
112 null();
113
115 void
116 boolean(bool x);
117
119 void
120 int64(int64_t x);
121
123 void
124 float64(double x);
125
127 void
128 complex(std::complex<double> x);
129
132 void
133 bytestring(const char* x);
134
139 void
140 bytestring(const char* x, int64_t length);
141
144 void
145 bytestring(const std::string& x);
146
149 void
150 string(const char* x);
151
156 void
157 string(const char* x, int64_t length);
158
161 void
162 string(const std::string& x);
163
170 void
171 begin_list();
172
174 void
175 add_begin_list();
176
178 void
179 end_list();
180
182 void
183 add_end_list();
184
187 void
188 tag(int8_t tag);
189
195 void
196 index(int64_t x);
197
200 template <typename D>
201 bool
202 find_index_of(D x, const std::string& vm_output_data) {
203 auto const& outputs = vm_.get()->outputs();
204 auto search = outputs.find(vm_output_data);
205 if (search != outputs.end()) {
206 auto data = std::static_pointer_cast<D>(search->second.get()->ptr());
207 auto size = search->second.get()->len();
208 for (int64_t i = 0; i < size; i++) {
209 if (data.get()[i] == x) {
210 index(i);
211 return true;
212 }
213 }
214 }
215 return false;
216 }
217
219 void
220 add_bool(bool x);
221
223 void
224 add_int64(int64_t x);
225
227 void
228 add_double(double x);
229
231 void
232 add_complex(std::complex<double> x);
233
235 void
236 add_string(const std::string& x);
237
239 static int64_t
240 next_id();
241
243 static int64_t
244 next_error_id();
245
247 void
248 resume() const;
249
250 // @brief Root node of the FormBuilder tree.
251 const FormBuilderPtr<T, I> builder() const { return builder_; }
252
253 protected:
256 static int64_t
257 next_node_id;
258
260 static int64_t
261 error_id;
262
263 private:
265 FormBuilderPtr<T, I>
266 form_builder_from_json(const std::string& json_form);
267
269 template <typename JSON>
270 FormBuilderPtr<T, I>
271 from_json(const JSON& json_doc);
272
274 void
275 initialise_builder(const std::string& json_form);
276
278 void
279 initialise();
280
282 template <typename D>
283 void
284 set_data(D x);
285
287 const std::string json_form_;
288
290 int64_t initial_;
291
293 FormBuilderPtr<T, I> builder_;
294
296 std::shared_ptr<ForthMachineOf<T, I>> vm_;
297
299 std::map<std::string, std::shared_ptr<ForthInputBuffer>> vm_inputs_map_;
300
302 std::string vm_input_data_;
303
305 std::string vm_source_;
306
308 std::set<util::ForthError> ignore_;
309
311 ForthOutputBufferMap vm_outputs_map_;
312
313 };
314
315 using LayoutBuilder32 = LayoutBuilder<int32_t, int32_t>;
316 using LayoutBuilder64 = LayoutBuilder<int64_t, int32_t>;
317
318}
319
320#endif // AWKWARD_LayoutBuilder_H_
FormBuilder.h
ForthMachine.h
awkward::BuffersContainer
Abstract class to represent the output of ak.to_buffers. In Python, this would be a dict of NumPy arr...
Definition: Builder.h:20
awkward::ForthMachineOf
Definition: ForthMachine.h:25
awkward::LayoutBuilder
User interface to the FormBuilder system: the LayoutBuilder is a fixed reference while the FormBuilde...
Definition: LayoutBuilder.h:62
awkward::LayoutBuilder::vm_source
const std::string vm_source() const
Returns an AwkwardForth source code generated from the 'Form' and passed to the 'ForthMachine' virtua...
awkward::LayoutBuilder::begin_list
void begin_list()
Begins building a nested list.
awkward::LayoutBuilder::pre_snapshot
void pre_snapshot() const
awkward::LayoutBuilder::boolean
void boolean(bool x)
Adds a boolean value x to the accumulated data.
awkward::LayoutBuilder::index
void index(int64_t x)
Issues an 'index' vm command. The value 'x' is pushed to the VM stack, it is not added to the accumul...
awkward::LayoutBuilder::complex
void complex(std::complex< double > x)
Adds a complex value x to the accumulated data.
awkward::LayoutBuilder::vm
const std::shared_ptr< ForthMachineOf< T, I > > vm() const
awkward::LayoutBuilder::bytestring
void bytestring(const std::string &x)
Adds an unencoded bytestring x in STL format to the accumulated data.
awkward::LayoutBuilder::json_form
const std::string & json_form() const
Definition: LayoutBuilder.h:76
awkward::LayoutBuilder::float64
void float64(double x)
Adds a real value x to the accumulated data.
awkward::LayoutBuilder::builder
const FormBuilderPtr< T, I > builder() const
Definition: LayoutBuilder.h:251
awkward::LayoutBuilder::next_id
static int64_t next_id()
Generates next unique ID.
awkward::LayoutBuilder::LayoutBuilder
LayoutBuilder(const std::string &json_form, const int64_t initial, bool vm_init=true)
Creates an LayoutBuilder from a full set of parameters.
awkward::LayoutBuilder::connect
void connect(const std::shared_ptr< ForthMachineOf< T, I > > &vm)
Connects a Virtual Machine if it was not initialized before.
awkward::LayoutBuilder::tag
void tag(int8_t tag)
Sets the pointer to a given tag tag; the next command will fill that slot.
awkward::LayoutBuilder::add_bool
void add_bool(bool x)
Adds a boolean value x to the accumulated data.
awkward::LayoutBuilder::error_id
static int64_t error_id
An error ID to be used to generate a user 'halt' message.
Definition: LayoutBuilder.h:261
awkward::LayoutBuilder::string
void string(const char *x)
Adds a UTF-8 encoded, null-terminated bytestring value x to the accumulated data.
awkward::LayoutBuilder::add_double
void add_double(double x)
Adds a double value x to the accumulated data.
awkward::LayoutBuilder::add_end_list
void add_end_list()
Ends a nested list.
awkward::LayoutBuilder::end_list
void end_list()
Ends a nested list.
awkward::LayoutBuilder::resume
void resume() const
Resume Virtual machine run.
awkward::LayoutBuilder::add_complex
void add_complex(std::complex< double > x)
Adds a complex value x to the accumulated data.
awkward::LayoutBuilder::add_begin_list
void add_begin_list()
Begins building a nested list.
awkward::LayoutBuilder::next_error_id
static int64_t next_error_id()
Generates a user-defined error ID.
awkward::LayoutBuilder::next_node_id
static int64_t next_node_id
A unique ID to use when Form nodes do not have Form key defined.
Definition: LayoutBuilder.h:257
awkward::LayoutBuilder::find_index_of
bool find_index_of(D x, const std::string &vm_output_data)
Finds an index of a data in a VM output buffer. This is used to build a 'categorical' array.
Definition: LayoutBuilder.h:202
awkward::LayoutBuilder::int64
void int64(int64_t x)
Adds an integer value x to the accumulated data.
awkward::LayoutBuilder::null
void null()
Adds a null value to the accumulated data.
awkward::LayoutBuilder::add_string
void add_string(const std::string &x)
Adds a string value x to the accumulated data.
awkward::LayoutBuilder::string
void string(const std::string &x)
Adds a UTF-8 encoded bytestring x in STL format to the accumulated data.
awkward::LayoutBuilder::add_int64
void add_int64(int64_t x)
Adds an int64_t value x to the accumulated data.
awkward::LayoutBuilder::string
void string(const char *x, int64_t length)
Adds a UTF-8 encoded bytestring value x with a given length to the accumulated data.
awkward::LayoutBuilder::debug_step
void debug_step() const
Prints debug information from the Virtual Machine stack.
awkward::LayoutBuilder::length
int64_t length() const
Current length of the accumulated array.
awkward::LayoutBuilder::bytestring
void bytestring(const char *x)
Adds an unencoded, null-terminated bytestring value x to the accumulated data.
awkward::LayoutBuilder::to_buffers
const std::string to_buffers(BuffersContainer &container) const
Copy the current snapshot into the BuffersContainer and return a Form as a std::string (JSON).
awkward::LayoutBuilder::bytestring
void bytestring(const char *x, int64_t length)
Adds an unencoded bytestring value x with a given length to the accumulated data.
common.h
LIBAWKWARD_EXPORT_SYMBOL
#define LIBAWKWARD_EXPORT_SYMBOL
Definition: common.h:45
awkward
Definition: BitMaskedArray.h:15
awkward::primitive_to_state
const std::string primitive_to_state(const std::string &name)
awkward::index_form_to_vm_format
const std::string index_form_to_vm_format(const std::string &form_index)
awkward::ForthOutputBufferMap
std::map< std::string, std::shared_ptr< ForthOutputBuffer > > ForthOutputBufferMap
Definition: FormBuilder.h:16
awkward::primitive_to_vm_format
const std::string primitive_to_vm_format(const std::string &name)
awkward::index_form_to_name
const std::string index_form_to_name(const std::string &form_index)
awkward::state
state
Definition: LayoutBuilder.h:23
awkward::state::int32
@ int32
awkward::state::float16
@ float16
awkward::state::int8
@ int8
awkward::state::uint64
@ uint64
awkward::state::null
@ null
awkward::state::uint32
@ uint32
awkward::state::float128
@ float128
awkward::state::complex256
@ complex256
awkward::state::uint8
@ uint8
awkward::state::index
@ index
awkward::state::complex128
@ complex128
awkward::state::complex64
@ complex64
awkward::state::end_list
@ end_list
awkward::state::uint16
@ uint16
awkward::state::begin_list
@ begin_list
awkward::state::datetime64
@ datetime64
awkward::state::timedelta64
@ timedelta64
awkward::state::int16
@ int16
awkward::state::float32
@ float32
awkward::state::tag
@ tag
awkward::state::float64
@ float64
awkward::state::int64
@ int64
awkward::FormBuilderPtr
std::shared_ptr< FormBuilder< T, I > > FormBuilderPtr
Definition: FormBuilder.h:177
awkward::utype
std::underlying_type< state >::type utype
Definition: LayoutBuilder.h:48
util.h