2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1996-10-29 13:57:31 adam
8 * Include of zebrautl.h instead of alexutil.h.
10 * Revision 1.4 1995/09/04 12:33:28 adam
11 * Various cleanup. YAZ util used instead.
13 * Revision 1.3 1995/01/25 11:30:51 adam
14 * Simple error reporting when parsing regular expressions.
15 * Memory usage reduced.
17 * Revision 1.2 1995/01/24 16:00:23 adam
18 * Added -ansi to CFLAGS.
19 * Some changes to the dfa module.
21 * Revision 1.1 1994/09/26 10:16:58 adam
22 * First version of dfa module in alex. This version uses yacc to parse
23 * regular expressions. This should be hand-made instead.
36 #define TRAN_CHUNK 100
38 int init_DFA_states (struct DFA_states **dfasp, SetType st, int hash)
40 struct DFA_states *dfas;
44 dfas = (struct DFA_states *) imalloc (sizeof(struct DFA_states));
46 dfas->hasharray = (struct DFA_state **)
47 imalloc (sizeof(struct DFA_state*) * hash);
48 assert (dfas->hasharray);
50 dfas->freelist = dfas->unmarked = dfas->marked = NULL;
51 dfas->statemem = NULL;
56 dfas->transmem = tm = (struct DFA_trans *)
57 imalloc (sizeof(struct DFA_trans));
60 tm->size = TRAN_CHUNK;
62 tm->tran_block = (struct DFA_tran *)
63 imalloc (sizeof(struct DFA_tran) * tm->size);
64 assert (tm->tran_block);
66 dfas->sortarray = NULL;
67 for (i=0; i<dfas->hash; i++)
68 dfas->hasharray[i] = NULL;
72 int rm_DFA_states (struct DFA_states **dfasp)
74 struct DFA_states *dfas = *dfasp;
76 struct DFA_trans *tm, *tm1;
80 ifree (dfas->hasharray);
81 ifree (dfas->sortarray);
83 for (tm=dfas->transmem; tm; tm=tm1)
85 ifree (tm->tran_block);
89 for (sm=dfas->statemem; sm; sm=sm1)
91 ifree (sm->state_block);
100 int add_DFA_state (struct DFA_states *dfas, Set *s, struct DFA_state **sp)
103 struct DFA_state *si, **sip;
108 assert (dfas->hasharray);
109 sip = dfas->hasharray + (hash_Set (dfas->st, *s) % dfas->hash);
110 for (si = *sip; si; si=si->link)
111 if (eq_Set (dfas->st, si->set, *s))
114 *s = rm_Set (dfas->st, *s);
119 sb = (DFA_stateb *) imalloc (sizeof(*sb));
120 sb->next = dfas->statemem;
122 sb->state_block = si = dfas->freelist =
123 (struct DFA_state *) imalloc (sizeof(struct DFA_state)*DFA_CHUNK);
124 for (i = 0; i<DFA_CHUNK-1; i++, si++)
130 dfas->freelist = si->next;
132 si->next = dfas->unmarked;
138 si->no = (dfas->no)++;
146 void add_DFA_tran (struct DFA_states *dfas, struct DFA_state *s,
147 int ch0, int ch1, int to)
149 struct DFA_trans *tm;
153 if (tm->ptr == tm->size)
155 tm = (struct DFA_trans *) imalloc (sizeof(struct DFA_trans));
157 tm->next = dfas->transmem;
159 tm->size = s->tran_no >= TRAN_CHUNK ? s->tran_no+8 : TRAN_CHUNK;
160 tm->tran_block = (struct DFA_tran *)
161 imalloc (sizeof(struct DFA_tran) * tm->size);
162 assert (tm->tran_block);
164 memcpy (tm->tran_block, s->trans,
165 s->tran_no*sizeof (struct DFA_tran));
166 tm->ptr = s->tran_no;
167 s->trans = tm->tran_block;
170 t = tm->tran_block + tm->ptr++;
176 struct DFA_state *get_DFA_state (struct DFA_states *dfas)
178 struct DFA_state *si;
180 if (!(si = dfas->unmarked))
182 dfas->unmarked = si->next;
183 si->next = dfas->marked;
185 si->trans = dfas->transmem->tran_block + dfas->transmem->ptr;
190 void sort_DFA_states (struct DFA_states *dfas)
194 dfas->sortarray = (struct DFA_state **)
195 imalloc (sizeof(struct DFA_state *)*dfas->no);
196 for (s = dfas->marked; s; s=s->next)
197 dfas->sortarray[s->no] = s;
198 ifree (dfas->hasharray);
199 dfas->hasharray = NULL;