summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2013-04-25 08:16:41 +0200
committerYorhel <git@yorhel.nl>2013-04-25 08:17:02 +0200
commitcfd1b1fea215a2a19d493e490dde8402896be602 (patch)
tree77b3334772d07e10f28bee0881106f7aae4dca15
parent09c444753aa120fc092a83d250a1f6c22e7835fc (diff)
deps: Update khash and yopt to their latest upstream versions
-rw-r--r--Makefile.am3
-rw-r--r--deps/khash.h143
-rw-r--r--deps/yopt.h228
3 files changed, 223 insertions, 151 deletions
diff --git a/Makefile.am b/Makefile.am
index 04dd5dd..8275232 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -38,3 +38,6 @@ EXTRA_DIST=ncdu.1 doc/ncdu.pod
ncdu.1: $(srcdir)/doc/ncdu.pod
pod2man --center "ncdu manual" --release "@PACKAGE@-@VERSION@" "$(srcdir)/doc/ncdu.pod" >ncdu.1
+update-deps:
+ wget -q https://raw.github.com/attractivechaos/klib/master/khash.h -O "$(srcdir)/deps/khash.h"
+ wget -q http://g.blicky.net/ylib.git/plain/yopt.h -O "$(srcdir)/deps/yopt.h"
diff --git a/deps/khash.h b/deps/khash.h
index 0c121a9..2b96035 100644
--- a/deps/khash.h
+++ b/deps/khash.h
@@ -33,7 +33,6 @@ int main() {
khiter_t k;
khash_t(32) *h = kh_init(32);
k = kh_put(32, h, 5, &ret);
- if (!ret) kh_del(32, h, k);
kh_value(h, k) = 10;
k = kh_get(32, h, 10);
is_missing = (k == kh_end(h));
@@ -47,6 +46,10 @@ int main() {
*/
/*
+ 2011-12-29 (0.2.7):
+
+ * Minor code clean up; no actual effect.
+
2011-09-16 (0.2.6):
* The capacity is a power of 2. This seems to dramatically improve the
@@ -113,7 +116,7 @@ int main() {
#include <string.h>
#include <limits.h>
-/* compipler specific configuration */
+/* compiler specific configuration */
#if UINT_MAX == 0xffffffffu
typedef unsigned int khint32_t;
@@ -128,7 +131,9 @@ typedef unsigned long long khint64_t;
#endif
#ifdef _MSC_VER
-#define inline __inline
+#define kh_inline __inline
+#else
+#define kh_inline inline
#endif
typedef khint32_t khint_t;
@@ -154,39 +159,48 @@ typedef khint_t khiter_t;
#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
#endif
+#ifndef kcalloc
+#define kcalloc(N,Z) calloc(N,Z)
+#endif
+#ifndef kmalloc
+#define kmalloc(Z) malloc(Z)
+#endif
+#ifndef krealloc
+#define krealloc(P,Z) realloc(P,Z)
+#endif
+#ifndef kfree
+#define kfree(P) free(P)
+#endif
+
static const double __ac_HASH_UPPER = 0.77;
-#define KHASH_DECLARE(name, khkey_t, khval_t) \
- typedef struct { \
- khint_t n_buckets, size, n_occupied, upper_bound; \
- khint32_t *flags; \
- khkey_t *keys; \
- khval_t *vals; \
- } kh_##name##_t; \
- extern kh_##name##_t *kh_init_##name(); \
+#define __KHASH_TYPE(name, khkey_t, khval_t) \
+ typedef struct { \
+ khint_t n_buckets, size, n_occupied, upper_bound; \
+ khint32_t *flags; \
+ khkey_t *keys; \
+ khval_t *vals; \
+ } kh_##name##_t;
+
+#define __KHASH_PROTOTYPES(name, khkey_t, khval_t) \
+ extern kh_##name##_t *kh_init_##name(void); \
extern void kh_destroy_##name(kh_##name##_t *h); \
extern void kh_clear_##name(kh_##name##_t *h); \
extern khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key); \
- extern void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \
+ extern int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \
extern khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret); \
extern void kh_del_##name(kh_##name##_t *h, khint_t x);
-#define KHASH_INIT2(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
- typedef struct { \
- khint_t n_buckets, size, n_occupied, upper_bound; \
- khint32_t *flags; \
- khkey_t *keys; \
- khval_t *vals; \
- } kh_##name##_t; \
- SCOPE kh_##name##_t *kh_init_##name() { \
- return (kh_##name##_t*)calloc(1, sizeof(kh_##name##_t)); \
+#define __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
+ SCOPE kh_##name##_t *kh_init_##name(void) { \
+ return (kh_##name##_t*)kcalloc(1, sizeof(kh_##name##_t)); \
} \
SCOPE void kh_destroy_##name(kh_##name##_t *h) \
{ \
if (h) { \
- free(h->keys); free(h->flags); \
- free(h->vals); \
- free(h); \
+ kfree((void *)h->keys); kfree(h->flags); \
+ kfree((void *)h->vals); \
+ kfree(h); \
} \
} \
SCOPE void kh_clear_##name(kh_##name##_t *h) \
@@ -210,8 +224,8 @@ static const double __ac_HASH_UPPER = 0.77;
return __ac_iseither(h->flags, i)? h->n_buckets : i; \
} else return 0; \
} \
- SCOPE void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
- { /* This function uses 0.25*n_bucktes bytes of working space instead of [sizeof(key_t+val_t)+.25]*n_buckets. */ \
+ SCOPE int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
+ { /* This function uses 0.25*n_buckets bytes of working space instead of [sizeof(key_t+val_t)+.25]*n_buckets. */ \
khint32_t *new_flags = 0; \
khint_t j = 1; \
{ \
@@ -219,11 +233,18 @@ static const double __ac_HASH_UPPER = 0.77;
if (new_n_buckets < 4) new_n_buckets = 4; \
if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; /* requested size is too small */ \
else { /* hash table size to be changed (shrink or expand); rehash */ \
- new_flags = (khint32_t*)malloc(__ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
+ new_flags = (khint32_t*)kmalloc(__ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
+ if (!new_flags) return -1; \
memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
if (h->n_buckets < new_n_buckets) { /* expand */ \
- h->keys = (khkey_t*)realloc(h->keys, new_n_buckets * sizeof(khkey_t)); \
- if (kh_is_map) h->vals = (khval_t*)realloc(h->vals, new_n_buckets * sizeof(khval_t)); \
+ khkey_t *new_keys = (khkey_t*)krealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \
+ if (!new_keys) return -1; \
+ h->keys = new_keys; \
+ if (kh_is_map) { \
+ khval_t *new_vals = (khval_t*)krealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \
+ if (!new_vals) return -1; \
+ h->vals = new_vals; \
+ } \
} /* otherwise shrink */ \
} \
} \
@@ -256,22 +277,28 @@ static const double __ac_HASH_UPPER = 0.77;
} \
} \
if (h->n_buckets > new_n_buckets) { /* shrink the hash table */ \
- h->keys = (khkey_t*)realloc(h->keys, new_n_buckets * sizeof(khkey_t)); \
- if (kh_is_map) h->vals = (khval_t*)realloc(h->vals, new_n_buckets * sizeof(khval_t)); \
+ h->keys = (khkey_t*)krealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \
+ if (kh_is_map) h->vals = (khval_t*)krealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \
} \
- free(h->flags); /* free the working space */ \
+ kfree(h->flags); /* free the working space */ \
h->flags = new_flags; \
h->n_buckets = new_n_buckets; \
h->n_occupied = h->size; \
h->upper_bound = (khint_t)(h->n_buckets * __ac_HASH_UPPER + 0.5); \
} \
+ return 0; \
} \
SCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \
{ \
khint_t x; \
if (h->n_occupied >= h->upper_bound) { /* update the hash table */ \
- if (h->n_buckets > (h->size<<1)) kh_resize_##name(h, h->n_buckets - 1); /* clear "deleted" elements */ \
- else kh_resize_##name(h, h->n_buckets + 1); /* expand the hash table */ \
+ if (h->n_buckets > (h->size<<1)) { \
+ if (kh_resize_##name(h, h->n_buckets - 1) < 0) { /* clear "deleted" elements */ \
+ *ret = -1; return h->n_buckets; \
+ } \
+ } else if (kh_resize_##name(h, h->n_buckets + 1) < 0) { /* expand the hash table */ \
+ *ret = -1; return h->n_buckets; \
+ } \
} /* TODO: to implement automatically shrinking; resize() already support shrinking */ \
{ \
khint_t inc, k, i, site, last, mask = h->n_buckets - 1; \
@@ -311,8 +338,16 @@ static const double __ac_HASH_UPPER = 0.77;
} \
}
+#define KHASH_DECLARE(name, khkey_t, khval_t) \
+ __KHASH_TYPE(name, khkey_t, khval_t) \
+ __KHASH_PROTOTYPES(name, khkey_t, khval_t)
+
+#define KHASH_INIT2(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
+ __KHASH_TYPE(name, khkey_t, khval_t) \
+ __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)
+
#define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
- KHASH_INIT2(name, static inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)
+ KHASH_INIT2(name, static kh_inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)
/* --- BEGIN OF HASH FUNCTIONS --- */
@@ -341,10 +376,10 @@ static const double __ac_HASH_UPPER = 0.77;
@param s Pointer to a null terminated string
@return The hash value
*/
-static inline khint_t __ac_X31_hash_string(const char *s)
+static kh_inline khint_t __ac_X31_hash_string(const char *s)
{
- khint_t h = *s;
- if (h) for (++s ; *s; ++s) h = (h << 5) - h + *s;
+ khint_t h = (khint_t)*s;
+ if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)*s;
return h;
}
/*! @function
@@ -358,7 +393,7 @@ static inline khint_t __ac_X31_hash_string(const char *s)
*/
#define kh_str_hash_equal(a, b) (strcmp(a, b) == 0)
-static inline khint_t __ac_Wang_hash(khint_t key)
+static kh_inline khint_t __ac_Wang_hash(khint_t key)
{
key += ~(key << 15);
key ^= (key >> 10);
@@ -426,7 +461,7 @@ static inline khint_t __ac_Wang_hash(khint_t key)
@param name Name of the hash table [symbol]
@param h Pointer to the hash table [khash_t(name)*]
@param k Key [type of keys]
- @return Iterator to the found element, or kh_end(h) is the element is absent [khint_t]
+ @return Iterator to the found element, or kh_end(h) if the element is absent [khint_t]
*/
#define kh_get(name, h, k) kh_get_##name(h, k)
@@ -496,6 +531,34 @@ static inline khint_t __ac_Wang_hash(khint_t key)
*/
#define kh_n_buckets(h) ((h)->n_buckets)
+/*! @function
+ @abstract Iterate over the entries in the hash table
+ @param h Pointer to the hash table [khash_t(name)*]
+ @param kvar Variable to which key will be assigned
+ @param vvar Variable to which value will be assigned
+ @param code Block of code to execute
+ */
+#define kh_foreach(h, kvar, vvar, code) { khint_t __i; \
+ for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
+ if (!kh_exist(h,__i)) continue; \
+ (kvar) = kh_key(h,__i); \
+ (vvar) = kh_val(h,__i); \
+ code; \
+ } }
+
+/*! @function
+ @abstract Iterate over the values in the hash table
+ @param h Pointer to the hash table [khash_t(name)*]
+ @param vvar Variable to which value will be assigned
+ @param code Block of code to execute
+ */
+#define kh_foreach_value(h, vvar, code) { khint_t __i; \
+ for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
+ if (!kh_exist(h,__i)) continue; \
+ (vvar) = kh_val(h,__i); \
+ code; \
+ } }
+
/* More conenient interfaces */
/*! @function
diff --git a/deps/yopt.h b/deps/yopt.h
index 86ee1b9..98d969b 100644
--- a/deps/yopt.h
+++ b/deps/yopt.h
@@ -1,6 +1,4 @@
-/* ncdu - NCurses Disk Usage
-
- Copyright (c) 2007-2012 Yoran Heling
+/* Copyright (c) 2012-2013 Yoran Heling
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@@ -20,7 +18,6 @@
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
*/
/* This is a simple command-line option parser. Operation is similar to
@@ -48,78 +45,82 @@
*/
+#ifndef YOPT_H
+#define YOPT_H
+
+#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct {
- /* Value yopt_next() will return for this option */
- int val;
- /* Whether this option needs an argument */
- int needarg;
- /* Name(s) of this option, prefixed with '-' or '--' and separated by a
- * comma. E.g. "-z", "--gzip", "-z,--gzip".
- * An option can have any number of aliasses.
- */
- const char *name;
+ /* Value yopt_next() will return for this option */
+ int val;
+ /* Whether this option needs an argument */
+ int needarg;
+ /* Name(s) of this option, prefixed with '-' or '--' and separated by a
+ * comma. E.g. "-z", "--gzip", "-z,--gzip".
+ * An option can have any number of aliasses.
+ */
+ const char *name;
} yopt_opt_t;
typedef struct {
- int argc;
- int cur;
- int argsep; /* '--' found */
- char **argv;
- char *sh;
- yopt_opt_t *opts;
- char errbuf[128];
+ int argc;
+ int cur;
+ int argsep; /* '--' found */
+ char **argv;
+ char *sh;
+ const yopt_opt_t *opts;
+ char errbuf[128];
} yopt_t;
/* opts must be an array of options, terminated with an option with val=0 */
-static inline void yopt_init(yopt_t *o, int argc, char **argv, yopt_opt_t *opts) {
- o->argc = argc;
- o->argv = argv;
- o->opts = opts;
- o->cur = 0;
- o->argsep = 0;
- o->sh = NULL;
+static inline void yopt_init(yopt_t *o, int argc, char **argv, const yopt_opt_t *opts) {
+ o->argc = argc;
+ o->argv = argv;
+ o->opts = opts;
+ o->cur = 0;
+ o->argsep = 0;
+ o->sh = NULL;
}
-static inline yopt_opt_t *_yopt_find(yopt_opt_t *o, const char *v) {
- const char *tn, *tv;
-
- for(; o->val; o++) {
- tn = o->name;
- while(*tn) {
- tv = v;
- while(*tn && *tn != ',' && *tv && *tv != '=' && *tn == *tv) {
- tn++;
- tv++;
- }
- if(!(*tn && *tn != ',') && !(*tv && *tv != '='))
- return o;
- while(*tn && *tn != ',')
- tn++;
- while(*tn == ',')
- tn++;
- }
- }
-
- return NULL;
+static inline const yopt_opt_t *_yopt_find(const yopt_opt_t *o, const char *v) {
+ const char *tn, *tv;
+
+ for(; o->val; o++) {
+ tn = o->name;
+ while(*tn) {
+ tv = v;
+ while(*tn && *tn != ',' && *tv && *tv != '=' && *tn == *tv) {
+ tn++;
+ tv++;
+ }
+ if(!(*tn && *tn != ',') && !(*tv && *tv != '='))
+ return o;
+ while(*tn && *tn != ',')
+ tn++;
+ while(*tn == ',')
+ tn++;
+ }
+ }
+
+ return NULL;
}
static inline int _yopt_err(yopt_t *o, char **val, const char *fmt, ...) {
- va_list va;
- va_start(va, fmt);
- vsnprintf(o->errbuf, sizeof(o->errbuf), fmt, va);
- va_end(va);
- *val = o->errbuf;
- return -2;
+ va_list va;
+ va_start(va, fmt);
+ vsnprintf(o->errbuf, sizeof(o->errbuf), fmt, va);
+ va_end(va);
+ *val = o->errbuf;
+ return -2;
}
@@ -131,62 +132,67 @@ static inline int _yopt_err(yopt_t *o, char **val, const char *fmt, ...) {
* value will be in val.
*/
static inline int yopt_next(yopt_t *o, char **val) {
- yopt_opt_t *opt;
- char sh[3];
-
- *val = NULL;
- if(o->sh)
- goto inshort;
-
- if(++o->cur >= o->argc)
- return -1;
-
- if(!o->argsep && o->argv[o->cur][0] == '-' && o->argv[o->cur][1] == '-' && o->argv[o->cur][2] == 0) {
- o->argsep = 1;
- if(++o->cur >= o->argc)
- return -1;
- }
-
- if(o->argsep || *o->argv[o->cur] != '-') {
- *val = o->argv[o->cur];
- return 0;
- }
-
- if(o->argv[o->cur][1] != '-') {
- o->sh = o->argv[o->cur]+1;
- goto inshort;
- }
-
- /* Now we're supposed to have a long option */
- if(!(opt = _yopt_find(o->opts, o->argv[o->cur])))
- return _yopt_err(o, val, "Unknown option '%s'", o->argv[o->cur]);
- if((*val = strchr(o->argv[o->cur], '=')) != NULL)
- (*val)++;
- if(!opt->needarg && *val)
- return _yopt_err(o, val, "Option '%s' does not accept an argument", o->argv[o->cur]);
- if(opt->needarg && !*val) {
- if(o->cur+1 >= o->argc)
- return _yopt_err(o, val, "Option '%s' requires an argument", o->argv[o->cur]);
- *val = o->argv[++o->cur];
- }
- return opt->val;
-
- /* And here we're supposed to have a short option */
+ const yopt_opt_t *opt;
+ char sh[3];
+
+ *val = NULL;
+ if(o->sh)
+ goto inshort;
+
+ if(++o->cur >= o->argc)
+ return -1;
+
+ if(!o->argsep && o->argv[o->cur][0] == '-' && o->argv[o->cur][1] == '-' && o->argv[o->cur][2] == 0) {
+ o->argsep = 1;
+ if(++o->cur >= o->argc)
+ return -1;
+ }
+
+ if(o->argsep || *o->argv[o->cur] != '-') {
+ *val = o->argv[o->cur];
+ return 0;
+ }
+
+ if(o->argv[o->cur][1] != '-') {
+ o->sh = o->argv[o->cur]+1;
+ goto inshort;
+ }
+
+ /* Now we're supposed to have a long option */
+ if(!(opt = _yopt_find(o->opts, o->argv[o->cur])))
+ return _yopt_err(o, val, "Unknown option '%s'", o->argv[o->cur]);
+ if((*val = strchr(o->argv[o->cur], '=')) != NULL)
+ (*val)++;
+ if(!opt->needarg && *val)
+ return _yopt_err(o, val, "Option '%s' does not accept an argument", o->argv[o->cur]);
+ if(opt->needarg && !*val) {
+ if(o->cur+1 >= o->argc)
+ return _yopt_err(o, val, "Option '%s' requires an argument", o->argv[o->cur]);
+ *val = o->argv[++o->cur];
+ }
+ return opt->val;
+
+ /* And here we're supposed to have a short option */
inshort:
- sh[0] = '-';
- sh[1] = *o->sh;
- sh[2] = 0;
- if(!(opt = _yopt_find(o->opts, sh)))
- return _yopt_err(o, val, "Unknown option '%s'", sh);
- o->sh++;
- if(opt->needarg && *o->sh)
- *val = o->sh;
- else if(opt->needarg) {
- if(++o->cur >= o->argc)
- return _yopt_err(o, val, "Option '%s' requires an argument", sh);
- *val = o->argv[o->cur];
- }
- if(!*o->sh || opt->needarg)
- o->sh = NULL;
- return opt->val;
+ sh[0] = '-';
+ sh[1] = *o->sh;
+ sh[2] = 0;
+ if(!(opt = _yopt_find(o->opts, sh)))
+ return _yopt_err(o, val, "Unknown option '%s'", sh);
+ o->sh++;
+ if(opt->needarg && *o->sh)
+ *val = o->sh;
+ else if(opt->needarg) {
+ if(++o->cur >= o->argc)
+ return _yopt_err(o, val, "Option '%s' requires an argument", sh);
+ *val = o->argv[o->cur];
+ }
+ if(!*o->sh || opt->needarg)
+ o->sh = NULL;
+ return opt->val;
}
+
+
+#endif
+
+/* vim: set noet sw=4 ts=4: */