summaryrefslogtreecommitdiffhomepage
path: root/include/stc/cvec.h
blob: 3dc7b4c16d5b582c5fa502119a851046f44d927f (plain)
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
/* MIT License
 *
 * Copyright (c) 2021 Tyge Løvset, NORCE, www.norceresearch.no
 *
 * 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.
 */
 
/*
#include <stc/cstr.h>
#include <stc/forward.h>

forward_cvec(i32, int);

struct MyStruct {
    cvec_i32 int_vec;
    cstr name;
} typedef MyStruct;

#define i_val float
#include <stc/cvec.h>

#define i_val_str // special for cstr
#include <stc/cvec.h>

#define F_tag i32 // forward declared above; shorthand for i_fwd
#define i_val int
#include <stc/cvec.h>

int main() {
    cvec_i32 vec = cvec_i32_init();
    cvec_i32_push_back(&vec, 123);
    cvec_i32_del(&vec);

    cvec_float fvec = cvec_float_init();
    cvec_float_push_back(&fvec, 123.3);
    cvec_float_del(&fvec);

    cvec_str svec = cvec_str_init();
    cvec_str_emplace_back(&svec, "Hello, friend");
    cvec_str_del(&svec);
}
*/

#ifndef CVEC_H_INCLUDED
#include "ccommon.h"
#include "forward.h"
#include <stdlib.h>
#include <string.h>

struct cvec_rep { size_t size, cap; void* data[]; };
#define cvec_rep_(self) c_container_of((self)->data, struct cvec_rep, data)
#endif // CVEC_H_INCLUDED

#ifndef i_prefix
#define i_prefix cvec_
#endif
#include "template.h"

#if !defined i_fwd
   cx_deftypes(_c_cvec_types, Self, i_val);
#endif
typedef i_valraw cx_rawvalue_t;

STC_API Self            cx_memb(_init)(void);
STC_API Self            cx_memb(_clone)(Self cx);
STC_API void            cx_memb(_del)(Self* self);
STC_API void            cx_memb(_clear)(Self* self);
STC_API void            cx_memb(_reserve)(Self* self, size_t cap);
STC_API void            cx_memb(_resize)(Self* self, size_t size, i_val fill_val);
STC_API int             cx_memb(_value_compare)(const cx_value_t* x, const cx_value_t* y);
STC_API cx_iter_t       cx_memb(_find_in)(cx_iter_t it1, cx_iter_t it2, i_valraw raw);
STC_API cx_iter_t       cx_memb(_bsearch_in)(cx_iter_t it1, cx_iter_t it2, i_valraw raw);
STC_API cx_value_t*     cx_memb(_push_back)(Self* self, i_val value);
STC_API cx_iter_t       cx_memb(_erase_range_p)(Self* self, cx_value_t* p1, cx_value_t* p2);
STC_API cx_iter_t       cx_memb(_insert_range_p)(Self* self, cx_value_t* pos,
                                                 const cx_value_t* p1, const cx_value_t* p2, bool clone);
STC_API cx_iter_t       cx_memb(_emplace_range_p)(Self* self, cx_value_t* pos,
                                                  const cx_rawvalue_t* p1, const cx_rawvalue_t* p2);

STC_INLINE size_t       cx_memb(_size)(Self cx) { return cvec_rep_(&cx)->size; }
STC_INLINE size_t       cx_memb(_capacity)(Self cx) { return cvec_rep_(&cx)->cap; }
STC_INLINE bool         cx_memb(_empty)(Self cx) { return !cvec_rep_(&cx)->size; }
STC_INLINE i_val        cx_memb(_value_fromraw)(i_valraw raw) { return i_valfrom(raw); }
STC_INLINE i_valraw     cx_memb(_value_toraw)(cx_value_t* val) { return i_valto(val); }
STC_INLINE i_val        cx_memb(_value_clone)(cx_value_t val)
                            { return i_valfrom(i_valto(&val)); }
