das2C
das core C utilities (v3)
Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
1/* Copyright (C) 2017-2024 Chris Piker <chris-piker@uiowa.edu>
2 *
3 * This file is part of das2C, the Core Das2 C Library.
4 *
5 * Das2C 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 * Das2C 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 das2C; if not, see <http://www.gnu.org/licenses/>.
16 */
17
20/* I'm sure this kind of buffer has been done many times before, but I haven't
21 * seen an implementation that handles arbitrarily ragged arrays. Thanks to
22 * Larry Granroth, and Matt Leifert for helping me talk through the issues
23 * around this class.
24 * -cwp
25 */
26
27#ifndef _das_array_h_
28#define _das_array_h_
29
30#include <stdarg.h>
31#include <stddef.h>
32#include <stdint.h>
33#include <stdbool.h>
34
35#include <das3/value.h>
36#include <das3/units.h>
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
43#define ARYIDX_MAX 8
44
46#define ARYIDX_LAST {-1,-1,-1,-1,-1,-1,-1,-1}
47
52#define ARYIDX_UNBOUND 0
53
55#define VARIDX_RAGGED -1
56
59#ifndef _das_array_c_
60extern const char g_sIdxLower[ARYIDX_MAX];
61extern const char g_sIdxUpper[ARYIDX_MAX];
62#endif
63
83 ptrdiff_t* pShape, int iFirstInternal, int nShapeLen, char* sBuf, int nBufLen
84);
85
87char* das_idx_prn(int nRank, ptrdiff_t* pIdx, size_t uLen, char* sBuf);
88
89
90#define RANK_1(I) 1, (size_t[1]){I}
91#define RANK_2(I, J) 2, (size_t[2]){I, J}
92#define RANK_3(I, J, K) 3, (size_t[3]){I, J, K}
93#define RANK_4(I, J, K, L) 4, (size_t[4]){I, J, K, L}
94#define RANK_5(I, J, K, L, M) 5, (size_t[5]){I, J, K, L, M}
95#define RANK_6(I, J, K, L, M, N) 6, (size_t[6]){I, J, K, L, M, N}
96#define RANK_7(I, J, K, L, M, N, O) 7, (size_t[7]){I, J, K, L, M, N, O}
97#define RANK_8(I, J, K, L, M, N, O, P) 8, (size_t[8]){I, J, K, L, M, N, O, P}
98
99#define DIM0 0, (NULL)
100#define DIM1_AT(I) 1, (ptrdiff_t[1]){I}
101#define DIM2_AT(I,J) 2, (ptrdiff_t[2]){I,J}
102#define DIM3_AT(I,J,K) 3, (ptrdiff_t[3]){I,J,K}
103#define DIM4_AT(I,J,K,L) 4, (ptrdiff_t[4]){I,J,K,L}
104#define DIM5_AT(I,J,K,L,M) 5, (ptrdiff_t[5]){I,J,K,L,M}
105#define DIM6_AT(I,J,K,L,M,N) 6, (ptrdiff_t[6]){I,J,K,L,M,N}
106#define DIM7_AT(I,J,K,L,M,N,O) 7, (ptrdiff_t[7]){I,J,K,L,M,N,O}
107
108#define IDX0(I) (ptrdiff_t[1]){I}
109#define IDX1(I,J) (ptrdiff_t[2]){I,J}
110#define IDX2(I,J,K) (ptrdiff_t[3]){I,J,K}
111#define IDX3(I,J,K,L) (ptrdiff_t[4]){I,J,K,L}
112#define IDX4(I,J,K,L,M) (ptrdiff_t[5]){I,J,K,L,M}
113#define IDX5(I,J,K,L,M,N) (ptrdiff_t[6]){I,J,K,L,M,N}
114#define IDX6(I,J,K,L,M,N,O) (ptrdiff_t[7]){I,J,K,L,M,N,O}
115#define IDX7(I,J,K,L,M,N,O,P) (ptrdiff_t[8]){I,J,K,L,M,N,O,P}
116
117#define DIM1 1
118#define DIM2 2
119#define DIM3 3
120#define DIM4 4
121#define DIM5 5
122#define DIM6 6
123#define DIM7 7
124
125#define RNG_1(i,I) 1, (ptrdiff_t[1]){i}, (ptrdiff_t[1]){I}
126#define RNG_2(i,I,j,J) 2, (ptrdiff_t[2]){i,j}, (ptrdiff_t[2]){I,J}
127#define RNG_3(i,I,j,J,k,K) 3, (ptrdiff_t[3]){i,j,k}, (ptrdiff_t[3]){I,J,K}
128#define RNG_4(i,I,j,J,k,K,l,L) 4, (ptrdiff_t[4]){i,j,k,l}, (ptrdiff_t[4]){I,J,K,L}
129#define RNG_5(i,I,j,J,k,K,l,L,m,M) 5, (ptrdiff_t[5]){i,j,k,l,m}, (ptrdiff_t[5]){I,J,K,L,M}
130#define RNG_6(i,I,j,J,k,K,l,L,m,M,n,N) 6, (ptrdiff_t[6]){i,j,k,l,m,n}, (ptrdiff_t[6]){I,J,K,L,M,N}
131#define RNG_7(i,I,j,J,k,K,l,L,m,M,n,N,o,O) 7, (ptrdiff_t[7]){i,j,k,l,m,n,o}, (ptrdiff_t[7]){I,J,K,L,M,N,O}
132
160 int nRngRank, const ptrdiff_t* pMin, const ptrdiff_t* pMax, size_t* pShape
161);
162
165/* Higher dimensions are handled by index buffers. These are the elements for
166 * the index buffers */
167typedef struct child_info_t{
168 ptrdiff_t nOffset; /* Start of child elements data in child buffer */
169 size_t uCount; /* The count child elements in child buffer */
170}das_idx_info;
171
172typedef struct dyna_buf{
173
174 ubyte* pBuf; /* The beginning a continuous buffer uSize long */
175 ubyte* pHead; /* The beginning of valid values in the buffer */
176 /*ubyte* pWrite;*/ /* The beginning of the append point for the buffer */
177 size_t uSize; /* The amount of space in the backing buffer */
178 size_t uValid; /* The number of valid elements in this buffer */
179 size_t uElemSz; /* The number of bytes occupied by each element */
180
181 ubyte* pFill; /* Pointer to fill value buffer */
182 ubyte fillBuf[sizeof(das_idx_info)]; /* Storage for short fill items */
183
184 size_t uChunkSz; /* Alloc helper, if set, allocate in even chunks
185 * of this size. */
186 size_t uShape; /* Qube helper, how many items in this array per
187 * parent item, 0 means ragged */
188 das_val_type etype; /* The element type */
189
190 bool bRollParent; /* The end mark. If set, adding data to the array
191 * will clear the end mark and trigger creation of
192 * a new parent. If this is set on all indexes
193 * attempting to add data will fail */
194
195 bool bKeepMem; /* If true memory will not be deleted when the
196 * buffer is deleted */
197
198} DynaBuf;
199
271typedef struct das_array {
272 char sId[DAS_MAX_ID_BUFSZ]; /* A text identifier of this instance of the array */
273 int nRank; /* The number of index dimensions for the array */
274 das_idx_info* pIdx0; /* top lever container, may not point here */
275
276 /* bool bTopOwned;*/ /* Same as pIdx0 == &index0 */
277 das_idx_info index0; /* Storage for element 0 of buffer 0, if needed */
278
279 /* Pointers to item arrays. One array is needed for each dimension,
280 * these may point to internal locations if the array is owned */
281 DynaBuf* pBufs[ARYIDX_MAX];
282
283 /* bool bOwned[16]; */ /* Same as pBufs[i] == &(bufs[i]) */
284 DynaBuf bufs[ARYIDX_MAX]; /* Storage for dynamic buffers, if needed */
285
286 /* Current compare function, set automatically if a known type is used,
287 * otherwise user needs to supply their own */
288 int (*compare)(const ubyte* vpFirst, const ubyte* vpSecond);
289
290 /* Where to send/read data when streaming */
291 int nSrcPktId;
292 size_t uStartItem;
293 size_t uItems;
294
295 /* Since so many datasets and functions can reference the same array,
296 * a reference count is needed to make sure they aren't deleted if
297 * still in use. */
298 int refcount;
299
300 /* Used to notify the owner of my memory that I'm not using it anymore. */
301 struct das_array* pMemOwner;
302
303 unsigned int uFlags; /* Store flags indicating intended use */
304
305 das_units units;
306
314 void* pUser;
315
316} DasAry;
317
394 const char* id, das_val_type et, size_t sz_each, const ubyte* fill,
395 int rank, size_t* shape, das_units units
396);
397
398
404DAS_API bool DasAry_init(
405 DasAry* pThis, const char* id, das_val_type et, size_t sz_each,
406 const ubyte* fill, int rank, size_t* shape, das_units units
407);
408
430DAS_API DasAry* new_DasPtrAry(const char* sType, int rank, size_t* shape);
431
435#define D2ARY_AS_SUBSEQ 0x00000001
436
441#define D2ARY_FILL_TERM 0x00000003
442
447#define D2ARY_AS_STRING 0x00000007
448
474DAS_API unsigned int DasAry_setUsage(DasAry* pThis, unsigned int uFlags);
475
479DAS_API unsigned int DasAry_getUsage(DasAry* pThis);
480
488DAS_API int inc_DasAry(DasAry* pThis);
489
493DAS_API int ref_DasAry(const DasAry* pThis);
494
504DAS_API void dec_DasAry(DasAry* pThis);
505
508DAS_API void DasAry_deInit(DasAry* pThis);
509
510
550 DasAry* pThis, size_t* pLen, size_t* pOffset
551);
552
561DAS_API bool DasAry_ownsElements(const DasAry* pThis);
562
569DAS_API const char* DasAry_id(const DasAry* pThis);
570
577DAS_API void DasAry_setId(DasAry* pThis, const char* sId);
578
585#define DasAry_rank(pThis) pThis->nRank
586
587/* DAS_API int DasAry_rank(const DasAry* pThis); */
588
592DAS_API das_units DasAry_units(const DasAry* pThis);
593
604DAS_API das_val_type DasAry_valType(const DasAry* pThis);
605
609DAS_API const char* DasAry_valTypeStr(const DasAry* pThis);
610
619DAS_API char* DasAry_toStr(const DasAry* pThis, char* sInfo, size_t uLen);
620
628DAS_API size_t DasAry_size(const DasAry* pThis);
629
636DAS_API size_t DasAry_valSize(const DasAry* pThis);
637
638
659DAS_API size_t DasAry_memUsed(const DasAry* pThis);
660
676DAS_API size_t DasAry_memOwned(const DasAry* pThis);
677
678
691DAS_API size_t DasAry_memIndexed(const DasAry* pThis);
692
693
735DAS_API size_t DasAry_lengthIn(const DasAry* pThis, int nIdx, ptrdiff_t* pLoc);
736
754DAS_API size_t DasAry_itemsIn(const DasAry* pThis, int nIdx, ptrdiff_t* pLoc);
755
775DAS_API int DasAry_shape(const DasAry* pThis, ptrdiff_t* pShape);
776
777
801DAS_API int DasAry_stride(
802 const DasAry* pThis, ptrdiff_t* pShape, ptrdiff_t* pStride
803);
804
809DAS_API const ubyte* DasAry_getFill(const DasAry* pThis);
810
827DAS_API bool DasAry_setFill(DasAry* pThis, das_val_type vt, const ubyte* pFill);
828
829
852DAS_API bool DasAry_validAt(const DasAry* pThis, ptrdiff_t* pLoc);
853
885DAS_API const ubyte* DasAry_getAt(const DasAry* pThis, das_val_type et, ptrdiff_t* pLoc);
886
889#define DasAry_getFloatAt(pThis, pLoc) *((float*)(DasAry_getAt(pThis, vtFloat, pLoc)))
892#define DasAry_getDoubleAt(pThis, pLoc) *((double*)(DasAry_getAt(pThis, vtDouble, pLoc)))
895#define DasAry_getByteAt(pThis, pLoc) *((ubyte*)(DasAry_getAt(pThis, vtUByte, pLoc)))
898#define DasAry_getUShortAt(pThis, pLoc) *((uint16_t*)(DasAry_getAt(pThis, etUint16, pLoc)))
901#define DasAry_getShortAt(pThis, pLoc) *((int16_t*)(DasAry_getAt(pThis, etInt16, pLoc)))
904#define DasAry_getIntAt(pThis, pLoc) *((int32_t*)(DasAry_getAt(pThis, etInt32, pLoc)))
907#define DasAry_getLongAt(pThis, pLoc) *((int64_t*)(DasAry_getAt(pThis, etInt64, pLoc)))
910#define DasAry_getTimeAt(pThis, pLoc) *((das_time*)(DasAry_getAt(pThis, vtTime, pLoc)))
913#define DasAry_getTextAt(pThis, pLoc) *((char**)(DasAry_getAt(pThis, vtText, pLoc)))
914
916#define DasAry_getPtrAt(pThis, pLoc) *((void**)(DasAry_getAt(pThis, vtUnknown, pLoc)))
917
918
934DAS_API bool DasAry_putAt(DasAry* pThis, ptrdiff_t* pStart, const ubyte* pVals, size_t uVals);
935
978DAS_API const ubyte* DasAry_getIn(
979 const DasAry* pThis, das_val_type et, int nDim, ptrdiff_t* pLoc, size_t* pCount
980);
981
989DAS_API ubyte* DasAry_getBuf(
990 DasAry* pThis, das_val_type et, int nDim, ptrdiff_t* pLoc, size_t* pCount
991);
992
995#define DasAry_getFloatsIn(T, ...) (const float*) DasAry_getIn(T, vtFloat, __VA_ARGS__)
996
999#define DasAry_getDoublesIn(T, ...) (const double*) DasAry_getIn(T, vtDouble, __VA_ARGS__)
1000
1003#define DasAry_getCharsIn(T, ...) (const char*) DasAry_getIn(T, vtUByte, __VA_ARGS__)
1004
1007#define DasAry_getBytesIn(T, ...) (const ubyte*) DasAry_getIn(T, vtUByte, __VA_ARGS__)
1008
1011#define DasAry_getUShortsIn(T, ...) (const uint16_t*) DasAry_getIn(T, vtUShort, __VA_ARGS__)
1012
1015#define DasAry_getShortsIn(T, ...) (const int16_t*) DasAry_getIn(T, vtShort, __VA_ARGS__)
1016
1019#define DasAry_getIntsIn(T, ...) (const int32_t*) DasAry_getIn(T, vtInt, __VA_ARGS__)
1020
1023#define DasAry_getLongsIn(T, ...) (const int64_t*) DasAry_getIn(T, vtLong, __VA_ARGS__)
1024
1027#define DasAry_getTimesIn(T, ...) (const das_time*) DasAry_getIn(T, vtTime, __VA_ARGS__)
1028
1031#define DasAry_getTextIn(T, ...) (const char**) DasAry_getIn(T, vtText, __VA_ARGS__)
1032
1053DAS_API const ubyte* DasAry_getAllVals(
1054 const DasAry* pThis, size_t* pElSize, size_t* pElements
1055);
1056
1091 DasAry* pThis, const char* id, int nIndices, ptrdiff_t* pLoc
1092);
1093
1125DAS_API size_t DasAry_qubeIn(DasAry* pThis, int iRecDim);
1126
1148DAS_API ubyte* DasAry_append(DasAry* pThis, const ubyte* pVals, size_t uCount);
1149
1177DAS_API void DasAry_markEnd(DasAry* pThis, int iDim);
1178
1179/* Remove some number of records in a given index.
1180 *
1181 * This does not actually delete any data it just removes references to it.
1182 * Thus any subsets of this array will still reference the existing values.
1183 *
1184 * @see rmLastRec() for the definition of a record
1185 */
1186/* size_t DasAry_rmHead(Array* pThis, size_t uCount, int nIndex);*/
1187
1188/* Remove N records from the end of the array
1189 *
1190 * For rank 1 arrays, a 'record' is just a single value, for higher rank
1191 * arrays a record is the number of values represented by a change in the
1192 * first index. For example, for the array:
1193 * @code
1194 * a[][10][4]
1195 * @endcode
1196 * calling
1197 * @code
1198 * rmTail(pArray, 2)
1199 * @endcode
1200 * will result in 80 individual values being remove from the array.
1201 *
1202 * @param pThis the array to change
1203 * @param uRecs the number of records to remove. If uRecs is greater than
1204 * the shape of the first index, everything is removed and an error
1205 * is thrown.
1206 * @returns The new size of the array
1207 * @memberof DasAry
1208 */
1209/* size_t DasAry_rmTail(Array* pThis, size_t uRecs); */
1210
1221DAS_API size_t DasAry_clear(DasAry* pThis);
1222
1231DAS_API int DasAry_cmp(DasAry* pThis, const ubyte* vpFirst, const ubyte* vpSecond );
1232
1243DAS_API void DasAry_setSrc(DasAry* pThis, int nPktId, size_t uStartItem, size_t uItems);
1244
1245
1246#ifdef __cplusplus
1247}
1248#endif
1249
1250#endif /* _das_array_h_ */
DAS_API const ubyte * DasAry_getFill(const DasAry *pThis)
Return the fill value for this array.
DAS_API void DasAry_deInit(DasAry *pThis)
Similar to dec_dasAry, but for stack objects.
char * das_shape_prnRng(ptrdiff_t *pShape, int iFirstInternal, int nShapeLen, char *sBuf, int nBufLen)
Print shape information using symbols i,j,k etc for index positions.
DAS_API size_t DasAry_memUsed(const DasAry *pThis)
Get number of bytes currently used for data values and associated indexes by this dynamic array.
char * das_idx_prn(int nRank, ptrdiff_t *pIdx, size_t uLen, char *sBuf)
print a generic set of ptrdiff_t values to a character array
const char g_sIdxLower[ARYIDX_MAX]
Index letters i,j,k,... for printing external index positions, and I,J,K,... for internal (item) inde...
DAS_API ubyte * DasAry_getBuf(DasAry *pThis, das_val_type et, int nDim, ptrdiff_t *pLoc, size_t *pCount)
This is the writable pointer version of DasAry_getIn.
DAS_API DasAry * new_DasPtrAry(const char *sType, int rank, size_t *shape)
A convenience wrapper for storing arrays of pointers.
DAS_API size_t DasAry_memOwned(const DasAry *pThis)
Get the number of bytes currently owned for use in storing data values and indexes.
DAS_API bool DasAry_init(DasAry *pThis, const char *id, das_val_type et, size_t sz_each, const ubyte *fill, int rank, size_t *shape, das_units units)
Same as new_DasAry, but for initializing stack objects instead of making new heap objects.
DAS_API const ubyte * DasAry_getAllVals(const DasAry *pThis, size_t *pElSize, size_t *pElements)
Forget all the fancy indexing, just get a pointer to all the elements.
#define ARYIDX_MAX
The maximum number of array indices in das3.
Definition array.h:43
DAS_API size_t DasAry_memIndexed(const DasAry *pThis)
Get the number of bytes needed to store these values and their associated indexes.
int das_rng2shape(int nRngRank, const ptrdiff_t *pMin, const ptrdiff_t *pMax, size_t *pShape)
Calculate a strided array shape from a min/max range.
const char * das_units
Handle SI and other units, with accommodations for Epoch systems, from units.h.
Definition units.h:144
das_val_type
Enumeration of types stored in Das Array (DasAry) objects from value.h.
Definition value.h:81
Dynamic recursive ragged arrays.
Definition array.h:271
DAS_API int inc_DasAry(DasAry *pThis)
Increment the count of objects using this Das Array.
DAS_API bool DasAry_ownsElements(const DasAry *pThis)
Does this array own it's own memory?
DAS_API char * DasAry_toStr(const DasAry *pThis, char *sInfo, size_t uLen)
Get a informational string representing the array.
DAS_API const char * DasAry_valTypeStr(const DasAry *pThis)
Get the type of value stored in the array as a text string.
DAS_API size_t DasAry_valSize(const DasAry *pThis)
Get the size in bytes of each element stored in the das array.
DAS_API int DasAry_shape(const DasAry *pThis, ptrdiff_t *pShape)
Return current valid ranges for this array indices.
DAS_API bool DasAry_putAt(DasAry *pThis, ptrdiff_t *pStart, const ubyte *pVals, size_t uVals)
Set values starting at a complete index.
DAS_API unsigned int DasAry_setUsage(DasAry *pThis, unsigned int uFlags)
Set usage flags to assist arbitrary consumers understand how to use this array.
DAS_API void DasAry_setSrc(DasAry *pThis, int nPktId, size_t uStartItem, size_t uItems)
Record which packets contain data destine for this array.
DAS_API int DasAry_stride(const DasAry *pThis, ptrdiff_t *pShape, ptrdiff_t *pStride)
Return the strides used for offset calculations.
DAS_API const ubyte * DasAry_getIn(const DasAry *pThis, das_val_type et, int nDim, ptrdiff_t *pLoc, size_t *pCount)
Get a pointer to the elements contained by a partial index.
DAS_API const char * DasAry_id(const DasAry *pThis)
Get the name of an array.
DAS_API size_t DasAry_itemsIn(const DasAry *pThis, int nIdx, ptrdiff_t *pLoc)
Count the logical items in a subset of the array.
DAS_API size_t DasAry_size(const DasAry *pThis)
Get the total number of data values in an array, regardless of it's shape.
DAS_API int DasAry_cmp(DasAry *pThis, const ubyte *vpFirst, const ubyte *vpSecond)
Compare two items of the type in the array.
DAS_API das_val_type DasAry_valType(const DasAry *pThis)
Get the type of value stored in the array if knownh.
DAS_API size_t DasAry_clear(DasAry *pThis)
Clear all values from the array.
DAS_API ubyte * DasAry_disownElements(DasAry *pThis, size_t *pLen, size_t *pOffset)
Take ownership of array element memory.
DAS_API ubyte * DasAry_append(DasAry *pThis, const ubyte *pVals, size_t uCount)
Append some number of items to the end of the array.
DAS_API void DasAry_markEnd(DasAry *pThis, int iDim)
Mark a ragged dimension as finished.
DAS_API bool DasAry_validAt(const DasAry *pThis, ptrdiff_t *pLoc)
Is a valid item located at a complete index.
DAS_API size_t DasAry_lengthIn(const DasAry *pThis, int nIdx, ptrdiff_t *pLoc)
Return the current max value + 1 for any index.
DAS_API const ubyte * DasAry_getAt(const DasAry *pThis, das_val_type et, ptrdiff_t *pLoc)
Get a pointer to an element at a complete index.
DAS_API das_units DasAry_units(const DasAry *pThis)
Get the units for the values in the array.
DAS_API size_t DasAry_qubeIn(DasAry *pThis, int iRecDim)
Use fill values to make sure the last subset in a dimension is a QUBE.
DAS_API DasAry * new_DasAry(const char *id, das_val_type et, size_t sz_each, const ubyte *fill, int rank, size_t *shape, das_units units)
Creates a new dynamic array buffer.
DAS_API unsigned int DasAry_getUsage(DasAry *pThis)
Returns the usage flags for this array.
DAS_API bool DasAry_setFill(DasAry *pThis, das_val_type vt, const ubyte *pFill)
Change the fill value for this array.
DAS_API void dec_DasAry(DasAry *pThis)
Maybe remove the array.
DAS_API int ref_DasAry(const DasAry *pThis)
Return the reference count of objects using this array.
DAS_API void DasAry_setId(DasAry *pThis, const char *sId)
Set the name of an array.
DAS_API DasAry * DasAry_subSetIn(DasAry *pThis, const char *id, int nIndices, ptrdiff_t *pLoc)
Get a lower rank array that is a sub-set of the current array.
void * pUser
User data pointer.
Definition array.h:314
Defines units used for items in the stream, most notably time units that reference an epoch and a ste...
#define DAS_MAX_ID_BUFSZ
The size of an char buffer large enough to hold valid object IDs.
Definition util.h:349
A generic value type for use in arrays, datums and variables.