1 ca99cfddSTim J. Robbins /*-
2 *4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3 1de7b4b8SPedro F. Giffuni *
4 ca99cfddSTim J. Robbins * Copyright (c) 2004 Tim J. Robbins.
5 ca99cfddSTim J. Robbins * All rights reserved.
6 ca99cfddSTim J. Robbins *
7 ca99cfddSTim J. Robbins * Redistribution and use in source and binary forms, with or without
8 ca99cfddSTim J. Robbins * modification, are permitted provided that the following conditions
9 ca99cfddSTim J. Robbins * are met:
10 ca99cfddSTim J. Robbins * 1. Redistributions of source code must retain the above copyright
11 ca99cfddSTim J. Robbins * notice, this list of conditions and the following disclaimer.
12 ca99cfddSTim J. Robbins * 2. Redistributions in binary form must reproduce the above copyright
13 ca99cfddSTim J. Robbins * notice, this list of conditions and the following disclaimer in the
14 ca99cfddSTim J. Robbins * documentation and/or other materials provided with the distribution.
15 ca99cfddSTim J. Robbins *
16 ca99cfddSTim J. Robbins * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 ca99cfddSTim J. Robbins * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 ca99cfddSTim J. Robbins * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ca99cfddSTim J. Robbins * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 ca99cfddSTim J. Robbins * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 ca99cfddSTim J. Robbins * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 ca99cfddSTim J. Robbins * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 ca99cfddSTim J. Robbins * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 ca99cfddSTim J. Robbins * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 ca99cfddSTim J. Robbins * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 ca99cfddSTim J. Robbins * SUCH DAMAGE.
27 ca99cfddSTim J. Robbins */
28 ca99cfddSTim J. Robbins /*
29 ca99cfddSTim J. Robbins * "Set of characters" ADT implemented as a splay tree of extents, with
30 ca99cfddSTim J. Robbins * a lookup table cache to simplify looking up the first bunch of
31 ca99cfddSTim J. Robbins * characters (which are presumably more common than others).
32 ca99cfddSTim J. Robbins */
33 ca99cfddSTim J. Robbins
34 ca99cfddSTim J. Robbins #include <sys/cdefs.h>
35 ca99cfddSTim J. Robbins #include <assert.h>
36 821df508SXin LI #include <stdbool.h>
37 ca99cfddSTim J. Robbins #include <stdlib.h>
38 821df508SXin LI #include <wchar.h>
39 821df508SXin LI #include <wctype.h>
40 ca99cfddSTim J. Robbins #include "cset.h"
41 ca99cfddSTim J. Robbins
42 ca99cfddSTim J. Robbins static struct csnode * cset_delete(struct csnode *, wchar_t);
43 ca99cfddSTim J. Robbins static __inline int cset_rangecmp(struct csnode *, wchar_t);
44 ca99cfddSTim J. Robbins static struct csnode * cset_splay(struct csnode *, wchar_t);
45 ca99cfddSTim J. Robbins
46 ca99cfddSTim J. Robbins /*
47 ca99cfddSTim J. Robbins * cset_alloc --
48 ca99cfddSTim J. Robbins * Allocate a set of characters.
49 ca99cfddSTim J. Robbins */
50 ca99cfddSTim J. Robbins struct cset *
cset_alloc(void)51 ca99cfddSTim J. Robbins cset_alloc(void)
52 ca99cfddSTim J. Robbins {
53 ca99cfddSTim J. Robbins struct cset *cs;
54 ca99cfddSTim J. Robbins
55 ca99cfddSTim J. Robbins if ((cs = malloc(sizeof(*cs))) == NULL)
56 ca99cfddSTim J. Robbins return (NULL);
57 ca99cfddSTim J. Robbins cs->cs_root = NULL;
58 ca99cfddSTim J. Robbins cs->cs_classes = NULL;
59 ca99cfddSTim J. Robbins cs->cs_havecache = false;
60 9c8fd487STim J. Robbins cs->cs_invert = false;
61 ca99cfddSTim J. Robbins return (cs);
62 ca99cfddSTim J. Robbins }
63 ca99cfddSTim J. Robbins
64 ca99cfddSTim J. Robbins /*
65 ca99cfddSTim J. Robbins * cset_add --
66 ca99cfddSTim J. Robbins * Add a character to the set.
67 ca99cfddSTim J. Robbins */
68 ca99cfddSTim J. Robbins bool
cset_add(struct cset * cs,wchar_t ch)69 ca99cfddSTim J. Robbins cset_add(struct cset *cs, wchar_t ch)
70 ca99cfddSTim J. Robbins {
71 ca99cfddSTim J. Robbins struct csnode *csn, *ncsn;
72 ca99cfddSTim J. Robbins wchar_t oval;
73 ca99cfddSTim J. Robbins
74 ca99cfddSTim J. Robbins cs->cs_havecache = false;
75 ca99cfddSTim J. Robbins
76 ca99cfddSTim J. Robbins /*
77 ca99cfddSTim J. Robbins * Inserting into empty tree; new item becomes the root.
78 ca99cfddSTim J. Robbins */
79 ca99cfddSTim J. Robbins if (cs->cs_root == NULL) {
80 ca99cfddSTim J. Robbins csn = malloc(sizeof(*cs->cs_root));
81 ca99cfddSTim J. Robbins if (csn == NULL)
82 ca99cfddSTim J. Robbins return (false);
83 ca99cfddSTim J. Robbins csn->csn_left = csn->csn_right = NULL;
84 ca99cfddSTim J. Robbins csn->csn_min = csn->csn_max = ch;
85 ca99cfddSTim J. Robbins cs->cs_root = csn;
86 ca99cfddSTim J. Robbins return (true);
87 ca99cfddSTim J. Robbins }
88 ca99cfddSTim J. Robbins
89 ca99cfddSTim J. Robbins /*
90 ca99cfddSTim J. Robbins * Splay to check whether the item already exists, and otherwise,
91 ca99cfddSTim J. Robbins * where we should put it.
92 ca99cfddSTim J. Robbins */
93 ca99cfddSTim J. Robbins csn = cs->cs_root = cset_splay(cs->cs_root, ch);
94 ca99cfddSTim J. Robbins
95 ca99cfddSTim J. Robbins /*
96 cfab3bddSTim J. Robbins * Avoid adding duplicate nodes.
97 ca99cfddSTim J. Robbins */
98 ca99cfddSTim J. Robbins if (cset_rangecmp(csn, ch) == 0)
99 ca99cfddSTim J. Robbins return (true);
100 ca99cfddSTim J. Robbins
101 ca99cfddSTim J. Robbins /*
102 cfab3bddSTim J. Robbins * Allocate a new node and make it the new root.
103 ca99cfddSTim J. Robbins */
104 ca99cfddSTim J. Robbins ncsn = malloc(sizeof(*ncsn));
105 ca99cfddSTim J. Robbins if (ncsn == NULL)
106 ca99cfddSTim J. Robbins return (false);
107 ca99cfddSTim J. Robbins ncsn->csn_min = ncsn->csn_max = ch;
108 ca99cfddSTim J. Robbins if (cset_rangecmp(csn, ch) < 0) {
109 ca99cfddSTim J. Robbins ncsn->csn_left = csn->csn_left;
110 ca99cfddSTim J. Robbins ncsn->csn_right = csn;
111 ca99cfddSTim J. Robbins csn->csn_left = NULL;
112 ca99cfddSTim J. Robbins } else {
113 ca99cfddSTim J. Robbins ncsn->csn_right = csn->csn_right;
114 ca99cfddSTim J. Robbins ncsn->csn_left = csn;
115 ca99cfddSTim J. Robbins csn->csn_right = NULL;
116 ca99cfddSTim J. Robbins }
117 ca99cfddSTim J. Robbins cs->cs_root = ncsn;
118 ca99cfddSTim J. Robbins
119 ca99cfddSTim J. Robbins /*
120 cfab3bddSTim J. Robbins * Coalesce with left and right neighbours if possible.
121 ca99cfddSTim J. Robbins */
122 cfab3bddSTim J. Robbins if (ncsn->csn_left != NULL) {
123 cfab3bddSTim J. Robbins ncsn->csn_left = cset_splay(ncsn->csn_left, ncsn->csn_min - 1);
124 cfab3bddSTim J. Robbins if (ncsn->csn_left->csn_max == ncsn->csn_min - 1) {
125 cfab3bddSTim J. Robbins oval = ncsn->csn_left->csn_min;
126 cfab3bddSTim J. Robbins ncsn->csn_left = cset_delete(ncsn->csn_left,
127 cfab3bddSTim J. Robbins ncsn->csn_left->csn_min);
128 ca99cfddSTim J. Robbins ncsn->csn_min = oval;
129 ca99cfddSTim J. Robbins }
130 cfab3bddSTim J. Robbins }
131 cfab3bddSTim J. Robbins if (ncsn->csn_right != NULL) {
132 cfab3bddSTim J. Robbins ncsn->csn_right = cset_splay(ncsn->csn_right,
133 cfab3bddSTim J. Robbins ncsn->csn_max + 1);
134 cfab3bddSTim J. Robbins if (ncsn->csn_right->csn_min == ncsn->csn_max + 1) {
135 cfab3bddSTim J. Robbins oval = ncsn->csn_right->csn_max;
136 cfab3bddSTim J. Robbins ncsn->csn_right = cset_delete(ncsn->csn_right,
137 cfab3bddSTim J. Robbins ncsn->csn_right->csn_min);
138 ca99cfddSTim J. Robbins ncsn->csn_max = oval;
139 ca99cfddSTim J. Robbins }
140 cfab3bddSTim J. Robbins }
141 ca99cfddSTim J. Robbins
142 ca99cfddSTim J. Robbins return (true);
143 ca99cfddSTim J. Robbins }
144 ca99cfddSTim J. Robbins
145 ca99cfddSTim J. Robbins /*
146 ca99cfddSTim J. Robbins * cset_in_hard --
147 ca99cfddSTim J. Robbins * Determine whether a character is in the set without using
148 ca99cfddSTim J. Robbins * the cache.
149 ca99cfddSTim J. Robbins */
150 ca99cfddSTim J. Robbins bool
cset_in_hard(struct cset * cs,wchar_t ch)151 ca99cfddSTim J. Robbins cset_in_hard(struct cset *cs, wchar_t ch)
152 ca99cfddSTim J. Robbins {
153 ca99cfddSTim J. Robbins struct csclass *csc;
154 ca99cfddSTim J. Robbins
155 ca99cfddSTim J. Robbins for (csc = cs->cs_classes; csc != NULL; csc = csc->csc_next)
156 50af444aSEd Schouten if (csc->csc_invert ^ (iswctype(ch, csc->csc_type) != 0))
157 ca99cfddSTim J. Robbins return (cs->cs_invert ^ true);
158 ca99cfddSTim J. Robbins if (cs->cs_root != NULL) {
159 ca99cfddSTim J. Robbins cs->cs_root = cset_splay(cs->cs_root, ch);
160 50af444aSEd Schouten return (cs->cs_invert ^ (cset_rangecmp(cs->cs_root, ch) == 0));
161 ca99cfddSTim J. Robbins }
162 ca99cfddSTim J. Robbins return (cs->cs_invert ^ false);
163 ca99cfddSTim J. Robbins }
164 ca99cfddSTim J. Robbins
165 ca99cfddSTim J. Robbins /*
166 ca99cfddSTim J. Robbins * cset_cache --
167 ca99cfddSTim J. Robbins * Update the cache.
168 ca99cfddSTim J. Robbins */
169 ca99cfddSTim J. Robbins void
cset_cache(struct cset * cs)170 ca99cfddSTim J. Robbins cset_cache(struct cset *cs)
171 ca99cfddSTim J. Robbins {
172 ca99cfddSTim J. Robbins wchar_t i;
173 ca99cfddSTim J. Robbins
174 ca99cfddSTim J. Robbins for (i = 0; i < CS_CACHE_SIZE; i++)
175 ca99cfddSTim J. Robbins cs->cs_cache[i] = cset_in_hard(cs, i);
176 ca99cfddSTim J. Robbins
177 ca99cfddSTim J. Robbins cs->cs_havecache = true;
178 ca99cfddSTim J. Robbins }
179 ca99cfddSTim J. Robbins
180 ca99cfddSTim J. Robbins /*
181 ca99cfddSTim J. Robbins * cset_invert --
182 ca99cfddSTim J. Robbins * Invert the character set.
183 ca99cfddSTim J. Robbins */
184 ca99cfddSTim J. Robbins void
cset_invert(struct cset * cs)185 ca99cfddSTim J. Robbins cset_invert(struct cset *cs)
186 ca99cfddSTim J. Robbins {
187 ca99cfddSTim J. Robbins
188 ca99cfddSTim J. Robbins cs->cs_invert ^= true;
189 ca99cfddSTim J. Robbins cs->cs_havecache = false;
190 ca99cfddSTim J. Robbins }
191 ca99cfddSTim J. Robbins
192 ca99cfddSTim J. Robbins /*
193 ca99cfddSTim J. Robbins * cset_addclass --
194 ca99cfddSTim J. Robbins * Add a wctype()-style character class to the set, optionally
195 ca99cfddSTim J. Robbins * inverting it.
196 ca99cfddSTim J. Robbins */
197 ca99cfddSTim J. Robbins bool
cset_addclass(struct cset * cs,wctype_t type,bool invert)198 ca99cfddSTim J. Robbins cset_addclass(struct cset *cs, wctype_t type, bool invert)
199 ca99cfddSTim J. Robbins {
200 ca99cfddSTim J. Robbins struct csclass *csc;
201 ca99cfddSTim J. Robbins
202 ca99cfddSTim J. Robbins csc = malloc(sizeof(*csc));
203 ca99cfddSTim J. Robbins if (csc == NULL)
204 ca99cfddSTim J. Robbins return (false);
205 ca99cfddSTim J. Robbins csc->csc_type = type;
206 ca99cfddSTim J. Robbins csc->csc_invert = invert;
207 ca99cfddSTim J. Robbins csc->csc_next = cs->cs_classes;
208 ca99cfddSTim J. Robbins cs->cs_classes = csc;
209 ca99cfddSTim J. Robbins cs->cs_havecache = false;
210 ca99cfddSTim J. Robbins return (true);
211 ca99cfddSTim J. Robbins }
212 ca99cfddSTim J. Robbins
213 ca99cfddSTim J. Robbins static __inline int
cset_rangecmp(struct csnode * t,wchar_t ch)214 ca99cfddSTim J. Robbins cset_rangecmp(struct csnode *t, wchar_t ch)
215 ca99cfddSTim J. Robbins {
216 ca99cfddSTim J. Robbins
217 ca99cfddSTim J. Robbins if (ch < t->csn_min)
218 ca99cfddSTim J. Robbins return (-1);
219 ca99cfddSTim J. Robbins if (ch > t->csn_max)
220 ca99cfddSTim J. Robbins return (1);
221 ca99cfddSTim J. Robbins return (0);
222 ca99cfddSTim J. Robbins }
223 ca99cfddSTim J. Robbins
224 ca99cfddSTim J. Robbins static struct csnode *
cset_splay(struct csnode * t,wchar_t ch)225 ca99cfddSTim J. Robbins cset_splay(struct csnode *t, wchar_t ch)
226 ca99cfddSTim J. Robbins {
227 ca99cfddSTim J. Robbins struct csnode N, *l, *r, *y;
228 ca99cfddSTim J. Robbins
229 ca99cfddSTim J. Robbins /*
230 ca99cfddSTim J. Robbins * Based on public domain code from Sleator.
231 ca99cfddSTim J. Robbins */
232 ca99cfddSTim J. Robbins
233 ca99cfddSTim J. Robbins assert(t != NULL);
234 ca99cfddSTim J. Robbins
235 ca99cfddSTim J. Robbins N.csn_left = N.csn_right = NULL;
236 ca99cfddSTim J. Robbins l = r = &N;
237 ca99cfddSTim J. Robbins for (;;) {
238 ca99cfddSTim J. Robbins if (cset_rangecmp(t, ch) < 0) {
239 ca99cfddSTim J. Robbins if (t->csn_left != NULL &&
240 ca99cfddSTim J. Robbins cset_rangecmp(t->csn_left, ch) < 0) {
241 ca99cfddSTim J. Robbins y = t->csn_left;
242 ca99cfddSTim J. Robbins t->csn_left = y->csn_right;
243 ca99cfddSTim J. Robbins y->csn_right = t;
244 ca99cfddSTim J. Robbins t = y;
245 ca99cfddSTim J. Robbins }
246 ca99cfddSTim J. Robbins if (t->csn_left == NULL)
247 ca99cfddSTim J. Robbins break;
248 ca99cfddSTim J. Robbins r->csn_left = t;
249 ca99cfddSTim J. Robbins r = t;
250 ca99cfddSTim J. Robbins t = t->csn_left;
251 ca99cfddSTim J. Robbins } else if (cset_rangecmp(t, ch) > 0) {
252 ca99cfddSTim J. Robbins if (t->csn_right != NULL &&
253 ca99cfddSTim J. Robbins cset_rangecmp(t->csn_right, ch) > 0) {
254 ca99cfddSTim J. Robbins y = t->csn_right;
255 ca99cfddSTim J. Robbins t->csn_right = y->csn_left;
256 ca99cfddSTim J. Robbins y->csn_left = t;
257 ca99cfddSTim J. Robbins t = y;
258 ca99cfddSTim J. Robbins }
259 ca99cfddSTim J. Robbins if (t->csn_right == NULL)
260 ca99cfddSTim J. Robbins break;
261 ca99cfddSTim J. Robbins l->csn_right = t;
262 ca99cfddSTim J. Robbins l = t;
263 ca99cfddSTim J. Robbins t = t->csn_right;
264 ca99cfddSTim J. Robbins } else
265 ca99cfddSTim J. Robbins break;
266 ca99cfddSTim J. Robbins }
267 ca99cfddSTim J. Robbins l->csn_right = t->csn_left;
268 ca99cfddSTim J. Robbins r->csn_left = t->csn_right;
269 ca99cfddSTim J. Robbins t->csn_left = N.csn_right;
270 ca99cfddSTim J. Robbins t->csn_right = N.csn_left;
271 ca99cfddSTim J. Robbins return (t);
272 ca99cfddSTim J. Robbins }
273 ca99cfddSTim J. Robbins
274 ca99cfddSTim J. Robbins static struct csnode *
cset_delete(struct csnode * t,wchar_t ch)275 ca99cfddSTim J. Robbins cset_delete(struct csnode *t, wchar_t ch)
276 ca99cfddSTim J. Robbins {
277 ca99cfddSTim J. Robbins struct csnode *x;
278 ca99cfddSTim J. Robbins
279 ca99cfddSTim J. Robbins assert(t != NULL);
280 ca99cfddSTim J. Robbins t = cset_splay(t, ch);
281 ca99cfddSTim J. Robbins assert(cset_rangecmp(t, ch) == 0);
282 ca99cfddSTim J. Robbins if (t->csn_left == NULL)
283 ca99cfddSTim J. Robbins x = t->csn_right;
284 ca99cfddSTim J. Robbins else {
285 ca99cfddSTim J. Robbins x = cset_splay(t->csn_left, ch);
286 ca99cfddSTim J. Robbins x->csn_right = t->csn_right;
287 ca99cfddSTim J. Robbins }
288 ca99cfddSTim J. Robbins free(t);
289 ca99cfddSTim J. Robbins return x;
290 ca99cfddSTim J. Robbins }
291