STC_INLINE void         cx_memb(_swap)(Self* a, Self* b) { c_swap(Self, *a, *b); }
STC_INLINE cx_value_t*  cx_memb(_front)(const Self* self) { return self->data; }
STC_INLINE cx_value_t*  cx_memb(_back)(const Self* self)
                            { return self->data + cvec_rep_(self)->size - 1; }
STC_INLINE cx_value_t*  cx_memb(_emplace_back)(Self* self, i_valraw raw)
                            { return cx_memb(_push_back)(self, i_valfrom(raw)); }
STC_INLINE void         cx_memb(_pop_back)(Self* self)
                            { i_valdel(&self->data[--cvec_rep_(self)->size]); }
STC_INLINE cx_iter_t    cx_memb(_begin)(const Self* self)
                            { return c_make(cx_iter_t){self->data}; }
STC_INLINE cx_iter_t    cx_memb(_end)(const Self* self)
                            { return c_make(cx_iter_t){self->data + cvec_rep_(self)->size}; }
STC_INLINE void         cx_memb(_next)(cx_iter_t* it) { ++it->ref; }
STC_INLINE cx_iter_t    cx_memb(_advance)(cx_iter_t it, intptr_t offs) 
                            { it.ref += offs; return it; }
STC_INLINE size_t       cx_memb(_index)(Self cx, cx_iter_t it) { return it.ref - cx.data; }

STC_INLINE Self
cx_memb(_with_size)(size_t size, i_val null_val) {
    Self cx = cx_memb(_init)();
    cx_memb(_resize)(&cx, size, null_val);
    return cx;
}

STC_INLINE Self
cx_memb(_with_capacity)(size_t size) {
    Self cx = cx_memb(_init)();
    cx_memb(_reserve)(&cx, size);
    return cx;
}

STC_INLINE void
cx_memb(_shrink_to_fit)(Self *self) {
    Self cx = cx_memb(_clone)(*self);
    cx_memb(_del)(self); *self = cx;
}

STC_INLINE void
cx_memb(_copy)(Self *self, Self other) {
    if (self->data == other.data) return;
    cx_memb(_del)(self); *self = cx_memb(_clone)(other);
}

STC_INLINE cx_iter_t
cx_memb(_insert)(Self* self, size_t idx, i_val value) {
    return cx_memb(_insert_range_p)(self, self->data + idx, &value, &value + 1, false);
}
STC_INLINE cx_iter_t
cx_memb(_insert_n)(Self* self, size_t idx, const cx_value_t arr[], size_t n) {
    return cx_memb(_insert_range_p)(self, self->data + idx, arr, arr + n, false);
}
STC_INLINE cx_iter_t
cx_memb(_insert_at)(Self* self, cx_iter_t it, i_val value) {
    return cx_memb(_insert_range_p)(self, it.ref, &value, &value + 1, false);
}

STC_INLINE cx_iter_t
cx_memb(_emplace)(Self* self, size_t idx, i_valraw raw) {
    return cx_memb(_emplace_range_p)(self, self->data + idx, &raw, &raw + 1);
}
STC_INLINE cx_iter_t
cx_memb(_emplace_n)(Self* self, size_t idx, const cx_rawvalue_t arr[], size_t n) {
    return cx_memb(_emplace_range_p)(self, self->data + idx, arr, arr + n);
}
STC_INLINE cx_iter_t
cx_memb(_emplace_at)(Self* self, cx_iter_t it, i_valraw raw) {
    return cx_memb(_emplace_range_p)(self, it.ref, &raw, &raw + 1);
}
STC_INLINE cx_iter_t
cx_memb(_emplace_range)(Self* self, cx_iter_t it, cx_iter_t it1, cx_iter_t it2) {
    return cx_memb(_insert_range_p)(self, it.ref, it1.ref, it2.ref, true);
}
STC_INLINE void
cx_memb(_emplace_items)(Self *self, const cx_rawvalue_t arr[], size_t n) {
    cx_memb(_emplace_range_p)(self, self->data + cvec_rep_(self)->size, arr, arr + n);
}

