74dea69694f9159f8d043b1f7889d153f170003a
[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 (an unsigned 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 0 and 0 states)
50 Var() : _label(0), _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 {
66 #ifdef DAI_DEBUG
67 if( _label == n._label )
68 DAI_ASSERT( _states == n._states );
69 #endif
70 return( _label < n._label );
71 }
72
73 /// Larger-than operator (only compares labels)
74 bool operator > ( const Var& n ) const {
75 #ifdef DAI_DEBUG
76 if( _label == n._label )
77 DAI_ASSERT( _states == n._states );
78 #endif
79 return( _label > n._label );
80 }
81
82 /// Smaller-than-or-equal-to operator (only compares labels)
83 bool operator <= ( const Var& n ) const {
84 #ifdef DAI_DEBUG
85 if( _label == n._label )
86 DAI_ASSERT( _states == n._states );
87 #endif
88 return( _label <= n._label );
89 }
90
91 /// Larger-than-or-equal-to operator (only compares labels)
92 bool operator >= ( const Var& n ) const {
93 #ifdef DAI_DEBUG
94 if( _label == n._label )
95 DAI_ASSERT( _states == n._states );
96 #endif
97 return( _label >= n._label );
98 }
99
100 /// Not-equal-to operator (only compares labels)
101 bool operator != ( const Var& n ) const {
102 #ifdef DAI_DEBUG
103 if( _label == n._label )
104 DAI_ASSERT( _states == n._states );
105 #endif
106 return( _label != n._label );
107 }
108
109 /// Equal-to operator (only compares labels)
110 bool operator == ( const Var& n ) const {
111 #ifdef DAI_DEBUG
112 if( _label == n._label )
113 DAI_ASSERT( _states == n._states );
114 #endif
115 return( _label == n._label );
116 }
117
118 /// Writes a Var to an output stream
119 friend std::ostream& operator << ( std::ostream& os, const Var& n ) {
120 return( os << "x" << n.label() );
121 }
122 };
123
124
125 } // end of namespace dai
126
127
128 #endif