2017-01-27 22:16:31 -08:00
|
|
|
#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.
|
|
|
|
*/
|
2017-02-03 21:02:07 -08:00
|
|
|
#define PAIRMAP_PUT(map, tmp, newx, newy, data) ((tmp)->x = newx, (tmp)->y = newy, ht_put(map, tmp, data))
|
2017-01-27 22:16:31 -08:00
|
|
|
#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.
|
|
|
|
*/
|
2017-07-20 21:58:51 -07:00
|
|
|
unsigned long pairmap_hash_func(const void* key);
|
2017-01-27 22:16:31 -08:00
|
|
|
/**
|
|
|
|
* Compares two pairmap_key's.
|
|
|
|
* @param a the first pairmap_key
|
|
|
|
* @param b the second pairmap_key
|
|
|
|
* @return
|
|
|
|
*/
|
2017-07-20 21:58:51 -07:00
|
|
|
int pairmap_cmp_func(const void* a, const void* b);
|
2017-01-27 22:16:31 -08:00
|
|
|
/**
|
|
|
|
* Copies a pairmap_key.
|
|
|
|
* @param key the key to copy.
|
|
|
|
* @return the copy of the key.
|
|
|
|
*/
|
2017-07-20 21:58:51 -07:00
|
|
|
void* pairmap_copy_func(const void* key);
|
2017-01-27 22:16:31 -08:00
|
|
|
/**
|
|
|
|
* Initializes a hash table with functions used for pair maps.
|
|
|
|
* @param ht the hash table to initialize.
|
|
|
|
*/
|
|
|
|
void pairmap_init_ht(ht* ht);
|
|
|
|
|
|
|
|
#endif
|