Skip to main content
added 13 characters in body
Source Link
chux
  • 36.5k
  • 2
  • 43
  • 97

Consider a mod by prime for general hash functions. I'd use a look-up table of primes just under a power-of-4 and grow the hash table approximately 4x when needed.

//#define FNV_OFFSET 14695981039346656037UL
//#define FNV_PRIME 1099511628211UL
#define FNV_OFFSET 14695981039346656037U
#define FNV_PRIME 1099511628211U

Minor: Avoid sign extension

// hash ^= (uint64_t)(*p);
hash ^= *(unsigned char *)p;

Consider a mod by prime. I'd use a look-up table of primes just under a power-of-4 and grow the hash table approximately 4x when needed.

//#define FNV_OFFSET 14695981039346656037UL
//#define FNV_PRIME 1099511628211UL
#define FNV_OFFSET 14695981039346656037U
#define FNV_PRIME 1099511628211U

Consider a mod by prime for general hash functions. I'd use a look-up table of primes just under a power-of-4 and grow the hash table approximately 4x when needed.

//#define FNV_OFFSET 14695981039346656037UL
//#define FNV_PRIME 1099511628211UL
#define FNV_OFFSET 14695981039346656037U
#define FNV_PRIME 1099511628211U

Minor: Avoid sign extension

// hash ^= (uint64_t)(*p);
hash ^= *(unsigned char *)p;
added 13 characters in body
Source Link
chux
  • 36.5k
  • 2
  • 43
  • 97

I disagree with restringrestricting use with "Values can't be NULL".

Rather than mix the result of the get() with a reserved pointer value, separate the error flag from data. The data value should be allowed to be anythingany void *.

I disagree with restring use with "Values can't be NULL".

Rather than mix the result of the get() with a reserved pointer value, separate the error flag from data. The data should be allowed to be anything.

I disagree with restricting use with "Values can't be NULL".

Rather than mix the result of the get() with a reserved pointer value, separate the error flag from data. The data value should be allowed to be any void *.

added 306 characters in body
Source Link
chux
  • 36.5k
  • 2
  • 43
  • 97

Consider the below. Is sizeof(_ht_entry) correct? To check, I needed to search for the type of table (some wheresomewhere at the start of the function) , find ht, search for ht definition (itsit is in another file).

Easy to code right, review and maintain. No need to look up table, notnor _entries definition.

(OP does not use _ht_create(0) yet, but ...)

Below tests for out-of-memory by table->_entries == NULL, yet a NULL is an acceptable return value when table->_capacity == 0. (C17 somewhat addresses this, but not enough).

Rather than rollmix the result of the get() with a reserved pointer value, separate the error flag from data. The data should be allowed to be anything.

// size_t ht_length(ht* table);
size_t ht_length(const ht* table);

Initialize all of the iterator

hti ht_iterator(ht* table) {
    //hti it;
    //it._table = table;
    //it._index = 0;
    hti it = { ._table = table, ._index = 0 }; // other members become 0
    return it;
}

Consider the below. Is sizeof(_ht_entry) correct? To check, I needed to search for the type of table (some where at the start of the function) , find ht, search for ht definition (its in another file).

Easy to code right, review and maintain. No need to look up table, not _entries definition.

Below tests for out-of-memory by table->_entries == NULL, yet a NULL is an acceptable return value when table->_capacity == 0. (C17 somewhat addresses this, but not enough).

Rather than roll the result of the get() with a reserved pointer value, separate the error flag from data. The data should be allowed to be anything.

// size_t ht_length(ht* table);
size_t ht_length(const ht* table);

Consider the below. Is sizeof(_ht_entry) correct? To check, I needed to search for the type of table (somewhere at the start of the function) , find ht, search for ht definition (it is in another file).

Easy to code right, review and maintain. No need to look up table, nor _entries definition.

(OP does not use _ht_create(0) yet, but ...)

Below tests for out-of-memory by table->_entries == NULL, yet a NULL is an acceptable return value when table->_capacity == 0. (C17 somewhat addresses this, but not enough).

Rather than mix the result of the get() with a reserved pointer value, separate the error flag from data. The data should be allowed to be anything.

// size_t ht_length(ht* table);
size_t ht_length(const ht* table);

Initialize all of the iterator

hti ht_iterator(ht* table) {
    //hti it;
    //it._table = table;
    //it._index = 0;
    hti it = { ._table = table, ._index = 0 }; // other members become 0
    return it;
}
Source Link
chux
  • 36.5k
  • 2
  • 43
  • 97
Loading