STC_INLINE cx_iter_t
cx_memb(_erase)(Self* self, size_t idx) {
    return cx_memb(_erase_range_p)(self, self->data + idx, self->data + idx + 1);
}
STC_INLINE cx_iter_t
cx_memb(_erase_n)(Self* self, size_t idx, size_t n) {
    return cx_memb(_erase_range_p)(self, self->data + idx, self->data + idx + n);
}
STC_INLINE cx_iter_t
cx_memb(_erase_at)(Self* self, cx_iter_t it) {
    return cx_memb(_erase_range_p)(self, it.ref, it.ref + 1);
}
STC_INLINE cx_iter_t
cx_memb(_erase_range)(Self* self, cx_iter_t it1, cx_iter_t it2) {
    return cx_memb(_erase_range_p)(self, it1.ref, it2.ref);
}

STC_INLINE cx_value_t*
cx_memb(_at)(const Self* self, size_t idx) {
    assert(idx < cvec_rep_(self)->size);
    return self->data + idx;
}

STC_INLINE cx_iter_t
cx_memb(_find)(const Self* self, i_valraw raw) {
    return cx_memb(_find_in)(cx_memb(_begin)(self), cx_memb(_end)(self), raw);
}

STC_INLINE cx_value_t*
cx_memb(_get)(const Self* self, i_valraw raw) {
    cx_iter_t end = cx_memb(_end)(self);
    cx_value_t* val = cx_memb(_find_in)(cx_memb(_begin)(self), end, raw).ref;
    return val == end.ref ? NULL : val;
}

STC_INLINE cx_iter_t
cx_memb(_bsearch)(const Self* self, i_valraw raw) {
    return cx_memb(_bsearch_in)(cx_memb(_begin)(self), cx_memb(_end)(self), raw);
}
STC_INLINE void
cx_memb(_sort_range)(cx_iter_t i1, cx_iter_t i2,
                     int(*_cmp_)(const cx_value_t*, const cx_value_t*)) {
    qsort(i1.ref, i2.ref - i1.ref, sizeof(cx_value_t), (int(*)(const void*, const void*)) _cmp_);
}
STC_INLINE void
cx_memb(_sort)(Self* self) {
    cx_memb(_sort_range)(cx_memb(_begin)(self), cx_memb(_end)(self), cx_memb(_value_compare));
}

/* -------------------------- IMPLEMENTATION ------------------------- */
#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) || defined(i_imp)

#ifndef CVEC_H_INCLUDED
static struct cvec_rep _cvec_sentinel = {0, 0};
#endif

STC_DEF Self
cx_memb(_init)(void) {
    Self cx = {(cx_value_t *) _cvec_sentinel.data};
    return cx;
}

STC_DEF void
cx_memb(_clear)(Self* self) {
    struct cvec_rep* rep = cvec_rep_(self); if (rep->cap) {
        for (cx_value_t *p = self->data, *q = p + rep->size; p != q; ++p)
            i_valdel(p);
        rep->size = 0;
    }
}

STC_DEF void
cx_memb(_del)(Self* self) {
    cx_memb(_clear)(self);
    if (cvec_rep_(self)->cap)
        c_free(cvec_rep_(self));
}

STC_DEF void
cx_memb(_reserve)(Self* self, size_t cap) {
    struct cvec_rep* rep = cvec_rep_(self);
    size_t len = rep->size, oldcap = rep->cap;
    if (cap > oldcap) {
        rep = (struct cvec_rep*) c_realloc(oldcap ? rep : NULL,
                                            offsetof(struct cvec_rep, data) + cap*sizeof(i_val));
        self->data = (cx_value_t*) rep->data;
        rep->size = len;
        rep->cap = cap;
    }
}

STC_DEF void
cx_memb(_resize)(Self* self, size_t len, i_val null_val) {
    cx_memb(_reserve)(self, len);
    struct cvec_rep* rep = cvec_rep_(self);
    size_t i, n = rep->size;
    for (i = len; i < n; ++i) i_valdel(self->data + i);
    for (i = n; i < len; ++i) self->data[i] = null_val;
    if (rep->cap) rep->size = len;
}

