libdas2
das2 core C utilities
|
#include <das2/array.h>
Dynamic recursive ragged arrays.
This class maps any number of indices, (i,j,k...) to elements stored into a continuous array. Any or all particular index in the array can be ragged, if desired, though typically for data streams only the first index has an arbitrary length.
The backing buffers for the array grows if needed as new elements are appended. Individual elements may be arbitrary composite types, though extra capabilites are provided for a handful of know types.
Public Member Functions | |
DasAry * | new_DasAry (const char *id, das_val_type et, size_t sz_each, const void *fill, int rank, size_t *shape, das_units units) |
Creates a new dynamic array buffer. More... | |
unsigned int | DasAry_setUsage (DasAry *pThis, unsigned int uFlags) |
Set usage flags to assist arbitrary consumers understand how to use this array. More... | |
unsigned int | DasAry_getUsage (DasAry *pThis) |
Returns the usage flags for this array. | |
int | inc_DasAry (DasAry *pThis) |
Increment the count of objects using this Das Array. More... | |
int | ref_DasAry (const DasAry *pThis) |
Return the reference count of objects using this array. | |
void | dec_DasAry (DasAry *pThis) |
Maybe remove the array. More... | |
byte * | DasAry_disownElements (DasAry *pThis, size_t *pLen) |
Remove ownership of the underlying element array from this DasArray. More... | |
const char * | DasAry_id (const DasAry *pThis) |
Get the name of an array. More... | |
int | DasAry_rank (const DasAry *pThis) |
Get the number of dimensions in an array. More... | |
das_units | DasAry_units (const DasAry *pThis) |
Get the units for the values in the array. | |
enum das_val_type | DasAry_valType (const DasAry *pThis) |
Get the type of value stored in the array if known. More... | |
const char * | DasAry_valTypeStr (const DasAry *pThis) |
Get the type of value stored in the array as a text string. | |
char * | DasAry_toStr (const DasAry *pThis, char *sInfo, size_t uLen) |
Get a informational string representing the array. More... | |
size_t | DasAry_size (const DasAry *pThis) |
Get the total number of data values in an array, regardless of it's shape. More... | |
size_t | DasAry_valSize (const DasAry *pThis) |
Get the size in bytes of each element stored in the das array. More... | |
size_t | DasAry_lengthIn (const DasAry *pThis, int nIdx, ptrdiff_t *pLoc) |
Return the current max value + 1 for any index. More... | |
int | DasAry_shape (const DasAry *pThis, ptrdiff_t *pShape) |
Return current valid ranges for this array indices. More... | |
bool | DasAry_validAt (const DasAry *pThis, ptrdiff_t *pLoc) |
Is a valid item located at a complete index. More... | |
const void * | DasAry_getAt (const DasAry *pThis, das_val_type et, ptrdiff_t *pLoc) |
Get a pointer to an element at a complete index. More... | |
#define | DasAry_getFloatAt(pThis, pLoc) *((float*)(DasAry_getAt(pThis, vtFloat, pLoc))) |
Wrapper around Array_get for IEEE-754 binary32 (float) More... | |
#define | DasAry_getDoubleAt(pThis, pLoc) *((double*)(DasAry_getAt(pThis, vtDouble, pLoc))) |
Wrapper around Array_get for IEEE-754 binary64 (double) More... | |
#define | DasAry_getByteAt(pThis, pLoc) *((byte*)(DasAry_getAt(pThis, vtByte, pLoc))) |
Wrapper around Array_get for unsigned bytes. More... | |
#define | DasAry_getUShortAt(pThis, pLoc) *((uint16_t*)(DasAry_getAt(pThis, etUint16, pLoc))) |
Wrapper around Array_get for unsigned 16-bit integers. More... | |
#define | DasAry_getShortAt(pThis, pLoc) *((int16_t*)(DasAry_getAt(pThis, etInt16, pLoc))) |
Wrapper around Array_get for signed 16-bit integers. More... | |
#define | DasAry_getIntAt(pThis, pLoc) *((int32_t*)(DasAry_getAt(pThis, etInt32, pLoc))) |
Wrapper around Array_get for 32-bit integers. More... | |
#define | DasAry_getLongAt(pThis, pLoc) *((int64_t*)(DasAry_getAt(pThis, etInt64, pLoc))) |
Wrapper around Array_get for signed 64-bit integers. More... | |
#define | DasAry_getTimeAt(pThis, pLoc) *((das_time*)(DasAry_getAt(pThis, vtTime, pLoc))) |
Wrapper around Array_get for das_time_t structures. More... | |
#define | DasAry_getTextAt(pThis, pLoc) *((char**)(DasAry_getAt(pThis, vtText, pLoc))) |
Wrapper around Array_get for pointers to null-terminated strings. More... | |
bool | DasAry_putAt (DasAry *pThis, ptrdiff_t *pStart, const void *pVals, size_t uVals) |
Set values starting at a complete index. More... | |
const void * | 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. More... | |
#define | DasAry_getFloatsIn(T,...) (const float*) DasAry_getIn(T, vtFloat, __VA_ARGS__) |
A wrapper around Array_getIn that casts the output and preforms type checking. More... | |
#define | DasAry_getDoublesIn(T,...) (const double*) DasAry_getIn(T, vtDouble, __VA_ARGS__) |
A wrapper around Array_getIn that casts the output and preforms type checking. More... | |
#define | DasAry_getBytesIn(T,...) (const byte*) DasAry_getIn(T, vtByte, __VA_ARGS__) |
A wrapper around Array_getIn that casts the output and preforms type checking. More... | |
#define | DasAry_getUShortsIn(T,...) (const uint16_t*) DasAry_getIn(T, vtUShort, __VA_ARGS__) |
A wrapper around Array_getIn that casts the output and preforms type checking. More... | |
#define | DasAry_getShortsIn(T,...) (const int16_t*) DasAry_getIn(T, vtShort, __VA_ARGS__) |
A wrapper around Array_getIn that casts the output and preforms type checking. More... | |
#define | DasAry_getLongsIn(T,...) (const int64_t*) DasAry_getIn(T, vtLong, __VA_ARGS__) |
A wrapper around Array_getIn that casts the output and preforms type checking. More... | |
#define | DasAry_getTimesIn(T,...) (const das_time*) DasAry_getIn(T, vtTime, __VA_ARGS__) |
A wrapper around Array_getIn that casts the output and preforms type checking. More... | |
#define | DasAry_getTextIn(T,...) (const char**) DasAry_getIn(T, vtText, __VA_ARGS__) |
A wrapper around Array_getIn that casts the output and preforms type checking. More... | |
DasAry * | DasAry_subSetIn (const DasAry *pThis, const char *id, int nIndices, ptrdiff_t *pLoc) |
Get a lower rank array that is a sub-set of the current array. More... | |
size_t | DasAry_qubeIn (DasAry *pThis, int iRecDim) |
Use fill values to make sure the last subset in a dimension is a QUBE. More... | |
bool | DasAry_append (DasAry *pThis, const void *pVals, size_t uCount) |
Append some number of items to the end of the array. More... | |
void | DasAry_markEnd (DasAry *pThis, int iDim) |
Mark a ragged dimension as finished. More... | |
size_t | DasAry_clear (DasAry *pThis) |
Clear all values from the array. More... | |
int | DasAry_cmp (DasAry *pThis, const void *vpFirst, const void *vpSecond) |
Compare two items of the type in the array. More... | |
void | DasAry_setSrc (DasAry *pThis, int nPktId, size_t uStartItem, size_t uItems) |
Record which packets contain data destine for this array. More... | |
DasAry * new_DasAry | ( | const char * | id, |
das_val_type | et, | ||
size_t | sz_each, | ||
const void * | fill, | ||
int | rank, | ||
size_t * | shape, | ||
das_units | units | ||
) |
Creates a new dynamic array buffer.
id | The name of this array. This may be used as a lookup key if an array pointer is stored in a lookup table. |
et | The element type, for arbitrary element storage use UNKNOWN, specific types are provided in enum element_type. |
sz_each | The size of each element in the array in bytes. This parameter is only used when the element type is unknown. |
fill | A pointer to the value for initializing all empty array records. The value should be size_each bytes long. These bytes are copied into the array and this pointer need not remain valid after the constructor call. For unknown types (et = etUnknown) this is a required, non NULL parameter. For known types you can use NULL to automatically set fill to the following values, based on the element_type: |
pFill can point to stack memory as the fill bytes are copied in.
rank | The number of dimensions in the array. This sets the number of arguments needed in the get() function call. To make your code easier to read, the defines RANK_1, RANK_2, ... RANK_16 are provided. |
shape | The initial shape of the array. One integer is needed here for each dimension in the array. Use the value 0 to set a dimension to be unbounded. Multi-dimension arrays used to hold an arbitrarily long set of records are typically only unbounded in the first index, though see the example in Array_markEnd for handling multiply ragged arrays. |
unsigned int DasAry_setUsage | ( | DasAry * | pThis, |
unsigned int | uFlags | ||
) |
Set usage flags to assist arbitrary consumers understand how to use this array.
Das2 arrays can store co-opertive flags, these do not change the array API but do indicate how the array should be used. The following two usage flags are currently defined:
pThis | |
uFlags | A flag value to set |
int inc_DasAry | ( | DasAry * | pThis | ) |
Increment the count of objects using this Das Array.
void dec_DasAry | ( | DasAry * | pThis | ) |
Maybe remove the array.
Calling this function decrements the reference count for the array. If the count reaches zero all backing buffers (owned by this array) are deleted.
byte * DasAry_disownElements | ( | DasAry * | pThis, |
size_t * | pLen | ||
) |
Remove ownership of the underlying element array from this DasArray.
Internally all Das Array elements are stored in a single continuous 1-D buffer. When the Das Array is deleted, this buffer is removed as well, if it's owned by the das array.
Implementation detail, the index tracking arrays may still be owned by the Array object and will be deleted when the overall structure is deleted
pThis | A pointer to a das array structure |
pLen | A pointer to a variable to hold the length of the element array in elements (not bytes). Use this value to distingu |
const char * DasAry_id | ( | const DasAry * | pThis | ) |
Get the name of an array.
pThis | A constant pointer to this array structure |
int DasAry_rank | ( | const DasAry * | pThis | ) |
Get the number of dimensions in an array.
pThis | a pointer to a das array structure |
enum das_val_type DasAry_valType | ( | const DasAry * | pThis | ) |
Get the type of value stored in the array if known.
This function is used by dataset objects to know how to cast pointers to different data array values.
pThis | A constant pointer to this array structure |
char * DasAry_toStr | ( | const DasAry * | pThis, |
char * | sInfo, | ||
size_t | uLen | ||
) |
Get a informational string representing the array.
pThis | The Array in question |
sInfo | pointer to the destination to hold the info string |
uLen | the length of the sInfo buffer |
size_t DasAry_size | ( | const DasAry * | pThis | ) |
Get the total number of data values in an array, regardless of it's shape.
pThis | A constant pointer to this array structure |
size_t DasAry_valSize | ( | const DasAry * | pThis | ) |
Get the size in bytes of each element stored in the das array.
pThis | The Array in question |
size_t DasAry_lengthIn | ( | const DasAry * | pThis, |
int | nIdx, | ||
ptrdiff_t * | pLoc | ||
) |
Return the current max value + 1 for any index.
This is a more general version of Array_shape that works for both cubic arrays and those with ragged dimensions. For any index to be inspected, the value of all previous indices must be given. This is less confusing then it sounds, see the example below
pThis | A pointer to an array object |
nIdx | The number of location indices, should be less than the rank in order to get the length of a range of values. The macros DIM0, DIM1_AT, DIM2_AT, etc. are provided which combine this argument and the one below to make calling code more readable, see the example below. |
pLoc | A list of the values for previous indices. The macros DIM1, DIM2_AT, DIM3_AT etc. are provided which combine this argument with the one above to make calling code more readable, see the example below. |
int DasAry_shape | ( | const DasAry * | pThis, |
ptrdiff_t * | pShape | ||
) |
Return current valid ranges for this array indices.
pThis | pointer to an array object | |
[out] | pShape | pointer to an array to received the current number of entries in each dimension, should be at least RANK in length. Each element of the output array will be one of the following. |
bool DasAry_validAt | ( | const DasAry * | pThis, |
ptrdiff_t * | pLoc | ||
) |
Is a valid item located at a complete index.
pThis | A pointer to the array |
pLoc | An array of indices of length RANK. A rank 1 Das Array requires two indices to access an element, a rank 2 requires three, etc. The macros LOC_1, LOC_2, etc have been provided to make code more readable. See the example below. |
const void * DasAry_getAt | ( | const DasAry * | pThis, |
das_val_type | et, | ||
ptrdiff_t * | pLoc | ||
) |
Get a pointer to an element at a complete index.
For type safety the macros Array_getFloat, Array_getDouble, etc have been provided and should be used instead of the base function here.
pThis | A pointer to the array |
et | The element type pointer expected at the given location, this is used by the type checking macros Array_getFloat and friends. |
pLoc | An array of indices of length RANK. A rank 2 Das Array requires two indices to access an element, a rank 3 requires three, etc. The macros IDX1, IDX2, IDX3, etc have been provided to make code more readable. See the example below. |
bool DasAry_putAt | ( | DasAry * | pThis, |
ptrdiff_t * | pStart, | ||
const void * | pVals, | ||
size_t | uVals | ||
) |
Set values starting at a complete index.
Note, this will not expand the size of the array. Use the function append() to automatically grow the array to store the desired number of items.
pThis | A pointer to the array |
pStart | The complete index to the starting point to write values, use the macros IDX1, IDX2, etc. to make calling code more readable |
pVals | The values to write |
uVals | The number of values to write |
const void * 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.
pThis | A Array containing the data of interest |
et | The expected type of element to be returned. This is used by the type safe macros Array_getDoubleAt, Array_getTextAt, etc. |
nDim | The number of location indices, should be less than the rank in order to get the length of a range of values. The macros DIM0, DIM1_AT, DIM2_AT, etc. are provided which combine this argument and the one below to make calling code more readable, see the example below. |
pLoc | An array of location indices, nIndices long. Use the macros DIM0, DIM1_AT, DIM2_AT, etc. for cleaner code. |
pCount | A pointer to a variable to hold the number of elements under the given index. If nIndices is equal to the array rank the returned value will be at most 1. |
Get a lower rank array that is a sub-set of the current array.
For some given number of indices, produce a sub-array. This is similar to Array_getAt but produces a whole new Array object whose data are provided stored in a separate Array.
pThis | |
id | A identifing name for the new sub-array |
nIndices | The number of location indices, should be less than the rank in order to get a range of values. Use the macros DIM0, DIM1_AT, DIM2_AT, etc. for cleaner code. |
pLoc | An array of location indices, nIndices long. Use the macros DIM0, DIM1_AT, DIM2_AT, etc. for cleaner code. |
size_t DasAry_qubeIn | ( | DasAry * | pThis, |
int | iRecDim | ||
) |
Use fill values to make sure the last subset in a dimension is a QUBE.
pThis | the array |
iRecDim | The dimension to qube, typically the last dimension in the array, dimensions are numbered starting from 0. Use the macro's DIM1, DIM2, etc. to make the code more readable. Dimension 0 can't be marked as ended and the macro DIM0 will not work here. |
bool DasAry_append | ( | DasAry * | pThis, |
const void * | pVals, | ||
size_t | uCount | ||
) |
Append some number of items to the end of the array.
This works as you would expect for all arrays that are only ragged in the 0th dimension.
pThis | The array which should copy in the new values. |
pVals | A constant pointer to values to add |
uCount | The number of values to add |
void DasAry_markEnd | ( | DasAry * | pThis, |
int | iDim | ||
) |
Mark a ragged dimension as finished.
pThis | The das array |
iDim | The dimension which should have it's index rolled back to zero on the next insert. Marking the end of a low index (say 1) automatically marks the end of any higher indices (ex 2,3). * // Read in lines of text into an array that stores data by page number,
* // line number and the byte number. Input processing is simplistic in
* // order to focus on Array calls.
*
* byte fill = 0;
* Array* pAry = new_Array("source", etByte, 0, &fill, RANK_3(0,0,0));
* char sBuf[1024] = {'\0'};
* size_t uLen = 0;
* while(!eof(stdin)){
* while(fgets(sBuf, 1024, stdin)){
* uLen = strlen(sBuf);
* if(sBuf[0] == 0x0C) Array_markEnd(pAry, DIM1); // end page
* Array_append(pAry, sBuf, uLen+1); // keep NULL terminators
* if(sBuf[uLen - 1] == '\n') Array_markEnd(pAry, DIM2); // end line
* }
* }
*
|
size_t DasAry_clear | ( | DasAry * | pThis | ) |
Clear all values from the array.
This operation internally just resets the count of items to 0 in all arrays it does not free memory. Dimensions above the 0th retain their shape.
pThis | The array to clear |
int DasAry_cmp | ( | DasAry * | pThis, |
const void * | vpFirst, | ||
const void * | vpSecond | ||
) |
Compare two items of the type in the array.
void DasAry_setSrc | ( | DasAry * | pThis, |
int | nPktId, | ||
size_t | uStartItem, | ||
size_t | uItems | ||
) |
Record which packets contain data destine for this array.
pThis | The array |
nPktId | The id of the Das packet which contains values that should be added to this array |
uStartItem | The location in the packet where this array's data starts |
uItems | The number of items to add from each packet |