forked from camel-cdr/cauldron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstretchy-buffer.h
156 lines (122 loc) · 5.11 KB
/
stretchy-buffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* stretchy-buffer.h -- generic dynamic array
* Olaf Bernstein <[email protected]>
* Distributed under the MIT license, see license at the end of the file.
* New versions available at https://github.com/camel-cdr/cauldron
*
* Inspired by Sean Barrett's stretchy-buffers and klib's kvec.
*
* Note that any arguments passed to a sb_* function macros is potentially
* evaluated multiple times except for arguments that have the name v in the
* code bellow.
*/
#ifndef STRETCHY_BUFFER_H_INCLUDED
/* by default: exit on allocation failure */
#if !(defined sb_malloc) || !(defined sb_realloc)
#include <stdlib.h>
#include <stdio.h>
static inline void *
sb__realloc(void *ptr, size_t num_bytes)
{
if (!(ptr = realloc(ptr, num_bytes)))
perror("realloc failed"), exit(EXIT_FAILURE);
return ptr;
}
#endif
#ifndef sb_malloc
#define sb_malloc(s) sb__realloc((void*)0, s)
#endif
#ifndef sb_realloc
#define sb_realloc(p,s) sb__realloc(p, s)
#endif
/* can be zero initialized */
#define Sb(T) struct { T *at; size_t _len, _cap; }
#define sb_len(a) (+(a)._len)
#define sb_cap(a) (+(a)._cap)
#define sb_begin(a) ((a).at)
#define sb_last(a) ((a).at + (a)._len - 1)
#define sb_end(a) ((a).at + (a)._len)
#define sb_initcap(a,n) ((a)->_len = 0, (a)->_cap = (n), \
(a)->at = sb_malloc((a)->_cap * sizeof *(a)->at))
#define sb_initlen(a,n) ((a)->_len = (a)->_cap = (n), \
(a)->at = sb_malloc((a)->_cap * sizeof *(a)->at))
#define sb_cpy(dest, src) \
(sb_setlen((dest), (src)._len), \
memcpy((dest)->at, (src).at, (dest)->_len * sizeof *(dest)->at))
#define sb_cpymem(dest, src, n) \
(assert(sizeof *(dest)->at == sizeof *(src)), \
sb_setlen((dest), (n)), \
memcpy((dest)->at, (src), (dest)->_len * sizeof *(dest)->at))
#define sb_cpyarr(dest, src) \
(sb_setlen((dest), sizeof (src) / sizeof *(src)), \
memcpy((dest)->at, (src), sizeof (src)))
#define sb_setlen(a,n) ((a)->_len = (n), sb_setcap((a), (a)->_len))
#define sb_setcap(a,n) ((a)->_cap < (n) ? \
((a)->_cap = ((a)->_cap * 3 >> 1) | (n), \
(a)->at = sb_realloc((a)->at, (a)->_cap * sizeof *(a)->at)) : 0)
#define sb_reserve(a,n) sb_setcap((a), (a)->_cap + (n))
#define sb_push(a,v) (sb_setlen((a), (a)->_len + 1), (a)->at[(a)->_len - 1] = (v))
#define sb_addn(a,n) sb_setlen((a), (a)->_len + (n))
#define sb_free(a) (free((a)->at), (a)->at = 0, (a)->_len = (a)->_cap = 0)
#define sb_shrink(a) ((((a)->_cap = (a)->_len) == 0) ? sb_free(a), 0 : \
((a)->at = realloc((a)->at, (a)->_cap * sizeof *(a)->at), 0))
/* n <= sb_len && n > 0*/
#define sb_popn(a,n) (assert((n) <= (a)->_len && (n) > 0), (a)->_len -= (n))
#define sb_pop(a) (sb_popn((a), 1))
/* n + i <= sb_len && i >= 0 && n > 0 */
#define sb_rmn(a,i,n) (assert((n) + (i) <= (a)->_len && (i) >= 0 && (n) > 0), \
memmove((a)->at + (i), (a)->at + (i) + (n), \
(((a)->_len -= (n)) - (i)) * sizeof *(a)->at))
#define sb_rm(a,i) sb_rmn((a), (i), 1)
/* faster rm, that doesn't preserve order */
/* n + i <= sb_len && i >= 0 && n > 0*/
#define sb_rmn_unstable(a,i,n) \
(assert((n) + (i) <= (a)->_len && (i) >= 0 && (n) > 0), \
memmove((a)->at + (i), \
(a)->at + ((a)->_len -= (n)) - 1, \
(n) * sizeof *(a)->at))
#define sb_rm_unstable(a,i) ((a)->at[i] = (a)->at[(a)->_len -= 1])
/* 0 <= i <= sb_len */
#define sb_insn(a,i,n) (assert(0 <= (i) && (i) <= (a)->_len), \
(sb_addn((a), (n)), \
memmove((a)->at + (i) + (n), (a)->at + (i), \
((a)->_len - (n) - (i)) * sizeof *(a)->at)))
#define sb_ins(a,i,v) (sb_insn((a), (i), 1), (a)->at[i] = (v))
#define STRETCHY_BUFFER_H_INCLUDED
#endif
/*
* Example:
*/
#ifdef STRETCHY_BUFFER_EXAMPLE
#include <stdlib.h>
#include <string.h>
int
main(void)
{
/* You could also typedef Sb(int) to be able to pass it to functions */
int i;
Sb(int) fib = { 0 };
sb_push(&fib, 1);
sb_push(&fib, 1);
for (i = 2; i < 32; ++i)
sb_push(&fib, fib.at[i-1] + fib.at[i-2]);
return 0;
}
#endif /* STRETCHY_BUFFER_EXAMPLE */
/*
* Copyright (c) 2022 Olaf Berstein
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 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.
*/