Jazz 1.25.+
Loading...
Searching...
No Matches
tuple.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 <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;
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 psb->buffer_size = (uintptr_t) p_dest - ((uintptr_t) &psb->buffer[0]);
166
167 for (int i = 0; i < num_items; i++) {
168 pBlock p_block = blocks[i];
169 ItemHeader *p_it_hea = &tensor.cell_item[i];
170
171 p_it_hea->data_start = (uintptr_t) p_dest - (uintptr_t) &tensor;
172
173 if ((uintptr_t) p_dest - (uintptr_t) &cell_type + p_block->total_bytes > num_bytes)
174 return SERVICE_ERROR_NO_MEM;
175
176 memcpy(p_dest, p_block, p_block->total_bytes);
177
178 p_dest = align64bit((uintptr_t) p_dest + p_block->total_bytes);
179 }
180
181 return SERVICE_NO_ERROR;
182 }
183
192 inline char *item_name(int idx) {
193 if (idx < 0 || idx >= size)
194 return nullptr;
195
196 return reinterpret_cast<char *>(&p_string_buffer()->buffer[tensor.cell_item[idx].name]);
197 }
198
205 inline int index(pChar name) {
206 for (int idx = 0; idx < size; idx++)
207 if (strcmp(reinterpret_cast<char *>(&p_string_buffer()->buffer[tensor.cell_item[idx].name]), name) == 0)
208 return idx;
209
210 return -1;
211 }
212
221 inline pBlock get_block(int idx) {
222 if (idx < 0 | idx >= size)
223 return nullptr;
224
225 return (pBlock) ((uintptr_t) &tensor + tensor.cell_item[idx].data_start);
226 }
227
228 // Tuple/Kind specific methods:
229
236 inline bool is_a(pKind kind) {
237 if (kind->cell_type != CELL_TYPE_TUPLE_KIND || kind->size != size)
238 return false;
239
240 std::map<String, int> dimension;
241
242 for (int i = 0; i < size; i++) {
244 || kind->tensor.cell_item[i].rank != tensor.cell_item[i].rank)
245 return false;
246
247 if (strcmp(kind->item_name(i), item_name(i)))
248 return false;
249
250 for (int j = 0; j < tensor.cell_item[i].rank; j++) {
251 int d_k = kind->tensor.cell_item[i].dim[j];
252
253 if (d_k < 0) {
254 String dim_name(reinterpret_cast<char *>(&kind->p_string_buffer()->buffer[-d_k]));
255 if (dimension.count(dim_name)) {
256 if (dimension[dim_name] != tensor.cell_item[i].dim[j])
257 return false;
258 } else {
259 dimension[dim_name] = tensor.cell_item[i].dim[j];
260 }
261 } else {
262 if (d_k != tensor.cell_item[i].dim[j])
263 return false;
264 }
265 }
266 }
267
268 return true;
269 }
270
271 int audit();
272};
273typedef Tuple *pTuple;
274
275} // namespace jazz_elements
276
277#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:96
char * item_name(int idx)
Definition kind.h:108
Tuple: A Jazz Block with multiple Tensors.
Definition tuple.h:94
bool is_a(pKind kind)
Definition tuple.h:236
char * item_name(int idx)
Definition tuple.h:192
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:205
int audit()
Definition tuple.cpp:45
pBlock get_block(int idx)
Definition tuple.h:221
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:273
std::string String
A standard string used in many other places in Jazz.
Definition types.h:239
bool valid_name(pChar p_name)
Check if a name is valid (Without using the regex).
Definition utils.h:189
char * pChar
A pointer to a char buffer.
Definition types.h:189
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:187
int StatusCode
Type returned by the Service API.
Definition utils.h:142
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 data_start
The data start of this tensor as an offset of &BlockHeader.tensor. (If is is a Tuple....
Definition types.h:208
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 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
int last_idx
The index to the first free space after the last stored string.
Definition types.h:290
int buffer_size
The size in bytes of buffer[].
Definition types.h:291
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