Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update swiss table header #274

Merged
merged 6 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,612 changes: 1,632 additions & 1,980 deletions include/sdb/cwisstable.h

Large diffs are not rendered by default.

25 changes: 19 additions & 6 deletions include/sdb/ht_pu.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@
#define SDB_HT_PU_H

/*
* This header provides an hashtable HtPU that has void* as key and ut64 as
* value. The API functions starts with "ht_pu_" and the types starts with "HtPU".
* This header provides a hashtable HtPU that has void* as key and ut64 as
* value. The hashtable does not take ownership of the keys, and so will not
* free the pointers once they are removed from the hashtable.
*/
#define HT_TYPE 4
#include "ht_inc.h"

#include "sdb/types.h"

#ifdef __cplusplus
extern "C" {
#endif

SDB_API HtName_(Ht)* Ht_(new0)(void);
#undef HT_TYPE
typedef uint64_t ut64;

typedef struct HtPU_t HtPU;

typedef bool (*HtPUForEachCallback)(void *user, const void *key, const ut64 value);

SDB_API HtPU* ht_pu_new0(void);
SDB_API void ht_pu_free(HtPU *hm);
SDB_API bool ht_pu_insert(HtPU *hm, void *key, ut64 value);
SDB_API bool ht_pu_update(HtPU *hm, void *key, ut64 value);
SDB_API bool ht_pu_update_key(HtPU *hm, void *old_key, void *new_key);
SDB_API bool ht_pu_delete(HtPU *hm, void *key);
SDB_API ut64 ht_pu_find(HtPU *hm, void *key, bool* found);
SDB_API void ht_pu_foreach(HtPU *hm, HtPUForEachCallback cb, void *user);

#ifdef __cplusplus
}
Expand Down
36 changes: 36 additions & 0 deletions include/sdb/ht_su.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef SDB_HT_SU_H
#define SDB_HT_SU_H

/*
* This header provides a hashtable HtSU that has char* as key and ut64 as
* value (a "stringmap"). The hashtable takes ownership of the keys by making a
* copy of the key, and will free the pointers once they are removed from the
* hashtable.
*/

#include "sdb/types.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef uint64_t ut64;

typedef struct HtSU_t HtSU;

typedef bool (*HtSUForEachCallback)(void *user, const char *key, const ut64 value);

SDB_API HtSU* ht_su_new0(void);
SDB_API void ht_su_free(HtSU *hm);
SDB_API bool ht_su_insert(HtSU *hm, const char *key, ut64 value);
SDB_API bool ht_su_update(HtSU *hm, const char *key, ut64 value);
SDB_API bool ht_su_update_key(HtSU *hm, const char *old_key, const char *new_key);
SDB_API bool ht_su_delete(HtSU *hm, const char *key);
SDB_API ut64 ht_su_find(HtSU *hm, const char *key, bool* found);
SDB_API void ht_su_foreach(HtSU *hm, HtSUForEachCallback cb, void *user);

#ifdef __cplusplus
}
#endif

#endif
21 changes: 14 additions & 7 deletions include/sdb/ht_uu.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@
#define SDB_HT_UU_H_

/*
* This header provides an hashtable Ht that has ut64 as key and ut64 as
* value. The API functions starts with "ht_" and the types starts with "Ht".
* This header provides a hashtable Ht that has ut64 as key and ut64 as value.
*/
#undef HT_TYPE
#define HT_TYPE 3
#include "ht_inc.h"
#include "ht.h"

#include "sdb/types.h"

