#include "attrhash.h" #include "hash.h" #include "shared.h" struct hash_table *attrhash; /************************************************************************** A supplied hash function where key is an int, stuffed directly into the void *. Prefers table sizes that are prime numbers. **************************************************************************/ unsigned int hash_fval_dint(const void *id, unsigned int numbuckets) { const unsigned int key = (unsigned int) vkey; return (key % num_buckets); } /************************************************************************** A supplied function for comparison of int, stuffed directly into the void *. **************************************************************************/ int hash_fcmp_dint(const void *vkey1, const void *vkey2) { const int key1 = (const int*)vkey1; const int key2 = (const int*)vkey2; /* avoid overflow issues: */ return (key1 < key2) ? -1 : (key1 > key2) ? 1 : 0; } void init_attrhash() { attrhash = hash_new(hash_fval_dint, hash_fcmp_dint); } void set_attrib(enum attr_type, int id, int len, const void *data) { unsigned int key = ((unsigned int) attr_type) << 24 + id & 0xffffff; void *newdata = NULL; if (len > 0) { newdata = fc_malloc(len + sizeof(int)); (int *) newdata[0] = len; memcpy(newdata + sizeof(int), data, len); } hash_replace(attrhash, (void *) key, newdata); /* send data to server */ } const void *get_attrib(enum attr_type, int id, int *len) { unsigned int key = ((unsigned int) attr_type) << 24 + id & 0xffffff; void *data = hash_lookup_data(attrhash, key); int newlen; if (data != NULL) { if (len != NULL) *len = (*(int *) newdata); return newdata + sizeof(int); } else { if (len != NULL) *len = 0; return NULL; } } /* void example(void) { // user marked settler as public worker: set_attrib(ATTR_PUBLIC_WORKER, unit_id, 0, NULL); // user marked tile for building irrigation char special = S_IRRIGATION; set_attrib(ATTR_TILE_IMPROVEMENT, x + y * map.xsize, 1, &special); } */