Jazz 1.26.+
Loading...
Searching...
No Matches
kind.h
1/* Jazz (c) 2018-2026 kaalam.ai (The Authors of Jazz), using (under the same license):
2
3 1. Biomodelling - The AATBlockQueue class (c) Jacques BasaldĂșa, 2009-2012 licensed
4 exclusively for the use in the Jazz server software.
5
6 Copyright 2009-2012 Jacques BasaldĂșa
7
8 2. BBVA - Jazz: A lightweight analytical web server for data-driven applications.
9
10 Copyright 2016-2017 Banco Bilbao Vizcaya Argentaria, S.A.
11
12 This product includes software developed at
13
14 BBVA (https://www.bbva.com/)
15
16 3. LMDB, Copyright 2011-2017 Howard Chu, Symas Corp. All rights reserved.
17
18 Licensed under http://www.OpenLDAP.org/license.html
19
20
21 Licensed under the Apache License, Version 2.0 (the "License");
22 you may not use this file except in compliance with the License.
23 You may obtain a copy of the License at
24
25 http://www.apache.org/licenses/LICENSE-2.0
26
27 Unless required by applicable law or agreed to in writing, software
28 distributed under the License is distributed on an "AS IS" BASIS,
29 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 See the License for the specific language governing permissions and
31 limitations under the License.
32*/
33
34
35#include "src/jazz_elements/block.h"
36
37#ifdef CATCH_TEST
38#ifndef INCLUDED_JAZZ_CATCH2
39#define INCLUDED_JAZZ_CATCH2
40
41#include "src/catch2/catch.hpp"
42
43#endif
44#endif
45
46
47#ifndef INCLUDED_JAZZ_ELEMENTS_KIND
48#define INCLUDED_JAZZ_ELEMENTS_KIND
49
50
51namespace jazz_elements
52{
53
96class Kind : public Block {
97
98 public:
99
108 inline char *item_name(int idx) {
109 if (idx < 0 || idx >= size)
110 return nullptr;
111
112 return reinterpret_cast<char *>(&p_string_buffer()->buffer[tensor.cell_item[idx].name]);
113 }
114
121 inline int index(pChar name) {
122 for (int idx = 0; idx < size; idx++) {
123 if (strcmp(reinterpret_cast<char *>(&p_string_buffer()->buffer[tensor.cell_item[idx].name]), name) == 0)
124 return idx;
125 }
126 return -1;
127 }
128
134 inline void dimensions(Dimensions &dims) {
135 for (int i = 0; i < size; i++) {
136 ItemHeader *p_it_hea = &tensor.cell_item[i];
137
138 for (int j = 0; j < p_it_hea->rank; j++) {
139 int k = p_it_hea->dim[j];
140
141 if (k < 0) {
142 char *pt = (&p_string_buffer()->buffer[-k]);
143
144 dims.insert(String(pt));
145 }
146 }
147 }
148 }
149
158 inline int new_kind(int num_items,
159 int num_bytes,
160 AttributeMap *att = nullptr) {
161
162 if (num_items < 1 || num_items > MAX_ITEMS_IN_KIND)
163 return SERVICE_ERROR_WRONG_ARGUMENTS;
164
165 int rq_sz = sizeof(BlockHeader) + sizeof(StringBuffer) + num_items*sizeof(ItemHeader) + 2*num_items;
166
167 if (att != nullptr)
168 rq_sz += 2*att->size();
169
170 if (num_bytes < rq_sz)
171 return SERVICE_ERROR_NO_MEM;
172
173 memset(&cell_type, 0, num_bytes);
174
175 cell_type = CELL_TYPE_TUPLE_KIND;
176 rank = 1;
177 range.dim[0] = 1;
178 size = num_items;
179 total_bytes = num_bytes;
180 num_attributes = 0; // set_attributes() is done just once when it is not already set.
181
182 set_attributes(att);
183
184 return SERVICE_NO_ERROR;
185 }
186
205 inline bool add_item(int idx,
206 char const *p_name,
207 int *p_dim,
208 int cell_type,
209 AttributeMap *p_dims) {
210
211 if (idx < 0 || idx >= size)
212 return false;
213
214 ItemHeader *p_it_hea = &tensor.cell_item[idx];
215
216 int rank = 6, j, k;
217
219
220 for (int i = MAX_TENSOR_RANK - 1; i >= 0; i--) {
221 j = p_dim[i];
222 p_it_hea->dim[i] = j;
223 if (j == 0) {
224 rank = i;
225
226 if (!i)
227 return false;
228
229 } else if (j < 0) {
230 if (p_dims == nullptr)
231 return false;
232
233 AttributeMap::iterator it = p_dims->find(j);
234 if (it == p_dims->end())
235 return false;
236
237 k = get_string_offset(psb, it->second);
238
239 if (k <= STRING_EMPTY)
240 return false;
241
242 p_it_hea->dim[i] = -k;
243 }
244 }
245
246 p_it_hea->name = get_string_offset(psb, p_name);
247
248 if (p_it_hea->name <= STRING_EMPTY)
249 return false;
250
251 p_it_hea->cell_type = cell_type;
252 p_it_hea->rank = rank;
253
254 return true;
255 }
256
266 inline int new_object_kind (int num_bytes,
267 char const *p_def,
268 char sep,
269 AttributeMap *att = nullptr) {
270 Name name;
271 int num_lines = 1, j = 0;
272 for (char const *p = p_def; *p; p++) {
273 if (*p == sep) {
274 if (j >= sizeof(Name))
275 return SERVICE_ERROR_WRONG_ARGUMENTS;
276
277 j = 0;
278 num_lines++;
279 } else {
280 j++;
281 }
282 }
283 if (j >= sizeof(Name))
284 return SERVICE_ERROR_WRONG_ARGUMENTS;
285
286 int rq_sz = sizeof(BlockHeader) + sizeof(StringBuffer) + strlen(p_def) + num_lines*sizeof(int);
287
288 if (att != nullptr)
289 rq_sz += 2*att->size();
290
291 if (rq_sz > num_bytes)
292 return SERVICE_ERROR_NO_MEM;
293
294 cell_type = CELL_TYPE_OBJECT_KIND;
295 rank = 1;
296 range.dim[0] = num_lines;
297 size = num_lines;
298 total_bytes = num_bytes;
299 num_attributes = 0; // set_attributes() is done just once when it is not already set.
300
301 set_attributes(att); // This also calls init_string_buffer()
302
304
305 int i = 0;
306 j = 0;
307 for (char const *p = p_def; *p; p++) {
308 if (*p == sep) {
309 name[j] = 0;
310 j = 0;
311 set_string(i, name);
312 i++;
313 } else {
314 name[j++] = *p;
315 }
316 }
317 name[j] = 0;
318 set_string(i, name);
319
320 return SERVICE_NO_ERROR;
321 }
322
327 inline bool to_block_kind() {
328 if (size != 1 || cell_type != CELL_TYPE_TUPLE_KIND)
329 return false;
330
331 cell_type = CELL_TYPE_BLOCK_KIND;
332 return true;
333 }
334
335 int audit();
336};
337typedef Kind *pKind;
338
339} // namespace jazz_elements
340
341#endif // ifndef INCLUDED_JAZZ_ELEMENTS_KIND
A block is a moveable BlockHeader followed by a Tensor and a StringBuffer.
Definition block.h:99
int get_string_offset(pStringBuffer psb, const char *p_str)
Definition block.cpp:128
void set_string(int *p_idx, const char *p_str)
Definition block.h:217
pStringBuffer p_string_buffer()
Definition block.h:363
void set_attributes(AttributeMap *all_att)
Definition block.h:268
Kind: A type definition for Jazz Blocks and Tuples.
Definition kind.h:96
int audit()
Audit a Kind.
Definition kind.cpp:47
bool to_block_kind()
Definition kind.h:327
bool add_item(int idx, char const *p_name, int *p_dim, int cell_type, AttributeMap *p_dims)
Definition kind.h:205
int new_kind(int num_items, int num_bytes, AttributeMap *att=nullptr)
Definition kind.h:158
int new_object_kind(int num_bytes, char const *p_def, char sep, AttributeMap *att=nullptr)
Definition kind.h:266
char * item_name(int idx)
Definition kind.h:108
void dimensions(Dimensions &dims)
Definition kind.h:134
int index(pChar name)
Definition kind.h:121
The namespace for Jazz Utils, Blocks, Kinds, Tuples, Containers, etc.
Definition block.cpp:39
std::string String
A standard string used in many other places in Jazz.
Definition types.h:239
std::set< String > Dimensions
An set::set with the dimension names returned by kind.dimensions()
Definition types.h:241
Kind * pKind
A pointer to a Kind object.
Definition kind.h:337
char * pChar
A pointer to a char buffer.
Definition types.h:189
char Name[NAME_SIZE]
A short identifier used in Blocks, Containers and API.
Definition types.h:187
std::map< int, const char * > AttributeMap
An stdlib map to store all the attributes of a Block at the same time used by the some Block methods.
Definition block.h:63
Header for a Movable Block (Tensor, Kind or Tuple) or a Dynamic Block (Index)
Definition types.h:247
Header for an item (of a Kind or Tuple)
Definition types.h:202
int name
The name of this item as an offset in StringBuffer.
Definition types.h:204
int dim[MAX_TENSOR_RANK]
Dimensions for the Tensor. For Kind: negative numbers are dimension names as -offset in StringBuffer.
Definition types.h:206
int rank
The number of dimensions.
Definition types.h:205
int cell_type
The type for the cells in the item. See CELL_TYPE_*.
Definition types.h:203
int size
The total number of cells in the tensor.
Definition types.h:272
int cell_type
The type for the cells in the tensor. See CELL_TYPE_*.
Definition types.h:271
Tensor tensor
A tensor for type cell_type and dimensions set by Block.set_dimensions()
Definition types.h:281
int total_bytes
Total size of the block everything included.
Definition types.h:277
int num_attributes
Number of elements in the JazzAttributesMap.
Definition types.h:276
int rank
The number of dimensions.
Definition types.h:274
TensorDim range
The dimensions of the tensor in terms of ranges (Max. size is 2 Gb.)
Definition types.h:275
Structure at the end of a Block, initially created with init_string_buffer()
Definition types.h:287
char buffer[]
The buffer where strings are stored starting with two zeroes for STRING_NA & STRING_EMPTY.
Definition types.h:292
int dim[MAX_TENSOR_RANK]
Dimensions for the Tensor. The product of all * (cell_type & 0xff) < 2 Gb.
Definition types.h:197
ItemHeader cell_item[0]
.. An array of BlockHeader used by Kinds and Tuples
Definition types.h:235