dfa.icc
Go to the documentation of this file.00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Christian Schulte <schulte@gecode.org> 00005 * 00006 * Copyright: 00007 * Christian Schulte, 2004 00008 * 00009 * Last modified: 00010 * $Date: 2008-02-06 18:48:22 +0100 (Wed, 06 Feb 2008) $ by $Author: schulte $ 00011 * $Revision: 6102 $ 00012 * 00013 * This file is part of Gecode, the generic constraint 00014 * development environment: 00015 * http://www.gecode.org 00016 * 00017 * Permission is hereby granted, free of charge, to any person obtaining 00018 * a copy of this software and associated documentation files (the 00019 * "Software"), to deal in the Software without restriction, including 00020 * without limitation the rights to use, copy, modify, merge, publish, 00021 * distribute, sublicense, and/or sell copies of the Software, and to 00022 * permit persons to whom the Software is furnished to do so, subject to 00023 * the following conditions: 00024 * 00025 * The above copyright notice and this permission notice shall be 00026 * included in all copies or substantial portions of the Software. 00027 * 00028 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00029 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00030 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00031 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00032 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00033 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00034 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00035 * 00036 */ 00037 00038 namespace Gecode { 00039 00044 class DFA::DFAI : public SharedHandle::Object { 00045 public: 00047 unsigned int n_states; 00049 unsigned int n_symbols; 00051 unsigned int n_trans; 00053 int final_fst; 00055 int final_lst; 00057 Transition* trans; 00059 class HashEntry { 00060 public: 00061 int symbol; 00062 const Transition* fst; 00063 const Transition* lst; 00064 }; 00066 HashEntry* table; 00068 int n_log; 00070 GECODE_INT_EXPORT void fill(void); 00072 DFAI(unsigned int nt); 00074 GECODE_INT_EXPORT DFAI(void); 00076 virtual ~DFAI(void); 00078 GECODE_INT_EXPORT virtual SharedHandle::Object* copy(void) const; 00079 }; 00080 00081 forceinline 00082 DFA::DFAI::DFAI(unsigned int nt) 00083 : trans(nt == 0 ? NULL : 00084 static_cast<Transition*>(Memory::malloc(sizeof(Transition)*nt))) {} 00085 00086 forceinline 00087 DFA::DFAI::~DFAI(void) { 00088 if (n_trans > 0) 00089 Memory::free(trans); 00090 Memory::free(table); 00091 } 00092 00093 forceinline 00094 DFA::DFA(void) {} 00095 00096 00097 forceinline 00098 DFA::DFA(const DFA& d) 00099 : SharedHandle(d) {} 00100 00101 forceinline unsigned int 00102 DFA::n_states(void) const { 00103 const DFAI* d = static_cast<DFAI*>(object()); 00104 return (d == NULL) ? 1 : d->n_states; 00105 } 00106 00107 forceinline unsigned int 00108 DFA::n_symbols(void) const { 00109 const DFAI* d = static_cast<DFAI*>(object()); 00110 return (d == NULL) ? 0 : d->n_symbols; 00111 } 00112 00113 forceinline unsigned int 00114 DFA::n_transitions(void) const { 00115 const DFAI* d = static_cast<DFAI*>(object()); 00116 return (d == NULL) ? 0 : d->n_trans; 00117 } 00118 00119 forceinline int 00120 DFA::final_fst(void) const { 00121 const DFAI* d = static_cast<DFAI*>(object()); 00122 return (d == NULL) ? 0 : d->final_fst; 00123 } 00124 00125 forceinline int 00126 DFA::final_lst(void) const { 00127 const DFAI* d = static_cast<DFAI*>(object()); 00128 return (d == NULL) ? 0 : d->final_lst; 00129 } 00130 00131 forceinline int 00132 DFA::symbol_min(void) const { 00133 const DFAI* d = static_cast<DFAI*>(object()); 00134 return ((d != NULL) && (d->n_trans > 0)) ? 00135 d->trans[0].symbol : Int::Limits::min; 00136 } 00137 00138 forceinline int 00139 DFA::symbol_max(void) const { 00140 const DFAI* d = static_cast<DFAI*>(object()); 00141 return ((d != NULL) && (d->n_trans > 0)) ? 00142 d->trans[d->n_trans-1].symbol : Int::Limits::max; 00143 } 00144 00145 00146 00147 /* 00148 * Iterating over all transitions 00149 * 00150 */ 00151 00152 forceinline 00153 DFA::Transitions::Transitions(const DFA& d) { 00154 const DFAI* o = static_cast<DFAI*>(d.object()); 00155 if (o != NULL) { 00156 c_trans = &o->trans[0]; 00157 e_trans = c_trans+o->n_trans; 00158 } else { 00159 c_trans = e_trans = NULL; 00160 } 00161 } 00162 00163 forceinline 00164 DFA::Transitions::Transitions(const DFA& d, int n) { 00165 const DFAI* o = static_cast<DFAI*>(d.object()); 00166 if (o != NULL) { 00167 int mask = (1<<o->n_log)-1; 00168 int p = n & mask; 00169 while ((o->table[p].fst != NULL) && (o->table[p].symbol != n)) 00170 p = (p+1) & mask; 00171 c_trans = o->table[p].fst; 00172 e_trans = o->table[p].lst; 00173 } else { 00174 c_trans = e_trans = NULL; 00175 } 00176 } 00177 00178 forceinline bool 00179 DFA::Transitions::operator()(void) const { 00180 return c_trans < e_trans; 00181 } 00182 00183 forceinline void 00184 DFA::Transitions::operator++(void) { 00185 c_trans++; 00186 } 00187 00188 forceinline int 00189 DFA::Transitions::i_state(void) const { 00190 return c_trans->i_state; 00191 } 00192 00193 forceinline int 00194 DFA::Transitions::symbol(void) const { 00195 return c_trans->symbol; 00196 } 00197 00198 forceinline int 00199 DFA::Transitions::o_state(void) const { 00200 return c_trans->o_state; 00201 } 00202 00203 /* 00204 * Iterating over symbols 00205 * 00206 */ 00207 00208 forceinline 00209 DFA::Symbols::Symbols(const DFA& d) { 00210 const DFAI* o = static_cast<DFAI*>(d.object()); 00211 if (o != NULL) { 00212 c_trans = &o->trans[0]; 00213 e_trans = c_trans+o->n_trans; 00214 } else { 00215 c_trans = e_trans = NULL; 00216 } 00217 } 00218 00219 forceinline bool 00220 DFA::Symbols::operator()(void) const { 00221 return c_trans < e_trans; 00222 } 00223 00224 forceinline void 00225 DFA::Symbols::operator++(void) { 00226 int s = c_trans->symbol; 00227 do { 00228 c_trans++; 00229 } while ((c_trans < e_trans) && (s == c_trans->symbol)); 00230 } 00231 00232 forceinline int 00233 DFA::Symbols::val(void) const { 00234 return c_trans->symbol; 00235 } 00236 00237 } 00238 00239 00240 // STATISTICS: int-prop 00241