Implement a few "pair map" functions, mostly used for hash tables.
In order to efficiently check if nodes are contained within a given set, without knowing exactly how many nodes there are and allocating a ton of memory, it's probably best to use a hash table (sparse sets would be more efficient but would require to allocate all of their space at once). It's also pointless to reimplement most of the hash table functionality from libds just to get it to work with pairs (pattern id + node id), so pairmap.h provides a few defaults that can be plugged into the default ht_s, as well as a few macros to facilitate some hash table operations.
This commit is contained in:
29
src/pairmap.c
Normal file
29
src/pairmap.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "pairmap.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
unsigned long pairmap_hash_func(void* key){
|
||||
pairmap_key* new_key = key;
|
||||
unsigned int hash = 0;
|
||||
hash |= ((new_key->x & 0xffff) << 8) + (new_key->y & 0xffff);
|
||||
return hash;
|
||||
}
|
||||
int pairmap_cmp_func(void* a, void* b){
|
||||
pairmap_key* new_key_a = a;
|
||||
pairmap_key* new_key_b = b;
|
||||
return (new_key_a->x == new_key_b->x) && (new_key_a->y == new_key_b->y);
|
||||
}
|
||||
void* pairmap_copy_func(void* key){
|
||||
pairmap_key* copy_from = key;
|
||||
pairmap_key* new_key = malloc(sizeof(*new_key));
|
||||
if(new_key){
|
||||
new_key->x = copy_from->x;
|
||||
new_key->y = copy_from->y;
|
||||
}
|
||||
return new_key;
|
||||
}
|
||||
void pairmap_init_ht(ht* ht){
|
||||
ht_init(ht);
|
||||
ht->hash_func = pairmap_hash_func;
|
||||
ht->cmp_func = pairmap_cmp_func;
|
||||
ht->copy_func = pairmap_copy_func;
|
||||
}
|
||||
Reference in New Issue
Block a user