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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/* MIT License
*
* Copyright (c) 2023 Tyge Løvset
*
* 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.
*/
/*
#define i_implement
#include <stc/cstr.h>
#include <stc/forward.h>
forward_cvec(cvec_i32, int);
struct MyStruct {
cvec_i32 int_vec;
cstr name;
} typedef MyStruct;
#define i_key float
#include <stc/cvec.h>
#define i_key_str // special for cstr
#include <stc/cvec.h>
#define i_key int
#define i_is_forward // forward declared
#define i_tag i32
#include <stc/cvec.h>
int main() {
cvec_i32 vec = {0};
cvec_i32_push(&vec, 123);
cvec_i32_drop(&vec);
cvec_float fvec = {0};
cvec_float_push(&fvec, 123.3);
cvec_float_drop(&fvec);
cvec_str svec = {0};
cvec_str_emplace(&svec, "Hello, friend");
cvec_str_drop(&svec);
}
*/
#include "ccommon.h"
#ifndef CVEC_H_INCLUDED
#include "forward.h"
#include <stdlib.h>
#include <string.h>
#define _it2_ptr(it1, it2) (it1.ref && !it2.ref ? it2.end : it2.ref)
#define _it_ptr(it) (it.ref ? it.ref : it.end)
#endif // CVEC_H_INCLUDED
#define _i_prefix cvec_
#include "priv/template.h"
#ifndef i_is_forward
_cx_deftypes(_c_cvec_types, _cx_self, i_key);
#endif
typedef i_keyraw _cx_raw;
STC_API _cx_self _cx_memb(_init)(void);
STC_API void _cx_memb(_drop)(_cx_self* self);
STC_API void _cx_memb(_clear)(_cx_self* self);
STC_API bool _cx_memb(_reserve)(_cx_self* self, intptr_t cap);
STC_API bool _cx_memb(_resize)(_cx_self* self, intptr_t size, i_key null);
STC_API _cx_value* _cx_memb(_push)(_cx_self* self, i_key value);
STC_API _cx_iter _cx_memb(_erase_n)(_cx_self* self, intptr_t idx, intptr_t n);
STC_API _cx_iter _cx_memb(_insert_uninit)(_cx_self* self, intptr_t idx, intptr_t n);
#if !defined i_no_cmp || defined _i_has_eq
STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, _cx_raw raw);
#endif
#ifndef i_no_cmp
STC_API int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y);
STC_API _cx_iter _cx_memb(_binary_search_in)(_cx_iter it1, _cx_iter it2, _cx_raw raw, _cx_iter* lower_bound);
#endif
STC_INLINE void _cx_memb(_value_drop)(_cx_value* val) { i_keydrop(val); }
#if !defined i_no_emplace
STC_API _cx_iter
_cx_memb(_emplace_n)(_cx_self* self, intptr_t idx, const _cx_raw raw[], intptr_t n);
STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) {
return _cx_memb(_push)(self, i_keyfrom(raw));
}
STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, _cx_raw raw) {
return _cx_memb(_push)(self, i_keyfrom(raw));
}
STC_INLINE _cx_iter _cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, _cx_raw raw) {
return _cx_memb(_emplace_n)(self, _it_ptr(it) - self->data, &raw, 1);
}
#endif // !i_no_emplace
#if !defined i_no_clone
STC_API _cx_self _cx_memb(_clone)(_cx_self cx);
STC_API _cx_iter _cx_memb(_copy_n)(_cx_self* self, intptr_t idx, const _cx_value arr[], intptr_t n);
STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n)
{ while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); }
STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n)
{ _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; }
STC_INLINE i_key _cx_memb(_value_clone)(_cx_value val)
{ return i_keyclone(val); }
STC_INLINE void _cx_memb(_copy)(_cx_self* self, const _cx_self* other) {
if (self->data == other->data) return;
_cx_memb(_clear)(self);
_cx_memb(_copy_n)(self, 0, other->data, other->_len);
}
#endif // !i_no_clone
STC_INLINE intptr_t _cx_memb(_size)(const _cx_self* self) { return self->_len; }
STC_INLINE intptr_t _cx_memb(_capacity)(const _cx_self* self) { return self->_cap; }
STC_INLINE bool _cx_memb(_empty)(const _cx_self* self) { return !self->_len; }
STC_INLINE _cx_raw _cx_memb(_value_toraw)(const _cx_value* val) { return i_keyto(val); }
STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) { return self->data; }
STC_INLINE _cx_value* _cx_memb(_back)(const _cx_self* self)
{ return self->data + self->_len - 1; }
STC_INLINE void _cx_memb(_pop)(_cx_self* self)
{ assert(!_cx_memb(_empty)(self)); _cx_value* p = &self->data[--self->_len]; i_keydrop(p); }
STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, i_key value)
{ return _cx_memb(_push)(self, value); }
STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) { _cx_memb(_pop)(self); }
STC_INLINE _cx_self
_cx_memb(_with_size)(const intptr_t size, i_key null) {
_cx_self cx = _cx_memb(_init)();
_cx_memb(_resize)(&cx, size, null);
return cx;
}
STC_INLINE _cx_self
_cx_memb(_with_capacity)(const intptr_t cap) {
_cx_self cx = _cx_memb(_init)();
_cx_memb(_reserve)(&cx, cap);
return cx;
}
STC_INLINE void
_cx_memb(_shrink_to_fit)(_cx_self* self) {
_cx_memb(_reserve)(self, _cx_memb(_size)(self));
}
STC_INLINE _cx_iter
_cx_memb(_insert_n)(_cx_self* self, const intptr_t idx, const _cx_value arr[], const intptr_t n) {
_cx_iter it = _cx_memb(_insert_uninit)(self, idx, n);
if (it.ref)
c_memcpy(it.ref, arr, n*c_sizeof *arr);
return it;
}
STC_INLINE _cx_iter
_cx_memb(_insert)(_cx_self* self, const intptr_t idx, const i_key value) {
return _cx_memb(_insert_n)(self, idx, &value, 1);
}
STC_INLINE _cx_iter
_cx_memb(_insert_at)(_cx_self* self, _cx_iter it, const i_key value) {
return _cx_memb(_insert_n)(self, _it_ptr(it) - self->data, &value, 1);
}
STC_INLINE _cx_iter
_cx_memb(_erase_at)(_cx_self* self, _cx_iter it) {
return _cx_memb(_erase_n)(self, it.ref - self->data, 1);
}
STC_INLINE _cx_iter
_cx_memb(_erase_range)(_cx_self* self, _cx_iter i1, _cx_iter i2) {
return _cx_memb(_erase_n)(self, i1.ref - self->data, _it2_ptr(i1, i2) - i1.ref);
}
STC_INLINE const _cx_value*
_cx_memb(_at)(const _cx_self* self, const intptr_t idx) {
assert(idx < self->_len); return self->data + idx;
}
STC_INLINE _cx_value*
_cx_memb(_at_mut)(_cx_self* self, const intptr_t idx) {
assert(idx < self->_len); return self->data + idx;
}
STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) {
intptr_t n = self->_len;
return c_LITERAL(_cx_iter){n ? self->data : NULL, self->data + n};
}
STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self)
{ return c_LITERAL(_cx_iter){NULL, self->data + self->_len}; }
STC_INLINE void _cx_memb(_next)(_cx_iter* it)
{ if (++it->ref == it->end) it->ref = NULL; }
STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, size_t n)
{ if ((it.ref += n) >= it.end) it.ref = NULL; return it; }
STC_INLINE intptr_t _cx_memb(_index)(const _cx_self* self, _cx_iter it)
{ return (it.ref - self->data); }
STC_INLINE void _cx_memb(_adjust_end_)(_cx_self* self, intptr_t n)
{ self->_len += n; }
#if !defined i_no_cmp || defined _i_has_eq
STC_INLINE _cx_iter
_cx_memb(_find)(const _cx_self* self, _cx_raw raw) {
return _cx_memb(_find_in)(_cx_memb(_begin)(self), _cx_memb(_end)(self), raw);
}
STC_INLINE const _cx_value*
_cx_memb(_get)(const _cx_self* self, _cx_raw raw) {
return _cx_memb(_find)(self, raw).ref;
}
STC_INLINE _cx_value*
_cx_memb(_get_mut)(const _cx_self* self, _cx_raw raw)
{ return (_cx_value*) _cx_memb(_get)(self, raw); }
STC_INLINE bool
_cx_memb(_eq)(const _cx_self* self, const _cx_self* other) {
if (self->_len != other->_len) return false;
for (intptr_t i = 0; i < self->_len; ++i) {
const _cx_raw _rx = i_keyto(self->data+i), _ry = i_keyto(other->data+i);
if (!(i_eq((&_rx), (&_ry)))) return false;
}
return true;
}
#endif
#ifndef i_no_cmp
STC_INLINE _cx_iter
_cx_memb(_binary_search)(const _cx_self* self, _cx_raw raw) {
_cx_iter lower;
return _cx_memb(_binary_search_in)(_cx_memb(_begin)(self), _cx_memb(_end)(self), raw, &lower);
}
STC_INLINE _cx_iter
_cx_memb(_lower_bound)(const _cx_self* self, _cx_raw raw) {
_cx_iter lower;
_cx_memb(_binary_search_in)(_cx_memb(_begin)(self), _cx_memb(_end)(self), raw, &lower);
return lower;
}
STC_INLINE void
_cx_memb(_sort_range)(_cx_iter i1, _cx_iter i2, int(*cmp)(const _cx_value*, const _cx_value*)) {
qsort(i1.ref, (size_t)(_it2_ptr(i1, i2) - i1.ref), sizeof(_cx_value),
(int(*)(const void*, const void*)) cmp);
}
STC_INLINE void
_cx_memb(_sort)(_cx_self* self) {
_cx_memb(_sort_range)(_cx_memb(_begin)(self), _cx_memb(_end)(self), _cx_memb(_value_cmp));
}
#endif // !c_no_cmp
/* -------------------------- IMPLEMENTATION ------------------------- */
#if defined(i_implement)
STC_DEF _cx_self
_cx_memb(_init)(void) {
return c_LITERAL(_cx_self){NULL};
}
STC_DEF void
_cx_memb(_clear)(_cx_self* self) {
if (self->_cap) {
for (_cx_value *p = self->data, *q = p + self->_len; p != q; ) {
--q; i_keydrop(q);
}
self->_len = 0;
}
}
STC_DEF void
_cx_memb(_drop)(_cx_self* self) {
if (self->_cap == 0)
return;
_cx_memb(_clear)(self);
i_free(self->data);
}
STC_DEF bool
_cx_memb(_reserve)(_cx_self* self, const intptr_t cap) {
if (cap > self->_cap || (cap && cap == self->_len)) {
_cx_value* d = (_cx_value*)i_realloc(self->data, cap*c_sizeof(i_key));
if (!d)
return false;
self->data = d;
self->_cap = cap;
}
return true;
}
STC_DEF bool
_cx_memb(_resize)(_cx_self* self, const intptr_t len, i_key null) {
if (!_cx_memb(_reserve)(self, len))
return false;
const intptr_t n = self->_len;
for (intptr_t i = len; i < n; ++i)
{ i_keydrop((self->data + i)); }
for (intptr_t i = n; i < len; ++i)
self->data[i] = null;
self->_len = len;
return true;
}
STC_DEF _cx_value*
_cx_memb(_push)(_cx_self* self, i_key value) {
if (self->_len == self->_cap)
if (!_cx_memb(_reserve)(self, self->_len*3/2 + 4))
return NULL;
_cx_value *v = self->data + self->_len++;
*v = value;
return v;
}
STC_DEF _cx_iter
_cx_memb(_insert_uninit)(_cx_self* self, const intptr_t idx, const intptr_t n) {
if (self->_len + n > self->_cap)
if (!_cx_memb(_reserve)(self, self->_len*3/2 + n))
return _cx_memb(_end)(self);
_cx_value* pos = self->data + idx;
c_memmove(pos + n, pos, (self->_len - idx)*c_sizeof *pos);
self->_len += n;
return c_LITERAL(_cx_iter){pos, self->data + self->_len};
}
STC_DEF _cx_iter
_cx_memb(_erase_n)(_cx_self* self, const intptr_t idx, const intptr_t len) {
_cx_value* d = self->data + idx, *p = d, *end = self->data + self->_len;
for (intptr_t i = 0; i < len; ++i, ++p)
{ i_keydrop(p); }
c_memmove(d, p, (end - p)*c_sizeof *d);
self->_len -= len;
return c_LITERAL(_cx_iter){p == end ? NULL : d, end - len};
}
#if !defined i_no_clone
STC_DEF _cx_self
_cx_memb(_clone)(_cx_self cx) {
_cx_self out = _cx_memb(_init)();
_cx_memb(_copy_n)(&out, 0, cx.data, cx._len);
return out;
}
STC_DEF _cx_iter
_cx_memb(_copy_n)(_cx_self* self, const intptr_t idx,
const _cx_value arr[], const intptr_t n) {
_cx_iter it = _cx_memb(_insert_uninit)(self, idx, n);
if (it.ref)
for (_cx_value* p = it.ref, *q = p + n; p != q; ++arr)
*p++ = i_keyclone((*arr));
return it;
}
#endif // !i_no_clone
#if !defined i_no_emplace
STC_DEF _cx_iter
_cx_memb(_emplace_n)(_cx_self* self, const intptr_t idx, const _cx_raw raw[], intptr_t n) {
_cx_iter it = _cx_memb(_insert_uninit)(self, idx, n);
if (it.ref)
for (_cx_value* p = it.ref; n--; ++raw, ++p)
*p = i_keyfrom((*raw));
return it;
}
#endif // !i_no_emplace
#if !defined i_no_cmp || defined _i_has_eq
STC_DEF _cx_iter
_cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, _cx_raw raw) {
const _cx_value* p2 = _it2_ptr(i1, i2);
for (; i1.ref != p2; ++i1.ref) {
const _cx_raw r = i_keyto(i1.ref);
if (i_eq((&raw), (&r)))
return i1;
}
i2.ref = NULL;
return i2;
}
#endif
#ifndef i_no_cmp
STC_DEF _cx_iter
_cx_memb(_binary_search_in)(_cx_iter i1, _cx_iter i2, const _cx_raw raw,
_cx_iter* lower_bound) {
_cx_value* w[2] = {i1.ref, _it2_ptr(i1, i2)};
_cx_iter mid = i1;
while (w[0] != w[1]) {
mid.ref = w[0] + (w[1] - w[0])/2;
const _cx_raw m = i_keyto(mid.ref);
const int c = i_cmp((&raw), (&m));
if (!c) return *lower_bound = mid;
w[c < 0] = mid.ref + (c > 0);
}
i1.ref = w[0] == i2.end ? NULL : w[0];
*lower_bound = i1;
i1.ref = NULL; return i1;
}
STC_DEF int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) {
const _cx_raw rx = i_keyto(x);
const _cx_raw ry = i_keyto(y);
return i_cmp((&rx), (&ry));
}
#endif // !c_no_cmp
#endif // i_implement
#define CVEC_H_INCLUDED
#include "priv/template2.h"
|