/* * DiscreteKey.h * @brief specialized key for discrete variables * @author Frank Dellaert * @date Feb 28, 2011 */ #pragma once #include #include #include #include #include namespace gtsam { typedef std::pair DiscreteKey; /** * Key type for discrete conditionals * Includes name and cardinality */ class OldDiscreteKey : std::pair { private: std::string name_; public: /** Default constructor */ OldDiscreteKey() : std::pair(0,0), name_("default") { } /** Constructor, defaults to binary */ OldDiscreteKey(Index j, const std::string& name, size_t cardinality = 2) : std::pair(j,cardinality), name_(name) { } virtual ~OldDiscreteKey() { } // Testable bool equals(const OldDiscreteKey& other, double tol = 1e-9) const; void print(const std::string& s = "") const; operator Index() const { return first; } const std::string& name() const { return name_; } size_t cardinality() const { return second; } /** compare 2 keys by their name */ bool operator <(const OldDiscreteKey& other) const { return name_ < other.name_; } /** equality */ bool operator==(const OldDiscreteKey& other) const { return (first == other.first) && (second == other.second) && (name_ == other.name_); } bool operator!=(const OldDiscreteKey& other) const { return !(*this == other); } /** provide streaming */ friend std::ostream& operator <<(std::ostream &os, const OldDiscreteKey &key); }; // OldDiscreteKey /// DiscreteKeys is a set of keys that can be assembled using the & operator struct DiscreteKeys: public std::vector { /// Default constructor DiscreteKeys() { } /// Construct from a key DiscreteKeys(const DiscreteKey& key) { push_back(key); } /// Construct from a vector of keys DiscreteKeys(const std::vector& keys) : std::vector(keys) { } /// Construct from cardinalities with default names DiscreteKeys(const std::vector& cs); /// Return a vector of indices std::vector indices() const; /// Return a map from index to cardinality std::map cardinalities() const; /// Add a key (non-const!) DiscreteKeys& operator&(const DiscreteKey& key) { push_back(key); return *this; } }; // DiscreteKeys /// Create a list from two keys DiscreteKeys operator&(const DiscreteKey& key1, const DiscreteKey& key2); /// traits class for DiscreteKey for use with DecisionTree/DecisionDiagram template<> struct label_traits { /** get cardinality from type */ static size_t cardinality(const OldDiscreteKey& key) { return key.cardinality(); } /** compare 2 keys by their name */ static bool higher(const OldDiscreteKey& a, const OldDiscreteKey& b) { return a.name() < b.name(); } /** hash function */ static size_t hash_value(const OldDiscreteKey& a) { boost::hash hasher; return hasher(a.name()); } }; }