das2C
das core C utilities (v3)
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 
32 /* Current max length of a vector (internal index) can be changed */
33 #define D2V_MAX_VEC_LEN 4
34 
35 enum var_type {
36  D2V_CONST, D2V_SEQUENCE, D2V_ARRAY, D2V_UNARY_OP, D2V_BINARY_OP
37 };
38 
39 #ifdef _D
40 #error macro _D already defined, pick something else
41 #else
42 #define _D DASIDX_UNUSED
43 #endif
44 
45 
55 #define SCALAR_0 0, (NULL), 0
56 #define SCALAR_1(I) 1, (int8_t[DASIDX_MAX]){I,_D,_D,_D,_D,_D,_D,_D}, 0
57 #define SCALAR_2(I,J) 2, (int8_t[DASIDX_MAX]){I,J,_D,_D,_D,_D,_D,_D}, 0
58 #define SCALAR_3(I,J,K) 3, (int8_t[DASIDX_MAX]){I,J,K,_D,_D,_D,_D,_D}, 0
59 #define SCALAR_4(I,J,K,L) 4, (int8_t[DASIDX_MAX]){I,J,K,L,_D,_D,_D,_D}, 0
60 #define SCALAR_5(I,J,K,L,M) 5, (int8_t[DASIDX_MAX]){I,J,K,L,M,_D,_D,_D}, 0
61 #define SCALAR_6(I,J,K,L,M,N) 6, (int8_t[DASIDX_MAX]){I,J,K,L,M,N,_D,_D}, 0
62 #define SCALAR_7(I,J,K,L,M,N,O) 7, (int8_t[DASIDX_MAX]){I,J,K,L,M,N,O,_D}, 0
63 #define SCALAR_8(I,J,K,L,M,N,O,P) 8, (int8_t[DASIDX_MAX]){I,J,K,L,M,N,O,P}, 0
64 
65 #define VEC_0 0, (int8_t[DASIDX_MAX]){_D,_D,_D,_D,_D,_D,_D,_D}, 1
66 #define VEC_1(I) 1, (int8_t[DASIDX_MAX]){I,_D,_D,_D,_D,_D,_D,_D}, 1
67 #define VEC_2(I,J) 2, (int8_t[DASIDX_MAX]){I,J,_D,_D,_D,_D,_D,_D}, 1
68 #define VEC_3(I,J,K) 3, (int8_t[DASIDX_MAX]){I,J,K,_D,_D,_D,_D,_D}, 1
69 #define VEC_4(I,J,K,L) 4, (int8_t[DASIDX_MAX]){I,J,K,L,_D,_D,_D,_D}, 1
70 #define VEC_5(I,J,K,L,M) 5, (int8_t[DASIDX_MAX]){I,J,K,L,M,_D,_D,_D}, 1
71 #define VEC_6(I,J,K,L,M,N) 6, (int8_t[DASIDX_MAX]){I,J,K,L,M,N,_D,_D}, 1
72 #define VEC_7(I,J,K,L,M,N,O) 7, (int8_t[DASIDX_MAX]){I,J,K,L,M,N,O,_D}, 1
73 
74 
75 /* Internal function for merging variable, and dimension shapes. Different
76  * rules apply for arrays to variable shape merges.
77  *
78  * Combinding index rules:
79  *
80  * '*' + '-' = '*'
81  * '*' + Number = Number ('*' means undefined length, represented by)
82  * '-' + Number = Number ('-' means no dependency, negative nums )
83  * Big Number + Small Number = Small Number
84  *
85  */
86 DAS_API void das_varindex_merge(int nRank, ptrdiff_t* pDest, ptrdiff_t* pSrc);
87 
88 /* Internal function for merging length in a particular dimension. */
89 DAS_API ptrdiff_t das_varlength_merge(ptrdiff_t nLeft, ptrdiff_t nRight);
90 
91 #define D2V_EXP_UNITS 0x02
92 #define D2V_EXP_RANGE 0x04
93 #define D2V_EXP_SUBEX 0x08
94 #define D2V_EXP_INTR 0x10
95 #define D2V_EXP_TYPE 0x20
96 
105 DAS_API void das_varindex_prndir(bool bFastLast);
106 
247 typedef struct das_variable{
248  enum var_type vartype; /* CONST, ARRAY, SEQUENCE, UNARY_OP, BINARY_OP ... */
249  das_val_type vt; /* vtUByte, vtText, vtTime, vtVector ... */
250 
251  size_t vsize; /* The size in bytes of each value in the variable
252  * for non-scalar items, this yields the unusual value
253  * of sizeof(void*) */
254 
255  /* Number of external indexes. Many of these may not be used and are
256  * thus marked as degenerate */
257  int nExtRank;
258 
259  /* Number of internal indexes, essentially the item rank. Is zero except
260  for text strings and geometric vectors */
261  int nIntRank;
262 
263  /* Since it is possible to create variables in all kinds of ways (not just
264  * backing arrays) we have to have our own units storage location.
265  * Transforming backing arrays such that they are no longer in the units
266  * they had when the variable was created will NOT automatically update this
267  * variable.
268  */
269  das_units units;
270 
271  /* Reference count on this variable. Needed to make sure it's not
272  * deleted out from under us. */
273  int nRef;
274 
275 
276  /* Get identifier for this variable, may be NULL for anoymous vars */
277  const char* (*id)(const struct das_variable* pThis);
278 
279  /* Get full shape of this variable */
280  int (*shape)(const struct das_variable* pThis, ptrdiff_t* pShape);
281 
282  /* Get the internal shape of this variable.
283  *
284  * Combine this with DasVar_intrType to get the purpose of the internal
285  * indicies.
286  */
287  int (*intrShape)(const struct das_variable* pThis, ptrdiff_t* pComp);
288 
289  /* Write an expression (i.e. a representation) of this variable to a
290  * buffer.
291  *
292  * @param uFlags - D2V_EXP_UNITS include units in the expression
293  * D2V_EXP_RANGE include the range in the expression
294  * D2V_EXP_INTER include internal component information
295  *
296  * @returns The write point to add more text to the buffer
297  */
298  char* (*expression)(const struct das_variable* pThis, char* sBuf, int nLen,
299  unsigned int uFlags);
300 
301  /* Get the external length of this variable at a partial index */
302  ptrdiff_t (*lengthIn)(
303  const struct das_variable* pThis, int nIdx, ptrdiff_t* pLoc
304  );
305 
307  bool (*get)(
308  const struct das_variable* pThis, ptrdiff_t* pIdx, das_datum* pDatum
309  );
310 
311  bool (*isFill)(
312  const struct das_variable* pThis, const ubyte* pCheck, das_val_type vt
313  );
314 
315  /* Does this variable provide simple numbers */
316  bool (*isNumeric)(const struct das_variable* pThis);
317 
318  DasAry* (*subset)(
319  const struct das_variable* pThis, int nRank, const ptrdiff_t* pMin,
320  const ptrdiff_t* pMax
321  );
322 
324  int (*incRef)(struct das_variable* pThis);
325 
331  int (*decRef)(struct das_variable* pThis);
332 
334  bool (*degenerate)(const struct das_variable* pThis, int iIndex);
335 
336 
344  void* pUser;
345 
346 } DasVar;
347 
376 DAS_API DasVar* new_DasVarUnary(const char* sOp, const DasVar* pVar);
377 
378 /* Internal version for use by the expression lexer */
379 DAS_API DasVar* new_DasVarUnary_tok(int nOpTok, const DasVar* pVar);
380 
428  const char* sId, DasVar* pLeft, const char* sOp, DasVar* pRight
429 );
430 
431 
446 DAS_API DasVar* new_DasConstant(const char* sId, const das_datum* pDm);
447 
448 
482  const char* sId, das_val_type vt, size_t vSz, const void* pMin,
483  const void* pInterval, int nExtRank, int8_t* pMap, int nIntRank,
484  das_units units
485 );
486 
521 DAS_API DasVar* new_DasVarArray(DasAry* pAry, int nExtRank, int8_t* pMap, int nIntRank);
522 
523 
587  DasAry* pAry, int nExtRank, int8_t* pMap, int nIntRank,
588  const char* sFrame, ubyte nFrameId, ubyte frametype, ubyte nDirs,
589  const ubyte* pDir
590 );
591 
597 DAS_API int inc_DasVar(DasVar* pThis);
598 
612 DAS_API int dec_DasVar(DasVar* pThis);
613 
614 
618 int ref_DasVar(const DasVar* pThis);
619 
623 const char* DasVar_id(const DasVar* pThis);
624 
628 enum var_type DasVar_type(const DasVar* pThis);
629 
634 
638 size_t DasVar_valSize(const DasVar* pThis);
639 
647 
648 
656 
657 
658 /* Evaluate all sub-variable expressions and a single array variable
659  */
660 DAS_API DasVar* new_DasVarEval(const DasVar* pVar);
661 
673 DAS_API bool DasVar_orthoginal(const DasVar* pThis, const DasVar* pOther);
674 
686 DAS_API bool DasVar_degenerate(const DasVar* pThis, int iIndex);
687 
688 
717 DAS_API int DasVar_shape(const DasVar* pThis, ptrdiff_t* pShape);
718 
743 DAS_API int DasVar_intrShape(const DasVar* pThis, ptrdiff_t* pShape);
744 
745 
763 DAS_API ptrdiff_t DasVar_lengthIn(const DasVar* pThis, int nIdx, ptrdiff_t* pLoc);
764 
778 DAS_API char* DasVar_toStr(const DasVar* pThis, char* sBuf, int nLen);
779 
780 
809 DAS_API bool DasVar_isNumeric(const DasVar* pThis);
810 
811 
830 DAS_API bool DasVar_get(
831  const DasVar* pThis, ptrdiff_t* pIdx, das_datum* pDatum
832 );
833 
834 
853 DAS_API bool DasVar_isFill(
854  const DasVar* pThis, const ubyte* pCheck, das_val_type vt
855 );
856 
863 DAS_API bool DasVar_isComposite(const DasVar* pVar);
864 
865 
902  const DasVar* pThis, int nRank, const ptrdiff_t* pMin, const ptrdiff_t* pMax
903 );
904 
907 #ifdef __cplusplus
908 }
909 #endif
910 
911 #endif /* _das_variable_h */
A dynamic buffer with multi-dimensional array style access.
const char * das_units
Handle SI and other units, with accommodations for Epoch systems, from units.h.
Definition: units.h:139
das_val_type
Enumeration of types stored in Das Array (DasAry) objects from value.h.
Definition: value.h:64
Dynamic recursive ragged arrays.
Definition: array.h:270
Das2 fexible variables.
Definition: variable.h:247
DAS_API bool DasVar_isComposite(const DasVar *pVar)
Is this a simple variable or more than one variable combinded via operators?
DAS_API bool DasVar_orthoginal(const DasVar *pThis, const DasVar *pOther)
Getting data from a variable.
const char * DasVar_id(const DasVar *pThis)
Get id token for variable, may be NULL for anoymous vars.
DAS_API bool DasVar_isFill(const DasVar *pThis, const ubyte *pCheck, das_val_type vt)
Check to see if a value is a fill value for this variable.
das_units DasVar_units(const DasVar *pThis)
Get the units for the values.
das_val_type DasVar_valType(const DasVar *pThis)
Get the type of values held by the variable.
DAS_API DasVar * new_DasVarArray(DasAry *pAry, int nExtRank, int8_t *pMap, int nIntRank)
Create a variable backed by an Array.
enum var_type DasVar_type(const DasVar *pThis)
Get the type of variable.
DAS_API DasVar * new_DasConstant(const char *sId, const das_datum *pDm)
Create a constant value on the heap.
DAS_API DasVar * new_DasVarSeq(const char *sId, das_val_type vt, size_t vSz, const void *pMin, const void *pInterval, int nExtRank, int8_t *pMap, int nIntRank, das_units units)
Create a simple linear sequence variable.
DAS_API DasAry * DasVar_subset(const DasVar *pThis, int nRank, const ptrdiff_t *pMin, const ptrdiff_t *pMax)
Copy a subset of a variable into a memory buffer.
size_t DasVar_valSize(const DasVar *pThis)
Get the size in bytes of each value.
DAS_API ptrdiff_t DasVar_lengthIn(const DasVar *pThis, int nIdx, ptrdiff_t *pLoc)
Return the current max value index value + 1 for any partial index.
DAS_API int DasVar_shape(const DasVar *pThis, ptrdiff_t *pShape)
Return the current shape of this variable.
DAS_API DasVar * new_DasVarVecAry(DasAry *pAry, int nExtRank, int8_t *pMap, int nIntRank, const char *sFrame, ubyte nFrameId, ubyte frametype, ubyte nDirs, const ubyte *pDir)
Create a vector backed by an array.
int ref_DasVar(const DasVar *pThis)
Get number of references.
DAS_API int dec_DasVar(DasVar *pThis)
Decrement the reference count on a variable.
DAS_API DasVar * new_DasVarBinary(const char *sId, DasVar *pLeft, const char *sOp, DasVar *pRight)
Create a new variable from a binary operation on two other variables.
DAS_API int DasVar_intrShape(const DasVar *pThis, ptrdiff_t *pShape)
Return the internal composition of this variable.
DAS_API DasVar * new_DasVarUnary(const char *sOp, const DasVar *pVar)
Create a new variable from unary operation on an existing variable.
DAS_API bool DasVar_get(const DasVar *pThis, ptrdiff_t *pIdx, das_datum *pDatum)
Get a value given an index.
DAS_API char * DasVar_toStr(const DasVar *pThis, char *sBuf, int nLen)
Get a string representation of this variable.
DAS_API int inc_DasVar(DasVar *pThis)
Increment the reference count on a variable.
DAS_API DasAry * DasVarAry_getArray(DasVar *pThis)
Get the backing array if present.
void * pUser
User data pointer.
Definition: variable.h:344
DAS_API bool DasVar_isNumeric(const DasVar *pThis)
Are the values in this variable convertable to doubles?
An atomic data processing unit, and it's units.
Definition: datum.h:67
Defines units used for items in the stream, most notably time units that reference an epoch and a ste...
DAS_API bool DasVar_degenerate(const DasVar *pThis, int iIndex)
Does a given extern index even matter the this variable?
DAS_API void das_varindex_prndir(bool bFastLast)
Set index printing direction.