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
51 static 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;
131 for (i = 0; i < dict->size; ++i)
148 void XLALDictForeach(LALDict *dict,
void (*func)(
char *, LALValue *,
void *),
void *thunk)
151 for (i = 0; i < dict->size; ++i) {
153 for (entry = dict->hashes[i]; entry != NULL; entry = entry->next)
154 func(entry->key, &entry->value, thunk);
159 LALDictEntry *
XLALDictFind(LALDict *dict,
int (*func)(
const char *,
const LALValue *,
void *),
void *thunk)
162 for (i = 0; i < dict->size; ++i) {
164 for (entry = dict->hashes[i]; entry != NULL; entry = entry->next)
165 if (func(entry->key, &entry->value, thunk))
184 LALDictEntry *entry = iter->next;
185 iter->next = entry->next;
190 if (iter->pos >= iter->dict->size)
193 iter->next = iter->dict->hashes[iter->pos++];
202 if(old==NULL)
return NULL;
206 for (i = 0; i < old->size; ++i) {
207 const LALDictEntry *entry;
208 for (entry = old->hashes[i]; entry != NULL; entry = entry->next) {
228 for (i = 0; i < dict->size; ++i) {
229 const LALDictEntry *entry;
230 for (entry = dict->hashes[i]; entry != NULL; entry = entry->next) {
248 for (i = 0; i < dict->size; ++i) {
249 const LALDictEntry *entry;
250 for (entry = dict->hashes[i]; entry != NULL; entry = entry->next) {
263 const LALDictEntry *entry;
264 for (entry = dict->hashes[
hash(
key) % dict->size]; entry != NULL; entry = entry->next)
265 if (strcmp(
key, entry->key) == 0)
274 for (i = 0; i < dict->size; ++i) {
275 const LALDictEntry *entry;
276 for (entry = dict->hashes[i]; entry != NULL; entry = entry->next)
285 for (entry = dict->hashes[
hash(
key) % dict->size]; entry != NULL; entry = entry->next)
286 if (strcmp(
key, entry->key) == 0)
293 size_t hashidx =
hash(
key) % dict->size;
294 LALDictEntry *
this = dict->hashes[hashidx];
295 LALDictEntry *prev =
this;
297 if (strcmp(this->key,
key) == 0) {
299 dict->hashes[hashidx] = this->
next;
315 size_t hashidx =
hash(
key) % dict->size;
316 LALDictEntry *
this = dict->hashes[hashidx];
317 LALDictEntry *prev = NULL;
322 if (strcmp(this->key,
key) == 0) {
328 dict->hashes[hashidx] = entry;
360 entry->next = dict->hashes[hashidx];
361 dict->hashes[hashidx] = entry;
382 size_t size = strlen(
string) + 1;
388 #define DEFINE_INSERT_FUNC(TYPE, TCODE) \
389 int XLALDictInsert ## TYPE ## Value(LALDict *dict, const char *key, TYPE value) \
391 if (XLALDictInsert(dict, key, &value, sizeof(value), TCODE) < 0) \
392 XLAL_ERROR(XLAL_EFUNC); \
409 #undef DEFINE_INSERT_FUNC
414 const LALValue *
value;
428 const LALValue *
value;
437 #define DEFINE_LOOKUP_FUNC(TYPE, FAILVAL) \
438 TYPE XLALDictLookup ## TYPE ## Value(LALDict *dict, const char *key) \
440 LALDictEntry *entry; \
441 const LALValue *value; \
442 entry = XLALDictLookup(dict, key); \
444 XLAL_ERROR_VAL(FAILVAL, XLAL_ENAME, "Key `%s' not found", key); \
445 value = XLALDictEntryGetValue(entry); \
447 XLAL_ERROR_VAL(FAILVAL, XLAL_EFUNC); \
448 return XLALValueGet ## TYPE (value); \
467 const LALValue *
value;
506 dprintf(fd,
"%s",
s);
int XLALDictContains(const LALDict *dict, const char *key)
LALList * XLALDictKeys(const LALDict *dict)
const char * XLALDictLookupStringValue(LALDict *dict, const char *key)
LALDictEntry * XLALDictEntrySetValue(LALDictEntry *entry, const void *data, size_t size, LALTYPECODE type)
static void XLALDictAsStringAppendValueFunc(char *key, LALValue *value, void *thunk)
const char * XLALDictEntryGetKey(const LALDictEntry *entry)
LALDictEntry * XLALDictEntrySetKey(LALDictEntry *entry, const char *key)
int XLALDictInsert(LALDict *dict, const char *key, const void *data, size_t size, LALTYPECODE type)
#define DEFINE_INSERT_FUNC(TYPE, TCODE)
void XLALDictEntryFree(LALDictEntry *list)
int XLALDictRemove(LALDict *dict, const char *key)
LALDictEntry * XLALDictIterNext(LALDictIter *iter)
#define DEFINE_LOOKUP_FUNC(TYPE, FAILVAL)
void XLALDestroyDict(LALDict *dict)
LALDictEntry * XLALDictEntryAlloc(size_t size)
LALDictEntry * XLALDictEntryRealloc(LALDictEntry *entry, size_t size)
int XLALDictInsertBLOBValue(LALDict *dict, const char *key, const void *blob, size_t size)
LALDictEntry * XLALDictLookup(LALDict *dict, const char *key)
LALDictEntry * XLALDictFind(LALDict *dict, int(*func)(const char *, const LALValue *, void *), void *thunk)
void * XLALDictLookupBLOBValue(LALDict *dict, const char *key)
size_t XLALDictSize(const LALDict *dict)
void XLALDictIterInit(LALDictIter *iter, LALDict *dict)
char * XLALDictAsStringAppend(char *s, LALDict *list)
LALDict * XLALDictDuplicate(LALDict *old)
const LALValue * XLALDictEntryGetValue(const LALDictEntry *entry)
REAL8 XLALDictLookupValueAsREAL8(LALDict *dict, const char *key)
#define LAL_DICT_HASHSIZE
int XLALDictInsertValue(LALDict *dict, const char *key, const LALValue *value)
static size_t hash(const char *s)
void XLALDictPrint(LALDict *dict, int fd)
int XLALDictInsertStringValue(LALDict *dict, const char *key, const char *string)
LALList * XLALDictValues(const LALDict *dict)
LALDict * XLALCreateDict(void)
void XLALDictForeach(LALDict *dict, void(*func)(char *, LALValue *, void *), void *thunk)
int XLALListAddValue(LALList *list, const LALValue *value)
int XLALListAddStringValue(LALList *list, const char *string)
void XLALDestroyList(LALList *list)
LALList * XLALCreateList(void)
char * XLALStringAppendFmt(char *s, const char *fmt,...)
Append the formatted string 'fmt' to the string 's', which is reallocated with XLALRealloc() to the r...
REAL8 XLALValueGetAsREAL8(const LALValue *value)
char * XLALValueAsStringAppend(char *s, const LALValue *value)
void * XLALValueGetBLOB(const LALValue *value)
size_t XLALValueGetSize(const LALValue *value)
const void * XLALValueGetDataPtr(const LALValue *value)
const char * XLALValueGetString(const LALValue *value)
LALTYPECODE XLALValueGetType(const LALValue *value)
LALValue * XLALValueSet(LALValue *value, const void *data, size_t size, LALTYPECODE type)
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_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_TRY(statement, errnum)
A macro to (i) disable the XLAL error handling and preserve the current value of xlalErrno (ii) perfo...
@ XLAL_ENOMEM
Memory allocation error.
@ XLAL_SUCCESS
Success return value (not an error number)
@ 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[]