Improved documentation of include/dai/enum.h and include/dai/factor.h
[libdai.git] / include / dai / var.h
1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
4
5 Copyright (C) 2002 Martijn Leisink [martijn@mbfys.kun.nl]
6 Radboud University Nijmegen, The Netherlands
7
8 This file is part of libDAI.
9
10 libDAI is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 libDAI is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with libDAI; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25
26 /// \file
27 /// \brief Defines class Var
28
29
30 #ifndef __defined_libdai_var_h
31 #define __defined_libdai_var_h
32
33
34 #include <iostream>
35
36
37 namespace dai {
38
39
40 /// Represents a discrete random variable.
41 /** A Var stores the \a label of the variable (an integer-valued unique ID)
42 * and the number of possible values (\a states) of that variable. Two
43 * Var objects with the same label are assumed to be identical (i.e., it
44 * is assumed that their states are also the same).
45 *
46 * In this manual, we use the following notational conventions. The discrete
47 * random variable with label \f$l\f$ is denoted as \f$x_l\f$, and the number
48 * of possible values of this variable as \f$S_l\f$; this is represented in
49 * code by the object Var(\f$l\f$,\f$S_l\f$). The set of possible values of
50 * variable \f$x_l\f$ is denoted \f$X_l := \{0,1,\dots,S_l-1\}\f$.
51 */
52 class Var {
53 private:
54 /// Label of the variable (its unique ID)
55 long _label;
56
57 /// Number of possible values
58 size_t _states;
59
60 public:
61 /// Default constructor
62 Var() : _label(-1), _states(0) {}
63 /// Constructor
64 Var( long label, size_t states ) : _label(label), _states(states) {}
65
66 /// Returns the label
67 long label() const { return _label; }
68 /// Returns reference to label
69 long & label() { return _label; }
70
71 /// Returns the number of states
72 size_t states () const { return _states; }
73 /// Returns reference to number of states
74 size_t& states () { return _states; }
75
76 /// Smaller-than operator (compares only labels)
77 bool operator < ( const Var& n ) const { return( _label < n._label ); }
78 /// Larger-than operator (compares only labels)
79 bool operator > ( const Var& n ) const { return( _label > n._label ); }
80 /// Smaller-than-or-equal-to operator (compares only labels)
81 bool operator <= ( const Var& n ) const { return( _label <= n._label ); }
82 /// Larger-than-or-equal-to operator (compares only labels)
83 bool operator >= ( const Var& n ) const { return( _label >= n._label ); }
84 /// Not-equal-to operator (compares only labels)
85 bool operator != ( const Var& n ) const { return( _label != n._label ); }
86 /// Equal-to operator (compares only labels)
87 bool operator == ( const Var& n ) const { return( _label == n._label ); }
88
89 /// Writes a Var to an output stream
90 friend std::ostream& operator << ( std::ostream& os, const Var& n ) {
91 return( os << "x" << n.label() );
92 }
93 };
94
95
96 } // end of namespace dai
97
98
99 #endif