//
//	File:		dfa_bin.hh
//      Version:        ASTL 1.1
//	Copyright:	Vincent LE MAOUT
//	Date:		Wed Jan 14 10:35:15 MET 1998
//	Descrition:	Determinisic Finite Automaton Class Template ASTL1.1
//                      Representation by arrays with binary search
//            

#ifndef ASTL_CLASS_DFA_BIN
#define ASTL_CLASS_DFA_BIN

#include <vector>
#include <algorithm>         // lower_bound()
#include <astl.h>
#include <functional>

template <class _Sigma    = Type_alphabet<char>, 
          class _Tag      = empty_tag>
class DFA_bin : public DFA_base
{
public:
  typedef DFA_bin                   self;
  typedef _Sigma                    Sigma;
  typedef typename _Sigma::Alphabet Alphabet;
  typedef _Tag                      Tag;
  
  struct StateData;  
  typedef vector<StateData*>::size_type State;
  
private:
  typedef vector<char> set_F;
  typedef vector<pair<Alphabet, State> > _Edges;  // internal edges structure

  struct cmp_pair 
    : public binary_function<pair<const Alphabet, State>, pair<const Alphabet, State>, bool>
  {
    bool operator () (const pair<const Alphabet, State> &x, const pair<const Alphabet, State> &y) {
      return (x.first < y.first);
    }
  } pair_compare;
  
public:  
  class Edges
  {
    friend class DFA_bin;
    friend struct StateData;
  private:
    typedef _Edges Container;
    const Container *container;

    struct cmp_pair 
      : public binary_function<pair<const Alphabet, State>, pair<const Alphabet, State>, bool>
    {
      bool operator () (const pair<const Alphabet, State> &x, const pair<const Alphabet, State> &y) {
	return (x.first < y.first);
      }
    } pair_compare;

  public:
    typedef Alphabet                    key_type;
    typedef pair<const key_type, State> value_type;
    typedef less<key_type>              key_compare;
    typedef value_type*                 pointer;
    typedef const value_type*           const_pointer;
    typedef value_type&                 reference;
    typedef const value_type&           const_reference;
    typedef unsigned long               size_type;
    typedef long                        difference_type;
     
    typedef typename _Edges::const_iterator const_iterator ;
    typedef typename _Edges::const_reverse_iterator const_reverse_iterator ;

    class value_compare : public binary_function<value_type, value_type, bool>
    {
      friend class Edges;
      
    protected:
      key_compare comp;
      value_compare(key_compare c) : comp(c) { }
      
    public:
      bool operator () (const value_type& x, const value_type& y)
      {
	return (comp(x.first, y.first));
      }
    };
   
    // allocation/deallocation:
    
    Edges() : container(NULL) { }
    
  private:
    Edges(const Container *c)
      : container(c) { }
    
  public:
    Edges(const Edges& x) : container(x.container) { }
    ~Edges() { }
    
    key_compare key_comp() const { 
      key_compare bogus;
      return (bogus); 
    }
    value_compare value_comp() const { 
      key_compare bogus;
      return (value_compare(bogus)); 
    }
    const_iterator begin() const { 
      return (container->begin()); 
    }
    const_iterator end() const { 
      return (container->end()); 
    }

    const_reverse_iterator rbegin() const { 
      return (container->rbegin()); 
    }
    const_reverse_iterator rend() const { 
      return (container->rend()); 
    }

    bool empty() const { 
      return (container->empty()); 
    }
    size_type size() const { 
      return (container->size()); 
    }
    size_type max_size() const { 
      return (container->max_size()); 
    }
    
    // map operations:
    
