/*
 * 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
 *
 */


//
//	File:	      dfa_matrix.h
//  Version:    ASTL 1.2
//	Copyright:  Vincent LE MAOUT
//	Date:	      Wed Feb 14 12:37:51  2001
//	Descrition: Determinisic Finite Automaton Class Template ASTL1.2
//              Representation by matrix Q x Sigma
// 

#ifndef ASTL_CLASS_DFA_MATRIX
#define ASTL_CLASS_DFA_MATRIX

#include <iterator>   // iterator_tag
#include <functional> // less
#include <utility>    // pair
#include <vector>

#include <astl.h>     // alphabets
#include <tag.h>      // empty_tag

template <class Tag, class Sigma>
struct MatrixStateData_
{
	Tag          _tag;
	unsigned int size;//    a count of the outgoing transitions
	unsigned int v[Sigma::size];

	typedef MatrixStateData_ self;

	MatrixStateData_() : size(0) {
		memset(v, 0, Sigma::size * sizeof(unsigned int));
	}
	MatrixStateData_(const self &x)
		: _tag(x._tag), size(x.size) {
			memcpy(v, x.v, Sigma::size * sizeof(unsigned int));
	}
	self& operator= (const self &x) {
		_tag = x._tag;
		size = x.size;
		memcpy(v, x.v, Sigma::size * sizeof(unsigned int));
		return (*this);
	}
	bool operator== (const self &x) const {
		return size == x.size && equal(v, v + Sigma::size, x.v);
	}
	const Tag& tag() const {
		return _tag;
	}
	Tag& tag() {
		return _tag;
	}
};

// Specialization of the state structure for empty tags

#ifndef WIN32
// VC++6.0 does not support partial template specialization
template <class Sigma>
struct MatrixStateData_<empty_tag, Sigma>
{
	unsigned int     size;
	unsigned int     v[Sigma::size];
	static empty_tag _tag;      // remains consistent with generic version

	typedef MatrixStateData_ self;

	MatrixStateData_() : size(0) {
		memset(v, 0, Sigma::size * sizeof(unsigned int));
	}

	MatrixStateData_(const self &x)
		: size(x.size) {
			memcpy(v, x.v, Sigma::size * sizeof(unsigned int));
	}

	self& operator = (const self &x) {
		size = x.size;
		memcpy(v, x.v, Sigma::size * sizeof(unsigned int));
		return (*this);
	}

	bool operator== (const self &x) const {
		return size == x.size && equal(v, v + Sigma::size, x.v);
	}

	const empty_tag& tag() const {
		return self::_tag;
	}

	empty_tag& tag() {
		return self::_tag;
	}
};

template <class Sigma> empty_tag MatrixStateData_<empty_tag, Sigma>::_tag;
#endif  // WIN32

template <class _Sigma    = plain,
          class _Tag      = empty_tag>
class DFA_matrix : public DFA_concept
{
public:
  typedef _Sigma                    Sigma;
  typedef typename _Sigma::Alphabet Alphabet;
  typedef _Tag                      Tag;
  typedef unsigned int              State;

private:
	typedef MatrixStateData_<Tag, Sigma> StateData;
  typedef DFA_matrix   self;
  typedef vector<char> set_F;

public:  
  typedef skip_blanks_iterator<StateData> const_iterator;

  class Edges
  {
	protected:
    typedef StateData Container;
    const Container *container;

  public:
    typedef Alphabet                    key_type;
		typedef State                       data_type;
    typedef pair<const Alphabet, State> value_type;
    typedef typename Sigma::int_type     size_type;

#ifdef WIN32
    class const_iterator : public iterator<bidirectional_iterator_tag, value_type>
#else
    class const_iterator : public bidirectional_iterator<value_type, ptrdiff_t>
#endif
    {
      friend class Edges;
      typedef const_iterator self;

    protected:
      const State *i;
      const Container *c;

      const_iterator(const State *_i, const Container *_c)
				: i(_i), c(_c) 
			{ }

    public:
      const_iterator()
      { }

      bool operator== (const self& x) const {
				return (x.i == i); 
      }

#ifdef WIN32
      bool operator!= (const self& x) const {
				return !(*this == x);
      }
#endif

      typename const_iterator::value_type operator* () const { 
				return (make_pair(Sigma::unmap(i - c->v), *i)); 
      }

      self& operator ++ ()       
      { 
				for(++i; i != (c->v + Sigma::size) && *i == 0; ++i);
				return (*this);
      }

      self operator ++ (int) 
      { 
				const_iterator tmp = *this;
				++(*this);
				return (tmp);
      }

      self& operator -- () 
      { 
				for(--i; *i == 0; --i);
				return (*this);
      }

      self operator -- (int) 
      { 
				const_iterator tmp = *this;
				--(*this);
				return (tmp);
      }
    };

#ifdef WIN32
    typedef reverse_iterator<const_iterator, const_iterator::value_type> const_reverse_iterator;
#else
    typedef reverse_iterator<const_iterator> const_reverse_iterator;
#endif

