1 /* This file is part of libDAI - http://www.libdai.org/
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.
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
14 /// \brief Defines class Var
17 #ifndef __defined_libdai_var_h
18 #define __defined_libdai_var_h
28 /// Represents a discrete random variable.
29 /** A Var stores the \a label of the variable (an integer-valued unique ID)
30 * and the number of possible values (\a states) of that variable. Two
31 * Var objects with the same label are assumed to be identical (i.e., it
32 * is assumed that their states are also the same).
34 * In this manual, 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$.
42 /// Label of the variable (its unique ID)
45 /// Number of possible values
49 /// Default constructor
50 Var() : _label(-1), _states(0) {}
52 Var( long label
, size_t states
) : _label(label
), _states(states
) {}
55 long label() const { return _label
; }
56 /// Returns reference to label
57 long & label() { return _label
; }
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
; }
64 /// Smaller-than operator (compares only labels)
65 bool operator < ( const Var
& n
) const { return( _label
< n
._label
); }
66 /// Larger-than operator (compares only 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 {
71 if( _label
== n
._label
)
72 assert( _states
== n
._states
);
74 return( _label
<= n
._label
);
76 /// Larger-than-or-equal-to operator (only compares labels)
77 bool operator >= ( const Var
& n
) const {
79 if( _label
== n
._label
)
80 assert( _states
== n
._states
);
82 return( _label
>= n
._label
);
84 /// Not-equal-to operator (only compares labels)
85 bool operator != ( const Var
& n
) const {
87 if( _label
== n
._label
)
88 assert( _states
== n
._states
);
90 return( _label
!= n
._label
);
92 /// Equal-to operator (only compares labels)
93 bool operator == ( const Var
& n
) const {
95 if( _label
== n
._label
)
96 assert( _states
== n
._states
);
98 return( _label
== n
._label
);
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() );
108 } // end of namespace dai