1 /* This file is part of libDAI - http://www.libdai.org/
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
10 /// \brief Defines class Var, which represents a discrete random variable.
13 #ifndef __defined_libdai_var_h
14 #define __defined_libdai_var_h
19 #include <dai/exceptions.h>
25 /// Represents a discrete random variable.
26 /** A Var stores the \a label of the variable (an unsigned integer-valued
27 * unique ID) and the number of possible values (\a states) of that variable.
28 * Two Var objects with the same label are assumed to be identical (i.e., it
29 * is assumed that they have the same number of possible states).
31 * In the documentation, we use the following notational conventions. The discrete
32 * random variable with label \f$l\f$ is denoted as \f$x_l\f$, and the number
33 * of possible values of this variable as \f$S_l\f$; this is represented in
34 * code by the object Var(\f$l\f$,\f$S_l\f$). The set of possible values of
35 * variable \f$x_l\f$ is denoted \f$X_l := \{0,1,\dots,S_l-1\}\f$.
39 /// Label of the variable (its unique ID)
42 /// Number of possible values
46 /// Default constructor (creates a variable with label 0 and 0 states)
47 Var() : _label(0), _states(0) {}
48 /// Constructs a variable with a given label and number of states
49 Var( size_t label
, size_t states
) : _label(label
), _states(states
) {}
52 size_t label() const { return _label
; }
53 /// Returns reference to label
54 size_t& label() { return _label
; }
56 /// Returns the number of states
57 size_t states() const { return _states
; }
58 /// Returns reference to number of states
59 size_t& states() { return _states
; }
61 /// Smaller-than operator (only compares labels)
62 bool operator< ( const Var
& n
) const {
64 if( _label
== n
._label
)
65 DAI_ASSERT( _states
== n
._states
);
67 return( _label
< n
._label
);
70 /// Larger-than operator (only compares labels)
71 bool operator> ( const Var
& n
) const {
73 if( _label
== n
._label
)
74 DAI_ASSERT( _states
== n
._states
);
76 return( _label
> n
._label
);
79 /// Smaller-than-or-equal-to operator (only compares labels)
80 bool operator<= ( const Var
& n
) const {
82 if( _label
== n
._label
)
83 DAI_ASSERT( _states
== n
._states
);
85 return( _label
<= n
._label
);
88 /// Larger-than-or-equal-to operator (only compares labels)
89 bool operator>= ( const Var
& n
) const {
91 if( _label
== n
._label
)
92 DAI_ASSERT( _states
== n
._states
);
94 return( _label
>= n
._label
);
97 /// Not-equal-to operator (only compares labels)
98 bool operator!= ( const Var
& n
) const {
100 if( _label
== n
._label
)
101 DAI_ASSERT( _states
== n
._states
);
103 return( _label
!= n
._label
);
106 /// Equal-to operator (only compares labels)
107 bool operator== ( const Var
& n
) const {
109 if( _label
== n
._label
)
110 DAI_ASSERT( _states
== n
._states
);
112 return( _label
== n
._label
);
115 /// Writes a Var to an output stream
116 friend std::ostream
& operator << ( std::ostream
& os
, const Var
& n
) {
117 return( os
<< "x" << n
.label() );
120 /// Formats a Var as a string
121 std::string
toString() const {
122 std::stringstream ss
;
129 } // end of namespace dai