⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 neighbor.h

📁 一个类似STL的自动机的源代码库
💻 H
字号:
/* * ASTL - the Automaton Standard Template Library. * C++ generic components for Finite State Automata handling. * Copyright (C) 2000-2003 Vincent Le Maout (vincent.lemaout@chello.fr). *  * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. *  * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. *  * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA * */#ifndef ASTL_NEIGHBOR_H#define ASTL_NEIGHBOR_H#include <stack>#include <vector>#include <utility>#include <cursor.h>#include <iostream>#include <cstring>   // strlenusing namespace std;ASTL_BEGIN_NAMESPACE// Defines:// neighbor_cursor (forward cursor)// A neighborood cursor hides the automaton parts straying too far from the// specified word.// Instanciation parameters:// 1. ForwardCursor: a forward cursor type//// Requirements:// None//// Constructor: // 1. A forward cursor, a word, a max editing distance// TODO:// Genericize the distance computing with an object functiontemplate <typename CharType>struct default_distance : public binary_function<CharType, CharType, int>{  int operator()(const CharType& x, const CharType& y) const {    return x == y ? 0 : 1;  }};template <typename ForwardCursor, typename RandomAccessIterator, 	  typename DistanceFunction = default_distance<typename ForwardCursor::char_type> >class neighbor_cursor : public forward_cursor_concept{protected:  ForwardCursor x;  RandomAccessIterator last;   typedef vector<pair<RandomAccessIterator, int> > container; // position, distance   container d;      static const int INSERTION = 1;  static const int DELETION = 1;public:  typedef neighbor_cursor          self;  typedef typename ForwardCursor::tag_type    tag_type;  typedef typename ForwardCursor::char_type   char_type;    typedef typename ForwardCursor::char_traits char_traits;  typedef typename ForwardCursor::state_type  state_type;  neighbor_cursor(const ForwardCursor& c, RandomAccessIterator i, 		  RandomAccessIterator j, int dist)    : x(c), last(j) {     d.push_back(make_pair(i, dist));  }  neighbor_cursor()  { }    state_type src() const {    return x.src();  }  bool sink() const {    return x.sink();  }  state_type sink_state() const {    return x.sink_state();  }  bool forward(const char_type &a) {    typename container::size_type i, j = d.size();    bool ok = false;    if (x.forward(a))      for(i = 0; i < j; ++i) {	// substitution & deletion	int dist = d[i].second;	for(RandomAccessIterator first = d[i].first; 	    first != last && dist >= 0; 	    ++first, dist -= DELETION) {	  int delta = DistanceFunction()(*first, a);	  if (dist >= delta) {	    ok = true;	    d.push_back(make_pair(first + 1, dist - delta));	  }	}	// insertion:	d[i].second -= INSERTION;	ok = ok || d[i].second >= 0;      }    return ok;  }  void forward() {    typename container::size_type i, j = d.size();    for(i = 0; i < j; ++i) {      // substitution & deletion      int dist = d[i].second;      for(RandomAccessIterator first = d[i].first; 	  first != last && dist >= 0; 	  ++first, dist -= DELETION) {	int delta = DistanceFunction()(*first, x.letter());	if (dist >= delta)	  d.push_back(make_pair(first + 1, dist - delta));      }      // insertion:      d[i].second -= INSERTION;    }    x.forward();  }  bool src_final() const {    if (x.src_final())      for(typename container::const_iterator i = d.begin(); i != d.end(); ++i)	if (i->second >= (last - i->first) * DELETION) return true;    return false;  }      tag_type src_tag() const {    return x.src_tag();  }  bool exists(const char_type &a) const {    if (x.exists(a))       for(typename container::const_iterator i = d.begin(); i != d.end(); ++i)	if (i->second >= INSERTION || 	    (i->first != last && i->second >= DistanceFunction()(*i->first, a))) 	  return true;    return false;  }  bool operator==(const self &s) const {    return x == s.x;  }  char_type letter() const {    return x.letter();  }  bool first_transition() {    if (x.first_transition())      do	for(typename container::const_iterator i = d.begin(); i != d.end(); ++i)	  if (i->first != last) {	    if (i->second >= DistanceFunction()(x.letter(), *i->first)) return true;	  }	  else if (i->second >= INSERTION) return true;      while (x.next_transition());    return false;  }    bool next_transition() {    while (x.next_transition())      for(typename container::const_iterator i = d.begin(); i != d.end(); ++i)	if (i->first != last) {	  if (i->second >= DistanceFunction()(x.letter(), *i->first)) return true;	}	else if (i->second >= INSERTION) return true;    return false;  }  bool find(const char_type &a) {    // TODO    return false;  }  state_type aim() const {    return x.aim();  }  bool aim_final() const {    self tmp = *this;    tmp.forward();    return tmp.src_final();  }  tag_type aim_tag() const {    return x.aim_tag();  }};template <typename ForwardCursor, typename ForwardIterator>inlineneighbor_cursor<ForwardCursor, ForwardIterator>neighborc(const ForwardCursor& x, ForwardIterator i, ForwardIterator j, int d) {  return neighbor_cursor<ForwardCursor, ForwardIterator>(x, i, j, d);}template <typename ForwardCursor>inlineneighbor_cursor<ForwardCursor, const char*>neighborc(const ForwardCursor& x, const char *word, int d) {  return neighbor_cursor<ForwardCursor, const char*>(x, word, word + strlen(word), d);}ASTL_END_NAMESPACE#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -