Small changes
[libdai.git] / include / dai / var.h
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Copyright (C) 2002 Martijn Leisink [martijn@mbfys.kun.nl]
3 Radboud University Nijmegen, The Netherlands
4
5 This file is part of libDAI.
6
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #ifndef __defined_libdai_var_h
24 #define __defined_libdai_var_h
25
26
27 #include <iostream>
28
29
30 namespace dai {
31
32
33 /// Represents a discrete variable.
34 /** It contains the label of the variable, an integer-valued
35 * unique ID for that variable, and the number of possible
36 * values ("states") of the variable.
37 */
38 class Var {
39 private:
40 /// Label of the variable (its unique ID)
41 long _label;
42
43 /// Number of possible values
44 size_t _states;
45
46 public:
47 /// Default constructor
48 Var() : _label(-1), _states(0) {}
49 /// Constructor
50 Var( long label, size_t states ) : _label(label), _states(states) {}
51
52 /// Returns the label
53 long label() const { return _label; }
54 /// Returns reference to label
55 long & label() { return _label; }
56
57 /// Returns the number of states
58 size_t states () const { return _states; }
59 /// Returns reference to number of states
60 size_t& states () { return _states; }
61
62 /// Smaller-than operator (only compares labels)
63 bool operator < ( const Var& n ) const { return( _label < n._label ); }
64 /// Larger-than operator (only compares labels)
65 bool operator > ( const Var& n ) const { return( _label > n._label ); }
66 /// Smaller-than-or-equal-to operator (only compares labels)
67 bool operator <= ( const Var& n ) const { return( _label <= n._label ); }
68 /// Larger-than-or-equal-to operator (only compares labels)
69 bool operator >= ( const Var& n ) const { return( _label >= n._label ); }
70 /// Not-equal-to operator (only compares labels)
71 bool operator != ( const Var& n ) const { return( _label != n._label ); }
72 /// Equal-to operator (only compares labels)
73 bool operator == ( const Var& n ) const { return( _label == n._label ); }
74
75 /// Write a Var to output stream
76 friend std::ostream& operator << ( std::ostream& os, const Var& n ) {
77 return( os << "[" << n.label() << "]" );
78 }
79 };
80
81
82 } // end of namespace dai
83
84
85 #endif