    const_iterator find(const key_type& x) const { 
      typename _Edges::const_iterator i = 
	::lower_bound(container->begin(), container->end(), make_pair(x, 0), pair_compare);
      if (i == container->end() || (*i).first != x)
	return (container->end());
      else
	return (i);
    }
    size_type count(const key_type& x) const { 
      return ((find(x) == end()) ? 0 : 1); 
    }
    const_iterator lower_bound(const key_type& x) const { 
      return (::lower_bound(container->begin(), container->end(), 
				       make_pair(x, 0), pair_compare)); 
    }
    const_iterator upper_bound(const key_type& x) const { 
      return (::lower_bound(container->begin(), container->end(), 
			    make_pair(x, 0), pair_compare));
    }
    pair<const_iterator, const_iterator> equal_range(const key_type& x) const {
      return (::equal_range(container->begin(), container->end(), make_pair(x, 0), pair_compare));
    }
    
    // comparison:
    
    friend bool operator == (const Edges& x, const Edges& y) {
      return (x.container == y.container || *x.container == *y.container);
    }
  };

private:
  struct StateData
  {
    Tag    tag;
    _Edges edges;

    StateData() : tag(Tag()), edges()
    { }
  };

  vector<StateData*> Q;
  State I;     // Initial state
  set_F F;     // Final states
  unsigned long _trans_count;
  unsigned long _state_count;

public:
  typedef skip_blanks_iterator<StateData> const_iterator;
  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())); 
  }

  State initial() const { 
    return (I); 
  }

  void initial(State s) { 
    I = s; 
  }

  char& final(State s) { 
    return (F[s]); 
  }

  bool final(State s) const { 
    return ((bool) 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) 
  { 
    _trans_count -= Q[s]->edges.size();
    delete Q[s];
    Q[s] = NULL;
    --_state_count;
    F[s] = '\0';
  }

  void set_trans(State s, const Alphabet &a, State aim)
  {
    pair<const Alphabet, State> val(a, aim);
    _Edges &edges = Q[s]->edges;
    edges.insert(lower_bound(edges.begin(), edges.end(), val, pair_compare), val);
    ++_trans_count;
  }

  void del_trans(State s, const Alphabet &a)
  {
    _Edges &edges = Q[s]->edges;
    edges.erase(lower_bound(edges.begin(), edges.end(), make_pair(a, null_state), pair_compare));
    --_trans_count;
  }

  void change_trans(State s, const Alphabet &a, State new_aim) {
    _Edges &edges = Q[s]->edges;
    (*lower_bound(edges.begin(), edges.end(), 
		  make_pair(a, null_state), pair_compare)).second = new_aim;
  }

  void copy_state(State from, State to)
  {
    _trans_count += Q[from]->edges.size() - Q[to]->edges.size();
    *Q[to] = *Q[from];
  }

  State duplicate_state(State q)
  {
    State s = new_state();
    *Q[s] = *Q[q];
    _trans_count += Q[q]->edges.size();
    final(s) = final(q);
    return (s);
  }

  Tag& tag(State s) {
    return (Q[s]->tag);
  }

  const Tag& tag(State s) const {
    return (Q[s]->tag);
  }

  State delta1(State s, const Alphabet &a) const
  {
    pair<const Alphabet, State> val(a, null_state);
    _Edges &edges = Q[s]->edges;
    typename _Edges::iterator i = lower_bound(edges.begin(), edges.end(), val, pair_compare);
    if (i == edges.end() || (*i).first != a)
      return (null_state);
    else
      return ((*i).second);
  }

  Edges delta2(State s) const {
    return (Edges(&(Q[s]->edges))); 
  }

  unsigned long state_count() const { 
    return (_state_count); 
  }
  
  unsigned long trans_count() const { 
    return (_trans_count); 
  }

  DFA_bin(unsigned long n = 0)
    : Q(1, (StateData*) 0), I(0), F(1, '\0'), _trans_count(0UL), 
      _state_count(0UL), null_state(0) { 
	Q.reserve(n + 1);
      }

  ~DFA_bin()
  {
    const_iterator start, finish = end();
    for(start = begin(); start != finish; ++start)
      del_state(*start);
  }
  
};

#endif





