Improved WeightedGraph code and added unit tests
[libdai.git] / src / properties.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 #include <iostream>
13 #include <dai/properties.h>
14 #include <dai/exceptions.h>
15
16
17 namespace dai {
18
19
20 std::ostream& operator<< (std::ostream & os, const Property & p) {
21 os << p.first << "=";
22 if( p.second.type() == typeid(size_t) )
23 os << boost::any_cast<size_t>(p.second);
24 else if( p.second.type() == typeid(std::string) )
25 os << boost::any_cast<std::string>(p.second);
26 else if( p.second.type() == typeid(double) )
27 os << boost::any_cast<double>(p.second);
28 else if( p.second.type() == typeid(long double) )
29 os << boost::any_cast<long double>(p.second);
30 else if( p.second.type() == typeid(bool) )
31 os << boost::any_cast<bool>(p.second);
32 else if( p.second.type() == typeid(PropertySet) )
33 os << boost::any_cast<PropertySet>(p.second);
34 else
35 DAI_THROW(UNKNOWN_PROPERTY_TYPE);
36 return( os );
37 }
38
39
40 /// Writes a PropertySet object to an output stream
41 std::ostream& operator<< (std::ostream & os, const PropertySet & ps) {
42 os << "[";
43 for( PropertySet::const_iterator p = ps.begin(); p != ps.end(); p++ ) {
44 if( p != ps.begin() )
45 os << ",";
46 os << (Property)*p;
47 }
48 os << "]";
49 return os;
50 }
51
52
53 /// Reads a PropertySet object from an input stream, storing values as strings
54 std::istream& operator>> (std::istream& is, PropertySet & ps) {
55 ps = PropertySet();
56
57 std::string s;
58 is >> s;
59
60 // Check whether s is of the form "[.*]"
61 if( (s.length() < 2) || (s.at(0) != '[') || (s.at(s.length()-1)) != ']' )
62 DAI_THROWE(MALFORMED_PROPERTY,"Malformed PropertySet: " + s);
63
64 size_t N = s.length() - 1;
65 for( size_t token_start = 1; token_start < N; ) {
66 size_t token_end;
67
68 // scan until '=' is found
69 for( token_end = token_start + 1; token_end < N; token_end++ )
70 if( s[token_end] == '=' )
71 break;
72 // we found a key
73 std::string key = s.substr(token_start, token_end - token_start);
74 if( token_end == N )
75 DAI_THROWE(MALFORMED_PROPERTY,"Malformed Property: " + key);
76
77 token_start = token_end + 1;
78 // scan until matching ',' is found
79 int level = 0;
80 for( token_end = token_start; token_end < N; token_end++ ) {
81 if( s[token_end] == '[' )
82 level++;
83 else if( s[token_end] == ']' )
84 level--;
85 else if( (s[token_end] == ',') && (level == 0) )
86 break;
87 }
88 if( !(level == 0) )
89 DAI_THROWE(MALFORMED_PROPERTY,"Malformed Property: " + s.substr(token_start, token_end - token_start));
90 // we found a vlue
91 std::string value = s.substr(token_start, token_end - token_start);
92
93 // store the key,value pair
94 ps.Set(key,value);
95
96 // go on with the next one
97 token_start = token_end + 1;
98 }
99
100 return is;
101 }
102
103
104 } // end of namespace dai