liblex/include/pairmap.h

60 lines
1.8 KiB
C
Raw Normal View History

#ifndef LIBLEX_PAIRMAP_H
#define LIBLEX_PAIRMAP_H
#define LIBLEX_PAIRMAP_SIZE 64
/**
* Since libds's hash table requires a pointer to a key object, it's necessary to modify a single
* key object (tmp) with the two values for the pair, and then pass it into the ht_xxx functions
* to be copied. This requires at least 3 things - set the x value, set the y value, call the
* hash table function the requires the key. These macros take care of these three things.
*/
#define PAIRMAP_PUT(map, tmp, newx, newy, data) ((tmp)->x = newx, (tmp)->y = newy, ht_put(map, tmp, data))
#define PAIRMAP_GET(map, tmp, newx, newy) ((tmp)->x = newx, (tmp)->y = newy, ht_get(map, tmp))
#include "ht.h"
/**
* A key object used for hash tables that take a pair of integers
* instead of a single pointer.
*/
struct pairmap_key_s {
int x;
int y;
};
typedef struct pairmap_key_s pairmap_key;
/**
* Hashes pairmap_key instead of the default string.
* This takes the lower 16 bits, and bit shifts them
* into a single integer.
*
* Why 16 bits? Well, the hash map implementation takes an unsigned long,
* which is only guaranteed to contain 32 bits. Therefore, assuming it
* has to be unique for any two equal-sized integers, it has to
* be twice the size. Hence, the largest the integers can be is 16.
* @param key the key for the hash table.
* @return the resulting hash.
*/
unsigned long pairmap_hash_func(void* key);
/**
* Compares two pairmap_key's.
* @param a the first pairmap_key
* @param b the second pairmap_key
* @return
*/
int pairmap_cmp_func(void* a, void* b);
/**
* Copies a pairmap_key.
* @param key the key to copy.
* @return the copy of the key.
*/
void* pairmap_copy_func(void* key);
/**
* Initializes a hash table with functions used for pair maps.
* @param ht the hash table to initialize.
*/
void pairmap_init_ht(ht* ht);
#endif