#ifndef UCX_MAP_H
#define UCX_MAP_H
#include "ucx.h"
#include "string.h"
#include "allocator.h"
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
#define UCX_MAP_FOREACH(key,value,iter) \
for(UcxKey key;ucx_map_iter_next(&iter,&key, (
void**)&value);)
typedef struct UcxMap UcxMap;
typedef struct UcxKey UcxKey;
typedef struct UcxMapElement UcxMapElement;
typedef struct UcxMapIterator UcxMapIterator;
struct UcxMap {
UcxAllocator *allocator;
UcxMapElement **map;
size_t size;
size_t count;
};
struct UcxKey {
void *data;
size_t len;
int hash;
};
struct UcxMapElement {
void *data;
UcxMapElement *next;
UcxKey key;
};
struct UcxMapIterator {
UcxMap *map;
UcxMapElement *cur;
size_t index;
};
UcxMap *ucx_map_new(
size_t size);
UcxMap *ucx_map_new_a(UcxAllocator *allocator,
size_t size);
void ucx_map_free(UcxMap *map);
void ucx_map_free_content(UcxMap *map, ucx_destructor destr);
void ucx_map_clear(UcxMap *map);
int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
copy_func fnc,
void *data);
UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc,
void *data);
int ucx_map_rehash(UcxMap *map);
int ucx_map_put(UcxMap *map, UcxKey key,
void *value);
void* ucx_map_get(UcxMap *map, UcxKey key);
void* ucx_map_remove(UcxMap *map, UcxKey key);
#define ucx_map_sstr_put(map, key, value) \
ucx_map_put(map, ucx_key(key.ptr, key.length), (
void*)value)
#define ucx_map_cstr_put(map, key, value) \
ucx_map_put(map, ucx_key((
void*)key, strlen(key)), (
void*)value)
#define ucx_map_int_put(map, key, value) \
ucx_map_put(map, ucx_key((
void*)&key,
sizeof(key)), (
void*)value)
#define ucx_map_sstr_get(map, key) \
ucx_map_get(map, ucx_key(key.ptr, key.length))
#define ucx_map_cstr_get(map, key) \
ucx_map_get(map, ucx_key((
void*)key, strlen(key)))
#define ucx_map_int_get(map, key) \
ucx_map_get(map, ucx_key((
void*)&key,
sizeof(
int)))
#define ucx_map_sstr_remove(map, key) \
ucx_map_remove(map, ucx_key(key.ptr, key.length))
#define ucx_map_cstr_remove(map, key) \
ucx_map_remove(map, ucx_key((
void*)key, strlen(key)))
#define ucx_map_int_remove(map, key) \
ucx_map_remove(map, ucx_key((
void*)&key,
sizeof(key)))
UcxKey ucx_key(
void *data,
size_t len);
int ucx_hash(
const char *data,
size_t len);
UcxMapIterator ucx_map_iterator(UcxMap *map);
int ucx_map_iter_next(UcxMapIterator *iterator, UcxKey *key,
void **value);
#ifdef __cplusplus
}
#endif
#endif