Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6800c6f
Add writing
dnbaker Sep 27, 2016
cbeb0bc
Merge branch 'master' of https://github.com/noseatbelts/klib
dnbaker Sep 27, 2016
061969d
Add write/load functions.
dnbaker Nov 6, 2016
3277f05
Update khash.h
dnbaker Mar 18, 2017
494da2e
Update write/read functions to work for both sets and maps.
dnbaker Mar 18, 2017
a6755a4
Merge branch 'master' of https://github.com/attractivechaos/klib
dnbaker Apr 27, 2017
9f5e92e
Add kputuw_, kputw_, and kputl_ functions (which don't set 0).
dnbaker Apr 27, 2017
4375b21
Eliminate -Wsign-compare.
dnbaker Apr 28, 2017
121adbd
Modify kputuw_.
dnbaker Jun 15, 2017
309d5e5
Add HAS_KPUTUW__ macro for checking for function definition.
dnbaker Jun 15, 2017
1de0874
Merge branch 'master' of https://github.com/attractivechaos/klib
dnbaker Jun 15, 2017
69dc025
Finished moving things around.
dnbaker Jun 15, 2017
8598156
Merge branch 'master' of https://github.com/dnbh/klib
dnbaker Jun 15, 2017
0089a12
Merge branch 'master' of https://github.com/attractivechaos/klib
dnbaker Feb 3, 2018
751f62a
save
dnbaker Apr 30, 2018
3597c24
Merge branch 'master' of https://github.com/noseatbelts/klib
dnbaker Apr 30, 2018
384eea5
Merge branch 'master' of https://github.com/attractivechaos/klib
dnbaker Apr 30, 2018
2327449
Merge branch 'master' of https://github.com/attractivechaos/klib
dnbaker Aug 24, 2018
3e30f52
Allow switch to 64-bit map using -DKH_USE_64_BIT
dnbaker Aug 24, 2018
f6247bb
Eliminate second definition of kh_write.
dnbaker Aug 24, 2018
718511e
Save changes.
dnbaker Aug 24, 2018
3122f7e
Save stuff.
dnbaker Aug 24, 2018
8fbee11
Resolve.
dnbaker Aug 24, 2018
6694ef8
Patch.
dnbaker Aug 24, 2018
16d6366
Remove unrelated changes.
dnbaker Aug 25, 2018
f0f7639
Update documentation. Add serialize method.
dnbaker Aug 26, 2018
653e8d8
Merge branch 'master' of https://github.com/attractivechaos/klib
dnbaker Jan 26, 2019
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
50 changes: 49 additions & 1 deletion khash.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ typedef khint_t khiter_t;
#define __ac_set_isempty_false(flag, i) (flag[i>>4]&=~(2ul<<((i&0xfU)<<1)))
#define __ac_set_isboth_false(flag, i) (flag[i>>4]&=~(3ul<<((i&0xfU)<<1)))
#define __ac_set_isdel_true(flag, i) (flag[i>>4]|=1ul<<((i&0xfU)<<1))
#define __ac_fw(item, fp) (fwrite(&(item), 1, sizeof(item), fp))

#define __ac_fsize(m) ((m) < 16? 1 : (m)>>4)

Expand Down Expand Up @@ -352,7 +353,39 @@ static const double __ac_HASH_UPPER = 0.77;
__ac_set_isdel_true(h->flags, x); \
--h->size; \
} \
}
} \
SCOPE void kh_write_##name(kh_##name##_t *map, const char *path) { \
FILE *fp = fopen(path, "wb"); \
if(fp == NULL) { \
fprintf(stderr, "[%s] Could not open file %s.\n", __func__, path);\
exit(EXIT_FAILURE); \
} \
__ac_fw(map->n_buckets, fp); \
__ac_fw(map->n_occupied, fp); \
__ac_fw(map->size, fp); \
__ac_fw(map->upper_bound, fp); \
fwrite(map->flags, __ac_fsize(map->n_buckets), sizeof(khint32_t), fp);\
fwrite(map->keys, map->n_buckets, sizeof(*map->keys), fp); \
if(kh_is_map) fwrite(map->vals, map->n_buckets, sizeof(*map->vals), fp); \
fclose(fp); \
} \
SCOPE kh_##name##_t *khash_load_##name(const char *path) \
{ \
kh_##name##_t *ret = (kh_##name##_t *)calloc(1, sizeof(kh_##name##_t)); \
FILE *fp = fopen(path, "rb"); \
fread(&ret->n_buckets, 1, sizeof(ret->n_buckets), fp); \
fread(&ret->n_occupied, 1, sizeof(ret->n_occupied), fp); \
fread(&ret->size, 1, sizeof(ret->size), fp); \
fread(&ret->upper_bound, 1, sizeof(ret->upper_bound), fp); \
ret->flags = (khint32_t *)malloc(sizeof(*ret->flags) * __ac_fsize(ret->n_buckets));\
ret->keys = (khkey_t *)malloc(sizeof(khkey_t) * ret->n_buckets); \
ret->vals = kh_is_map ? (khval_t *)malloc(sizeof(khval_t) * ret->n_buckets) : 0; \
fread(ret->flags, __ac_fsize(ret->n_buckets), sizeof(*ret->flags), fp);\
fread(ret->keys, 1, ret->n_buckets * sizeof(*ret->keys), fp); \
if(kh_is_map) fread(ret->vals, 1, ret->n_buckets * sizeof(*ret->vals), fp); \
fclose(fp); \
return ret; \
}

