1 /* This file is part of libDAI - http://www.libdai.org/
2 *
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
9 /// \file
10 /// \brief Defines class Var, which represents a discrete random variable.
13 #ifndef __defined_libdai_var_h
14 #define __defined_libdai_var_h
17 #include <iostream>
18 #include <dai/exceptions.h>
21 namespace dai {
24 /// Represents a discrete random variable.
25 /** A Var stores the \a label of the variable (an unsigned integer-valued
26 * unique ID) and the number of possible values (\a states) of that variable.
27 * Two Var objects with the same label are assumed to be identical (i.e., it
28 * is assumed that they have the same number of possible states).
29 *
30 * In the documentation, we use the following notational conventions. The discrete
31 * random variable with label \f\$l\f\$ is denoted as \f\$x_l\f\$, and the number
32 * of possible values of this variable as \f\$S_l\f\$; this is represented in
33 * code by the object Var(\f\$l\f\$,\f\$S_l\f\$). The set of possible values of
34 * variable \f\$x_l\f\$ is denoted \f\$X_l := \{0,1,\dots,S_l-1\}\f\$.
35 */
36 class Var {
37 private:
38 /// Label of the variable (its unique ID)
39 size_t _label;
41 /// Number of possible values
42 size_t _states;
44 public:
45 /// Default constructor (creates a variable with label 0 and 0 states)
46 Var() : _label(0), _states(0) {}
47 /// Constructs a variable with a given label and number of states
48 Var( size_t label, size_t states ) : _label(label), _states(states) {}
50 /// Returns the label
51 size_t label() const { return _label; }
52 /// Returns reference to label
53 size_t& label() { return _label; }
55 /// Returns the number of states
56 size_t states() const { return _states; }
57 /// Returns reference to number of states
58 size_t& states() { return _states; }
60 /// Smaller-than operator (only compares labels)
61 bool operator< ( const Var& n ) const {
62 #ifdef DAI_DEBUG
63 if( _label == n._label )
64 DAI_ASSERT( _states == n._states );
65 #endif
66 return( _label < n._label );
67 }
69 /// Larger-than operator (only compares labels)
70 bool operator> ( const Var& n ) const {
71 #ifdef DAI_DEBUG
72 if( _label == n._label )
73 DAI_ASSERT( _states == n._states );
74 #endif
75 return( _label > n._label );
76 }
78 /// Smaller-than-or-equal-to operator (only compares labels)
79 bool operator<= ( const Var& n ) const {
80 #ifdef DAI_DEBUG
81 if( _label == n._label )
82 DAI_ASSERT( _states == n._states );
83 #endif
84 return( _label <= n._label );
85 }
87 /// Larger-than-or-equal-to operator (only compares labels)
88 bool operator>= ( const Var& n ) const {
89 #ifdef DAI_DEBUG
90 if( _label == n._label )
91 DAI_ASSERT( _states == n._states );
92 #endif
93 return( _label >= n._label );
94 }
96 /// Not-equal-to operator (only compares labels)
97 bool operator!= ( const Var& n ) const {
98 #ifdef DAI_DEBUG
99 if( _label == n._label )
100 DAI_ASSERT( _states == n._states );
101 #endif
102 return( _label != n._label );
103 }
105 /// Equal-to operator (only compares labels)
106 bool operator== ( const Var& n ) const {
107 #ifdef DAI_DEBUG
108 if( _label == n._label )
109 DAI_ASSERT( _states == n._states );
110 #endif
111 return( _label == n._label );
112 }
114 /// Writes a Var to an output stream
115 friend std::ostream& operator << ( std::ostream& os, const Var& n ) {
116 return( os << "x" << n.label() );
117 }
118 };
121 } // end of namespace dai
124 #endif