fx.collections: update header directories
This commit is contained in:
+92
-41
@@ -1,7 +1,7 @@
|
||||
#include <fx/core/misc.h>
|
||||
#include <fx/core/status.h>
|
||||
#include <fx/ds/hashmap.h>
|
||||
#include <fx/ds/string.h>
|
||||
#include <fx/collections/hashmap.h>
|
||||
#include <fx/collections/string.h>
|
||||
#include <fx/misc.h>
|
||||
#include <fx/status.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -42,9 +42,16 @@ struct fx_hashmap_iterator_p {
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
static FX_BST_DEFINE_SIMPLE_GET(
|
||||
struct fx_hashmap_bucket, uint64_t, bk_node, bk_hash, get_bucket);
|
||||
struct fx_hashmap_bucket,
|
||||
uint64_t,
|
||||
bk_node,
|
||||
bk_hash,
|
||||
get_bucket);
|
||||
static FX_BST_DEFINE_SIMPLE_INSERT(
|
||||
struct fx_hashmap_bucket, bk_node, bk_hash, put_bucket);
|
||||
struct fx_hashmap_bucket,
|
||||
bk_node,
|
||||
bk_hash,
|
||||
put_bucket);
|
||||
|
||||
static uint64_t hash_data(const void *p, size_t size)
|
||||
{
|
||||
@@ -69,7 +76,8 @@ static uint64_t hash_key(const struct fx_hashmap_key *key)
|
||||
}
|
||||
|
||||
static bool compare_key(
|
||||
const struct fx_hashmap_key *a, const struct fx_hashmap_key *b)
|
||||
const struct fx_hashmap_key *a,
|
||||
const struct fx_hashmap_key *b)
|
||||
{
|
||||
const void *a_data = NULL, *fx_data = NULL;
|
||||
size_t a_len = 0, fx_len = 0;
|
||||
@@ -99,8 +107,10 @@ static bool compare_key(
|
||||
}
|
||||
|
||||
static bool get_next_node(
|
||||
struct fx_bst_node *cur_node, struct fx_queue_entry *cur_entry,
|
||||
struct fx_bst_node **out_next_node, struct fx_queue_entry **out_next_entry)
|
||||
struct fx_bst_node *cur_node,
|
||||
struct fx_queue_entry *cur_entry,
|
||||
struct fx_bst_node **out_next_node,
|
||||
struct fx_queue_entry **out_next_entry)
|
||||
{
|
||||
struct fx_hashmap_bucket *cur_bucket
|
||||
= fx_unbox(struct fx_hashmap_bucket, cur_node, bk_node);
|
||||
@@ -122,8 +132,10 @@ static bool get_next_node(
|
||||
return false;
|
||||
}
|
||||
|
||||
struct fx_hashmap_bucket *next_bucket
|
||||
= fx_unbox(struct fx_hashmap_bucket, next_node, bk_node);
|
||||
struct fx_hashmap_bucket *next_bucket = fx_unbox(
|
||||
struct fx_hashmap_bucket,
|
||||
next_node,
|
||||
bk_node);
|
||||
if (!next_bucket) {
|
||||
return false;
|
||||
}
|
||||
@@ -169,11 +181,13 @@ static struct fx_hashmap_bucket_item *create_bucket_item(void)
|
||||
}
|
||||
|
||||
static fx_status hashmap_put(
|
||||
struct fx_hashmap_p *hashmap, const fx_hashmap_key *key,
|
||||
struct fx_hashmap_p *hashmap,
|
||||
const fx_hashmap_key *key,
|
||||
const fx_hashmap_value *value)
|
||||
{
|
||||
uint64_t hash = hash_key(key);
|
||||
struct fx_hashmap_bucket *bucket = get_bucket(&hashmap->h_buckets, hash);
|
||||
struct fx_hashmap_bucket *bucket
|
||||
= get_bucket(&hashmap->h_buckets, hash);
|
||||
|
||||
if (!bucket) {
|
||||
bucket = create_bucket();
|
||||
@@ -187,8 +201,10 @@ static fx_status hashmap_put(
|
||||
|
||||
struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items);
|
||||
while (entry) {
|
||||
struct fx_hashmap_bucket_item *item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, entry, bi_entry);
|
||||
struct fx_hashmap_bucket_item *item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
entry,
|
||||
bi_entry);
|
||||
|
||||
if (compare_key(&item->bi_key, key)) {
|
||||
memcpy(&item->bi_value, value, sizeof *value);
|
||||
@@ -213,19 +229,23 @@ static fx_status hashmap_put(
|
||||
}
|
||||
|
||||
static const struct fx_hashmap_value *hashmap_get(
|
||||
const struct fx_hashmap_p *hashmap, const struct fx_hashmap_key *key)
|
||||
const struct fx_hashmap_p *hashmap,
|
||||
const struct fx_hashmap_key *key)
|
||||
{
|
||||
uint64_t hash = hash_key(key);
|
||||
|
||||
struct fx_hashmap_bucket *bucket = get_bucket(&hashmap->h_buckets, hash);
|
||||
struct fx_hashmap_bucket *bucket
|
||||
= get_bucket(&hashmap->h_buckets, hash);
|
||||
if (!bucket) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items);
|
||||
while (entry) {
|
||||
struct fx_hashmap_bucket_item *item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, entry, bi_entry);
|
||||
struct fx_hashmap_bucket_item *item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
entry,
|
||||
bi_entry);
|
||||
|
||||
if (compare_key(&item->bi_key, key)) {
|
||||
return &item->bi_value;
|
||||
@@ -238,18 +258,22 @@ static const struct fx_hashmap_value *hashmap_get(
|
||||
}
|
||||
|
||||
static bool hashmap_has_key(
|
||||
const struct fx_hashmap_p *hashmap, const fx_hashmap_key *key)
|
||||
const struct fx_hashmap_p *hashmap,
|
||||
const fx_hashmap_key *key)
|
||||
{
|
||||
uint64_t hash = hash_key(key);
|
||||
struct fx_hashmap_bucket *bucket = get_bucket(&hashmap->h_buckets, hash);
|
||||
struct fx_hashmap_bucket *bucket
|
||||
= get_bucket(&hashmap->h_buckets, hash);
|
||||
if (!bucket) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items);
|
||||
while (entry) {
|
||||
struct fx_hashmap_bucket_item *item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, entry, bi_entry);
|
||||
struct fx_hashmap_bucket_item *item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
entry,
|
||||
bi_entry);
|
||||
|
||||
if (compare_key(&item->bi_key, key)) {
|
||||
return true;
|
||||
@@ -276,8 +300,10 @@ static bool hashmap_is_empty(const struct fx_hashmap_p *hashmap)
|
||||
}
|
||||
|
||||
fx_queue_entry *first_entry = fx_queue_first(&first_bucket->bk_items);
|
||||
struct fx_hashmap_bucket_item *first_item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, first_entry, bi_entry);
|
||||
struct fx_hashmap_bucket_item *first_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
first_entry,
|
||||
bi_entry);
|
||||
if (!first_item) {
|
||||
return true;
|
||||
}
|
||||
@@ -286,7 +312,8 @@ static bool hashmap_is_empty(const struct fx_hashmap_p *hashmap)
|
||||
}
|
||||
|
||||
static fx_status delete_item(
|
||||
struct fx_hashmap_p *hashmap, struct fx_hashmap_bucket *bucket,
|
||||
struct fx_hashmap_p *hashmap,
|
||||
struct fx_hashmap_bucket *bucket,
|
||||
struct fx_hashmap_bucket_item *item)
|
||||
{
|
||||
fx_queue_delete(&bucket->bk_items, &item->bi_entry);
|
||||
@@ -313,7 +340,8 @@ static fx_status delete_item(
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
fx_hashmap *fx_hashmap_create(
|
||||
fx_hashmap_key_destructor key_dtor, fx_hashmap_value_destructor value_dtor)
|
||||
fx_hashmap_key_destructor key_dtor,
|
||||
fx_hashmap_value_destructor value_dtor)
|
||||
{
|
||||
fx_hashmap *hashmap = fx_object_create(FX_TYPE_HASHMAP);
|
||||
if (!hashmap) {
|
||||
@@ -330,9 +358,11 @@ fx_hashmap *fx_hashmap_create_with_items(const fx_hashmap_item *items)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_hashmap_p *p = fx_object_get_private(hashmap, FX_TYPE_HASHMAP);
|
||||
struct fx_hashmap_p *p
|
||||
= fx_object_get_private(hashmap, FX_TYPE_HASHMAP);
|
||||
|
||||
for (size_t i = 0; items[i].key.key_data && items[i].key.key_size; i++) {
|
||||
for (size_t i = 0; items[i].key.key_data && items[i].key.key_size;
|
||||
i++) {
|
||||
hashmap_put(p, &items[i].key, &items[i].value);
|
||||
}
|
||||
|
||||
@@ -340,20 +370,32 @@ fx_hashmap *fx_hashmap_create_with_items(const fx_hashmap_item *items)
|
||||
}
|
||||
|
||||
fx_status fx_hashmap_put(
|
||||
fx_hashmap *hashmap, const fx_hashmap_key *key, const fx_hashmap_value *value)
|
||||
fx_hashmap *hashmap,
|
||||
const fx_hashmap_key *key,
|
||||
const fx_hashmap_value *value)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_HASHMAP, hashmap_put, hashmap, key, value);
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_HASHMAP,
|
||||
hashmap_put,
|
||||
hashmap,
|
||||
key,
|
||||
value);
|
||||
}
|
||||
|
||||
const struct fx_hashmap_value *fx_hashmap_get(
|
||||
const fx_hashmap *hashmap, const struct fx_hashmap_key *key)
|
||||
const fx_hashmap *hashmap,
|
||||
const struct fx_hashmap_key *key)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_HASHMAP, hashmap_get, hashmap, key);
|
||||
}
|
||||
|
||||
bool fx_hashmap_has_key(const fx_hashmap *hashmap, const fx_hashmap_key *key)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_HASHMAP, hashmap_has_key, hashmap, key);
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_HASHMAP,
|
||||
hashmap_has_key,
|
||||
hashmap,
|
||||
key);
|
||||
}
|
||||
|
||||
size_t fx_hashmap_get_size(const fx_hashmap *hashmap)
|
||||
@@ -368,7 +410,8 @@ bool fx_hashmap_is_empty(const fx_hashmap *hashmap)
|
||||
|
||||
fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap)
|
||||
{
|
||||
fx_hashmap_iterator *it_obj = fx_object_create(FX_TYPE_HASHMAP_ITERATOR);
|
||||
fx_hashmap_iterator *it_obj
|
||||
= fx_object_create(FX_TYPE_HASHMAP_ITERATOR);
|
||||
struct fx_hashmap_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_HASHMAP_ITERATOR);
|
||||
|
||||
@@ -391,9 +434,12 @@ fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap)
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
struct fx_queue_entry *first_entry = fx_queue_first(&first_bucket->bk_items);
|
||||
struct fx_hashmap_bucket_item *first_item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, first_entry, bi_entry);
|
||||
struct fx_queue_entry *first_entry
|
||||
= fx_queue_first(&first_bucket->bk_items);
|
||||
struct fx_hashmap_bucket_item *first_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
first_entry,
|
||||
bi_entry);
|
||||
if (!first_item) {
|
||||
memset(&it->item, 0x0, sizeof it->item);
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
@@ -435,8 +481,11 @@ static void hashmap_fini(fx_object *obj, void *priv)
|
||||
struct fx_queue_entry *entry = fx_queue_first(&b->bk_items);
|
||||
while (entry) {
|
||||
struct fx_hashmap_bucket_item *item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item, entry, bi_entry);
|
||||
struct fx_queue_entry *next_entry = fx_queue_next(entry);
|
||||
struct fx_hashmap_bucket_item,
|
||||
entry,
|
||||
bi_entry);
|
||||
struct fx_queue_entry *next_entry
|
||||
= fx_queue_next(entry);
|
||||
fx_queue_delete(&b->bk_items, entry);
|
||||
|
||||
if (map->h_key_dtor) {
|
||||
@@ -444,7 +493,8 @@ static void hashmap_fini(fx_object *obj, void *priv)
|
||||
}
|
||||
|
||||
if (map->h_value_dtor) {
|
||||
map->h_value_dtor((void *)item->bi_value.value_data);
|
||||
map->h_value_dtor(
|
||||
(void *)item->bi_value.value_data);
|
||||
}
|
||||
|
||||
free(item);
|
||||
@@ -524,7 +574,8 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
|
||||
if (next_item) {
|
||||
memcpy(&it->item.key, &next_item->bi_key, sizeof it->item.key);
|
||||
memcpy(&it->item.value, &next_item->bi_value,
|
||||
memcpy(&it->item.value,
|
||||
&next_item->bi_value,
|
||||
sizeof it->item.value);
|
||||
|
||||
it->_cbn = next_node;
|
||||
|
||||
Reference in New Issue
Block a user