pipian

markov_map.h

Dec 11th, 2011
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. /* -*- mode: c++ -*- */
  2. #ifndef __MARKOV_MAP_H
  3. #define __MARKOV_MAP_H 1
  4.  
  5. #include <cstring>
  6. #include <string>
  7. #include <ext/hash_map>
  8.  
  9. namespace stdext = __gnu_cxx;
  10.  
  11. /** A value and the number of observations of the value. */
  12. class markov_count {
  13. public:
  14.     /** The value. */
  15.     virtual const std::string value() const = 0;
  16.     /** The number of observations of the value. */
  17.     virtual unsigned long count() const = 0;
  18. };
  19.  
  20. struct eqstr {
  21.     bool operator()(const char* s1, const char* s2) const {
  22.     return strcmp(s1, s2) == 0;
  23.     }
  24. };
  25.  
  26. /** A tree for storing Markov counts for a single key. */
  27. class markov_tree {
  28. public:
  29.     /** A node in a markov_tree. */
  30.     class node : public markov_count {
  31.     private:
  32.     const char* node_value;
  33.     unsigned long left_count, node_count, right_count;
  34.     node *left, *node_parent, *right;
  35.     int balance;
  36.    
  37.     /** Rebalance this node in an AVL tree. */
  38.     void rebalance(int balance_increment);
  39.     /** Fix the count of the given node. */
  40.     void fix_count(const node* child);
  41.    
  42.     /** Get the left_count scaled to a probability. */
  43.     double scaled_left_count() const;
  44.     /** Get the right_count scaled to a probability. */
  45.     double scaled_right_count() const;
  46.     public:
  47.     /** Create a new node with a node_count of 0 and no children. */
  48.     node(node* parent, const std::string& value);
  49.     /** The copy constructor. */
  50.     node(const node&);
  51.     /** The destructor. */
  52.     ~node();
  53.     /** The assignment operator. */
  54.     node& operator=(const node&);
  55.     /** Add an instance of x into the subtree having this node as root. */
  56.     const markov_count& add(const std::string& x);
  57.     /** Select a "random" value in this subtree deterministically. */
  58.     const char* random(int seed) const;
  59.  
  60.     /** The value. */
  61.     const std::string value() const;
  62.     /** The number of observations of the value. */
  63.     unsigned long count() const;
  64.     };
  65. private:
  66.     node* root;
  67. public:
  68.     /** Create an empty markov_tree. */
  69.     markov_tree();
  70.     /** The copy constructor. */
  71.     markov_tree(const markov_tree&);
  72.     /** The destructor. */
  73.     ~markov_tree();
  74.     /** The assignment operator. */
  75.     markov_tree& operator=(const markov_tree&);
  76.     /** Add an instance of x into the markov_tree. */
  77.     const markov_count& add(const std::string& x);
  78.     /** Select a value at random. */
  79.     const char* random() const;
  80. };
  81.  
  82. /** A map for storing Markov counts for a key-pair. */
  83. class markov_map {
  84. private:
  85.     stdext::hash_map<const char*, markov_tree, stdext::hash<const char*>, eqstr> trees;
  86. public:
  87.     /** Create an empty markov_map. */
  88.     markov_map();
  89.     /** The copy constructor. */
  90.     markov_map(const markov_map&);
  91.     /** The assignment operator. */
  92.     markov_map& operator=(const markov_map&);
  93.     /** Add an instance of y given x into the markov_map. */
  94.     const markov_count& add(const std::string& x, const std::string& y);
  95.     /** Select a value at random given x as a prior. */
  96.     const std::string random(const std::string& x) const;
  97. };
  98.  
  99. #endif  /* __MARKOV_MAP_H */
  100.  
Advertisement
Add Comment
Please, Sign In to add comment