File: dfa.hpp

package info (click to toggle)
gecode-snapshot 6.2.0%2Bgit20240207-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 35,308 kB
  • sloc: cpp: 475,516; perl: 2,077; makefile: 1,816; sh: 198
file content (356 lines) | stat: -rw-r--r-- 9,248 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
 *  Main authors:
 *     Christian Schulte <schulte@gecode.org>
 *
 *  Copyright:
 *     Christian Schulte, 2004
 *
 *  This file is part of Gecode, the generic constraint
 *  development environment:
 *     http://www.gecode.org
 *
 *  Permission is hereby granted, free of charge, to any person obtaining
 *  a copy of this software and associated documentation files (the
 *  "Software"), to deal in the Software without restriction, including
 *  without limitation the rights to use, copy, modify, merge, publish,
 *  distribute, sublicense, and/or sell copies of the Software, and to
 *  permit persons to whom the Software is furnished to do so, subject to
 *  the following conditions:
 *
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include <sstream>

namespace Gecode {

  /**
   * \brief Data stored for a %DFA
   *
   */
  class DFA::DFAI : public SharedHandle::Object {
  public:
    /// Number of states
    int n_states;
    /// Number of symbols
    unsigned int n_symbols;
    /// Number of transitions
    int n_trans;
    /// Maximal degree (in-degree and out-degree of any state) and maximal number of transitions per symbol
    unsigned int max_degree;
    /// First final state
    int final_fst;
    /// Last final state
    int final_lst;
    /// Hash key
    std::size_t key;
    /// The transitions
    Transition* trans;
    /// Specification of transition range
    class HashEntry {
    public:
      int symbol;            ///< Symbol
      const Transition* fst; ///< First transition for the symbol
      const Transition* lst; ///< Last transition for the symbol
    };
    /// The transition hash table by symbol
    HashEntry* table;
    /// Size of table (as binary logarithm)
    int n_log;
    /// Fill hash table
    void fill(void);
    /// Initialize automaton implementation with \a nt transitions
    DFAI(int nt);
    /// Initialize automaton implementation as empty
    GECODE_INT_EXPORT DFAI(void);
    /// Delete automaton implemenentation
    virtual ~DFAI(void);
  };

  forceinline
  DFA::DFAI::DFAI(int nt)
    : trans(nt == 0 ? nullptr : heap.alloc<Transition>(nt)) {}

  forceinline
  DFA::DFAI::~DFAI(void) {
    if (n_trans > 0)
      heap.rfree(trans);
    heap.rfree(table);
  }

  forceinline void
  DFA::DFAI::fill(void) {
    key = static_cast<std::size_t>(n_states);
    cmb_hash(key, n_trans);
    cmb_hash(key, n_symbols);
    cmb_hash(key, final_fst);
    cmb_hash(key, final_lst);
    // Compute smallest logarithm larger than n_symbols
    n_log = 1;
    while (n_symbols >= static_cast<unsigned int>(1<<n_log))
      n_log++;
    // Allocate memory
    table = heap.alloc<HashEntry>(1<<n_log);
    // Initialize table
    for (int i=(1<<n_log); i--; )
      table[i].fst = table[i].lst = nullptr;
    int mask = (1 << n_log) - 1;
    // Enter transitions to table
    for (int i = 0; i<n_trans; ) {
      cmb_hash(key, trans[i].i_state);
      cmb_hash(key, trans[i].symbol);
      cmb_hash(key, trans[i].o_state);
      int s = trans[i].symbol;
      Transition* fst = &trans[i];
      i++;
      while ((i<n_trans) && (trans[i].symbol == s))
        i++;
      Transition* lst = &trans[i];
      // Enter with linear collision resolution
      int p = s & mask;
      while (table[p].fst != nullptr)
        p = (p+1) & mask;
      table[p].symbol = s;
      table[p].fst    = fst;
      table[p].lst    = lst;
    }
  }

  forceinline
  DFA::DFA(void) {}


  forceinline
  DFA::DFA(const DFA& d)
    : SharedHandle(d) {}

  forceinline int
  DFA::n_states(void) const {
    const DFAI* d = static_cast<DFAI*>(object());
    return (d == nullptr) ? 1 : d->n_states;
  }

  forceinline unsigned int
  DFA::n_symbols(void) const {
    const DFAI* d = static_cast<DFAI*>(object());
    return (d == nullptr) ? 0 : d->n_symbols;
  }

  forceinline int
  DFA::n_transitions(void) const {
    const DFAI* d = static_cast<DFAI*>(object());
    return (d == nullptr) ? 0 : d->n_trans;
  }

  forceinline unsigned int
  DFA::max_degree(void) const {
    const DFAI* d = static_cast<DFAI*>(object());
    return (d == nullptr) ? 0 : d->max_degree;
  }

  forceinline int
  DFA::final_fst(void) const {
    const DFAI* d = static_cast<DFAI*>(object());
    return (d == nullptr) ? 0 : d->final_fst;
  }

