annotate PsiCLASS-1.0.2/samtools-0.1.19/klist.h @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 #ifndef _LH3_KLIST_H
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #define _LH3_KLIST_H
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 #include <stdlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 #define KMEMPOOL_INIT(name, kmptype_t, kmpfree_f) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 typedef struct { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 size_t cnt, n, max; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 kmptype_t **buf; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 } kmp_##name##_t; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 static inline kmp_##name##_t *kmp_init_##name() { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 return calloc(1, sizeof(kmp_##name##_t)); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 static inline void kmp_destroy_##name(kmp_##name##_t *mp) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 size_t k; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 for (k = 0; k < mp->n; ++k) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 kmpfree_f(mp->buf[k]); free(mp->buf[k]); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 free(mp->buf); free(mp); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 static inline kmptype_t *kmp_alloc_##name(kmp_##name##_t *mp) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 ++mp->cnt; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 if (mp->n == 0) return calloc(1, sizeof(kmptype_t)); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 return mp->buf[--mp->n]; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 static inline void kmp_free_##name(kmp_##name##_t *mp, kmptype_t *p) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 --mp->cnt; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 if (mp->n == mp->max) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 mp->max = mp->max? mp->max<<1 : 16; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 mp->buf = realloc(mp->buf, sizeof(void*) * mp->max); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 mp->buf[mp->n++] = p; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 #define kmempool_t(name) kmp_##name##_t
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 #define kmp_init(name) kmp_init_##name()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 #define kmp_destroy(name, mp) kmp_destroy_##name(mp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 #define kmp_alloc(name, mp) kmp_alloc_##name(mp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 #define kmp_free(name, mp, p) kmp_free_##name(mp, p)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 #define KLIST_INIT(name, kltype_t, kmpfree_t) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 struct __kl1_##name { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 kltype_t data; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 struct __kl1_##name *next; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 }; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 typedef struct __kl1_##name kl1_##name; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 KMEMPOOL_INIT(name, kl1_##name, kmpfree_t) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 typedef struct { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 kl1_##name *head, *tail; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 kmp_##name##_t *mp; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 size_t size; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 } kl_##name##_t; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 static inline kl_##name##_t *kl_init_##name() { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 kl_##name##_t *kl = calloc(1, sizeof(kl_##name##_t)); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 kl->mp = kmp_init(name); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 kl->head = kl->tail = kmp_alloc(name, kl->mp); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 kl->head->next = 0; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 return kl; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 static inline void kl_destroy_##name(kl_##name##_t *kl) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 kl1_##name *p; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 for (p = kl->head; p != kl->tail; p = p->next) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 kmp_free(name, kl->mp, p); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 kmp_free(name, kl->mp, p); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 kmp_destroy(name, kl->mp); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 free(kl); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 static inline kltype_t *kl_pushp_##name(kl_##name##_t *kl) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 kl1_##name *q, *p = kmp_alloc(name, kl->mp); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 q = kl->tail; p->next = 0; kl->tail->next = p; kl->tail = p; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 ++kl->size; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 return &q->data; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 static inline int kl_shift_##name(kl_##name##_t *kl, kltype_t *d) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 kl1_##name *p; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 if (kl->head->next == 0) return -1; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 --kl->size; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 p = kl->head; kl->head = kl->head->next; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 if (d) *d = p->data; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 kmp_free(name, kl->mp, p); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 return 0; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 #define kliter_t(name) kl1_##name
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 #define klist_t(name) kl_##name##_t
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 #define kl_val(iter) ((iter)->data)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 #define kl_next(iter) ((iter)->next)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 #define kl_begin(kl) ((kl)->head)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 #define kl_end(kl) ((kl)->tail)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 #define kl_init(name) kl_init_##name()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 #define kl_destroy(name, kl) kl_destroy_##name(kl)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 #define kl_pushp(name, kl) kl_pushp_##name(kl)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 #define kl_shift(name, kl, d) kl_shift_##name(kl, d)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 #endif