chiark / gitweb /
Switch to GPL v3
[disorder] / lib / vector.h
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
5aff007d 3 * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
b8956e9e 18/** @file lib/vector.h @brief Dynamic array template */
460b9539 19
20#ifndef VECTOR_H
21#define VECTOR_H
22
05b75f8d
RK
23#include "mem.h"
24
b8956e9e
RK
25/** @brief Dynamic array template
26 * @param NAME type name
27 * @param ETYPE element type
28 * @param REALLOC realloc function
29 *
30 * Defines @c struct @p NAME as a dynamic array with element type @p
31 * ETYPE. @p REALLOC should have the same signature as realloc() and
32 * will be used for all memory allocation. Typically it would be
33 * xrealloc() for pointer-containing element types and
34 * xrealloc_noptr() for pointer-free element types.
35 *
36 * Clients are inspected to read the @p vec member of the structure,
37 * which points to the first element, and the @p nvec member, which is
38 * the number of elements. It is safe to reduce @p nvec. Do not
39 * touch any other members.
40 *
41 * The functions defined are:
42 * - NAME_init(struct NAME *v) which initializes @p v
43 * - NAME_append(struct NAME *v, ETYPE value) which appends @p value to @p v
44 * - NAME_terminate(struct NAME *v) which zeroes out the element beyond the last
45 */
460b9539 46#define VECTOR_TYPE(NAME,ETYPE,REALLOC) \
47 \
48struct NAME { \
ff8ddcef 49 /** @brief Pointer to elements */ \
460b9539 50 ETYPE *vec; \
ff8ddcef
RK
51 /** @brief Number of elements */ \
52 int nvec; \
53 /** @brief Number of slots */ \
54 int nslots; \
460b9539 55}; \
56 \
57static inline void NAME##_init(struct NAME *v) { \
58 memset(v, 0, sizeof *v); \
59} \
60 \
61static inline void NAME##_append(struct NAME *v, ETYPE val) { \
62 if(v->nvec >= v->nslots) { \
63 v->nslots = v->nslots ? 2 * v->nslots : 16; \
64 v->vec = REALLOC(v->vec, v->nslots * sizeof(ETYPE)); \
65 } \
66 v->vec[v->nvec++] = val; \
67} \
68 \
69static inline void NAME##_terminate(struct NAME *v) { \
70 if(v->nvec >= v->nslots) \
71 v->vec = REALLOC(v->vec, ++v->nslots * sizeof(ETYPE)); \
72 memset(&v->vec[v->nvec], 0, sizeof (ETYPE)); \
73} \
b8956e9e
RK
74 \
75struct vector_swallow_semicolon
460b9539 76
b8956e9e
RK
77/** @brief A dynamic array of pointers to strings */
78VECTOR_TYPE(vector, char *, xrealloc);
79/** @brief A dynamic string */
80VECTOR_TYPE(dynstr, char, xrealloc_noptr);
81/** @brief A dynamic unicode string */
82VECTOR_TYPE(dynstr_ucs4, uint32_t, xrealloc_noptr);
8818b7fc
RK
83/** @brief A dynamic array of pointers to unicode string */
84VECTOR_TYPE(vector32, uint32_t *, xrealloc);
460b9539 85
b8956e9e 86/** @brief Append many strings to a @ref vector */
460b9539 87void vector_append_many(struct vector *v, char **vec, int nvec);
88
b8956e9e 89/** @brief Append @p n bytes to a @ref dynstr */
460b9539 90void dynstr_append_bytes(struct dynstr *v, const char *ptr, size_t n);
91
b8956e9e 92/** @brief Append a string to a @ref dynstr */
460b9539 93static inline void dynstr_append_string(struct dynstr *v, const char *ptr) {
94 dynstr_append_bytes(v, ptr, strlen(ptr));
95}
96
97#endif /* VECTOR_H */
98
99/*
100Local Variables:
101c-basic-offset:2
102comment-column:40
103End:
104*/