mirror of
https://github.com/albfan/miraclecast.git
synced 2025-03-09 23:38:56 +00:00
shl: add u64 htable helpers
Add u64 helpers to shl_htable. They're fairly trivial and just copied from unsigned-long, however, in case size_t is not 64bit wide we need to do some trivial hashing. Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
This commit is contained in:
parent
87b804c481
commit
920f3ae250
2 changed files with 77 additions and 2 deletions
|
@ -18,9 +18,9 @@
|
|||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "shl_htable.h"
|
||||
|
@ -443,6 +443,16 @@ size_t shl_htable_rehash_ulong(const void *elem, void *priv)
|
|||
return (size_t)*(const unsigned long*)elem;
|
||||
}
|
||||
|
||||
bool shl_htable_compare_u64(const void *a, const void *b)
|
||||
{
|
||||
return *(const uint64_t*)a == *(const uint64_t*)b;
|
||||
}
|
||||
|
||||
size_t shl_htable_rehash_u64(const void *elem, void *priv)
|
||||
{
|
||||
return shl__htable_rehash_u64((const uint64_t*)elem);
|
||||
}
|
||||
|
||||
bool shl_htable_compare_str(const void *a, const void *b)
|
||||
{
|
||||
if (!*(char**)a || !*(char**)b)
|
||||
|
|
|
@ -19,10 +19,10 @@
|
|||
#ifndef SHL_HTABLE_H
|
||||
#define SHL_HTABLE_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* miscellaneous */
|
||||
|
@ -224,6 +224,71 @@ static inline bool shl_htable_remove_ulong(struct shl_htable *htable,
|
|||
(void**)out);
|
||||
}
|
||||
|
||||
/* uint64 htables */
|
||||
|
||||
bool shl_htable_compare_u64(const void *a, const void *b);
|
||||
size_t shl_htable_rehash_u64(const void *elem, void *priv);
|
||||
|
||||
static inline size_t shl__htable_rehash_u64(const uint64_t *p)
|
||||
{
|
||||
#if SIZE_MAX < UINT64_MAX
|
||||
return (size_t)((*p ^ (*p >> 32)) & 0xffffffff);
|
||||
#else
|
||||
return (size_t)*p;
|
||||
#endif
|
||||
}
|
||||
|
||||
#define SHL_HTABLE_INIT_U64(_obj) \
|
||||
SHL_HTABLE_INIT((_obj), shl_htable_compare_u64, \
|
||||
shl_htable_rehash_u64, \
|
||||
NULL)
|
||||
|
||||
static inline void shl_htable_init_u64(struct shl_htable *htable)
|
||||
{
|
||||
shl_htable_init(htable, shl_htable_compare_u64,
|
||||
shl_htable_rehash_u64, NULL);
|
||||
}
|
||||
|
||||
static inline void shl_htable_clear_u64(struct shl_htable *htable,
|
||||
void (*cb) (uint64_t *elem, void *ctx),
|
||||
void *ctx)
|
||||
{
|
||||
shl_htable_clear(htable, (void (*) (void*, void*))cb, ctx);
|
||||
}
|
||||
|
||||
static inline void shl_htable_visit_u64(struct shl_htable *htable,
|
||||
void (*cb) (uint64_t *elem, void *ctx),
|
||||
void *ctx)
|
||||
{
|
||||
shl_htable_visit(htable, (void (*) (void*, void*))cb, ctx);
|
||||
}
|
||||
|
||||
static inline bool shl_htable_lookup_u64(struct shl_htable *htable,
|
||||
uint64_t key,
|
||||
uint64_t **out)
|
||||
{
|
||||
return shl_htable_lookup(htable, (const void*)&key,
|
||||
shl__htable_rehash_u64(&key),
|
||||
(void**)out);
|
||||
}
|
||||
|
||||
static inline int shl_htable_insert_u64(struct shl_htable *htable,
|
||||
const uint64_t *key)
|
||||
{
|
||||
return shl_htable_insert(htable,
|
||||
(const void*)key,
|
||||
shl__htable_rehash_u64(key));
|
||||
}
|
||||
|
||||
static inline bool shl_htable_remove_u64(struct shl_htable *htable,
|
||||
uint64_t key,
|
||||
uint64_t **out)
|
||||
{
|
||||
return shl_htable_remove(htable, (const void*)&key,
|
||||
shl__htable_rehash_u64(&key),
|
||||
(void**)out);
|
||||
}
|
||||
|
||||
/* string htables */
|
||||
|
||||
bool shl_htable_compare_str(const void *a, const void *b);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue