Extended SWIG python interface (inspired by Kyle Ellrott): inference is possible...
[libdai.git] / include / dai / var.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 /// \file
10 /// \brief Defines class Var, which represents a discrete random variable.
11
12
13 #ifndef __defined_libdai_var_h
14 #define __defined_libdai_var_h
15
16
17 #include <iostream>
18 #include <sstream>
19 #include <dai/exceptions.h>
20
21
22 namespace dai {
23
24
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).
30 *
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$.
36 */
37 class Var {
38 private:
39 /// Label of the variable (its unique ID)
40 size_t _label;
41
42 /// Number of possible values
43 size_t _states;
44
45 public:
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) {}
50
51 /// Returns the label
52 size_t label() const { return _label; }
53 /// Returns reference to label
54 size_t& label() { return _label; }
55
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; }
60
61 /// Smaller-than operator (only compares labels)
62 bool operator< ( const Var& n ) const {
63 #ifdef DAI_DEBUG
64 if( _label == n._label )
65 DAI_ASSERT( _states == n._states );
66 #endif
67 return( _label < n._label );
68 }
69
70 /// Larger-than operator (only compares labels)
71 bool operator> ( const Var& n ) const {
72 #ifdef DAI_DEBUG
73 if( _label == n._label )
74 DAI_ASSERT( _states == n._states );
75 #endif
76 return( _label > n._label );
77 }
78
79 /// Smaller-than-or-equal-to operator (only compares labels)
80 bool operator<= ( const Var& n ) const {
81 #ifdef DAI_DEBUG
82 if( _label == n._label )
83 DAI_ASSERT( _states == n._states );
84 #endif
85 return( _label <= n._label );
86 }
87
88 /// Larger-than-or-equal-to operator (only compares labels)
89 bool operator>= ( const Var& n ) const {
90 #ifdef DAI_DEBUG
91 if( _label == n._label )
92 DAI_ASSERT( _states == n._states );
93 #endif
94 return( _label >= n._label );
95 }
96
97 /// Not-equal-to operator (only compares labels)
98 bool operator!= ( const Var& n ) const {
99 #ifdef DAI_DEBUG
100 if( _label == n._label )
101 DAI_ASSERT( _states == n._states );
102 #endif
103 return( _label != n._label );
104 }
105
106 /// Equal-to operator (only compares labels)
107 bool operator== ( const Var& n ) const {
108 #ifdef DAI_DEBUG
109 if( _label == n._label )
110 DAI_ASSERT( _states == n._states );
111 #endif
112 return( _label == n._label );
113 }
114
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() );
118 }
119
120 /// Formats a Var as a string
121 std::string toString() const {
122 std::stringstream ss;
123 ss << *this;
124 return ss.str();
125 }
126 };
127
128
129 } // end of namespace dai
130
131
132 #endif