libDAI
var.h
Go to the documentation of this file.
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 
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 
26 
37 class Var {
38  private:
40  size_t _label;
41 
43  size_t _states;
44 
45  public:
47  Var() : _label(0), _states(0) {}
49  Var( size_t label, size_t states ) : _label(label), _states(states) {}
50 
52  size_t label() const { return _label; }
54  size_t& label() { return _label; }
55 
57  size_t states() const { return _states; }
59  size_t& states() { return _states; }
60 
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 
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 
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 
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 
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 
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 
116  friend std::ostream& operator << ( std::ostream& os, const Var& n ) {
117  return( os << "x" << n.label() );
118  }
119 
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
size_t & states()
Returns reference to number of states.
Definition: var.h:59
size_t _states
Number of possible values.
Definition: var.h:43
bool operator==(const Var &n) const
Equal-to operator (only compares labels)
Definition: var.h:107
bool operator>(const Var &n) const
Larger-than operator (only compares labels)
Definition: var.h:71
bool operator<(const Var &n) const
Smaller-than operator (only compares labels)
Definition: var.h:62
friend std::ostream & operator<<(std::ostream &os, const Var &n)
Writes a Var to an output stream.
Definition: var.h:116
Defines the Exception class and macros for throwing exceptions and doing assertions.
size_t & label()
Returns reference to label.
Definition: var.h:54
size_t states() const
Returns the number of states.
Definition: var.h:57
Var(size_t label, size_t states)
Constructs a variable with a given label and number of states.
Definition: var.h:49
Represents a discrete random variable.
Definition: var.h:37
Namespace for libDAI.
Definition: alldai.cpp:16
std::string toString() const
Formats a Var as a string.
Definition: var.h:121
#define DAI_ASSERT(condition)
Assertion mechanism, similar to the standard assert() macro. It is always active, even if NDEBUG is d...
Definition: exceptions.h:60
bool operator<=(const Var &n) const
Smaller-than-or-equal-to operator (only compares labels)
Definition: var.h:80
bool operator>=(const Var &n) const
Larger-than-or-equal-to operator (only compares labels)
Definition: var.h:89
size_t label() const
Returns the label.
Definition: var.h:52
Var()
Default constructor (creates a variable with label 0 and 0 states)
Definition: var.h:47
bool operator!=(const Var &n) const
Not-equal-to operator (only compares labels)
Definition: var.h:98
size_t _label
Label of the variable (its unique ID)
Definition: var.h:40