    // allocation/deallocation:
    Edges(const Container *c = NULL)
      : container(c) { }
    
    const_iterator begin() const { 
      const_iterator result(container->v, container);
      if (*(result.i) == 0)
				++result;
      return (result); 
    }
    
    const_iterator end() const {
      const_iterator result(container->v + Sigma::size, container);
      return (result);
    }

    const_reverse_iterator rbegin() const { 
      return (const_reverse_iterator(end())); 
    }
    
    const_reverse_iterator rend() const {
      return (const_reverse_iterator(begin())); 
    }
    
    bool empty() const { 
      return (container->size == 0); 
    }
    
    size_type size() const { 
      return (container->size); 
    }

    size_type max_size() const { 
      return (Sigma::size); 
    }
    
    // map operations:
    const_iterator find(const key_type& x) const { 
      return (container->v[Sigma::map(x)] == 0) ? 
				end() : const_iterator(container->v + Sigma::map(x), container);
    }
    
    size_type count(const key_type& x) const { 
      return (container->v[Sigma::map(x)] == 0) ? 0 : 1;
    }
    
    // comparison:
    friend bool operator == (const Edges& x, const Edges& y) {
      return (x.container == y.container || *x.container == *y.container);
    }
  };

  // Back to DFA_matrix class

private:
  vector<StateData*> Q;
  State              I;     // Initial state
  set_F              F;     // Final states
  unsigned long      _state_count;
  unsigned long      _trans_count;

public:
  State null_state;

  const_iterator begin() const { 
    const_iterator result(&Q, 0);
    ++result;
    return (result);
  }

  const_iterator  end() const { 
    return (const_iterator(&Q, Q.size())); 
  }

  void initial(State s) { 
    I = s; 
  }

  State initial() const { 
    return (I); 
  }

  bool final(State s) const { 
    return (F[s] != '\0'); 
  }
  
  char& final(State s) { 
    return (F[s]); 
  }

  State new_state() {
    Q.push_back(new StateData);
    F.push_back('\0');
    ++_state_count;
    return (Q.size() - 1);
  }

  template <class OutputIterator>
  OutputIterator new_state(unsigned long how_many, OutputIterator x)
  {
    for(; how_many > 0; --how_many)
      *x++ = new_state();
    return (x);
  }
      
  void del_state(State s) 
  {
		if (s == initial()) initial(null_state);
    _trans_count -= Q[s]->size;
    delete Q[s];
    Q[s] = NULL;
    --_state_count;
    F[s] = '\0';
  }

  void del_trans(State s, const Alphabet &l)
  {
    Q[s]->v[Sigma::map(l)] = 0;
    --(Q[s]->size);
    --_trans_count;
  }

  void change_trans(State s, const Alphabet &l, State new_aim) {
    Q[s]->v[Sigma::map(l)] = new_aim;
  }

  State delta1(State s, const Alphabet &l) const {
    return (Q[s]->v[Sigma::map(l)]);
  }

  void set_trans(State s, const Alphabet &l, State aim)
  {
    Q[s]->v[Sigma::map(l)] = aim;
    ++(Q[s]->size);
    ++_trans_count;
  }

  void copy_state(State from, State to) {
    _trans_count += Q[from]->size - Q[to]->size;
    *Q[to] = *Q[from];
    final(to) = final(from);
  }

  State duplicate_state(State q)
  {
    State s = new_state();
    *Q[s] = *Q[q];
    _trans_count += Q[q]->size;
    final(s) = final(q);
    return (s);
  }

  unsigned long state_count() const { 
    return (_state_count); 
  }
  
  unsigned long trans_count() const { 
    return (_trans_count); 
  }
  
  Tag& tag(State s) { 
    return (Q[s]->tag()); 
  }
  
  const Tag& tag(State s) const { 
    return (Q[s]->tag()); 
  }

  Edges delta2(State s) const { 
    return (Edges(Q[s])); 
  }

  DFA_matrix(unsigned long n = 0) :
    Q(1, (StateData*) 0), I(0), F(1, '\0'), _state_count(0UL), 
    _trans_count(0UL), null_state(0) { 
      Q.reserve(n + 1);
  }

  ~DFA_matrix()
  {
    const_iterator start, finish = end();
    for(start = begin(); start != finish; ++start)
       del_state(*start);
  }
};

#endif  // CLASS_DFA_MATRIX






