1 /* ecs - equivalence class routines */
4 * Copyright (c) 1990 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
10 * The United States Government has rights in this work pursuant
11 * to contract no. DE-AC03-76SF00098 between the United States
12 * Department of Energy and the University of California.
14 * Redistribution and use in source and binary forms are permitted provided
15 * that: (1) source distributions retain this entire copyright notice and
16 * comment, and (2) distributions including binaries display the following
17 * acknowledgement: ``This product includes software developed by the
18 * University of California, Berkeley and its contributors'' in the
19 * documentation or other materials provided with the distribution and in
20 * all advertising materials mentioning features or use of this software.
21 * Neither the name of the University nor the names of its contributors may
22 * be used to endorse or promote products derived from this software without
23 * specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
31 "@(#) $Id: ecs.c,v 1.2 1994/06/24 10:56:45 ceriel Exp $ (LBL)";
36 /* ccl2ecl - convert character classes to set of equivalence classes
45 int i, ich, newlen, cclp, ccls, cclmec;
47 for ( i = 1; i <= lastccl; ++i )
49 /* we loop through each character class, and for each character
50 * in the class, add the character's equivalence class to the
51 * new "character" class we are creating. Thus when we are all
52 * done, character classes will really consist of collections
53 * of equivalence classes
59 for ( ccls = 0; ccls < ccllen[i]; ++ccls )
61 ich = ccltbl[cclp + ccls];
62 cclmec = ecgroup[ich];
64 if ( xlation && cclmec < 0 )
66 /* special hack--if we're doing %t tables then it's
67 * possible that no representative of this character's
68 * equivalence class is in the ccl. So waiting till
69 * we see the representative would be disastrous. Instead,
70 * we add this character's equivalence class anyway, if it's
71 * not already present.
75 /* this loop makes this whole process n^2; but we don't
76 * really care about %t performance anyway
78 for ( j = 0; j < newlen; ++j )
79 if ( ccltbl[cclp + j] == -cclmec )
83 { /* no representative yet, add this one in */
84 ccltbl[cclp + newlen] = -cclmec;
89 else if ( cclmec > 0 )
91 ccltbl[cclp + newlen] = cclmec;
101 /* cre8ecs - associate equivalence class numbers with class members
105 * number of classes = cre8ecs( fwd, bck, num );
107 * fwd is the forward linked-list of equivalence class members. bck
108 * is the backward linked-list, and num is the number of class members.
110 * Returned is the number of classes.
113 int cre8ecs( fwd, bck, num )
114 int fwd[], bck[], num;
121 /* create equivalence class numbers. From now on, abs( bck(x) )
122 * is the equivalence class number for object x. If bck(x)
123 * is positive, then x is the representative of its equivalence
126 for ( i = 1; i <= num; ++i )
130 for ( j = fwd[i]; j != NIL; j = fwd[j] )
138 /* ecs_from_xlation - associate equivalence class numbers using %t table
141 * numecs = ecs_from_xlation( ecmap );
143 * Upon return, ecmap will map each character code to its equivalence
144 * class. The mapping will be positive if the character is the representative
145 * of its class, negative otherwise.
147 * Returns the number of equivalence classes used.
150 int ecs_from_xlation( ecmap )
155 int nul_is_alone = false;
156 int did_default_xlation_class = false;
158 if ( xlation[0] != 0 )
160 /* if NUL shares its translation with other characters, choose one
161 * of the other characters as the representative for the equivalence
162 * class. This allows a cheap test later to see whether we can
163 * do away with NUL's equivalence class.
165 for ( i = 1; i < csize; ++i )
166 if ( xlation[i] == -xlation[0] )
168 xlation[i] = xlation[0];
169 ecmap[0] = -xlation[0];
174 /* didn't find a companion character--remember this fact */
178 for ( i = 1; i < csize; ++i )
179 if ( xlation[i] == 0 )
181 if ( did_default_xlation_class )
182 ecmap[i] = -num_xlations;
186 /* make an equivalence class for those characters not
187 * specified in the %t table
190 ecmap[i] = num_xlations;
191 did_default_xlation_class = true;
196 ecmap[i] = xlation[i];
199 /* force NUL's equivalence class to be the last one */
202 ecmap[0] = num_xlations;
204 /* there's actually a bug here: if someone is fanatic enough to
205 * put every character in its own translation class, then right
206 * now we just promoted NUL's equivalence class to be csize + 1;
207 * we can handle NUL's class number being == csize (by instead
208 * putting it in its own table), but we can't handle some *other*
209 * character having to be put in its own table, too. So in
210 * this case we bail out.
212 if ( num_xlations > csize )
213 flexfatal( "too many %t classes!" );
220 /* mkeccl - update equivalence classes based on character class xtions
224 * int lenccl, fwd[llsiz], bck[llsiz], llsiz, NUL_mapping;
225 * mkeccl( ccls, lenccl, fwd, bck, llsiz, NUL_mapping );
227 * where ccls contains the elements of the character class, lenccl is the
228 * number of elements in the ccl, fwd is the forward link-list of equivalent
229 * characters, bck is the backward link-list, and llsiz size of the link-list
231 * NUL_mapping is the value which NUL (0) should be mapped to.
234 void mkeccl( ccls, lenccl, fwd, bck, llsiz, NUL_mapping )
236 int lenccl, fwd[], bck[], llsiz, NUL_mapping;
239 int cclp, oldec, newec;
241 static unsigned char cclflags[CSIZE]; /* initialized to all '\0' */
243 /* note that it doesn't matter whether or not the character class is
244 * negated. The same results will be obtained in either case.
249 while ( cclp < lenccl )
253 if ( NUL_mapping && cclm == 0 )
261 for ( i = fwd[cclm]; i != NIL && i <= llsiz; i = fwd[i] )
262 { /* look for the symbol in the character class */
263 for ( ; j < lenccl; ++j )
265 register int ccl_char;
267 if ( NUL_mapping && ccls[j] == 0 )
268 ccl_char = NUL_mapping;
275 if ( ccl_char == i && ! cclflags[j] )
277 /* we found an old companion of cclm in the ccl.
278 * link it into the new equivalence class and flag it as
279 * having been processed
285 cclflags[j] = 1; /* set flag so we don't reprocess */
287 /* get next equivalence class member */
293 /* symbol isn't in character class. Put it in the old equivalence
307 if ( bck[cclm] != NIL || oldec != bck[cclm] )
315 /* find next ccl member to process */
317 for ( ++cclp; cclflags[cclp] && cclp < lenccl; ++cclp )
319 /* reset "doesn't need processing" flag */
326 /* mkechar - create equivalence class for single character
329 * int tch, fwd[], bck[];
330 * mkechar( tch, fwd, bck );
333 void mkechar( tch, fwd, bck )
334 int tch, fwd[], bck[];
337 /* if until now the character has been a proper subset of
338 * an equivalence class, break it away to create a new ec
341 if ( fwd[tch] != NIL )
342 bck[fwd[tch]] = bck[tch];
344 if ( bck[tch] != NIL )
345 fwd[bck[tch]] = fwd[tch];