1 /* $Id: dba_array.c,v 1.1 2016/07/19 21:31:55 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".
25 #include "mandoc_aux.h"
26 #include "dba_write.h"
27 #include "dba_array.h"
30 void **ep
; /* Array of entries. */
31 int32_t *em
; /* Array of map positions. */
33 int32_t ea
; /* Entries allocated. */
34 int32_t eu
; /* Entries used (including deleted). */
35 int32_t ed
; /* Entries deleted. */
36 int32_t ec
; /* Currently active entry. */
37 int32_t pos
; /* Map position of this array. */
42 dba_array_new(int32_t ea
, int flags
)
44 struct dba_array
*array
;
47 array
= mandoc_malloc(sizeof(*array
));
48 array
->ep
= mandoc_reallocarray(NULL
, ea
, sizeof(*array
->ep
));
49 array
->em
= mandoc_reallocarray(NULL
, ea
, sizeof(*array
->em
));
60 dba_array_free(struct dba_array
*array
)
66 if (array
->flags
& DBA_STR
)
67 for (ie
= 0; ie
< array
->eu
; ie
++)
75 dba_array_set(struct dba_array
*array
, int32_t ie
, void *entry
)
78 assert(ie
< array
->ea
);
79 assert(ie
<= array
->eu
);
82 if (array
->flags
& DBA_STR
)
83 entry
= mandoc_strdup(entry
);
84 array
->ep
[ie
] = entry
;
89 dba_array_add(struct dba_array
*array
, void *entry
)
91 if (array
->eu
== array
->ea
) {
92 assert(array
->flags
& DBA_GROW
);
93 array
->ep
= mandoc_reallocarray(array
->ep
,
94 2, sizeof(*array
->ep
) * array
->ea
);
95 array
->em
= mandoc_reallocarray(array
->em
,
96 2, sizeof(*array
->em
) * array
->ea
);
99 dba_array_set(array
, array
->eu
, entry
);
103 dba_array_get(struct dba_array
*array
, int32_t ie
)
105 if (ie
< 0 || ie
>= array
->eu
|| array
->em
[ie
] == -1)
107 return array
->ep
[ie
];
111 dba_array_start(struct dba_array
*array
)
113 array
->ec
= array
->eu
;
117 dba_array_next(struct dba_array
*array
)
119 if (array
->ec
< array
->eu
)
123 while (array
->ec
< array
->eu
&& array
->em
[array
->ec
] == -1)
125 return array
->ec
< array
->eu
? array
->ep
[array
->ec
] : NULL
;
129 dba_array_del(struct dba_array
*array
)
131 if (array
->ec
< array
->eu
&& array
->em
[array
->ec
] != -1) {
132 array
->em
[array
->ec
] = -1;
138 dba_array_undel(struct dba_array
*array
)
140 memset(array
->em
, 0, sizeof(*array
->em
) * array
->eu
);
144 dba_array_setpos(struct dba_array
*array
, int32_t ie
, int32_t pos
)
150 dba_array_getpos(struct dba_array
*array
)
156 dba_array_sort(struct dba_array
*array
, dba_compare_func func
)
158 assert(array
->ed
== 0);
159 qsort(array
->ep
, array
->eu
, sizeof(*array
->ep
), func
);
163 dba_array_writelen(struct dba_array
*array
, int32_t nmemb
)
165 dba_int_write(array
->eu
- array
->ed
);
166 return dba_skip(nmemb
, array
->eu
- array
->ed
);
170 dba_array_writepos(struct dba_array
*array
)
174 array
->pos
= dba_tell();
175 for (ie
= 0; ie
< array
->eu
; ie
++)
176 if (array
->em
[ie
] != -1)
177 dba_int_write(array
->em
[ie
]);
181 dba_array_writelst(struct dba_array
*array
)
185 dba_array_FOREACH(array
, str
)
187 dba_char_write('\0');