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"
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>
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
109
111 void
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
172
174 void
176
178 void
180
182 void
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
241
243 static int64_t
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
258
260 static int64_t
262
263 private:
266 form_builder_from_json(const std::string& json_form);
267
269 template <typename JSON>
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
317
318}
319
320#endif // AWKWARD_LayoutBuilder_H_
Abstract class to represent the output of ak.to_buffers. In Python, this would be a dict of NumPy arr...
Definition: Builder.h:20
Definition: ForthMachine.h:25
User interface to the FormBuilder system: the LayoutBuilder is a fixed reference while the FormBuilde...
Definition: LayoutBuilder.h:62
const std::string vm_source() const
Returns an AwkwardForth source code generated from the 'Form' and passed to the 'ForthMachine' virtua...
void begin_list()
Begins building a nested list.
void pre_snapshot() const
void boolean(bool x)
Adds a boolean value x to the accumulated data.
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...
void complex(std::complex< double > x)
Adds a complex value x to the accumulated data.
const std::shared_ptr< ForthMachineOf< T, I > > vm() const
void bytestring(const std::string &x)
Adds an unencoded bytestring x in STL format to the accumulated data.
const std::string & json_form() const
Definition: LayoutBuilder.h:76
void float64(double x)
Adds a real value x to the accumulated data.
const FormBuilderPtr< T, I > builder() const
Definition: LayoutBuilder.h:251
static int64_t next_id()
Generates next unique ID.
LayoutBuilder(const std::string &json_form, const int64_t initial, bool vm_init=true)
Creates an LayoutBuilder from a full set of parameters.
void connect(const std::shared_ptr< ForthMachineOf< T, I > > &vm)
Connects a Virtual Machine if it was not initialized before.
void tag(int8_t tag)
Sets the pointer to a given tag tag; the next command will fill that slot.
void add_bool(bool x)
Adds a boolean value x to the accumulated data.
static int64_t error_id
An error ID to be used to generate a user 'halt' message.
Definition: LayoutBuilder.h:261
void string(const char *x)
Adds a UTF-8 encoded, null-terminated bytestring value x to the accumulated data.
void add_double(double x)
Adds a double value x to the accumulated data.
void add_end_list()
Ends a nested list.
void end_list()
Ends a nested list.
void resume() const
Resume Virtual machine run.
void add_complex(std::complex< double > x)
Adds a complex value x to the accumulated data.
void add_begin_list()
Begins building a nested list.
static int64_t next_error_id()
Generates a user-defined error 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
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
void int64(int64_t x)
Adds an integer value x to the accumulated data.
void null()
Adds a null value to the accumulated data.
void add_string(const std::string &x)
Adds a string value x to the accumulated data.
void string(const std::string &x)
Adds a UTF-8 encoded bytestring x in STL format to the accumulated data.
void add_int64(int64_t x)
Adds an int64_t value x to the accumulated data.
void string(const char *x, int64_t length)
Adds a UTF-8 encoded bytestring value x with a given length to the accumulated data.
void debug_step() const
Prints debug information from the Virtual Machine stack.
int64_t length() const
Current length of the accumulated array.
void bytestring(const char *x)
Adds an unencoded, null-terminated bytestring value x to the accumulated data.
const std::string to_buffers(BuffersContainer &container) const
Copy the current snapshot into the BuffersContainer and return a Form as a std::string (JSON).
void bytestring(const char *x, int64_t length)
Adds an unencoded bytestring value x with a given length to the accumulated data.
#define LIBAWKWARD_EXPORT_SYMBOL
Definition: common.h:45
Definition: BitMaskedArray.h:15
const std::string primitive_to_state(const std::string &name)
const std::string index_form_to_vm_format(const std::string &form_index)
std::map< std::string, std::shared_ptr< ForthOutputBuffer > > ForthOutputBufferMap
Definition: FormBuilder.h:16
const std::string primitive_to_vm_format(const std::string &name)
const std::string index_form_to_name(const std::string &form_index)
state
Definition: LayoutBuilder.h:23
std::shared_ptr< FormBuilder< T, I > > FormBuilderPtr
Definition: FormBuilder.h:177
std::underlying_type< state >::type utype
Definition: LayoutBuilder.h:48