Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/klib/blob/master/kdq.h # klib [Ported from github.com/attractivechaos/klib] added block arena Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `klib / kdq.h` 128 lines ยท 4272 bytes Raw Blame History 1 #ifndef __AC_KDQ_H 2 #define __AC_KDQ_H 3 4 #include 5 #include 6 7 #define __KDQ_TYPE(type) \ 8 typedef struct { \ 9 size_t front:58, bits:6, count, mask; \ 10 type *a; \ 11 } kdq_##type##_t; 12 13 #define kdq_t(type) kdq_##type##_t 14 #define kdq_size(q) ((q)->count) 15 #define kdq_first(q) ((q)->a[(q)->front]) 16 #define kdq_last(q) ((q)->a[((q)->front + (q)->count - 1) & (q)->mask]) 17 #define kdq_at(q, i) ((q)->a[((q)->front + (i)) & (q)->mask]) 18 19 #define __KDQ_IMPL(type, SCOPE) \ 20 SCOPE kdq_##type##_t *kdq_init_##type() \ 21 { \ 22 kdq_##type##_t *q; \ 23 q = (kdq_##type##_t*)calloc(1, sizeof(kdq_##type##_t)); \ 24 q->bits = 2, q->mask = (1ULL<bits) - 1; \ 25 q->a = (type*)malloc((1<bits) * sizeof(type)); \ 26 return q; \ 27 } \ 28 SCOPE void kdq_destroy_##type(kdq_##type##_t *q) \ 29 { \ 30 if (q == 0) return; \ 31 free(q->a); free(q); \ 32 } \ 33 SCOPE int kdq_resize_##type(kdq_##type##_t *q, int new_bits) \ 34 { \ 35 size_t new_size = 1ULL<bits; \ 36 if (new_size < q->count) { /* not big enough */ \ 37 int i; \ 38 for (i = 0; i < 64; ++i) \ 39 if (1ULL< q->count) break; \ 40 new_bits = i, new_size = 1ULL<bits) return q->bits; /* unchanged */ \ 43 if (new_bits > q->bits) q->a = (type*)realloc(q->a, (1ULL<front + q->count <= old_size) { /* unwrapped */ \ 45 if (q->front + q->count > new_size) /* only happens for shrinking */ \ 46 memmove(q->a, q->a + new_size, (q->front + q->count - new_size) * sizeof(type)); \ 47 } else { /* wrapped */ \ 48 memmove(q->a + (new_size - (old_size - q->front)), q->a + q->front, (old_size - q->front) * sizeof(type)); \ 49 q->front = new_size - (old_size - q->front); \ 50 } \ 51 q->bits = new_bits, q->mask = (1ULL<bits) - 1; \ 52 if (new_bits < q->bits) q->a = (type*)realloc(q->a, (1ULL<bits; \ 54 } \ 55 SCOPE type *kdq_pushp_##type(kdq_##type##_t *q) \ 56 { \ 57 if (q->count == 1ULL<bits) kdq_resize_##type(q, q->bits + 1); \ 58 return &q->a[((q->count++) + q->front) & (q)->mask]; \ 59 } \ 60 SCOPE void kdq_push_##type(kdq_##type##_t *q, type v) \ 61 { \ 62 if (q->count == 1ULL<bits) kdq_resize_##type(q, q->bits + 1); \ 63 q->a[((q->count++) + q->front) & (q)->mask] = v; \ 64 } \ 65 SCOPE type *kdq_unshiftp_##type(kdq_##type##_t *q) \ 66 { \ 67 if (q->count == 1ULL<bits) kdq_resize_##type(q, q->bits + 1); \ 68 ++q->count; \ 69 q->front = q->front? q->front - 1 : (1ULL<bits) - 1; \ 70 return &q->a[q->front]; \ 71 } \ 72 SCOPE void kdq_unshift_##type(kdq_##type##_t *q, type v) \ 73 { \ 74 type *p; \ 75 p = kdq_unshiftp_##type(q); \ 76 *p = v; \ 77 } \ 78 SCOPE type *kdq_pop_##type(kdq_##type##_t *q) \ 79 { \ 80 return q->count? &q->a[((--q->count) + q->front) & q->mask] : 0; \ 81 } \ 82 SCOPE type *kdq_shift_##type(kdq_##type##_t *q) \ 83 { \ 84 type *d = 0; \ 85 if (q->count == 0) return 0; \ 86 d = &q->a[q->front++]; \ 87 q->front &= q->mask; \ 88 --q->count; \ 89 return d; \ 90 } 91 92 #define KDQ_INIT2(type, SCOPE) \ 93 __KDQ_TYPE(type) \ 94 __KDQ_IMPL(type, SCOPE) 95 96 #ifndef klib_unused 97 #if (defined __clang__ && __clang_major__ >= 3) || (defined __GNUC__ && __GNUC__ >= 3) 98 #define klib_unused __attribute__ ((__unused__)) 99 #else 100 #define klib_unused 101 #endif 102 #endif /* klib_unused */ 103 104 #define KDQ_INIT(type) KDQ_INIT2(type, static inline klib_unused) 105 106 #define KDQ_DECLARE(type) \ 107 __KDQ_TYPE(type) \ 108 kdq_##type##_t *kdq_init_##type(); \ 109 void kdq_destroy_##type(kdq_##type##_t *q); \ 110 int kdq_resize_##type(kdq_##type##_t *q, int new_bits); \ 111 type *kdq_pushp_##type(kdq_##type##_t *q); \ 112 void kdq_push_##type(kdq_##type##_t *q, type v); \ 113 type *kdq_unshiftp_##type(kdq_##type##_t *q); \ 114 void kdq_unshift_##type(kdq_##type##_t *q, type v); \ 115 type *kdq_pop_##type(kdq_##type##_t *q); \ 116 type *kdq_shift_##type(kdq_##type##_t *q); 117 118 #define kdq_init(type) kdq_init_##type() 119 #define kdq_destroy(type, q) kdq_destroy_##type(q) 120 #define kdq_resize(type, q, new_bits) kdq_resize_##type(q, new_bits) 121 #define kdq_pushp(type, q) kdq_pushp_##type(q) 122 #define kdq_push(type, q, v) kdq_push_##type(q, v) 123 #define kdq_pop(type, q) kdq_pop_##type(q) 124 #define kdq_unshiftp(type, q) kdq_unshiftp_##type(q) 125 #define kdq_unshift(type, q, v) kdq_unshift_##type(q, v) 126 #define kdq_shift(type, q) kdq_shift_##type(q) 127 128 #endif