Cleaned up variable elimination code in ClusterGraph
[libdai.git] / include / dai / var.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2002 Martijn Leisink [martijn@mbfys.kun.nl]
8 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
9 * Copyright (C) 2002-2007 Radboud University Nijmegen, The Netherlands
10 */
11
12
13 /// \file
14 /// \brief Defines class Var, which represents a discrete random variable.
15
16
17 #ifndef __defined_libdai_var_h
18 #define __defined_libdai_var_h
19
20
21 #include <iostream>
22 #include <dai/exceptions.h>
23
24
25 namespace dai {
26
27
28 /// Represents a discrete random variable.
29 /** A Var stores the \a label of the variable (a nonnegative integer-valued
30 * unique ID) and the number of possible values (\a states) of that variable.
31 * Two Var objects with the same label are assumed to be identical (i.e., it
32 * is assumed that they have the same number of possible states).
33 *
34 * In the documentation, we use the following notational conventions. The discrete
35 * random variable with label \f$l\f$ is denoted as \f$x_l\f$, and the number
36 * of possible values of this variable as \f$S_l\f$; this is represented in
37 * code by the object Var(\f$l\f$,\f$S_l\f$). The set of possible values of
38 * variable \f$x_l\f$ is denoted \f$X_l := \{0,1,\dots,S_l-1\}\f$.
39 */
40 class Var {
41 private:
42 /// Label of the variable (its unique ID)
43 size_t _label;
44
45 /// Number of possible values
46 size_t _states;
47
48 public:
49 /// Default constructor (creates a variable with label -1 and 0 states)
50 Var() : _label(-1), _states(0) {}
51 /// Constructs a variable with a given label and number of states
52 Var( size_t label, size_t states ) : _label(label), _states(states) {}
53
54 /// Returns the label
55 size_t label() const { return _label; }
56 /// Returns reference to label
57 size_t& label() { return _label; }
58
59 /// Returns the number of states
60 size_t states () const { return _states; }
61 /// Returns reference to number of states
62 size_t& states () { return _states; }
63
64 /// Smaller-than operator (only compares labels)
65 bool operator < ( const Var& n ) const { return( _label < n._label ); }
66 /// Larger-than operator (only compares labels)
67 bool operator > ( const Var& n ) const { return( _label > n._label ); }
68 /// Smaller-than-or-equal-to operator (only compares labels)
69 bool operator <= ( const Var& n ) const {
70 #ifdef DAI_DEBUG
71 if( _label == n._label )
72 DAI_ASSERT( _states == n._states );
73 #endif
74 return( _label <= n._label );
75 }
76 /// Larger-than-or-equal-to operator (only compares labels)
77 bool operator >= ( const Var& n ) const {
78 #ifdef DAI_DEBUG
79 if( _label == n._label )
80 DAI_ASSERT( _states == n._states );
81 #endif
82 return( _label >= n._label );
83 }
84 /// Not-equal-to operator (only compares labels)
85 bool operator != ( const Var& n ) const {
86 #ifdef DAI_DEBUG
87 if( _label == n._label )
88 DAI_ASSERT( _states == n._states );
89 #endif
90 return( _label != n._label );
91 }
92 /// Equal-to operator (only compares labels)
93 bool operator == ( const Var& n ) const {
94 #ifdef DAI_DEBUG
95 if( _label == n._label )
96 DAI_ASSERT( _states == n._states );
97 #endif
98 return( _label == n._label );
99 }
100
101 /// Writes a Var to an output stream
102 friend std::ostream& operator << ( std::ostream& os, const Var& n ) {
103 return( os << "x" << n.label() );
104 }
105 };
106
107
108 } // end of namespace dai
109
110
111 #endif