Several changes by Giuseppe Passino
[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 #include <cassert>
36
37
38 namespace dai {
39
40
41 /// Represents a discrete random variable.
42 /** It contains the \a label of the variable (an integer-valued unique ID)
43 * and the number of possible values (\a states) of the variable.
44 */
45 class Var {
46 private:
47 /// Label of the variable (its unique ID)
48 long _label;
49
50 /// Number of possible values
51 size_t _states;
52
53 public:
54 /// Default constructor
55 Var() : _label(-1), _states(0) {}
56 /// Constructor
57 Var( long label, size_t states ) : _label(label), _states(states) {}
58
59 /// Returns the label
60 long label() const { return _label; }
61 /// Returns reference to label
62 long & label() { return _label; }
63
64 /// Returns the number of states
65 size_t states () const { return _states; }
66 /// Returns reference to number of states
67 size_t& states () { return _states; }
68
69 /// Smaller-than operator (only compares labels)
70 bool operator < ( const Var& n ) const { return( _label < n._label ); }
71 /// Larger-than operator (only compares labels)
72 bool operator > ( const Var& n ) const { return( _label > n._label ); }
73 /// Smaller-than-or-equal-to operator (only compares labels)
74 bool operator <= ( const Var& n ) const {
75 #ifdef DAI_DEBUG
76 if( _label == n._label )
77 assert( _states == n._states );
78 #endif
79 return( _label <= n._label );
80 }
81 /// Larger-than-or-equal-to operator (only compares labels)
82 bool operator >= ( const Var& n ) const {
83 #ifdef DAI_DEBUG
84 if( _label == n._label )
85 assert( _states == n._states );
86 #endif
87 return( _label >= n._label );
88 }
89 /// Not-equal-to operator (only compares labels)
90 bool operator != ( const Var& n ) const {
91 #ifdef DAI_DEBUG
92 if( _label == n._label )
93 assert( _states == n._states );
94 #endif
95 return( _label != n._label );
96 }
97 /// Equal-to operator (only compares labels)
98 bool operator == ( const Var& n ) const {
99 #ifdef DAI_DEBUG
100 if( _label == n._label )
101 assert( _states == n._states );
102 #endif
103 return( _label == n._label );
104 }
105
106 /// Writes a Var to an output stream
107 friend std::ostream& operator << ( std::ostream& os, const Var& n ) {
108 return( os << "[" << n.label() << "]" );
109 }
110 };
111
112
113 } // end of namespace dai
114
115
116 #endif