]> git.kaiwu.me - klib.git/commitdiff
optionally declare kseq_*() as global functions
authorHeng Li <lh3@live.co.uk>
Thu, 29 Dec 2011 15:52:48 +0000 (10:52 -0500)
committerHeng Li <lh3@live.co.uk>
Thu, 29 Dec 2011 15:52:48 +0000 (10:52 -0500)
khash.h
kseq.h

diff --git a/khash.h b/khash.h
index 0c121a9f8329e454ac46a8a7133a7cfa073bb3fa..a482fe1d878bce1f4c90750cf59988aaa60828f1 100644 (file)
--- a/khash.h
+++ b/khash.h
@@ -47,6 +47,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
@@ -156,13 +160,16 @@ typedef khint_t khiter_t;
 
 static const double __ac_HASH_UPPER = 0.77;
 
+#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_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;                                                                                                        \
+       __KHASH_TYPE(name, khkey_t, khval_t)                                                            \
        extern kh_##name##_t *kh_init_##name();                                                         \
        extern void kh_destroy_##name(kh_##name##_t *h);                                        \
        extern void kh_clear_##name(kh_##name##_t *h);                                          \
@@ -172,12 +179,7 @@ static const double __ac_HASH_UPPER = 0.77;
        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;                                                                                                        \
+       __KHASH_TYPE(name, khkey_t, khval_t)                                                            \
        SCOPE kh_##name##_t *kh_init_##name() {                                                         \
                return (kh_##name##_t*)calloc(1, sizeof(kh_##name##_t));                \
        }                                                                                                                                       \
diff --git a/kseq.h b/kseq.h
index 8b2bd3b5b32c0efc42dea5720eb106fce659c92e..437fefb16be80cb930c02690bb167d990c804072 100644 (file)
--- a/kseq.h
+++ b/kseq.h
@@ -23,7 +23,7 @@
    SOFTWARE.
 */
 
-/* Last Modified: 18AUG2011 */
+/* Last Modified: 29DEC2011 */
 
 #ifndef AC_KSEQ_H
 #define AC_KSEQ_H
@@ -51,7 +51,7 @@
        {                                                                                                                               \
                kstream_t *ks = (kstream_t*)calloc(1, sizeof(kstream_t));       \
                ks->f = f;                                                                                                      \
-               ks->buf = (unsigned char*)malloc(__bufsize);                            \
+               ks->buf = malloc(__bufsize);                                                            \
                return ks;                                                                                                      \
        }                                                                                                                               \
        static inline void ks_destroy(kstream_t *ks)                                    \
@@ -113,7 +113,7 @@ typedef struct __kstring_t {
                                for (i = ks->begin; i < ks->end; ++i)                                   \
                                        if (isspace(ks->buf[i]) && ks->buf[i] != ' ') break; \
                        } else i = 0; /* never come to here! */                                         \
-                       if (str->m - str->l < (size_t)i - ks->begin + 1) {                      \
+                       if (str->m - str->l < i - ks->begin + 1) {                                      \
                                str->m = str->l + (i - ks->begin) + 1;                                  \
                                kroundup32(str->m);                                                                             \
                                str->s = (char*)realloc(str->s, str->m);                                \
@@ -142,19 +142,19 @@ typedef struct __kstring_t {
        __KS_GETC(__read, __bufsize)                            \
        __KS_GETUNTIL(__read, __bufsize)
 
-#define __KSEQ_BASIC(type_t)                                                                                   \
-       static inline kseq_t *kseq_init(type_t fd)                                                      \
+#define __KSEQ_BASIC(SCOPE, type_t)                                                                            \
+       SCOPE kseq_t *kseq_init(type_t fd)                                                                      \
        {                                                                                                                                       \
                kseq_t *s = (kseq_t*)calloc(1, sizeof(kseq_t));                                 \
                s->f = ks_init(fd);                                                                                             \
                return s;                                                                                                               \
        }                                                                                                                                       \
-       static inline void kseq_rewind(kseq_t *ks)                                                      \
+       SCOPE void kseq_rewind(kseq_t *ks)                                                                      \
        {                                                                                                                                       \
                ks->last_char = 0;                                                                                              \
                ks->f->is_eof = ks->f->begin = ks->f->end = 0;                                  \
        }                                                                                                                                       \
-       static inline void kseq_destroy(kseq_t *ks)                                                     \
+       SCOPE void kseq_destroy(kseq_t *ks)                                                                     \
        {                                                                                                                                       \
                if (!ks) return;                                                                                                \
                free(ks->name.s); free(ks->comment.s); free(ks->seq.s); free(ks->qual.s); \
@@ -167,8 +167,8 @@ typedef struct __kstring_t {
    -1   end-of-file
    -2   truncated quality string
  */
-#define __KSEQ_READ \
-       static int kseq_read(kseq_t *seq) \
+#define __KSEQ_READ(SCOPE) \
+       SCOPE int kseq_read(kseq_t *seq) \
        { \
                int c; \
                kstream_t *ks = seq->f; \
@@ -215,10 +215,20 @@ typedef struct __kstring_t {
                kstream_t *f;                                                   \
        } kseq_t;
 
-#define KSEQ_INIT(type_t, __read)                              \
-       KSTREAM_INIT(type_t, __read, 16384)                     \
+#define KSEQ_INIT2(SCOPE, type_t, __read)              \
+       KSTREAM_INIT(type_t, __read, 4096)                      \
        __KSEQ_TYPE(type_t)                                                     \
-       __KSEQ_BASIC(type_t)                                            \
-       __KSEQ_READ
+       __KSEQ_BASIC(SCOPE, type_t)                                     \
+       __KSEQ_READ(SCOPE)
+
+#define KSEQ_INIT(type_t, __read) KSEQ_INIT2(static, type_t, __read)
+
+#define KSEQ_DECLARE(type_t) \
+       __KS_TYPE(type_t) \
+       __KSEQ_TYPE(type_t) \
+       extern kseq_t *kseq_init(type_t fd); \
+       extern void kseq_rewind(kseq_t *ks); \
+       void kseq_destroy(kseq_t *ks); \
+       int kseq_read(kseq_t *seq);
 
 #endif