#define KHASH_DECLARE(name, khkey_t, khval_t) \
__KHASH_TYPE(name, khkey_t, khval_t) \
Expand Down Expand Up @@ -490,6 +523,21 @@ static kh_inline khint_t __ac_Wang_hash(khint_t key)
*/
#define kh_del(name, h, k) kh_del_##name(h, k)

/*! @function
@abstract Write a hash map to disk.
@param h Pointer to the hash table [khash_t(name)*]
@param path Path to which to write. [const char *]
*/
#define kh_write(name, h, path) kh_write_##name(h, path)

/*! @function
@abstract Load a hash table from disk
@param name Name of the hash table [symbol]
@param path Path to file from which to load [const char *]
*/

#define kh_load(name, path) khash_load_##name(path)

/*! @function
@abstract Test whether a bucket contains data.
@param h Pointer to the hash table [khash_t(name)*]
Expand Down
70 changes: 70 additions & 0 deletions kstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,29 @@ static inline int kputw(int c, kstring_t *s)
return 0;
}


static inline int kputw_(int c, kstring_t *s)
{
char buf[16];
int i, l = 0;
unsigned int x = c;
if (c < 0) x = -x;
do { buf[l++] = x%10 + '0'; x /= 10; } while (x > 0);
if (c < 0) buf[l++] = '-';
if (s->l + l + 1 >= s->m) {
char *tmp;
s->m = s->l + l + 2;
kroundup32(s->m);
if ((tmp = (char*)realloc(s->s, s->m)))
s->s = tmp;
else
return EOF;
}
for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i];
return 0;
}


static inline int kputuw(unsigned c, kstring_t *s)
{
char buf[16];
Expand All @@ -241,6 +264,29 @@ static inline int kputuw(unsigned c, kstring_t *s)
return 0;
}


static inline int kputuw_(unsigned c, kstring_t *s)
{
char buf[16];
int l, i;
unsigned x;
if (c == 0) return kputc('0', s);
for (l = 0, x = c; x > 0; x /= 10) buf[l++] = x%10 + '0';
if (s->l + l + 1 >= s->m) {
char *tmp;
s->m = s->l + l + 2;
kroundup32(s->m);
if ((tmp = (char*)realloc(s->s, s->m)))
s->s = tmp;
else
return EOF;
}
for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i];
s->s[s->l] = 0;
return 0;
}


static inline int kputl(long c, kstring_t *s)
{
char buf[32];
Expand All @@ -263,6 +309,30 @@ static inline int kputl(long c, kstring_t *s)
return 0;
}


static inline int kputl_(long c, kstring_t *s)
Copy link

@justinmk justinmk Aug 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This duplicates a lot of code and it's not clear how it's related to the serialization.

kputl could call kputl_.

Copy link
Contributor Author

@dnbaker dnbaker Aug 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated to serialization. I added this so that I'd have the option of integer formatting routines which did not null-terminate for cases where I knew I would be appending to the string further. I can separate that out from this pull request later if requested. Compare kputw and kputw_ which were already present, for example. I'm not quite sure I understand why only some types were provided separate functions.

{
char buf[32];
int i, l = 0;
unsigned long x = c;
if (c < 0) x = -x;
do { buf[l++] = x%10 + '0'; x /= 10; } while (x > 0);
if (c < 0) buf[l++] = '-';
if (s->l + l + 1 >= s->m) {
char *tmp;
s->m = s->l + l + 2;
kroundup32(s->m);
if ((tmp = (char*)realloc(s->s, s->m)))
s->s = tmp;
else
return EOF;
}
for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i];
return 0;
}

/*

/*
* Returns 's' split by delimiter, with *n being the number of components;
* NULL on failue.
Expand Down