#ifdef __cplusplus
extern "C" {
#endif

SDB_API HtName_(Ht)* Ht_(new0)(void);
typedef struct HtUU_t HtUU;
typedef bool (*HtUUForEachCallback)(void *user, const ut64 key, const ut64 value);

SDB_API HtUU* ht_uu_new0(void);
SDB_API void ht_uu_free(HtUU *hm);
SDB_API bool ht_uu_insert(HtUU *hm, const ut64 key, ut64 value);
SDB_API bool ht_uu_update(HtUU *hm, const ut64 key, ut64 value);
SDB_API bool ht_uu_update_key(HtUU *hm, const ut64 old_key, const ut64 new_key);
SDB_API bool ht_uu_delete(HtUU *hm, const ut64 key);
SDB_API ut64 ht_uu_find(HtUU *hm, const ut64 key, bool* found);
SDB_API void ht_uu_foreach(HtUU *hm, HtUUForEachCallback cb, void *user);

#ifdef __cplusplus
}
Expand Down
4 changes: 3 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ libsdb_sources = [
'src/ht_up.c',
'src/ht_pp.c',
'src/ht_pu.c',
'src/ht_su.c',
'src/journal.c',
'src/json.c',
'src/lock.c',
Expand Down Expand Up @@ -112,7 +113,8 @@ if not meson.is_subproject()
'include/sdb/ht_pp.h',
'include/sdb/ht_up.h',
'include/sdb/ht_uu.h',
'include/sdb/ht_up.h',
'include/sdb/ht_pu.h',
'include/sdb/ht_su.h',
'include/sdb/ls.h',
'include/sdb/sdb.h',
'include/sdb/ht.h',
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ include ../config.mk
# CFLAGS:=-g $(CFLAGS)
OBJ=cdb.o buffer.o cdb_make.o ls.o ht.o ht_uu.o sdb.o num.o base64.o match.o
OBJ+=json.o ns.o lock.o util.o disk.o query.o array.o fmt.o journal.o text.o
OBJ+=dict.o ht_pp.o ht_up.o ht_pu.o set.o diff.o heap.o main.o
OBJ+=dict.o ht_pp.o ht_up.o ht_pu.o ht_su.o set.o diff.o heap.o main.o
SDB_CFLAGS+=-I../include
SDB_CXXFLAGS+=-I../include
SOBJ=$(subst .o,.o.o,${OBJ})
Expand Down
126 changes: 105 additions & 21 deletions src/ht_pu.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,108 @@
/* sdb - MIT - Copyright 2018-2022 - ret2libc, pancake */
/* sdb - MIT - Copyright 2018-2023 - ret2libc, pancake, luc-tielen */

#include "sdb/sdb.h"
#include <stdint.h>
#include "sdb/ht_pu.h"
#include "ht.inc.c"

static void free_kv_key(HT_(Kv) *kv) {
sdb_gh_free (kv->key);
}

// creates a default HtPU that has strings as keys
SDB_API HtName_(Ht)* Ht_(new0)(void) {
HT_(Options) opt = {
.cmp = (HT_(ListComparator))strcmp,
.hashfn = (HT_(HashFunction))sdb_hash,
.dupkey = (HT_(DupKey))sdb_strdup,
.dupvalue = NULL,
.calcsizeK = (HT_(CalcSizeK))strlen,
.calcsizeV = NULL,
.freefn = free_kv_key,
.elem_size = sizeof (HT_(Kv)),
};
return Ht_(new_opt) (&opt);
#include "sdb/heap.h"
#include "sdb/cwisstable.h"

CWISS_DECLARE_FLAT_HASHMAP_DEFAULT(HtPU_, void*, ut64);
Fixed Show fixed Hide fixed

struct HtPU_t {
HtPU_ inner;
};

SDB_API HtPU* ht_pu_new0(void) {
HtPU *hm = (HtPU*) sdb_gh_calloc (1, sizeof (HtPU));
if (hm) {
hm->inner = HtPU__new (0);
}
return hm;
}

SDB_API void ht_pu_free(HtPU *hm) {
if (hm) {
HtPU__destroy (&hm->inner);
sdb_gh_free (hm);
}
}

SDB_API bool ht_pu_insert(HtPU *hm, void *key, ut64 value) {
assert (hm);

HtPU__Entry entry = { .key = key, .val = value };
HtPU__Insert result = HtPU__insert (&hm->inner, &entry);
return result.inserted;
}

SDB_API bool ht_pu_update(HtPU *hm, void *key, ut64 value) {
assert (hm);

HtPU__Entry entry = { .key = key, .val = value };
HtPU__Insert insert_result = HtPU__insert (&hm->inner, &entry);
const bool should_update = !insert_result.inserted;
if (should_update) {
HtPU__Entry *existing_entry = HtPU__Iter_get (&insert_result.iter);
existing_entry->val = value;
}

return true;
}

// Update the key of an element in the hashtable
SDB_API bool ht_pu_update_key(HtPU *hm, void *old_key, void *new_key) {
assert (hm);

HtPU__Iter iter = HtPU__find (&hm->inner, &old_key);
HtPU__Entry *entry = HtPU__Iter_get (&iter);
if (!entry) {
return false;
}

// First try inserting the new key
HtPU__Entry new_entry = { .key = new_key, .val = entry->val };
HtPU__Insert result = HtPU__insert (&hm->inner, &new_entry);
if (!result.inserted) {
return false;
}

// Then remove entry for the old key
HtPU__erase_at (iter);
return true;
}

SDB_API bool ht_pu_delete(HtPU *hm, void *key) {
assert (hm);
return HtPU__erase (&hm->inner, &key);
}

SDB_API ut64 ht_pu_find(HtPU *hm, void *key, bool* found) {
assert (hm);
if (found) {
*found = false;
}

HtPU__Iter iter = HtPU__find (&hm->inner, &key);
HtPU__Entry *entry = HtPU__Iter_get (&iter);
if (!entry) {
return 0;
}

if (found) {
*found = true;
}
return entry->val;
}

// Iterates over all elements in the hashtable, calling the cb function on each Kv.
// If the cb returns false, the iteration is stopped.
// cb should not modify the hashtable.
SDB_API void ht_pu_foreach(HtPU *hm, HtPUForEachCallback cb, void *user) {
assert (hm);
HtPU__CIter iter;
const HtPU__Entry *entry;
for (iter = HtPU__citer (&hm->inner); (entry = HtPU__CIter_get (&iter)) != NULL; HtPU__CIter_next (&iter)) {
if (!cb (user, entry->key, entry->val)) {
return;
}
}
}
Loading
Loading