- Improved constructors of TProb and TFactor to use iterators instead of pointers
- Added TProb::draw() function, which draws a random index
- Fixed whitespace in tests/testdai.cpp
/// Constructs TFactor depending on variables in ns, with all values set to p
TFactor( const VarSet& ns, Real p ) : _vs(ns), _p(_vs.nrStates(),p) {}
- /// Constructs TFactor depending on variables in ns, copying the values from the array p
- TFactor( const VarSet& ns, const Real *p ) : _vs(ns), _p(_vs.nrStates(),p) {}
+ /// Constructs TFactor depending on variables in ns, copying the values from the range starting at begin
+ /** \param ns contains the variables that the new TFactor should depend on.
+ * \tparam Iterator Iterates over instances of type T; should support addition of size_t.
+ * \param begin Points to first element to be added.
+ */
+ template<typename TIterator>
+ TFactor( const VarSet& ns, TIterator begin ) : _vs(ns), _p(begin, begin + _vs.nrStates(), _vs.nrStates()) {}
/// Constructs TFactor depending on variables in ns, with values set to the TProb p
TFactor( const VarSet& ns, const TProb<T>& p ) : _vs(ns), _p(p) {
/// Construct vector of length n with each entry set to p
explicit TProb( size_t n, Real p ) : _p(n, (T)p) {}
- /// Construct vector of length n by copying the elements between p and p+n
- TProb( size_t n, const Real* p ) : _p(p, p + n ) {}
+ /// Construct vector from a range
+ /** \tparam Iterator Iterates over instances that can be cast to T.
+ * \param begin Points to first instance to be added.
+ * \param end Points just beyond last instance to be added.
+ * \param sizeHint For efficiency, the number of elements can be speficied by sizeHint.
+ */
+ template <typename Iterator>
+ TProb( Iterator begin, Iterator end, size_t sizeHint=0 ) : _p() {
+ _p.reserve( sizeHint );
+ _p.insert( _p.begin(), begin, end );
+ }
/// Returns a const reference to the vector
const std::vector<T> & p() const { return _p; }
S -= (_p[i] == 0 ? 0 : _p[i] * std::log(_p[i]));
return S;
}
+
+ /// Returns a random index, according to the (normalized) distribution described by *this
+ size_t draw() {
+ double x = rnd_uniform() * totalSum();
+ T s = 0;
+ for( size_t i = 0; i < size(); i++ ) {
+ s += _p[i];
+ if( s > x )
+ return i;
+ }
+ return( size() - 1 );
+ }
};
bool BipartiteGraph::isConnected() const {
+ // TODO: use BGL, like:
+ // std::vector<int> component( num_vertices( g ) );
+ // int num_comp = connected_components( g, make_iterator_property_map(component.begin(), get(vertex_index, g)) );
if( nr1() == 0 ) {
return true;
} else {
cout.width( 39 );
cout << left << "# METHOD" << "\t";
if( report_time )
- cout << right << "SECONDS" << "\t";
+ cout << right << "SECONDS " << "\t";
if( report_iters )
cout << "ITERS" << "\t";
cout << "MAX ERROR" << "\t";