31#include <lal/LALStdio.h>
32#include <lal/LALStdlib.h>
33#include <lal/LALString.h>
34#include <lal/LALDict.h>
38#define LAL_DICT_HASHSIZE 101
51static size_t hash(
const char *s)
54 for (hashval = 0; *s !=
'\0'; ++s)
55 hashval = *s + 31 * hashval;
80 entry->value.size = size;
88 if (entry->value.size == size)
93 entry->value.size = size;
108 if (
XLALValueSet(&entry->value, data, size, type) == NULL)
122 return &entry->value;
130 for (
size_t i = 0; i < dict->size; ++i)
141 for (i = 0; i < dict->size; ++i)
158void XLALDictForeach(LALDict *dict,
void (*func)(
char *, LALValue *,
void *),
void *thunk)
161 for (i = 0; i < dict->size; ++i) {
163 for (entry = dict->hashes[i]; entry != NULL; entry = entry->next)
164 func(entry->key, &entry->value, thunk);
169LALDictEntry *
XLALDictFind(LALDict *dict,
int (*func)(
const char *,
const LALValue *,
void *),
void *thunk)
172 for (i = 0; i < dict->size; ++i) {
174 for (entry = dict->hashes[i]; entry != NULL; entry = entry->next)
175 if (func(entry->key, &entry->value, thunk))
194 LALDictEntry *entry = iter->next;
195 iter->next = entry->next;
200 if (iter->pos >= iter->dict->size)
203 iter->next = iter->dict->hashes[iter->pos++];
213 for (i = 0; i <
src->size; ++i) {
214 const LALDictEntry *entry;
215 for (entry =
src->hashes[i]; entry != NULL; entry = entry->next) {
251 for (i = 0; i < dict->size; ++i) {
252 const LALDictEntry *entry;
253 for (entry = dict->hashes[i]; entry != NULL; entry = entry->next) {
271 for (i = 0; i < dict->size; ++i) {
272 const LALDictEntry *entry;
273 for (entry = dict->hashes[i]; entry != NULL; entry = entry->next) {
286 const LALDictEntry *entry;
287 for (entry = dict->hashes[
hash(
key) % dict->size]; entry != NULL; entry = entry->next)
288 if (strcmp(
key, entry->key) == 0)
297 for (i = 0; i < dict->size; ++i) {
298 const LALDictEntry *entry;
299 for (entry = dict->hashes[i]; entry != NULL; entry = entry->next)
308 for (entry = dict->hashes[
hash(
key) % dict->size]; entry != NULL; entry = entry->next)
309 if (strcmp(
key, entry->key) == 0)
316 size_t hashidx =
hash(
key) % dict->size;
317 LALDictEntry *
this = dict->hashes[hashidx];
318 LALDictEntry *prev =
this;
320 if (strcmp(this->key,
key) == 0) {
322 dict->hashes[hashidx] = this->
next;
346 size_t hashidx =
hash(
key) % dict->size;
347 LALDictEntry *
this = dict->hashes[hashidx];
348 LALDictEntry *prev = NULL;
353 if (strcmp(this->key,
key) == 0) {
359 dict->hashes[hashidx] = entry;
391 entry->next = dict->hashes[hashidx];
392 dict->hashes[hashidx] = entry;
413 size_t size = strlen(
string) + 1;
419#define DEFINE_INSERT_FUNC(TYPE, TCODE) \
420 int XLALDictInsert ## TYPE ## Value(LALDict *dict, const char *key, TYPE value) \
422 if (XLALDictInsert(dict, key, &value, sizeof(value), TCODE) < 0) \
423 XLAL_ERROR(XLAL_EFUNC); \
440#undef DEFINE_INSERT_FUNC
445 const LALValue *
value;
458 const LALValue *
value;
467#define DEFINE_LOOKUP_FUNC(TYPE, FAILVAL) \
468 TYPE XLALDictLookup ## TYPE ## Value(const LALDict *dict, const char *key) \
470 LALDictEntry *entry; \
471 const LALValue *value; \
472 entry = XLALDictLookup(dict, key); \
474 XLAL_ERROR_VAL(FAILVAL, XLAL_ENAME, "Key `%s' not found", key); \
475 value = XLALDictEntryGetValue(entry); \
477 XLAL_ERROR_VAL(FAILVAL, XLAL_EFUNC); \
478 return XLALValueGet ## TYPE (value); \
497 const LALValue *
value;
541#define DEFINE_POP_FUNC(TYPE, FAILVAL) \
542 TYPE XLALDictPop ## TYPE ## Value(LALDict *dict, const char *key) \
544 LALDictEntry *entry; \
546 entry = XLALDictPop(dict, key); \
547 XLAL_CHECK_VAL(FAILVAL, entry, XLAL_EFUNC); \
548 value = XLALValueGet ## TYPE(XLALDictEntryGetValue(entry)); \
549 XLALDictEntryFree(entry); \
593 LALDict *d = (LALDict *)(uintptr_t)dict;
607 dprintf(fd,
"%s",
s);
int XLALDictContains(const LALDict *dict, const char *key)
LALDictEntry * XLALDictIterNext(LALDictIter *iter)
void * XLALDictPopBLOBValue(LALDict *dict, const char *key)
LALValue * XLALDictPopValue(LALDict *dict, const char *key)
REAL8 XLALDictLookupValueAsREAL8(const LALDict *dict, const char *key)
LALList * XLALDictKeys(const LALDict *dict)
int XLALDictUpdate(LALDict *dst, const LALDict *src)
static void XLALDictAsStringAppendValueFunc(char *key, LALValue *value, void *thunk)
LALDictEntry * XLALDictEntrySetValue(LALDictEntry *entry, const void *data, size_t size, LALTYPECODE type)
LALDictEntry * XLALDictPop(LALDict *dict, const char *key)
LALDictEntry * XLALDictEntryAlloc(size_t size)
int XLALDictInsert(LALDict *dict, const char *key, const void *data, size_t size, LALTYPECODE type)
#define DEFINE_INSERT_FUNC(TYPE, TCODE)
LALDictEntry * XLALDictLookup(const LALDict *dict, const char *key)
char * XLALDictPopStringValue(LALDict *dict, const char *key)
LALDict * XLALDictMerge(const LALDict *dict1, const LALDict *dict2)
void * XLALDictLookupBLOBValue(const LALDict *dict, const char *key)
const char * XLALDictLookupStringValue(const LALDict *dict, const char *key)
void XLALDictEntryFree(LALDictEntry *list)
int XLALDictRemove(LALDict *dict, const char *key)
#define DEFINE_LOOKUP_FUNC(TYPE, FAILVAL)
void XLALDestroyDict(LALDict *dict)
LALDict * XLALDictDuplicate(const LALDict *orig)
LALDict * XLALCreateDict(void)
LALList * XLALDictValues(const LALDict *dict)
void XLALClearDict(LALDict *dict)
LALDictEntry * XLALDictEntrySetKey(LALDictEntry *entry, const char *key)
int XLALDictInsertBLOBValue(LALDict *dict, const char *key, const void *blob, size_t size)
#define DEFINE_POP_FUNC(TYPE, FAILVAL)
size_t XLALDictSize(const LALDict *dict)
void XLALDictIterInit(LALDictIter *iter, LALDict *dict)
#define LAL_DICT_HASHSIZE
void XLALDictPrint(const LALDict *dict, int fd)
int XLALDictInsertValue(LALDict *dict, const char *key, const LALValue *value)
static size_t hash(const char *s)
char * XLALDictAsStringAppend(char *s, const LALDict *dict)
const char * XLALDictEntryGetKey(const LALDictEntry *entry)
int XLALDictInsertStringValue(LALDict *dict, const char *key, const char *string)
const LALValue * XLALDictEntryGetValue(const LALDictEntry *entry)
LALDictEntry * XLALDictFind(LALDict *dict, int(*func)(const char *, const LALValue *, void *), void *thunk)
void XLALDictForeach(LALDict *dict, void(*func)(char *, LALValue *, void *), void *thunk)
LALDictEntry * XLALDictEntryRealloc(LALDictEntry *entry, size_t size)
REAL8 XLALDictPopValueAsREAL8(LALDict *dict, const char *key)
int XLALListAddValue(LALList *list, const LALValue *value)
LALList * XLALCreateList(void)
int XLALListAddStringValue(LALList *list, const char *string)
void XLALDestroyList(LALList *list)
char * XLALStringAppendFmt(char *s, const char *fmt,...)
Append the formatted string 'fmt' to the string 's', which is reallocated with XLALRealloc() to the r...
char * XLALValueAsStringAppend(char *s, const LALValue *value)
const void * XLALValueGetDataPtr(const LALValue *value)
REAL8 XLALValueGetAsREAL8(const LALValue *value)
LALValue * XLALValueSet(LALValue *value, const void *data, size_t size, LALTYPECODE type)
LALValue * XLALValueDuplicate(const LALValue *value)
const char * XLALValueGetString(const LALValue *value)
size_t XLALValueGetSize(const LALValue *value)
void * XLALValueGetBLOB(const LALValue *value)
LALTYPECODE XLALValueGetType(const LALValue *value)
LALTYPECODE
Type codes: use these type codes to identify a LAL atomic data type, see Headers LAL(Atomic)Datatypes...
unsigned char UCHAR
One-byte unsigned integer, see Headers LAL(Atomic)Datatypes.h for more details.
uint64_t UINT8
Eight-byte unsigned integer; on some platforms this is equivalent to unsigned long int instead.
double complex COMPLEX16
Double-precision floating-point complex number (16 bytes total)
double REAL8
Double precision real floating-point number (8 bytes).
int16_t INT2
Two-byte signed integer.
int64_t INT8
Eight-byte signed integer; on some platforms this is equivalent to long int instead.
uint16_t UINT2
Two-byte unsigned integer.
char CHAR
One-byte signed integer, see Headers LAL(Atomic)Datatypes.h for more details.
uint32_t UINT4
Four-byte unsigned integer.
float complex COMPLEX8
Single-precision floating-point complex number (8 bytes total)
int32_t INT4
Four-byte signed integer.
float REAL4
Single precision real floating-point number (4 bytes).
@ LAL_C_TYPE_CODE
COMPLEX8 type code (27)
@ LAL_CHAR_TYPE_CODE
CHAR type code (0)
@ LAL_U2_TYPE_CODE
UINT2 type code (33)
@ LAL_Z_TYPE_CODE
COMPLEX16 type code (28)
@ LAL_S_TYPE_CODE
REAL4 type code (18)
@ LAL_I2_TYPE_CODE
INT2 type code (1)
@ LAL_I8_TYPE_CODE
INT8 type code (3)
@ LAL_D_TYPE_CODE
REAL8 type code (19)
@ LAL_I4_TYPE_CODE
INT4 type code (2)
@ LAL_UCHAR_TYPE_CODE
UCHAR type code (32)
@ LAL_U8_TYPE_CODE
UINT8 type code (35)
@ LAL_U4_TYPE_CODE
UINT4 type code (34)
#define XLALRealloc(p, n)
char * XLALStringDuplicate(const char *s)
Like strdup but uses LAL allocation routines (free with LALFree).
char * XLALStringAppend(char *s, const char *append)
Like strcat but dynamically reallocates string with LALRealloc.
#define XLAL_ERROR_VOID(...)
Macro to invoke a failure from a XLAL routine returning void.
#define XLAL_ERROR_REAL8(...)
Macro to invoke a failure from a XLAL routine returning a REAL8.
#define XLAL_ERROR_NULL(...)
Macro to invoke a failure from a XLAL routine returning a pointer.
#define XLAL_ERROR(...)
Macro to invoke a failure from a XLAL routine returning an integer.
#define XLAL_CHECK(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns an integ...
#define XLAL_CHECK_VOID(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns void.
#define XLAL_REAL4_FAIL_NAN
Floating-point value of the XLAL REAL4 failure NaN.
#define XLAL_REAL8_FAIL_NAN
Floating-point value of the XLAL REAL8 failure NaN.
#define XLAL_CHECK_FAIL(assertion,...)
Macro to test an assertion and invoke a failure if it is not true by jumping to a XLAL_FAIL label.
#define XLAL_CHECK_REAL8(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns a REAL8.
#define XLAL_CHECK_NULL(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns a pointe...
@ XLAL_ENOMEM
Memory allocation error.
@ XLAL_SUCCESS
Success return value (not an error number)
@ XLAL_EFAULT
Invalid pointer.
@ XLAL_EFUNC
Internal function call failed bit: "or" this with existing error number.
@ XLAL_FAILURE
Failure return value (not an error number)
struct tagLALDictEntry * next
struct tagLALDictEntry * hashes[]