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 
13 namespace awkward {
14  class ArrayBuilderOptions;
15 
16  using ForthOutputBufferMap = std::map<std::string, std::shared_ptr<ForthOutputBuffer>>;
17 
18  const std::string
19  index_form_to_name(const std::string& form_index);
20 
21  const std::string
22  index_form_to_vm_format(const std::string& form_index);
23 
24  enum class state : std::int32_t {
25  int64 = 0,
26  float64 = 1,
27  begin_list = 2,
28  end_list = 3,
29  boolean = 4,
30  int8 = 5,
31  int16 = 6,
32  int32 = 7,
33  uint8 = 8,
34  uint16 = 9,
35  uint32 = 10,
36  uint64 = 11,
37  float16 = 12,
38  float32 = 13,
39  float128 = 14,
40  complex64 = 15,
41  complex128 = 16,
42  complex256 = 17,
43  null = 18,
44  index = 19,
45  tag = 20,
46  datetime64 = 21,
47  timedelta64 = 22
48  };
49  using utype = std::underlying_type<state>::type;
50 
51  const std::string
52  primitive_to_state(const std::string& name);
53 
54  const std::string
55  primitive_to_vm_format(const std::string& name);
56 
62  template <typename T, typename I>
64  public:
72  LayoutBuilder(const std::string& json_form,
73  const ArrayBuilderOptions& options,
74  bool vm_init = true);
75 
77  void
78  connect(const std::shared_ptr<ForthMachineOf<T, I>>& vm);
79 
81  void
82  debug_step() const;
83 
86  const std::string
87  vm_source() const;
88 
90  const std::shared_ptr<ForthMachineOf<T, I>>
91  vm() const;
92 
94  int64_t
95  length() const;
96 
98  void
99  pre_snapshot() const;
100 
102  void
103  null();
104 
106  void
107  boolean(bool x);
108 
110  void
111  int64(int64_t x);
112 
114  void
115  float64(double x);
116 
118  void
119  complex(std::complex<double> x);
120 
123  void
124  bytestring(const char* x);
125 
130  void
131  bytestring(const char* x, int64_t length);
132 
135  void
136  bytestring(const std::string& x);
137 
140  void
141  string(const char* x);
142 
147  void
148  string(const char* x, int64_t length);
149 
152  void
153  string(const std::string& x);
154 
161  void
162  begin_list();
163 
165  void
166  add_begin_list();
167 
169  void
170  end_list();
171 
173  void
174  add_end_list();
175 
178  void
179  tag(int8_t tag);
180 
186  void
187  index(int64_t x);
188 
191  template <typename D>
192  bool
193  find_index_of(D x, const std::string& vm_output_data) {
194  auto const& outputs = vm_.get()->outputs();
195  auto search = outputs.find(vm_output_data);
196  if (search != outputs.end()) {
197  auto data = std::static_pointer_cast<D>(search->second.get()->ptr());
198  auto size = search->second.get()->len();
199  for (int64_t i = 0; i < size; i++) {
200  if (data.get()[i] == x) {
201  index(i);
202  return true;
203  }
204  }
205  }
206  return false;
207  }
208 
210  void
211  add_bool(bool x);
212 
214  void
215  add_int64(int64_t x);
216 
218  void
219  add_double(double x);
220 
222  void
223  add_complex(std::complex<double> x);
224 
226  void
227  add_string(const std::string& x);
228 
230  static int64_t
231  next_id();
232 
234  static int64_t
235  next_error_id();
236 
238  void
239  resume() const;
240 
241  // @brief Root node of the FormBuilder tree.
242  const FormBuilderPtr<T, I> builder() const { return builder_; }
243 
244  protected:
247  static int64_t
249 
251  static int64_t
253 
254  private:
257  form_builder_from_json(const std::string& json_form);
258 
260  template <typename JSON>
262  from_json(const JSON& json_doc);
263 
265  void
266  initialise_builder(const std::string& json_form);
267 
269  void
270  initialise();
271 
273  template <typename D>
274  void
275  set_data(D x);
276 
278  int64_t initial_;
279 
281  int64_t length_;
282 
284  FormBuilderPtr<T, I> builder_;
285 
287  std::shared_ptr<ForthMachineOf<T, I>> vm_;
288 
290  std::map<std::string, std::shared_ptr<ForthInputBuffer>> vm_inputs_map_;
291 
293  std::string vm_input_data_;
294 
296  std::string vm_source_;
297 
299  std::set<util::ForthError> ignore_;
300 
302  ForthOutputBufferMap vm_outputs_map_;
303 
304  };
305 
308 
309 }
310 
311 #endif // AWKWARD_LayoutBuilder_H_
std::map< std::string, std::shared_ptr< ForthOutputBuffer > > ForthOutputBufferMap
Definition: FormBuilder.h:15
Container for all configuration options needed by ArrayBuilder, GrowableBuffer, and the Builder subcl...
Definition: ArrayBuilderOptions.h:13
const std::string primitive_to_state(const std::string &name)
#define LIBAWKWARD_EXPORT_SYMBOL
Definition: common.h:45
state
Definition: LayoutBuilder.h:24
Definition: BitMaskedArray.h:15
Definition: ForthMachine.h:25
static int64_t error_id
An error ID to be used to generate a user &#39;halt&#39; message.
Definition: LayoutBuilder.h:252
std::string name(Mapping a, V value)
Definition: datetime_util.h:39
const FormBuilderPtr< T, I > builder() const
Definition: LayoutBuilder.h:242
std::underlying_type< state >::type utype
Definition: LayoutBuilder.h:49
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 &#39;categorical&#39; array...
Definition: LayoutBuilder.h:193
User interface to the FormBuilder system: the LayoutBuilder is a fixed reference while the FormBuilde...
Definition: FormBuilder.h:17
const std::string primitive_to_vm_format(const std::string &name)
const std::string index_form_to_vm_format(const std::string &form_index)
const std::string index_form_to_name(const std::string &form_index)
std::shared_ptr< FormBuilder< T, I > > FormBuilderPtr
Definition: FormBuilder.h:148
static int64_t next_node_id
A unique ID to use when Form nodes do not have Form key defined.
Definition: LayoutBuilder.h:248