Deleted Added
full compact
cmap.c (200420) cmap.c (200462)
1/*-
2 * Copyright (c) 2004 Tim J. Robbins.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26/*
27 * "Character map" ADT. Stores mappings between pairs of characters in a
28 * splay tree, with a lookup table cache to simplify looking up the first
29 * bunch of characters (which are presumably more common than others).
30 */
31
32#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2004 Tim J. Robbins.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26/*
27 * "Character map" ADT. Stores mappings between pairs of characters in a
28 * splay tree, with a lookup table cache to simplify looking up the first
29 * bunch of characters (which are presumably more common than others).
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/usr.bin/tr/cmap.c 200420 2009-12-11 23:35:38Z delphij $");
33__FBSDID("$FreeBSD: head/usr.bin/tr/cmap.c 200462 2009-12-13 03:14:06Z delphij $");
34
35#include <assert.h>
34
35#include <assert.h>
36#include <limits.h>
37#include <stdbool.h>
36#include <stdlib.h>
38#include <stdlib.h>
39#include <wchar.h>
37#include "cmap.h"
38
39static struct cmapnode *cmap_splay(struct cmapnode *, wint_t);
40
41/*
42 * cmap_alloc --
43 * Allocate a character map.
44 */
45struct cmap *
46cmap_alloc(void)
47{
48 struct cmap *cm;
49
50 cm = malloc(sizeof(*cm));
51 if (cm == NULL)
52 return (NULL);
53 cm->cm_root = NULL;
54 cm->cm_def = CM_DEF_SELF;
55 cm->cm_havecache = false;
56 cm->cm_min = cm->cm_max = 0;
57 return (cm);
58}
59
60/*
61 * cmap_add --
62 * Add a mapping from "from" to "to" to the map.
63 */
64bool
65cmap_add(struct cmap *cm, wint_t from, wint_t to)
66{
67 struct cmapnode *cmn, *ncmn;
68
69 cm->cm_havecache = false;
70
71 if (cm->cm_root == NULL) {
72 cmn = malloc(sizeof(*cmn));
73 if (cmn == NULL)
74 return (false);
75 cmn->cmn_from = from;
76 cmn->cmn_to = to;
77 cmn->cmn_left = cmn->cmn_right = NULL;
78 cm->cm_root = cmn;
79 cm->cm_min = cm->cm_max = from;
80 return (true);
81 }
82
83 cmn = cm->cm_root = cmap_splay(cm->cm_root, from);
84
85 if (cmn->cmn_from == from) {
86 cmn->cmn_to = to;
87 return (true);
88 }
89
90 ncmn = malloc(sizeof(*ncmn));
91 if (ncmn == NULL)
92 return (false);
93 ncmn->cmn_from = from;
94 ncmn->cmn_to = to;
95 if (from < cmn->cmn_from) {
96 ncmn->cmn_left = cmn->cmn_left;
97 ncmn->cmn_right = cmn;
98 cmn->cmn_left = NULL;
99 } else {
100 ncmn->cmn_right = cmn->cmn_right;
101 ncmn->cmn_left = cmn;
102 cmn->cmn_right = NULL;
103 }
104 if (from < cm->cm_min)
105 cm->cm_min = from;
106 if (from > cm->cm_max)
107 cm->cm_max = from;
108 cm->cm_root = ncmn;
109
110 return (true);
111}
112
113/*
114 * cmap_lookup_hard --
115 * Look up the mapping for a character without using the cache.
116 */
117wint_t
118cmap_lookup_hard(struct cmap *cm, wint_t ch)
119{
120
121 if (cm->cm_root != NULL) {
122 cm->cm_root = cmap_splay(cm->cm_root, ch);
123 if (cm->cm_root->cmn_from == ch)
124 return (cm->cm_root->cmn_to);
125 }
126 return (cm->cm_def == CM_DEF_SELF ? ch : cm->cm_def);
127}
128
129/*
130 * cmap_cache --
131 * Update the cache.
132 */
133void
134cmap_cache(struct cmap *cm)
135{
136 wint_t ch;
137
138 for (ch = 0; ch < CM_CACHE_SIZE; ch++)
139 cm->cm_cache[ch] = cmap_lookup_hard(cm, ch);
140
141 cm->cm_havecache = true;
142}
143
144/*
145 * cmap_default --
146 * Change the value that characters without mappings map to, and
147 * return the old value. The special character value CM_MAP_SELF
148 * means characters map to themselves.
149 */
150wint_t
151cmap_default(struct cmap *cm, wint_t def)
152{
153 wint_t old;
154
155 old = cm->cm_def;
156 cm->cm_def = def;
157 cm->cm_havecache = false;
158 return (old);
159}
160
161static struct cmapnode *
162cmap_splay(struct cmapnode *t, wint_t ch)
163{
164 struct cmapnode N, *l, *r, *y;
165
166 /*
167 * Based on public domain code from Sleator.
168 */
169
170 assert(t != NULL);
171
172 N.cmn_left = N.cmn_right = NULL;
173 l = r = &N;
174 for (;;) {
175 if (ch < t->cmn_from) {
176 if (t->cmn_left != NULL &&
177 ch < t->cmn_left->cmn_from) {
178 y = t->cmn_left;
179 t->cmn_left = y->cmn_right;
180 y->cmn_right = t;
181 t = y;
182 }
183 if (t->cmn_left == NULL)
184 break;
185 r->cmn_left = t;
186 r = t;
187 t = t->cmn_left;
188 } else if (ch > t->cmn_from) {
189 if (t->cmn_right != NULL &&
190 ch > t->cmn_right->cmn_from) {
191 y = t->cmn_right;
192 t->cmn_right = y->cmn_left;
193 y->cmn_left = t;
194 t = y;
195 }
196 if (t->cmn_right == NULL)
197 break;
198 l->cmn_right = t;
199 l = t;
200 t = t->cmn_right;
201 } else
202 break;
203 }
204 l->cmn_right = t->cmn_left;
205 r->cmn_left = t->cmn_right;
206 t->cmn_left = N.cmn_right;
207 t->cmn_right = N.cmn_left;
208 return (t);
209}
40#include "cmap.h"
41
42static struct cmapnode *cmap_splay(struct cmapnode *, wint_t);
43
44/*
45 * cmap_alloc --
46 * Allocate a character map.
47 */
48struct cmap *
49cmap_alloc(void)
50{
51 struct cmap *cm;
52
53 cm = malloc(sizeof(*cm));
54 if (cm == NULL)
55 return (NULL);
56 cm->cm_root = NULL;
57 cm->cm_def = CM_DEF_SELF;
58 cm->cm_havecache = false;
59 cm->cm_min = cm->cm_max = 0;
60 return (cm);
61}
62
63/*
64 * cmap_add --
65 * Add a mapping from "from" to "to" to the map.
66 */
67bool
68cmap_add(struct cmap *cm, wint_t from, wint_t to)
69{
70 struct cmapnode *cmn, *ncmn;
71
72 cm->cm_havecache = false;
73
74 if (cm->cm_root == NULL) {
75 cmn = malloc(sizeof(*cmn));
76 if (cmn == NULL)
77 return (false);
78 cmn->cmn_from = from;
79 cmn->cmn_to = to;
80 cmn->cmn_left = cmn->cmn_right = NULL;
81 cm->cm_root = cmn;
82 cm->cm_min = cm->cm_max = from;
83 return (true);
84 }
85
86 cmn = cm->cm_root = cmap_splay(cm->cm_root, from);
87
88 if (cmn->cmn_from == from) {
89 cmn->cmn_to = to;
90 return (true);
91 }
92
93 ncmn = malloc(sizeof(*ncmn));
94 if (ncmn == NULL)
95 return (false);
96 ncmn->cmn_from = from;
97 ncmn->cmn_to = to;
98 if (from < cmn->cmn_from) {
99 ncmn->cmn_left = cmn->cmn_left;
100 ncmn->cmn_right = cmn;
101 cmn->cmn_left = NULL;
102 } else {
103 ncmn->cmn_right = cmn->cmn_right;
104 ncmn->cmn_left = cmn;
105 cmn->cmn_right = NULL;
106 }
107 if (from < cm->cm_min)
108 cm->cm_min = from;
109 if (from > cm->cm_max)
110 cm->cm_max = from;
111 cm->cm_root = ncmn;
112
113 return (true);
114}
115
116/*
117 * cmap_lookup_hard --
118 * Look up the mapping for a character without using the cache.
119 */
120wint_t
121cmap_lookup_hard(struct cmap *cm, wint_t ch)
122{
123
124 if (cm->cm_root != NULL) {
125 cm->cm_root = cmap_splay(cm->cm_root, ch);
126 if (cm->cm_root->cmn_from == ch)
127 return (cm->cm_root->cmn_to);
128 }
129 return (cm->cm_def == CM_DEF_SELF ? ch : cm->cm_def);
130}
131
132/*
133 * cmap_cache --
134 * Update the cache.
135 */
136void
137cmap_cache(struct cmap *cm)
138{
139 wint_t ch;
140
141 for (ch = 0; ch < CM_CACHE_SIZE; ch++)
142 cm->cm_cache[ch] = cmap_lookup_hard(cm, ch);
143
144 cm->cm_havecache = true;
145}
146
147/*
148 * cmap_default --
149 * Change the value that characters without mappings map to, and
150 * return the old value. The special character value CM_MAP_SELF
151 * means characters map to themselves.
152 */
153wint_t
154cmap_default(struct cmap *cm, wint_t def)
155{
156 wint_t old;
157
158 old = cm->cm_def;
159 cm->cm_def = def;
160 cm->cm_havecache = false;
161 return (old);
162}
163
164static struct cmapnode *
165cmap_splay(struct cmapnode *t, wint_t ch)
166{
167 struct cmapnode N, *l, *r, *y;
168
169 /*
170 * Based on public domain code from Sleator.
171 */
172
173 assert(t != NULL);
174
175 N.cmn_left = N.cmn_right = NULL;
176 l = r = &N;
177 for (;;) {
178 if (ch < t->cmn_from) {
179 if (t->cmn_left != NULL &&
180 ch < t->cmn_left->cmn_from) {
181 y = t->cmn_left;
182 t->cmn_left = y->cmn_right;
183 y->cmn_right = t;
184 t = y;
185 }
186 if (t->cmn_left == NULL)
187 break;
188 r->cmn_left = t;
189 r = t;
190 t = t->cmn_left;
191 } else if (ch > t->cmn_from) {
192 if (t->cmn_right != NULL &&
193 ch > t->cmn_right->cmn_from) {
194 y = t->cmn_right;
195 t->cmn_right = y->cmn_left;
196 y->cmn_left = t;
197 t = y;
198 }
199 if (t->cmn_right == NULL)
200 break;
201 l->cmn_right = t;
202 l = t;
203 t = t->cmn_right;
204 } else
205 break;
206 }
207 l->cmn_right = t->cmn_left;
208 r->cmn_left = t->cmn_right;
209 t->cmn_left = N.cmn_right;
210 t->cmn_right = N.cmn_left;
211 return (t);
212}