Skip to content

Commit b7d126c

Browse files
committed
use lookup_or_insert
1 parent 47319c3 commit b7d126c

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

src/data.table.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ hashtab * hash_create(size_t n);
294294
void hash_set(hashtab *, SEXP key, R_xlen_t value);
295295
// Returns the value corresponding to the key present in the hash, otherwise returns ifnotfound.
296296
R_xlen_t hash_lookup(const hashtab *, SEXP key, R_xlen_t ifnotfound);
297+
// Returns the value corresponding to the key present in the hash, otherwise inserts value.
298+
R_xlen_t hash_lookup_or_insert(hashtab *, SEXP key, R_xlen_t value);
297299

298300
// The dynamically-allocated hash table has a public field for the R protection wrapper.
299301
// Keep it PROTECTed while the table is in use.

src/hash.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ R_xlen_t hash_lookup_or_insert(hashtab *h, SEXP key, R_xlen_t value) {
9999
struct hash_pair *cell = h->tb + hash_index(key, h->multiplier) % h->size, *end = h->tb + h->size - 1;
100100
for (size_t i = 0; i < h->size; ++i, cell = (cell == end ? h->tb : cell + 1)) {
101101
if (cell->key == key) {
102-
cell->value = value;
103-
return cell->value;
102+
return cell->value; // found key, only lookup, no insert
104103
} else if (!cell->key) {
105104
if (!h->free) internal_error(
106105
__func__, "no free slots left (full size=%zu)", h->size

0 commit comments

Comments
 (0)