#ifndef UCX_MAP_H
#define UCX_MAP_H
#include "common.h"
#include "collection.h"
#include "string.h"
#include "hash_key.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct cx_map_s CxMap;
typedef struct cx_map_entry_s CxMapEntry;
typedef struct cx_map_class_s cx_map_class;
struct cx_map_s {
CX_COLLECTION_BASE;
cx_map_class *cl;
};
enum cx_map_iterator_type {
CX_MAP_ITERATOR_PAIRS,
CX_MAP_ITERATOR_KEYS,
CX_MAP_ITERATOR_VALUES
};
struct cx_map_class_s {
__attribute__((__nonnull__))
void (*destructor)(
struct cx_map_s *map);
__attribute__((__nonnull__))
void (*clear)(
struct cx_map_s *map);
__attribute__((__nonnull__))
int (*put)(
CxMap *map,
CxHashKey key,
void *value
);
__attribute__((__nonnull__, __warn_unused_result__))
void *(*get)(
CxMap
const *map,
CxHashKey key
);
__attribute__((__nonnull__))
void *(*remove)(
CxMap *map,
CxHashKey key,
bool destroy
);
__attribute__((__nonnull__, __warn_unused_result__))
CxIterator (*iterator)(CxMap
const *map,
enum cx_map_iterator_type type);
};
struct cx_map_entry_s {
CxHashKey
const *key;
void *value;
};
extern CxMap *
const cxEmptyMap;
__attribute__((__nonnull__))
static inline
void cxMapStoreObjects(CxMap *map) {
map->collection.store_pointer = false;
}
__attribute__((__nonnull__))
static inline
void cxMapStorePointers(CxMap *map) {
map->collection.store_pointer = true;
map->collection.elem_size =
sizeof(
void *);
}
__attribute__((__nonnull__))
static inline bool cxMapIsStoringPointers(CxMap
const *map) {
return map->collection.store_pointer;
}
__attribute__((__nonnull__))
static inline
void cxMapDestroy(CxMap *map) {
map->cl->destructor(map);
}
__attribute__((__nonnull__))
static inline
void cxMapClear(CxMap *map) {
map->cl->clear(map);
}
__attribute__((__nonnull__))
static inline
size_t cxMapSize(CxMap
const *map) {
return map->collection.size;
}
__attribute__((__nonnull__, __warn_unused_result__))
static inline CxIterator cxMapIteratorValues(CxMap
const *map) {
return map->cl->iterator(map,
CX_MAP_ITERATOR_VALUES);
}
__attribute__((__nonnull__, __warn_unused_result__))
static inline CxIterator cxMapIteratorKeys(CxMap
const *map) {
return map->cl->iterator(map,
CX_MAP_ITERATOR_KEYS);
}
__attribute__((__nonnull__, __warn_unused_result__))
static inline CxIterator cxMapIterator(CxMap
const *map) {
return map->cl->iterator(map,
CX_MAP_ITERATOR_PAIRS);
}
/**
__attribute__((__nonnull__, __warn_unused_result__))
CxIterator cxMapMutIteratorValues(CxMap *map);
__attribute__((__nonnull__, __warn_unused_result__))
CxIterator cxMapMutIteratorKeys(CxMap *map);
__attribute__((__nonnull__, __warn_unused_result__))
CxIterator cxMapMutIterator(CxMap *map);
#ifdef __cplusplus
}
__attribute__((__nonnull__))
static inline
int cxMapPut(
CxMap *map,
CxHashKey
const &key,
void *value
) {
return map->cl->put(map, key, value);
}
__attribute__((__nonnull__))
static inline
int cxMapPut(
CxMap *map,
cxstring
const &key,
void *value
) {
return map->cl->put(map, cx_hash_key_cxstr(key), value);
}
__attribute__((__nonnull__))
static inline
int cxMapPut(
CxMap *map,
cxmutstr
const &key,
void *value
) {
return map->cl->put(map, cx_hash_key_cxstr(key), value);
}
__attribute__((__nonnull__))
static inline
int cxMapPut(
CxMap *map,
char const *key,
void *value
) {
return map->cl->put(map, cx_hash_key_str(key), value);
}
__attribute__((__nonnull__, __warn_unused_result__))
static inline
void *cxMapGet(
CxMap
const *map,
CxHashKey
const &key
) {
return map->cl->get(map, key);
}
__attribute__((__nonnull__, __warn_unused_result__))
static inline
void *cxMapGet(
CxMap
const *map,
cxstring
const &key
) {
return map->cl->get(map, cx_hash_key_cxstr(key));
}
__attribute__((__nonnull__, __warn_unused_result__))
static inline
void *cxMapGet(
CxMap
const *map,
cxmutstr
const &key
) {
return map->cl->get(map, cx_hash_key_cxstr(key));
}
__attribute__((__nonnull__, __warn_unused_result__))
static inline
void *cxMapGet(
CxMap
const *map,
char const *key
) {
return map->cl->get(map, cx_hash_key_str(key));
}
__attribute__((__nonnull__))
static inline
void cxMapRemove(
CxMap *map,
CxHashKey
const &key
) {
(
void) map->cl->remove(map, key, true);
}
__attribute__((__nonnull__))
static inline
void cxMapRemove(
CxMap *map,
cxstring
const &key
) {
(
void) map->cl->remove(map, cx_hash_key_cxstr(key), true);
}
__attribute__((__nonnull__))
static inline
void cxMapRemove(
CxMap *map,
cxmutstr
const &key
) {
(
void) map->cl->remove(map, cx_hash_key_cxstr(key), true);
}
__attribute__((__nonnull__))
static inline
void cxMapRemove(
CxMap *map,
char const *key
) {
(
void) map->cl->remove(map, cx_hash_key_str(key), true);
}
__attribute__((__nonnull__))
static inline
void cxMapDetach(
CxMap *map,
CxHashKey
const &key
) {
(
void) map->cl->remove(map, key, false);
}