/*
 * ASTL - the Automaton Standard Template Library.
 * C++ generic components for Finite State Machine handling.
 * Copyright (C) 2000 Vincent Le Maout (vlemaout@lexiquest.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_CLOSURE_CURSOR
#define ASTL_CLOSURE_CURSOR

// A closure cursor is a forward cursor adapter
// hiding all outgoing transitions not labelled with the user-specified
// letter

#include <cursor.h>

template <class ForwardCursor>
class closure_cursor : public ForwardCursor
{
protected:
  Alphabet x;

public:
  typedef ForwardCursor  super;
  typedef closure_cursor self;

  closure_cursor(const ForwardCursor &c, const Alphabet &a)
    : super(c), x(a)
  { }

  self& operator=(const super &c) {
    super::operator=(c);
    return *this;
  }

  self& operator=(State q) {
    super::operator=(q);
    return *this;
  }

  bool first_transition() {
    if (super::first_transition()) {
      if (letter() != x)
	return next_transition();
      return true;
    }
    return false;
  }
    
  bool next_transition() {
    while (super::next_transition())
      if (letter() == x) return true;
    return false;
  }

  bool forward(const Alphabet &a) {
    if (a == x) return super::forward(a);
    to_sink();
    return false;
  }
};

// Helper function:
template <class ForwardCursor>
closure_cursor<ForwardCursor> closurec(const ForwardCursor &c, 
				       const typename ForwardCursor::Alphabet &a)
{
  return closure_cursor<ForwardCursor>(c, a);
}
  
    
#endif // ASTL_CLOSURE_CURSOR