STC_DEF cx_value_t*
cx_memb(_push_back)(Self* self, i_val value) {
    size_t len = cvec_rep_(self)->size;
    if (len == cx_memb(_capacity)(*self))
        cx_memb(_reserve)(self, (len*13 >> 3) + 4);
    cx_value_t *v = self->data + cvec_rep_(self)->size++;
    *v = value; return v;
}

STC_DEF Self
cx_memb(_clone)(Self cx) {
    size_t len = cvec_rep_(&cx)->size;
    Self out = cx_memb(_with_capacity)(len);
    cx_memb(_insert_range_p)(&out, out.data, cx.data, cx.data + len, true);
    return out;
}

STC_DEF cx_value_t*
cx_memb(_insert_space_)(Self* self, cx_value_t* pos, size_t len) {
    size_t idx = pos - self->data, size = cvec_rep_(self)->size;
    if (len == 0) return pos;
    if (size + len > cx_memb(_capacity)(*self))
        cx_memb(_reserve)(self, (size*13 >> 3) + len),
        pos = self->data + idx;
    cvec_rep_(self)->size += len;
    memmove(pos + len, pos, (size - idx) * sizeof(i_val));
    return pos;
}

STC_DEF cx_iter_t
cx_memb(_insert_range_p)(Self* self, cx_value_t* pos, const cx_value_t* p1,
                                                      const cx_value_t* p2, bool clone) {
    pos = cx_memb(_insert_space_)(self, pos, p2 - p1);
    cx_iter_t it = {pos};
    if (clone) while (p1 != p2) *pos++ = i_valfrom(i_valto(p1++));
    else memcpy(pos, p1, (p2 - p1)*sizeof *p1);
    return it;
}

STC_DEF cx_iter_t
cx_memb(_emplace_range_p)(Self* self, cx_value_t* pos, const cx_rawvalue_t* p1,
                                                       const cx_rawvalue_t* p2) {
    pos = cx_memb(_insert_space_)(self, pos, p2 - p1);
    cx_iter_t it = {pos};
    while (p1 != p2) *pos++ = i_valfrom(*p1++);
    return it;
}

STC_DEF cx_iter_t
cx_memb(_erase_range_p)(Self* self, cx_value_t* p1, cx_value_t* p2) {
    intptr_t len = p2 - p1;
    if (len > 0) {
        cx_value_t* p = p1, *end = self->data + cvec_rep_(self)->size;
        while (p != p2) i_valdel(p++);
        memmove(p1, p2, (end - p2) * sizeof(i_val));
        cvec_rep_(self)->size -= len;
    }
    return c_make(cx_iter_t){.ref = p1};
}

STC_DEF cx_iter_t
cx_memb(_find_in)(cx_iter_t i1, cx_iter_t i2, i_valraw raw) {
    for (; i1.ref != i2.ref; ++i1.ref) {
        i_valraw r = i_valto(i1.ref);
        if (i_cmp(&raw, &r) == 0) return i1;
    }
    return i2;
}

STC_DEF cx_iter_t
cx_memb(_bsearch_in)(cx_iter_t i1, cx_iter_t i2, i_valraw raw) {
    cx_iter_t mid, last = i2;
    while (i1.ref != i2.ref) {
        mid.ref = i1.ref + ((i2.ref - i1.ref)>>1);
        int c; i_valraw m = i_valto(mid.ref);
        if ((c = i_cmp(&raw, &m)) == 0) return mid;
        else if (c < 0) i2.ref = mid.ref;
        else i1.ref = mid.ref + 1;
    }
    return last;
}

STC_DEF int
cx_memb(_value_compare)(const cx_value_t* x, const cx_value_t* y) {
    i_valraw rx = i_valto(x);
    i_valraw ry = i_valto(y);
    return i_cmp(&rx, &ry);
}

#endif
#include "template.h"
#define CVEC_H_INCLUDED