libdas2
das2 core C utilities
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
variable.h
Go to the documentation of this file.
1 /* Copyright (C) 2017-2018 Chris Piker <chris-piker@uiowa.edu>
2  *
3  * This file is part of libdas2, the Core Das2 C Library.
4  *
5  * Libdas2 is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License version 2.1 as published
7  * by the Free Software Foundation.
8  *
9  * Libdas2 is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * version 2.1 along with libdas2; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
20 #ifndef _das_variable_h_
21 #define _das_variable_h_
22 
23 #include <das2/array.h>
24 #include <das2/datum.h>
25 
26 #include "units.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
36 enum var_type {
37  D2V_DATUM, D2V_SEQUENCE, D2V_ARRAY, D2V_UNARY_OP, D2V_BINARY_OP
38 };
39 
40 #define MAP_0 0, (NULL)
41 #define MAP_1(I) 1, (int8_t[1]){I}
42 #define MAP_2(I,J) 2, (int8_t[2]){I,J}
43 #define MAP_3(I,J,K) 3, (int8_t[3]){I,J,K}
44 #define MAP_4(I,J,K,L) 4, (int8_t[4]){I,J,K,L}
45 #define MAP_5(I,J,K,L,M) 5, (int8_t[5]){I,J,K,L,M}
46 #define MAP_6(I,J,K,L,M,N) 6, (int8_t[6]){I,J,K,L,M,N}
47 #define MAP_7(I,J,K,L,M,N,O) 7, (int8_t[7]){I,J,K,L,M,N,O}
48 
49 /* Internal function for merging variable, and dimension shapes. Different
50  * rules apply for arrays to variable shape merges.
51  *
52  * Combinding index rules:
53  *
54  * '*' + '-' = '*'
55  * '*' + Number = Number ('*' means undefined length, represented by)
56  * '-' + Number = Number ('-' means no dependency, negative nums )
57  * Big Number + Small Number = Small Number
58  *
59  */
60 void das_varindex_merge(int nRank, ptrdiff_t* pDest, ptrdiff_t* pSrc);
61 
62 /* Internal function for merging length in a particular dimension. */
63 ptrdiff_t das_varlength_merge(ptrdiff_t nLeft, ptrdiff_t nRight);
64 
65 #define D2V_EXP_UNITS 0x02
66 #define D2V_EXP_RANGE 0x04
67 
207 typedef struct das_variable{
208  enum var_type vartype; /* CONST, ARRAY, SEQUENCE, UNARY_OP, BINARY_OP ... */
209  das_val_type vt; /* vtByte, vtText, vtTime ... */
210  size_t vsize; /* The size in bytes of each value in the variable
211  * for strings this yields the unusual value of
212  * sizeof(void*) */
213 
214  /* The units of this variable, since it is possible to create variables
215  * in all kinds of ways (not just backing arrays) we have to have our
216  * own units storage location. Transforming backing arrays such that they
217  * are no longer in the units they had when the variable was created will
218  * NOT automatically update this variable. */
219  das_units units;
220 
221  /* Reference count on this variable. Needed to make sure it's not
222  * deleted out from under us. */
223  int nRef;
224 
225  /* Position of the first internal index. Any array indices after
226  * this point will be considered to be internal indices and will not
227  * be reported in the shape function */
228  int iFirstInternal;
229 
230  /* Get the external shape of this variable */
231  int (*shape)(const struct das_variable* pThis, ptrdiff_t* pShape);
232 
233  /* Get the external length of this variable at a partial index */
234  ptrdiff_t (*lengthIn)(
235  const struct das_variable* pThis, int nIdx, ptrdiff_t* pLoc
236  );
237 
239  bool (*get)(
240  const struct das_variable* pThis, ptrdiff_t* pIdx, das_datum* pDatum
241  );
242 
244  int (*incRef)(struct das_variable* pThis);
245 
251  int (*decRef)(struct das_variable* pThis);
252 
253  /* Write an expression (i.e. a representation) of this variable to a
254  * buffer.
255  *
256  *
257  * @param uFlags - D2V_EXP_UNITS include units in the expression
258  * D2V_EXP_RANGE include the range in the expression
259  *
260  * @returns The write point to add more text to the buffer
261  */
262  char* (*expression)(const struct das_variable* pThis, char* sBuf, int nLen,
263  unsigned int uFlags);
264 } DasVar;
265 
266 
292 DasVar* new_DasVarUnary(const char* sOp, const DasVar* pVar);
293 
294 /* Internal version for use by the expression lexer */
295 DasVar* new_DasVarUnary_tok(int nOpTok, const DasVar* pVar);
296 
336 DasVar* new_DasVarBinary(DasVar* pLeft, const char* sOp, DasVar* pRight);
337 
338 
359 DasVar* new_DasConstant(das_val_type vt, size_t sz, const void* val, das_units units);
360 
361 
378  const char* sId, das_val_type vt, size_t vSz, const void* pMin,
379  const void* pInterval, int8_t* pMap, das_units units
380 );
381 
412 DasVar* new_DasVarArray(DasAry* pAry, int iInternal, int8_t* pMap);
413 
421 
424 DasVar* new_DasVarEval(const DasVar* pVar);
425 
437 bool DasVar_orthoginal(const DasVar* pThis, const DasVar* pOther);
438 
439 
466 int DasVar_shape(const DasVar* pThis, ptrdiff_t* pShape);
467 
484 ptrdiff_t DasVar_lengthIn(const DasVar* pThis, int nIdx, ptrdiff_t* pLoc);
485 
498 char* DasVar_toStr(const DasVar* pThis, char* sBuf, int nLen);
499 
500 
519 bool DasVar_getDatum(const DasVar* pThis, ptrdiff_t* pIdx, das_datum* pDatum);
520 
521 
528 bool DasVar_isComposite(const DasVar* pVar);
529 
530 
531 
542 void dec_DasVar(DasVar* pThis);
543 
544 
547 #ifdef __cplusplus
548 }
549 #endif
550 
551 #endif /* _das_variable_h */
DasAry * DasVarAry_getArray(DasVar *pThis)
Get the backing array if present.
Das2 fexible variables.
Definition: variable.h:207
A value and it&#39;s units.
Definition: datum.h:62
DasVar * new_DasVarEval(const DasVar *pVar)
Evaluate all sub-variable expressions and a single array variable.
das_val_type
Enumeration of types stored in Das Array (DasAry) objects Not that any kind of value may be stored in...
Definition: value.h:51
ptrdiff_t DasVar_lengthIn(const DasVar *pThis, int nIdx, ptrdiff_t *pLoc)
Return the current max value index value + 1 for any partial index.
A dynamic buffer with multi-dimensional array style access.
DasVar * new_DasVarSeq(const char *sId, das_val_type vt, size_t vSz, const void *pMin, const void *pInterval, int8_t *pMap, das_units units)
Create a sequence variable.
int DasVar_shape(const DasVar *pThis, ptrdiff_t *pShape)
Return the current shape of this variable.
DasVar * new_DasConstant(das_val_type vt, size_t sz, const void *val, das_units units)
Create a constant value on the heap.
DasVar * new_DasVarBinary(DasVar *pLeft, const char *sOp, DasVar *pRight)
Create a new variable from a binary operation on two other variables.
bool DasVar_isComposite(const DasVar *pVar)
Is this a simple variable or more than one variable combinded via operators?
void dec_DasVar(DasVar *pThis)
Decrement the reference count on a variable.
bool DasVar_getDatum(const DasVar *pThis, ptrdiff_t *pIdx, das_datum *pDatum)
Get a value given an index.
Defines units used for items in the stream, most notably time units that reference an epoch and a ste...
bool DasVar_orthoginal(const DasVar *pThis, const DasVar *pOther)
Getting data from a variable.
DasVar * new_DasVarArray(DasAry *pAry, int iInternal, int8_t *pMap)
Create a function backed by an Array.
DasVar * new_DasVarUnary(const char *sOp, const DasVar *pVar)
Create a new variable from unary operation on an existing variable.
const char * das_units
Enumeration of unit types, that correspond to physical unit types.
Definition: units.h:135
char * DasVar_toStr(const DasVar *pThis, char *sBuf, int nLen)
Get a string representation of this variable.
Dynamic recursive ragged arrays.
Definition: array.h:193