diff options
author | Xavier Roche <xroche@users.noreply.github.com> | 2014-06-15 08:29:02 +0000 |
---|---|---|
committer | Xavier Roche <xroche@users.noreply.github.com> | 2014-06-15 08:29:02 +0000 |
commit | 967932e75d1ab512d4c5c3cb9443d90d68ec39a5 (patch) | |
tree | a1f5421cfe65c2bfd65222b5258e567a9056282e | |
parent | 42bb06703a5ddb44380643097ae8d0deb18cc8f2 (diff) |
Added coucal_fetch_value_hashes()
-rw-r--r-- | src/coucal.c | 33 | ||||
-rw-r--r-- | src/coucal.h | 14 |
2 files changed, 30 insertions, 17 deletions
diff --git a/src/coucal.c b/src/coucal.c index 5ade339..e925c75 100644 --- a/src/coucal.c +++ b/src/coucal.c @@ -968,13 +968,6 @@ static int coucal_add_item_(coucal hashtable, coucal_item item) { coucal_crit(hashtable, "\t.. collisionning with a free slot (%d)!", (int) pos2); } } - //struct_coucal_enum e = coucal_enum_new(hashtable); - //while((item = coucal_enum_next(&e)) != NULL) { - // coucal_crit(hashtable, "element key='%s' value='%s' hash1=%04x hash2=%04x", - // hashtable->custom.print.key(hashtable->custom.print.arg, item->name), - // hashtable->custom.print.value(hashtable->custom.print.arg, item->value.ptr), - // item->hashes.hash1, item->hashes.hash2); - //} } /* we are doomed. hopefully the probability is lower than being killed @@ -1131,20 +1124,20 @@ int coucal_read(coucal hashtable, coucal_key_const name, intptr_t * intvalue) { return ret; } -static coucal_value* coucal_read_value_(coucal hashtable, - coucal_key_const name) { - const coucal_hashkeys hashes = coucal_calc_hashes(hashtable, name); +coucal_value* coucal_fetch_value_hashes(coucal hashtable, + coucal_key_const name, + const coucal_hashkeys *hashes) { size_t pos; /* found at position 1 ? */ - pos = coucal_hash_to_pos(hashtable, hashes.hash1); - if (coucal_matches(hashtable, pos, name, &hashes)) { + pos = coucal_hash_to_pos(hashtable, hashes->hash1); + if (coucal_matches(hashtable, pos, name, hashes)) { return &hashtable->items[pos].value; } /* found at position 2 ? */ - pos = coucal_hash_to_pos(hashtable, hashes.hash2); - if (coucal_matches(hashtable, pos, name, &hashes)) { + pos = coucal_hash_to_pos(hashtable, hashes->hash2); + if (coucal_matches(hashtable, pos, name, hashes)) { return &hashtable->items[pos].value; } @@ -1153,7 +1146,7 @@ static coucal_value* coucal_read_value_(coucal hashtable, size_t i; for(i = 0 ; i < hashtable->stash.size ; i++) { if (coucal_matches_(hashtable, &hashtable->stash.items[i], name, - &hashes)) { + hashes)) { return &hashtable->stash.items[i].value; } } @@ -1163,9 +1156,15 @@ static coucal_value* coucal_read_value_(coucal hashtable, return NULL; } +static coucal_value* coucal_fetch_value(coucal hashtable, + coucal_key_const name) { + const coucal_hashkeys hashes = coucal_calc_hashes(hashtable, name); + return coucal_fetch_value_hashes(hashtable, name, &hashes); +} + int coucal_read_value(coucal hashtable, coucal_key_const name, coucal_value * pvalue) { - coucal_value* const value = coucal_read_value_(hashtable, name); + coucal_value* const value = coucal_fetch_value(hashtable, name); if (value != NULL) { if (pvalue != NULL) { *pvalue = *value; @@ -1177,7 +1176,7 @@ int coucal_read_value(coucal hashtable, coucal_key_const name, static size_t coucal_inc_(coucal hashtable, coucal_key_const name, size_t inc) { - coucal_value* const value = coucal_read_value_(hashtable, name); + coucal_value* const value = coucal_fetch_value(hashtable, name); if (value != NULL) { value->uintg += inc; return value->uintg; diff --git a/src/coucal.h b/src/coucal.h index 1d6585f..2012db5 100644 --- a/src/coucal.h +++ b/src/coucal.h @@ -423,6 +423,20 @@ COUCAL_EXTERN int coucal_inc(coucal hashtable, coucal_key_const name); COUCAL_EXTERN int coucal_dec(coucal hashtable, coucal_key_const name); /** + * Fetch an entry value from the hashtable, given a name, and its hashes. + * Returns NULL if the entry could not be found. + * The returned pointer is only valid until next call to this library, and can + * be used for read or write operations. + * The hashes MUST have been computed using either coucal_hash_string(), or + * coucal_hash_data(), or by copying an existing hash during an enumeration. + * The use of a non-matching hash is safe, but will return NULL. + **/ +COUCAL_EXTERN coucal_value* coucal_fetch_value_hashes(coucal hashtable, + coucal_key_const name, + const coucal_hashkeys + *hashes); + +/** * Remove an entry from the hashtable * Return non-zero value if the entry was removed, zero otherwise. **/ |