dfa.hpp
Go to the documentation of this file.
/* -*- 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 {
class DFA::DFAI : public SharedHandle::Object {
public:
int n_states;
unsigned int n_symbols;
int n_trans;
unsigned int max_degree;
int final_fst;
int final_lst;
std::size_t key;
Transition* trans;
class HashEntry {
public:
int symbol;
const Transition* fst;
const Transition* lst;
};
HashEntry* table;
int n_log;
void fill(void);
DFAI(int nt);
GECODE_INT_EXPORT DFAI(void);
virtual ~DFAI(void);
};
forceinline
DFA::DFAI::DFAI(int nt)
: trans(nt == 0 ? NULL : 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 = NULL;
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 != NULL)
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 == NULL) ? 1 : d->n_states;
}
forceinline unsigned int
DFA::n_symbols(void) const {
const DFAI* d = static_cast<DFAI*>(object());
return (d == NULL) ? 0 : d->n_symbols;
}
forceinline int
DFA::n_transitions(void) const {
const DFAI* d = static_cast<DFAI*>(object());
return (d == NULL) ? 0 : d->n_trans;
}
forceinline unsigned int
DFA::max_degree(void) const {
const DFAI* d = static_cast<DFAI*>(object());
return (d == NULL) ? 0 : d->max_degree;
}
forceinline int
DFA::final_fst(void) const {
const DFAI* d = static_cast<DFAI*>(object());
return (d == NULL) ? 0 : d->final_fst;
}
forceinline int
DFA::final_lst(void) const {
const DFAI* d = static_cast<DFAI*>(object());
return (d == NULL) ? 0 : d->final_lst;
}
forceinline int
DFA::symbol_min(void) const {
const DFAI* d = static_cast<DFAI*>(object());
return ((d != NULL) && (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 != NULL) && (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 != NULL) ? 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 != NULL) {
c_trans = &o->trans[0];
e_trans = c_trans+o->n_trans;
} else {
c_trans = e_trans = NULL;
}
}
forceinline
DFA::Transitions::Transitions(const DFA& d, int n) {
const DFAI* o = static_cast<DFAI*>(d.object());
if (o != NULL) {
int mask = (1<<o->n_log)-1;
int p = n & mask;
while ((o->table[p].fst != NULL) && (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 = NULL;
}
}
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 != NULL) {
c_trans = &o->trans[0];
e_trans = c_trans+o->n_trans;
} else {
c_trans = e_trans = NULL;
}
}
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
Gecode::DFA::DFAI::HashEntry
Specification of transition range.
Definition dfa.hpp:61
Gecode::DFA::DFAI::HashEntry::lst
const Transition * lst
Last transition for the symbol.
Definition dfa.hpp:65
Gecode::DFA::DFAI::HashEntry::fst
const Transition * fst
First transition for the symbol.
Definition dfa.hpp:64
Gecode::DFA::DFAI::HashEntry::symbol
int symbol
Symbol.
Definition dfa.hpp:63
Gecode::DFA::DFAI
Data stored for a DFA.
Definition dfa.hpp:42
Gecode::DFA::DFAI::final_lst
int final_lst
Last final state.
Definition dfa.hpp:55
Gecode::DFA::DFAI::table
HashEntry * table
The transition hash table by symbol.
Definition dfa.hpp:68
Gecode::DFA::DFAI::n_states
int n_states
Number of states.
Definition dfa.hpp:45
Gecode::DFA::DFAI::key
std::size_t key
Hash key.
Definition dfa.hpp:57
Gecode::DFA::DFAI::max_degree
unsigned int max_degree
Maximal degree (in-degree and out-degree of any state) and maximal number of transitions per symbol.
Definition dfa.hpp:51
Gecode::DFA::DFAI::n_trans
int n_trans
Number of transitions.
Definition dfa.hpp:49
Gecode::DFA::DFAI::n_symbols
unsigned int n_symbols
Number of symbols.
Definition dfa.hpp:47
Gecode::DFA::DFAI::~DFAI
virtual ~DFAI(void)
Delete automaton implemenentation.
Definition dfa.hpp:86
Gecode::DFA::DFAI::final_fst
int final_fst
First final state.
Definition dfa.hpp:53
Gecode::DFA::DFAI::n_log
int n_log
Size of table (as binary logarithm).
Definition dfa.hpp:70
Gecode::DFA::DFAI::DFAI
DFAI(void)
Initialize automaton implementation as empty.
Gecode::DFA::DFAI::fill
void fill(void)
Fill hash table.
Definition dfa.hpp:93
Gecode::DFA::DFAI::trans
Transition * trans
The transitions.
Definition dfa.hpp:59
Gecode::DFA::Symbols::val
int val(void) const
Return current symbol.
Definition dfa.hpp:293
Gecode::DFA::Symbols::operator++
void operator++(void)
Move iterator to next symbol.
Definition dfa.hpp:285
Gecode::DFA::Symbols::operator()
bool operator()(void) const
Test whether iterator still at a symbol.
Definition dfa.hpp:280
Gecode::DFA::Symbols::Symbols
Symbols(const DFA &d)
Initialize to symbols of DFA d.
Definition dfa.hpp:269
Gecode::DFA::Transition
Specification of a DFA transition.
Definition int.hh:2058
Gecode::DFA::Transition::i_state
int i_state
input state
Definition int.hh:2060
Gecode::DFA::Transition::o_state
int o_state
output state Default constructor
Definition int.hh:2062
Gecode::DFA::Transition::symbol
int symbol
symbol
Definition int.hh:2061
Gecode::DFA::Transition::Transition
Transition(void)
Definition dfa.hpp:201
Gecode::DFA::Transitions
Iterator for DFA transitions (sorted by symbols).
Definition int.hh:2069
Gecode::DFA::Transitions::o_state
int o_state(void) const
Return out-state of current transition.
Definition dfa.hpp:259
Gecode::DFA::Transitions::operator++
void operator++(void)
Move iterator to next transition.
Definition dfa.hpp:244
Gecode::DFA::Transitions::operator()
bool operator()(void) const
Test whether iterator still at a transition.
Definition dfa.hpp:239
Gecode::DFA::Transitions::Transitions
Transitions(const DFA &d)
Initialize to all transitions of DFA d.
Definition dfa.hpp:213
Gecode::DFA::Transitions::i_state
int i_state(void) const
Return in-state of current transition.
Definition dfa.hpp:249
Gecode::DFA::Transitions::symbol
int symbol(void) const
Return symbol of current transition.
Definition dfa.hpp:254
Gecode::DFA
Deterministic finite automaton (DFA).
Definition int.hh:2049
Gecode::DFA::operator<<
std::basic_ostream< Char, Traits > & operator<<(std::basic_ostream< Char, Traits > &os, const DFA &d)
Definition dfa.hpp:300
Gecode::DFA::operator==
bool operator==(const DFA &d) const
Test whether DFA is equal to d.
Definition dfa.hpp:330
Gecode::DFA::symbol_max
int symbol_max(void) const
Return largest symbol in DFA.
Definition dfa.hpp:182
Gecode::DFA::max_degree
unsigned int max_degree(void) const
Return maximal degree (in-degree and out-degree) of any state.
Definition dfa.hpp:157
Gecode::DFA::n_states
int n_states(void) const
Return the number of states.
Definition dfa.hpp:139
Gecode::DFA::symbol_min
int symbol_min(void) const
Return smallest symbol in DFA.
Definition dfa.hpp:175
Gecode::DFA::DFA
DFA(void)
Initialize for DFA accepting the empty word.
Definition dfa.hpp:131
Gecode::DFA::final_lst
int final_lst(void) const
Return the number of the last final state.
Definition dfa.hpp:169
Gecode::DFA::n_transitions
int n_transitions(void) const
Return the number of transitions.
Definition dfa.hpp:151
Gecode::DFA::n_symbols
unsigned int n_symbols(void) const
Return the number of symbols.
Definition dfa.hpp:145
Gecode::DFA::hash
std::size_t hash(void) const
Return hash key.
Definition dfa.hpp:189
Gecode::DFA::operator!=
bool operator!=(const DFA &d) const
Test whether DFA is not equal to d.
Definition dfa.hpp:347
Gecode::DFA::final_fst
int final_fst(void) const
Return the number of the first final state.
Definition dfa.hpp:163
Gecode::SharedHandle::Object
The shared object.
Definition shared-object.hpp:53
Gecode::SharedHandle::SharedHandle
SharedHandle(void)
Create shared handle with no object pointing to.
Definition shared-object.hpp:122
Gecode::SharedHandle::object
SharedHandle::Object * object(void) const
Access to the shared object.
Definition shared-object.hpp:102
GECODE_INT_EXPORT
#define GECODE_INT_EXPORT
Definition int.hh:81
Gecode::heap
Heap heap
The single global heap.
Definition heap.cpp:44
Gecode::Int::Limits::min
const int min
Smallest allowed integer value.
Definition int.hh:118
Gecode::Int::Limits::max
const int max
Largest allowed integer value.
Definition int.hh:116
Gecode
Gecode toplevel namespace
Gecode::cmb_hash
void cmb_hash(std::size_t &seed, const T h)
Combine hash value h into seed.
Definition hash.hpp:44
Test::Int::Distinct::d
Gecode::IntSet d(v, 7)
forceinline
#define forceinline
Definition config.hpp:187