1
1

deps: Update khash and yopt to their latest upstream versions

Этот коммит содержится в:
Yorhel 2013-04-25 08:16:41 +02:00
родитель 09c444753a
Коммит cfd1b1fea2
3 изменённых файлов: 214 добавлений и 142 удалений

Просмотреть файл

@ -38,3 +38,6 @@ EXTRA_DIST=ncdu.1 doc/ncdu.pod
ncdu.1: $(srcdir)/doc/ncdu.pod ncdu.1: $(srcdir)/doc/ncdu.pod
pod2man --center "ncdu manual" --release "@PACKAGE@-@VERSION@" "$(srcdir)/doc/ncdu.pod" >ncdu.1 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"

143
deps/khash.h поставляемый
Просмотреть файл

@ -33,7 +33,6 @@ int main() {
khiter_t k; khiter_t k;
khash_t(32) *h = kh_init(32); khash_t(32) *h = kh_init(32);
k = kh_put(32, h, 5, &ret); k = kh_put(32, h, 5, &ret);
if (!ret) kh_del(32, h, k);
kh_value(h, k) = 10; kh_value(h, k) = 10;
k = kh_get(32, h, 10); k = kh_get(32, h, 10);
is_missing = (k == kh_end(h)); 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): 2011-09-16 (0.2.6):
* The capacity is a power of 2. This seems to dramatically improve the * The capacity is a power of 2. This seems to dramatically improve the
@ -113,7 +116,7 @@ int main() {
#include <string.h> #include <string.h>
#include <limits.h> #include <limits.h>
/* compipler specific configuration */ /* compiler specific configuration */
#if UINT_MAX == 0xffffffffu #if UINT_MAX == 0xffffffffu
typedef unsigned int khint32_t; typedef unsigned int khint32_t;
@ -128,7 +131,9 @@ typedef unsigned long long khint64_t;
#endif #endif
#ifdef _MSC_VER #ifdef _MSC_VER
#define inline __inline #define kh_inline __inline
#else
#define kh_inline inline
#endif #endif
typedef khint32_t khint_t; 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)) #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
#endif #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; static const double __ac_HASH_UPPER = 0.77;
#define KHASH_DECLARE(name, khkey_t, khval_t) \ #define __KHASH_TYPE(name, khkey_t, khval_t) \
typedef struct { \ typedef struct { \
khint_t n_buckets, size, n_occupied, upper_bound; \ khint_t n_buckets, size, n_occupied, upper_bound; \
khint32_t *flags; \ khint32_t *flags; \
khkey_t *keys; \ khkey_t *keys; \
khval_t *vals; \ khval_t *vals; \
} kh_##name##_t; \ } kh_##name##_t;
extern kh_##name##_t *kh_init_##name(); \
#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_destroy_##name(kh_##name##_t *h); \
extern void kh_clear_##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 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 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); 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) \ #define __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
typedef struct { \ SCOPE kh_##name##_t *kh_init_##name(void) { \
khint_t n_buckets, size, n_occupied, upper_bound; \ return (kh_##name##_t*)kcalloc(1, sizeof(kh_##name##_t)); \
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)); \
} \ } \
SCOPE void kh_destroy_##name(kh_##name##_t *h) \ SCOPE void kh_destroy_##name(kh_##name##_t *h) \
{ \ { \
if (h) { \ if (h) { \
free(h->keys); free(h->flags); \ kfree((void *)h->keys); kfree(h->flags); \
free(h->vals); \ kfree((void *)h->vals); \
free(h); \ kfree(h); \
} \ } \
} \ } \
SCOPE void kh_clear_##name(kh_##name##_t *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; \ return __ac_iseither(h->flags, i)? h->n_buckets : i; \
} else return 0; \ } else return 0; \
} \ } \
SCOPE void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \ SCOPE int 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. */ \ { /* 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; \ khint32_t *new_flags = 0; \
khint_t j = 1; \ 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 (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 */ \ 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 */ \ 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)); \ memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
if (h->n_buckets < new_n_buckets) { /* expand */ \ if (h->n_buckets < new_n_buckets) { /* expand */ \
h->keys = (khkey_t*)realloc(h->keys, new_n_buckets * sizeof(khkey_t)); \ khkey_t *new_keys = (khkey_t*)krealloc((void *)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)); \ 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 */ \ } /* 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 */ \ if (h->n_buckets > new_n_buckets) { /* shrink the hash table */ \
h->keys = (khkey_t*)realloc(h->keys, new_n_buckets * sizeof(khkey_t)); \ h->keys = (khkey_t*)krealloc((void *)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)); \ 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->flags = new_flags; \
h->n_buckets = new_n_buckets; \ h->n_buckets = new_n_buckets; \
h->n_occupied = h->size; \ h->n_occupied = h->size; \
h->upper_bound = (khint_t)(h->n_buckets * __ac_HASH_UPPER + 0.5); \ 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) \ SCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \
{ \ { \
khint_t x; \ khint_t x; \
if (h->n_occupied >= h->upper_bound) { /* update the hash table */ \ 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 */ \ if (h->n_buckets > (h->size<<1)) { \
else kh_resize_##name(h, h->n_buckets + 1); /* expand the hash table */ \ 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 */ \ } /* TODO: to implement automatically shrinking; resize() already support shrinking */ \
{ \ { \
khint_t inc, k, i, site, last, mask = h->n_buckets - 1; \ 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) \ #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 --- */ /* --- BEGIN OF HASH FUNCTIONS --- */
@ -341,10 +376,10 @@ static const double __ac_HASH_UPPER = 0.77;
@param s Pointer to a null terminated string @param s Pointer to a null terminated string
@return The hash value @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; khint_t h = (khint_t)*s;
if (h) for (++s ; *s; ++s) h = (h << 5) - h + *s; if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)*s;
return h; return h;
} }
/*! @function /*! @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) #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 << 15);
key ^= (key >> 10); 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 name Name of the hash table [symbol]
@param h Pointer to the hash table [khash_t(name)*] @param h Pointer to the hash table [khash_t(name)*]
@param k Key [type of keys] @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) #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) #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 */ /* More conenient interfaces */
/*! @function /*! @function

210
deps/yopt.h поставляемый
Просмотреть файл

@ -1,6 +1,4 @@
/* ncdu - NCurses Disk Usage /* Copyright (c) 2012-2013 Yoran Heling
Copyright (c) 2007-2012 Yoran Heling
Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the 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, CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
/* This is a simple command-line option parser. Operation is similar to /* 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 <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
typedef struct { typedef struct {
/* Value yopt_next() will return for this option */ /* Value yopt_next() will return for this option */
int val; int val;
/* Whether this option needs an argument */ /* Whether this option needs an argument */
int needarg; int needarg;
/* Name(s) of this option, prefixed with '-' or '--' and separated by a /* Name(s) of this option, prefixed with '-' or '--' and separated by a
* comma. E.g. "-z", "--gzip", "-z,--gzip". * comma. E.g. "-z", "--gzip", "-z,--gzip".
* An option can have any number of aliasses. * An option can have any number of aliasses.
*/ */
const char *name; const char *name;
} yopt_opt_t; } yopt_opt_t;
typedef struct { typedef struct {
int argc; int argc;
int cur; int cur;
int argsep; /* '--' found */ int argsep; /* '--' found */
char **argv; char **argv;
char *sh; char *sh;
yopt_opt_t *opts; const yopt_opt_t *opts;
char errbuf[128]; char errbuf[128];
} yopt_t; } yopt_t;
/* opts must be an array of options, terminated with an option with val=0 */ /* 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) { static inline void yopt_init(yopt_t *o, int argc, char **argv, const yopt_opt_t *opts) {
o->argc = argc; o->argc = argc;
o->argv = argv; o->argv = argv;
o->opts = opts; o->opts = opts;
o->cur = 0; o->cur = 0;
o->argsep = 0; o->argsep = 0;
o->sh = NULL; o->sh = NULL;
} }
static inline yopt_opt_t *_yopt_find(yopt_opt_t *o, const char *v) { static inline const yopt_opt_t *_yopt_find(const yopt_opt_t *o, const char *v) {
const char *tn, *tv; const char *tn, *tv;
for(; o->val; o++) { for(; o->val; o++) {
tn = o->name; tn = o->name;
while(*tn) { while(*tn) {
tv = v; tv = v;
while(*tn && *tn != ',' && *tv && *tv != '=' && *tn == *tv) { while(*tn && *tn != ',' && *tv && *tv != '=' && *tn == *tv) {
tn++; tn++;
tv++; tv++;
} }
if(!(*tn && *tn != ',') && !(*tv && *tv != '=')) if(!(*tn && *tn != ',') && !(*tv && *tv != '='))
return o; return o;
while(*tn && *tn != ',') while(*tn && *tn != ',')
tn++; tn++;
while(*tn == ',') while(*tn == ',')
tn++; tn++;
} }
} }
return NULL; return NULL;
} }
static inline int _yopt_err(yopt_t *o, char **val, const char *fmt, ...) { static inline int _yopt_err(yopt_t *o, char **val, const char *fmt, ...) {
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
vsnprintf(o->errbuf, sizeof(o->errbuf), fmt, va); vsnprintf(o->errbuf, sizeof(o->errbuf), fmt, va);
va_end(va); va_end(va);
*val = o->errbuf; *val = o->errbuf;
return -2; return -2;
} }
@ -131,62 +132,67 @@ static inline int _yopt_err(yopt_t *o, char **val, const char *fmt, ...) {
* value will be in val. * value will be in val.
*/ */
static inline int yopt_next(yopt_t *o, char **val) { static inline int yopt_next(yopt_t *o, char **val) {
yopt_opt_t *opt; const yopt_opt_t *opt;
char sh[3]; char sh[3];
*val = NULL; *val = NULL;
if(o->sh) if(o->sh)
goto inshort; goto inshort;
if(++o->cur >= o->argc) if(++o->cur >= o->argc)
return -1; return -1;
if(!o->argsep && o->argv[o->cur][0] == '-' && o->argv[o->cur][1] == '-' && o->argv[o->cur][2] == 0) { if(!o->argsep && o->argv[o->cur][0] == '-' && o->argv[o->cur][1] == '-' && o->argv[o->cur][2] == 0) {
o->argsep = 1; o->argsep = 1;
if(++o->cur >= o->argc) if(++o->cur >= o->argc)
return -1; return -1;
} }
if(o->argsep || *o->argv[o->cur] != '-') { if(o->argsep || *o->argv[o->cur] != '-') {
*val = o->argv[o->cur]; *val = o->argv[o->cur];
return 0; return 0;
} }
if(o->argv[o->cur][1] != '-') { if(o->argv[o->cur][1] != '-') {
o->sh = o->argv[o->cur]+1; o->sh = o->argv[o->cur]+1;
goto inshort; goto inshort;
} }
/* Now we're supposed to have a long option */ /* Now we're supposed to have a long option */
if(!(opt = _yopt_find(o->opts, o->argv[o->cur]))) if(!(opt = _yopt_find(o->opts, o->argv[o->cur])))
return _yopt_err(o, val, "Unknown option '%s'", o->argv[o->cur]); return _yopt_err(o, val, "Unknown option '%s'", o->argv[o->cur]);
if((*val = strchr(o->argv[o->cur], '=')) != NULL) if((*val = strchr(o->argv[o->cur], '=')) != NULL)
(*val)++; (*val)++;
if(!opt->needarg && *val) if(!opt->needarg && *val)
return _yopt_err(o, val, "Option '%s' does not accept an argument", o->argv[o->cur]); return _yopt_err(o, val, "Option '%s' does not accept an argument", o->argv[o->cur]);
if(opt->needarg && !*val) { if(opt->needarg && !*val) {
if(o->cur+1 >= o->argc) if(o->cur+1 >= o->argc)
return _yopt_err(o, val, "Option '%s' requires an argument", o->argv[o->cur]); return _yopt_err(o, val, "Option '%s' requires an argument", o->argv[o->cur]);
*val = o->argv[++o->cur]; *val = o->argv[++o->cur];
} }
return opt->val; return opt->val;
/* And here we're supposed to have a short option */ /* And here we're supposed to have a short option */
inshort: inshort:
sh[0] = '-'; sh[0] = '-';
sh[1] = *o->sh; sh[1] = *o->sh;
sh[2] = 0; sh[2] = 0;
if(!(opt = _yopt_find(o->opts, sh))) if(!(opt = _yopt_find(o->opts, sh)))
return _yopt_err(o, val, "Unknown option '%s'", sh); return _yopt_err(o, val, "Unknown option '%s'", sh);
o->sh++; o->sh++;
if(opt->needarg && *o->sh) if(opt->needarg && *o->sh)
*val = o->sh; *val = o->sh;
else if(opt->needarg) { else if(opt->needarg) {
if(++o->cur >= o->argc) if(++o->cur >= o->argc)
return _yopt_err(o, val, "Option '%s' requires an argument", sh); return _yopt_err(o, val, "Option '%s' requires an argument", sh);
*val = o->argv[o->cur]; *val = o->argv[o->cur];
} }
if(!*o->sh || opt->needarg) if(!*o->sh || opt->needarg)
o->sh = NULL; o->sh = NULL;
return opt->val; return opt->val;
} }
#endif
/* vim: set noet sw=4 ts=4: */