]>
git.cameronkatri.com Git - mandoc.git/blob - dba_array.c
1 /* $Id: dba_array.c,v 1.2 2020/06/22 19:20:40 schwarze Exp $ */
3 * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Allocation-based arrays for the mandoc database, for read-write access.
18 * The interface is defined in "dba_array.h".
27 #include "mandoc_aux.h"
28 #include "dba_write.h"
29 #include "dba_array.h"
32 void **ep
; /* Array of entries. */
33 int32_t *em
; /* Array of map positions. */
35 int32_t ea
; /* Entries allocated. */
36 int32_t eu
; /* Entries used (including deleted). */
37 int32_t ed
; /* Entries deleted. */
38 int32_t ec
; /* Currently active entry. */
39 int32_t pos
; /* Map position of this array. */
44 dba_array_new(int32_t ea
, int flags
)
46 struct dba_array
*array
;
49 array
= mandoc_malloc(sizeof(*array
));
50 array
->ep
= mandoc_reallocarray(NULL
, ea
, sizeof(*array
->ep
));
51 array
->em
= mandoc_reallocarray(NULL
, ea
, sizeof(*array
->em
));
62 dba_array_free(struct dba_array
*array
)
68 if (array
->flags
& DBA_STR
)
69 for (ie
= 0; ie
< array
->eu
; ie
++)
77 dba_array_set(struct dba_array
*array
, int32_t ie
, void *entry
)
80 assert(ie
< array
->ea
);
81 assert(ie
<= array
->eu
);
84 if (array
->flags
& DBA_STR
)
85 entry
= mandoc_strdup(entry
);
86 array
->ep
[ie
] = entry
;
91 dba_array_add(struct dba_array
*array
, void *entry
)
93 if (array
->eu
== array
->ea
) {
94 assert(array
->flags
& DBA_GROW
);
95 array
->ep
= mandoc_reallocarray(array
->ep
,
96 2, sizeof(*array
->ep
) * array
->ea
);
97 array
->em
= mandoc_reallocarray(array
->em
,
98 2, sizeof(*array
->em
) * array
->ea
);
101 dba_array_set(array
, array
->eu
, entry
);
105 dba_array_get(struct dba_array
*array
, int32_t ie
)
107 if (ie
< 0 || ie
>= array
->eu
|| array
->em
[ie
] == -1)
109 return array
->ep
[ie
];
113 dba_array_start(struct dba_array
*array
)
115 array
->ec
= array
->eu
;
119 dba_array_next(struct dba_array
*array
)
121 if (array
->ec
< array
->eu
)
125 while (array
->ec
< array
->eu
&& array
->em
[array
->ec
] == -1)
127 return array
->ec
< array
->eu
? array
->ep
[array
->ec
] : NULL
;
131 dba_array_del(struct dba_array
*array
)
133 if (array
->ec
< array
->eu
&& array
->em
[array
->ec
] != -1) {
134 array
->em
[array
->ec
] = -1;
140 dba_array_undel(struct dba_array
*array
)
142 memset(array
->em
, 0, sizeof(*array
->em
) * array
->eu
);
146 dba_array_setpos(struct dba_array
*array
, int32_t ie
, int32_t pos
)
152 dba_array_getpos(struct dba_array
*array
)
158 dba_array_sort(struct dba_array
*array
, dba_compare_func func
)
160 assert(array
->ed
== 0);
161 qsort(array
->ep
, array
->eu
, sizeof(*array
->ep
), func
);
165 dba_array_writelen(struct dba_array
*array
, int32_t nmemb
)
167 dba_int_write(array
->eu
- array
->ed
);
168 return dba_skip(nmemb
, array
->eu
- array
->ed
);
172 dba_array_writepos(struct dba_array
*array
)
176 array
->pos
= dba_tell();
177 for (ie
= 0; ie
< array
->eu
; ie
++)
178 if (array
->em
[ie
] != -1)
179 dba_int_write(array
->em
[ie
]);
183 dba_array_writelst(struct dba_array
*array
)
187 dba_array_FOREACH(array
, str
)
189 dba_char_write('\0');