  forceinline int
  DFA::final_lst(void) const {
    const DFAI* d = static_cast<DFAI*>(object());
    return (d == nullptr) ? 0 : d->final_lst;
  }

  forceinline int
  DFA::symbol_min(void) const {
    const DFAI* d = static_cast<DFAI*>(object());
    return ((d != nullptr) && (d->n_trans > 0)) ?
      d->trans[0].symbol : Int::Limits::min;
  }

  forceinline int
  DFA::symbol_max(void) const {
    const DFAI* d = static_cast<DFAI*>(object());
    return ((d != nullptr) && (d->n_trans > 0)) ?
      d->trans[d->n_trans-1].symbol : Int::Limits::max;
  }

  forceinline std::size_t
  DFA::hash(void) const {
    const DFAI* d = static_cast<DFAI*>(object());
    return (d != nullptr) ? d->key : 0;
  }


  /*
   * Constructing transitions
   *
   */

  forceinline
  DFA::Transition::Transition(void) {}

  forceinline
  DFA::Transition::Transition(int i_state0, int symbol0, int o_state0)
    : i_state(i_state0), symbol(symbol0), o_state(o_state0) {}

  /*
   * Iterating over all transitions
   *
   */

  forceinline
  DFA::Transitions::Transitions(const DFA& d) {
    const DFAI* o = static_cast<DFAI*>(d.object());
    if (o != nullptr) {
      c_trans = &o->trans[0];
      e_trans = c_trans+o->n_trans;
    } else {
      c_trans = e_trans = nullptr;
    }
  }

  forceinline
  DFA::Transitions::Transitions(const DFA& d, int n) {
    const DFAI* o = static_cast<DFAI*>(d.object());
    if (o != nullptr) {
      int mask = (1<<o->n_log)-1;
      int p = n & mask;
      while ((o->table[p].fst != nullptr) && (o->table[p].symbol != n))
        p = (p+1) & mask;
      c_trans = o->table[p].fst;
      e_trans = o->table[p].lst;
    } else {
      c_trans = e_trans = nullptr;
    }
  }

  forceinline bool
  DFA::Transitions::operator ()(void) const {
    return c_trans < e_trans;
  }

  forceinline void
  DFA::Transitions::operator ++(void) {
    c_trans++;
  }

  forceinline int
  DFA::Transitions::i_state(void) const {
    return c_trans->i_state;
  }

  forceinline int
  DFA::Transitions::symbol(void) const {
    return c_trans->symbol;
  }

  forceinline int
  DFA::Transitions::o_state(void) const {
    return c_trans->o_state;
  }

  /*
   * Iterating over symbols
   *
   */

  forceinline
  DFA::Symbols::Symbols(const DFA& d) {
    const DFAI* o = static_cast<DFAI*>(d.object());
    if (o != nullptr) {
      c_trans = &o->trans[0];
      e_trans = c_trans+o->n_trans;
    } else {
      c_trans = e_trans = nullptr;
    }
  }

  forceinline bool
  DFA::Symbols::operator ()(void) const {
    return c_trans < e_trans;
  }

  forceinline void
  DFA::Symbols::operator ++(void) {
    int s = c_trans->symbol;
    do {
      c_trans++;
    } while ((c_trans < e_trans) && (s == c_trans->symbol));
  }

  forceinline int
  DFA::Symbols::val(void) const {
    return c_trans->symbol;
  }


  template<class Char, class Traits>
  std::basic_ostream<Char,Traits>&
  operator <<(std::basic_ostream<Char,Traits>& os, const DFA& d) {
    std::basic_ostringstream<Char,Traits> st;
    st.copyfmt(os); st.width(0);
    st << "Start state: 0" << std::endl
       << "States:      0..." << d.n_states()-1 << std::endl
       << "Transitions:";
    for (int s = 0; s < static_cast<int>(d.n_states()); s++) {
      DFA::Transitions t(d);
      int n = 0;
      while (t()) {
        if (t.i_state() == s) {
          if ((n % 4) == 0)
            st << std::endl << "\t";
          st << "[" << t.i_state() << "]"
             << "- " << t.symbol() << " >"
             << "[" << t.o_state() << "]  ";
          ++n;
        }
        ++t;
      }
    }
    st << std::endl << "Final states: "
       << std::endl
       << "\t[" << d.final_fst() << "] ... ["
       << d.final_lst()-1 << "]"
       << std::endl;
    return os << st.str();
  }

  forceinline bool
  DFA::operator ==(const DFA& d) const {
    if (n_states() != d.n_states())
      return false;
    if (n_transitions() != d.n_transitions())
      return false;
    if (n_symbols() != d.n_symbols())
      return false;
    if (max_degree() != d.max_degree())
      return false;
    if (final_fst() != d.final_fst())
      return false;
    if (final_lst() != d.final_lst())
      return false;
    return equal(d);
  }

  forceinline bool
  DFA::operator !=(const DFA& d) const {
    return !(*this == d);
  }


}


// STATISTICS: int-prop