dba_array.c revision 1.1 1 1.1 christos /* Id: dba_array.c,v 1.1 2016/07/19 21:31:55 schwarze Exp */
2 1.1 christos /*
3 1.1 christos * Copyright (c) 2016 Ingo Schwarze <schwarze (at) openbsd.org>
4 1.1 christos *
5 1.1 christos * Permission to use, copy, modify, and distribute this software for any
6 1.1 christos * purpose with or without fee is hereby granted, provided that the above
7 1.1 christos * copyright notice and this permission notice appear in all copies.
8 1.1 christos *
9 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 1.1 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 1.1 christos *
17 1.1 christos * Allocation-based arrays for the mandoc database, for read-write access.
18 1.1 christos * The interface is defined in "dba_array.h".
19 1.1 christos */
20 1.1 christos #include <assert.h>
21 1.1 christos #include <stdint.h>
22 1.1 christos #include <stdlib.h>
23 1.1 christos #include <string.h>
24 1.1 christos
25 1.1 christos #include "mandoc_aux.h"
26 1.1 christos #include "dba_write.h"
27 1.1 christos #include "dba_array.h"
28 1.1 christos
29 1.1 christos struct dba_array {
30 1.1 christos void **ep; /* Array of entries. */
31 1.1 christos int32_t *em; /* Array of map positions. */
32 1.1 christos int flags;
33 1.1 christos int32_t ea; /* Entries allocated. */
34 1.1 christos int32_t eu; /* Entries used (including deleted). */
35 1.1 christos int32_t ed; /* Entries deleted. */
36 1.1 christos int32_t ec; /* Currently active entry. */
37 1.1 christos int32_t pos; /* Map position of this array. */
38 1.1 christos };
39 1.1 christos
40 1.1 christos
41 1.1 christos struct dba_array *
42 1.1 christos dba_array_new(int32_t ea, int flags)
43 1.1 christos {
44 1.1 christos struct dba_array *array;
45 1.1 christos
46 1.1 christos assert(ea > 0);
47 1.1 christos array = mandoc_malloc(sizeof(*array));
48 1.1 christos array->ep = mandoc_reallocarray(NULL, ea, sizeof(*array->ep));
49 1.1 christos array->em = mandoc_reallocarray(NULL, ea, sizeof(*array->em));
50 1.1 christos array->ea = ea;
51 1.1 christos array->eu = 0;
52 1.1 christos array->ed = 0;
53 1.1 christos array->ec = 0;
54 1.1 christos array->flags = flags;
55 1.1 christos array->pos = 0;
56 1.1 christos return array;
57 1.1 christos }
58 1.1 christos
59 1.1 christos void
60 1.1 christos dba_array_free(struct dba_array *array)
61 1.1 christos {
62 1.1 christos int32_t ie;
63 1.1 christos
64 1.1 christos if (array == NULL)
65 1.1 christos return;
66 1.1 christos if (array->flags & DBA_STR)
67 1.1 christos for (ie = 0; ie < array->eu; ie++)
68 1.1 christos free(array->ep[ie]);
69 1.1 christos free(array->ep);
70 1.1 christos free(array->em);
71 1.1 christos free(array);
72 1.1 christos }
73 1.1 christos
74 1.1 christos void
75 1.1 christos dba_array_set(struct dba_array *array, int32_t ie, void *entry)
76 1.1 christos {
77 1.1 christos assert(ie >= 0);
78 1.1 christos assert(ie < array->ea);
79 1.1 christos assert(ie <= array->eu);
80 1.1 christos if (ie == array->eu)
81 1.1 christos array->eu++;
82 1.1 christos if (array->flags & DBA_STR)
83 1.1 christos entry = mandoc_strdup(entry);
84 1.1 christos array->ep[ie] = entry;
85 1.1 christos array->em[ie] = 0;
86 1.1 christos }
87 1.1 christos
88 1.1 christos void
89 1.1 christos dba_array_add(struct dba_array *array, void *entry)
90 1.1 christos {
91 1.1 christos if (array->eu == array->ea) {
92 1.1 christos assert(array->flags & DBA_GROW);
93 1.1 christos array->ep = mandoc_reallocarray(array->ep,
94 1.1 christos 2, sizeof(*array->ep) * array->ea);
95 1.1 christos array->em = mandoc_reallocarray(array->em,
96 1.1 christos 2, sizeof(*array->em) * array->ea);
97 1.1 christos array->ea *= 2;
98 1.1 christos }
99 1.1 christos dba_array_set(array, array->eu, entry);
100 1.1 christos }
101 1.1 christos
102 1.1 christos void *
103 1.1 christos dba_array_get(struct dba_array *array, int32_t ie)
104 1.1 christos {
105 1.1 christos if (ie < 0 || ie >= array->eu || array->em[ie] == -1)
106 1.1 christos return NULL;
107 1.1 christos return array->ep[ie];
108 1.1 christos }
109 1.1 christos
110 1.1 christos void
111 1.1 christos dba_array_start(struct dba_array *array)
112 1.1 christos {
113 1.1 christos array->ec = array->eu;
114 1.1 christos }
115 1.1 christos
116 1.1 christos void *
117 1.1 christos dba_array_next(struct dba_array *array)
118 1.1 christos {
119 1.1 christos if (array->ec < array->eu)
120 1.1 christos array->ec++;
121 1.1 christos else
122 1.1 christos array->ec = 0;
123 1.1 christos while (array->ec < array->eu && array->em[array->ec] == -1)
124 1.1 christos array->ec++;
125 1.1 christos return array->ec < array->eu ? array->ep[array->ec] : NULL;
126 1.1 christos }
127 1.1 christos
128 1.1 christos void
129 1.1 christos dba_array_del(struct dba_array *array)
130 1.1 christos {
131 1.1 christos if (array->ec < array->eu && array->em[array->ec] != -1) {
132 1.1 christos array->em[array->ec] = -1;
133 1.1 christos array->ed++;
134 1.1 christos }
135 1.1 christos }
136 1.1 christos
137 1.1 christos void
138 1.1 christos dba_array_undel(struct dba_array *array)
139 1.1 christos {
140 1.1 christos memset(array->em, 0, sizeof(*array->em) * array->eu);
141 1.1 christos }
142 1.1 christos
143 1.1 christos void
144 1.1 christos dba_array_setpos(struct dba_array *array, int32_t ie, int32_t pos)
145 1.1 christos {
146 1.1 christos array->em[ie] = pos;
147 1.1 christos }
148 1.1 christos
149 1.1 christos int32_t
150 1.1 christos dba_array_getpos(struct dba_array *array)
151 1.1 christos {
152 1.1 christos return array->pos;
153 1.1 christos }
154 1.1 christos
155 1.1 christos void
156 1.1 christos dba_array_sort(struct dba_array *array, dba_compare_func func)
157 1.1 christos {
158 1.1 christos assert(array->ed == 0);
159 1.1 christos qsort(array->ep, array->eu, sizeof(*array->ep), func);
160 1.1 christos }
161 1.1 christos
162 1.1 christos int32_t
163 1.1 christos dba_array_writelen(struct dba_array *array, int32_t nmemb)
164 1.1 christos {
165 1.1 christos dba_int_write(array->eu - array->ed);
166 1.1 christos return dba_skip(nmemb, array->eu - array->ed);
167 1.1 christos }
168 1.1 christos
169 1.1 christos void
170 1.1 christos dba_array_writepos(struct dba_array *array)
171 1.1 christos {
172 1.1 christos int32_t ie;
173 1.1 christos
174 1.1 christos array->pos = dba_tell();
175 1.1 christos for (ie = 0; ie < array->eu; ie++)
176 1.1 christos if (array->em[ie] != -1)
177 1.1 christos dba_int_write(array->em[ie]);
178 1.1 christos }
179 1.1 christos
180 1.1 christos void
181 1.1 christos dba_array_writelst(struct dba_array *array)
182 1.1 christos {
183 1.1 christos const char *str;
184 1.1 christos
185 1.1 christos dba_array_FOREACH(array, str)
186 1.1 christos dba_str_write(str);
187 1.1 christos dba_char_write('\0');
188 1.1 christos }
189