Jazz 1.25.+
Loading...
Searching...
No Matches
tuple.h
1/* Jazz (c) 2018-2025 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 <map>
36
37
38#include "src/jazz_elements/kind.h"
39
40#ifdef CATCH_TEST
41#ifndef INCLUDED_JAZZ_CATCH2
42#define INCLUDED_JAZZ_CATCH2
43
44#include "src/catch2/catch.hpp"
45
46#endif
47#endif
48
49
50#ifndef INCLUDED_JAZZ_ELEMENTS_TUPLE
51#define INCLUDED_JAZZ_ELEMENTS_TUPLE
52
53
54namespace jazz_elements
55{
56
94class Tuple : public Block {
95
96 public:
97
98 // Methods to create a Tuple:
99
110 inline StatusCode new_tuple(int num_items,
111 pBlock blocks[],
112 Name p_names[],
113 int num_bytes,
114 AttributeMap *att = nullptr) {
115
116 if (num_items < 1 || num_items > MAX_ITEMS_IN_KIND)
117 return SERVICE_ERROR_WRONG_ARGUMENTS;
118
119 int rq_sz = sizeof(BlockHeader) + sizeof(StringBuffer) + num_items*sizeof(ItemHeader) + 2*num_items;
120
121 if (att != nullptr)
122 rq_sz += 2*att->size();
123
124 if (num_bytes < rq_sz)
125 return SERVICE_ERROR_NO_MEM;
126
127 memset(&cell_type, 0, rq_sz);
128
129 cell_type = CELL_TYPE_TUPLE_ITEM;
130 rank = 1;
131 range.dim[0] = 1;
132 size = num_items;
133 total_bytes = num_bytes;
134
135 set_attributes(att);
136
138
139 for (int i = 0; i < num_items; i++) {
140 pBlock p_block = blocks[i];
141 if (p_block == nullptr)
142 return SERVICE_ERROR_WRONG_ARGUMENTS;
143
144 ItemHeader *p_it_hea = &tensor.cell_item[i];
145
146 if ((p_block->cell_type & 0xff) > 8)
147 return SERVICE_ERROR_WRONG_TYPE;
148
149 pChar p_name = (pChar) &p_names[i];
150
151 if (!valid_name(p_name))
152 return SERVICE_ERROR_WRONG_NAME;
153
154 p_it_hea->cell_type = p_block->cell_type;
155 p_it_hea->name = get_string_offset(psb, p_name);
156 p_it_hea->rank = p_block->rank;
157 p_block->get_dimensions((int *) &p_it_hea->dim);
158
159 if (p_it_hea->name <= STRING_EMPTY)
160 return SERVICE_ERROR_NO_MEM;
161 }
162
163 int *p_dest = align64bit((uintptr_t) &psb->buffer[psb->last_idx]);
164
165 if ((uintptr_t) p_dest - (uintptr_t) &cell_type > num_bytes)
166 return SERVICE_ERROR_NO_MEM; // May never happen. "(p_it_hea->name <= STRING_EMPTY)" catches it first.
167
168 psb->buffer_size = (uintptr_t) p_dest - ((uintptr_t) &psb->buffer[0]);
169
170 for (int i = 0; i < num_items; i++) {
171 pBlock p_block = blocks[i];
172 ItemHeader *p_it_hea = &tensor.cell_item[i];
173
174 p_it_hea->data_start = (uintptr_t) p_dest - (uintptr_t) &tensor;
175
176 if ((uintptr_t) p_dest - (uintptr_t) &cell_type + p_block->total_bytes > num_bytes)
177 return SERVICE_ERROR_NO_MEM;
178
179 memcpy(p_dest, p_block, p_block->total_bytes);
180
181 p_dest = align64bit((uintptr_t) p_dest + p_block->total_bytes);
182 }
183
184 return SERVICE_NO_ERROR;
185 }
186
195 inline char *item_name(int idx) {
196 if (idx < 0 || idx >= size)
197 return nullptr;
198
199 return reinterpret_cast<char *>(&p_string_buffer()->buffer[tensor.cell_item[idx].name]);
200 }
201
208 inline int index(pChar name) {
209 for (int idx = 0; idx < size; idx++)
210 if (strcmp(reinterpret_cast<char *>(&p_string_buffer()->buffer[tensor.cell_item[idx].name]), name) == 0)
211 return idx;
212
213 return -1;
214 }
215
224 inline pBlock get_block(int idx) {
225 if (idx < 0 | idx >= size)
226 return nullptr;
227
228 return (pBlock) ((uintptr_t) &tensor + tensor.cell_item[idx].data_start);
229 }
230
231 // Tuple/Kind specific methods:
232
239 inline bool is_a(pKind kind) {
240 if (kind->cell_type != CELL_TYPE_KIND_ITEM || kind->size != size)
241 return false;
242
243 std::map<std::string, int> dimension;
244
245 for (int i = 0; i < size; i++) {
247 || kind->tensor.cell_item[i].rank != tensor.cell_item[i].rank)
248 return false;
249
250 if (strcmp(kind->item_name(i), item_name(i)))
251 return false;
252
253 for (int j = 0; j < tensor.cell_item[i].rank; j++) {
254 int d_k = kind->tensor.cell_item[i].dim[j];
255
256 if (d_k < 0) {
257 std::string dim_name(reinterpret_cast<char *>(&kind->p_string_buffer()->buffer[-d_k]));
258 if (dimension.count(dim_name)) {
259 if (dimension[dim_name] != tensor.cell_item[i].dim[j])
260 return false;
261 } else {
262 dimension[dim_name] = tensor.cell_item[i].dim[j];
263 }
264 } else {
265 if (d_k != tensor.cell_item[i].dim[j])
266 return false;
267 }
268 }
269 }
270
271 return true;
272 }
273
274 int audit();
275};
276typedef Tuple *pTuple;
277
278} // namespace jazz_elements
279
280#endif // ifndef INCLUDED_JAZZ_ELEMENTS_TUPLE
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
int * align64bit(uintptr_t ipt)
Align a pointer (as uintptr_t) to the next 8 byte boundary assuming the block is aligned.
Definition block.h:339
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:83
char * item_name(int idx)
Definition kind.h:95
Tuple: A Jazz Block with multiple Tensors.
Definition tuple.h:94
bool is_a(pKind kind)
Definition tuple.h:239
char * item_name(int idx)
Definition tuple.h:195
StatusCode new_tuple(int num_items, pBlock blocks[], Name p_names[], int num_bytes, AttributeMap *att=nullptr)
Definition tuple.h:110
int index(pChar name)
Definition tuple.h:208
int audit()
Definition tuple.cpp:45
pBlock get_block(int idx)
Definition tuple.h:224
The namespace for Jazz Utils, Blocks, Kinds, Tuples, Containers, etc.
Definition block.cpp:39
Tuple * pTuple
A pointer to a Tuple object.
Definition tuple.h:276
bool valid_name(pChar p_name)
Check if a name is valid (Without using the regex).
Definition utils.h:188
char * pChar
A pointer to a char buffer.
Definition types.h:185
class Block * pBlock
A (forward defined) pointer to a Block.
Definition block.h:66
char Name[NAME_SIZE]
A short identifier used in Blocks, Containers and API.
Definition types.h:183
int StatusCode
Type returned by the Service API.
Definition utils.h:141
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:242
Header for an item (of a Kind or Tuple)
Definition types.h:198
int data_start
The data start of this tensor as an offset of &BlockHeader.tensor. (If is is a Tuple....
Definition types.h:204
int name
The name of this item as an offset in StringBuffer.
Definition types.h:200
int dim[MAX_TENSOR_RANK]
Dimensions for the Tensor. For Kind: negative numbers are dimension names as -offset in StringBuffer.
Definition types.h:202
int rank
The number of dimensions.
Definition types.h:201
int cell_type
The type for the cells in the item. See CELL_TYPE_*.
Definition types.h:199
int size
The total number of cells in the tensor.
Definition types.h:267
int cell_type
The type for the cells in the tensor. See CELL_TYPE_*.
Definition types.h:266
Tensor tensor
A tensor for type cell_type and dimensions set by Block.set_dimensions()
Definition types.h:276
int total_bytes
Total size of the block everything included.
Definition types.h:272
int rank
The number of dimensions.
Definition types.h:269
TensorDim range
The dimensions of the tensor in terms of ranges (Max. size is 2 Gb.)
Definition types.h:270
Structure at the end of a Block, initially created with init_string_buffer()
Definition types.h:282
int last_idx
The index to the first free space after the last stored string.
Definition types.h:285
int buffer_size
The size in bytes of buffer[].
Definition types.h:286
char buffer[]
The buffer where strings are stored starting with two zeroes for STRING_NA & STRING_EMPTY.
Definition types.h:287
int dim[MAX_TENSOR_RANK]
Dimensions for the Tensor. The product of all * (cell_type & 0xff) < 2 Gb.
Definition types.h:193
ItemHeader cell_item[0]
.. An array of BlockHeader used by Kinds and Tuples
Definition types.h:231