Below is the file 'markov.hh' from this revision. You can also download the file.

#ifndef __MARKOV_HH
#define __MARKOV_HH

#include "tokens.hh"
#include <iostream>
#include <vector>
#include <deque>
#include <map>

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

class MarkovState {
    friend class boost::serialization::access;

public:
    std::deque<token_t> state;
    bool valid;

    MarkovState ();
    MarkovState (const std::deque<token_t> &);
    ~MarkovState ();
    void vdump () const;
    bool operator==(const MarkovState &);
    bool operator!=(const MarkovState &);
    bool operator<(const MarkovState &);
    template <class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & state;
        ar & valid;
    }
};

typedef std::pair< MarkovState, MarkovState > markov_pair;

namespace __EXT__ {
    template <>
    struct hash<MarkovState>
    {
      size_t
      operator()(MarkovState __s) const
      {
          size_t rv=0;
          __EXT__::hash<int> h;

          for (std::deque<token_t>::const_iterator i=__s.state.begin(); i!=__s.state.end(); i++) {
              rv ^= h(*i);
          }
          return rv;
      }
    };

    template <>
    struct hash<markov_pair>
    {
      size_t
      operator()(markov_pair __s) const
      {
          __EXT__::hash<MarkovState> h;
          size_t h1, h2;

          h1 = h(__s.first);
          h2 = h(__s.second);
          return h1 ^ (h2*h2);
      }
    };
}

namespace std {
    template <>
    struct equal_to<markov_pair>
    {
        size_t
        operator()(markov_pair p1, markov_pair p2)
        {
            return p1.first == p2.first && p1.second == p2.second;
        }
    };

    template <>
    struct less<markov_pair>
    {
        bool
        operator()(markov_pair p1, markov_pair p2)
        {
            if (p1.first < p2.first) {
                return true;
            } else if (p1.first != p2.first) {
                return false;
            } else {
                return p1.second < p2.second;
            }
        }
    };
}

class MarkovModel {
private:
    std::map<markov_pair, int > edges;
    friend class boost::serialization::access;

public:
    MarkovModel (const Stash&, int);
    ~MarkovModel ();
    void Dump();
    template <class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & edges;
    }
};

#endif