2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Tim J. Robbins.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * "Character map" ADT. Stores mappings between pairs of characters in a
30 * splay tree, with a lookup table cache to simplify looking up the first
31 * bunch of characters (which are presumably more common than others).
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
44 static struct cmapnode *cmap_splay(struct cmapnode *, wint_t);
48 * Allocate a character map.
55 cm = malloc(sizeof(*cm));
59 cm->cm_def = CM_DEF_SELF;
60 cm->cm_havecache = false;
61 cm->cm_min = cm->cm_max = 0;
67 * Add a mapping from "from" to "to" to the map.
70 cmap_add(struct cmap *cm, wint_t from, wint_t to)
72 struct cmapnode *cmn, *ncmn;
74 cm->cm_havecache = false;
76 if (cm->cm_root == NULL) {
77 cmn = malloc(sizeof(*cmn));
82 cmn->cmn_left = cmn->cmn_right = NULL;
84 cm->cm_min = cm->cm_max = from;
88 cmn = cm->cm_root = cmap_splay(cm->cm_root, from);
90 if (cmn->cmn_from == from) {
95 ncmn = malloc(sizeof(*ncmn));
98 ncmn->cmn_from = from;
100 if (from < cmn->cmn_from) {
101 ncmn->cmn_left = cmn->cmn_left;
102 ncmn->cmn_right = cmn;
103 cmn->cmn_left = NULL;
105 ncmn->cmn_right = cmn->cmn_right;
106 ncmn->cmn_left = cmn;
107 cmn->cmn_right = NULL;
109 if (from < cm->cm_min)
111 if (from > cm->cm_max)
119 * cmap_lookup_hard --
120 * Look up the mapping for a character without using the cache.
123 cmap_lookup_hard(struct cmap *cm, wint_t ch)
126 if (cm->cm_root != NULL) {
127 cm->cm_root = cmap_splay(cm->cm_root, ch);
128 if (cm->cm_root->cmn_from == ch)
129 return (cm->cm_root->cmn_to);
131 return (cm->cm_def == CM_DEF_SELF ? ch : cm->cm_def);
139 cmap_cache(struct cmap *cm)
143 for (ch = 0; ch < CM_CACHE_SIZE; ch++)
144 cm->cm_cache[ch] = cmap_lookup_hard(cm, ch);
146 cm->cm_havecache = true;
151 * Change the value that characters without mappings map to, and
152 * return the old value. The special character value CM_MAP_SELF
153 * means characters map to themselves.
156 cmap_default(struct cmap *cm, wint_t def)
162 cm->cm_havecache = false;
166 static struct cmapnode *
167 cmap_splay(struct cmapnode *t, wint_t ch)
169 struct cmapnode N, *l, *r, *y;
172 * Based on public domain code from Sleator.
177 N.cmn_left = N.cmn_right = NULL;
180 if (ch < t->cmn_from) {
181 if (t->cmn_left != NULL &&
182 ch < t->cmn_left->cmn_from) {
184 t->cmn_left = y->cmn_right;
188 if (t->cmn_left == NULL)
193 } else if (ch > t->cmn_from) {
194 if (t->cmn_right != NULL &&
195 ch > t->cmn_right->cmn_from) {
197 t->cmn_right = y->cmn_left;
201 if (t->cmn_right == NULL)
209 l->cmn_right = t->cmn_left;
210 r->cmn_left = t->cmn_right;
211 t->cmn_left = N.cmn_right;
212 t->cmn_right = N.cmn_left;