votca 2024-dev
Loading...
Searching...
No Matches
graphnode.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team
3 * (http://www.votca.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License")
6 *
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20// Standard includes
21#include <algorithm>
22#include <iomanip>
23#include <iostream>
24#include <sstream>
25#include <string>
26#include <unordered_map>
27#include <vector>
28
29// Third party includes
30#include <boost/lexical_cast.hpp>
31
32// Local VOTCA includes
34
35namespace votca {
36namespace tools {
37
38using namespace std;
39using namespace boost;
40
42// Local Functions
46string sig_fig_(double val, Index sf) {
47 return ([val](Index number_of_sig_figs) -> string {
48 stringstream lStream;
49 lStream << setprecision(int(number_of_sig_figs)) << val;
50 return lStream.str();
51 })(sf);
52}
53
56string getIntStringId_(const unordered_map<string, Index> int_vals) {
57 vector<string> keys;
58 // Grab integer keys sort alphabetically and store in string_id
59 string int_string_id;
60 for (auto it : int_vals) {
61 keys.push_back(it.first);
62 }
63 sort(keys.begin(), keys.end());
64 for (auto key : keys) {
65 int_string_id.append(key);
66 auto it = int_vals.find(key);
67 int_string_id.append(lexical_cast<string>(it->second));
68 }
69 return int_string_id;
70}
71
74string getDoubleStringId_(const unordered_map<string, double> double_vals) {
75 vector<string> keys;
76 // Grab double keys sort alphabetically and store in string_id
77 string double_string_id;
78 for (auto it : double_vals) {
79 keys.push_back(it.first);
80 }
81 sort(keys.begin(), keys.end());
82 for (auto key : keys) {
83 double_string_id.append(key);
84 auto it = double_vals.find(key);
85 double_string_id.append(sig_fig_(it->second, 8));
86 }
87 return double_string_id;
88}
89
92string getStrStringId_(const unordered_map<string, string> str_vals) {
93 vector<string> keys;
94 // Grab string keys sort alphabetically and store in string_id
95 string str_string_id;
96 for (auto it : str_vals) {
97 keys.push_back(it.first);
98 }
99 sort(keys.begin(), keys.end());
100 for (auto key : keys) {
101 str_string_id.append(key);
102 auto it = str_vals.find(key);
103 str_string_id.append(lexical_cast<string>(it->second));
104 }
105 return str_string_id;
106}
107
109// Private Functions
114 str_id_.clear();
118}
119
121// Public Functions
123GraphNode::GraphNode(const unordered_map<string, Index> int_vals,
124 const unordered_map<string, double> double_vals,
125 const unordered_map<string, string> str_vals) {
126 int_vals_ = int_vals;
127 double_vals_ = double_vals;
128 str_vals_ = str_vals;
130}
131
132void GraphNode::setInt(const unordered_map<string, Index> int_vals) {
133 int_vals_ = int_vals;
135}
136
137void GraphNode::setDouble(const unordered_map<string, double> double_vals) {
138 double_vals_ = double_vals;
140}
141
142void GraphNode::setStr(const unordered_map<string, string> str_vals) {
143 str_vals_ = str_vals;
145}
146
147Index GraphNode::getInt(const string str) {
148 if (int_vals_.count(str) == 0) {
149 throw invalid_argument(
150 "GraphNode does not "
151 "contain value");
152 }
153 return int_vals_[str];
154}
155
156double GraphNode::getDouble(const string str) {
157 if (double_vals_.count(str) == 0) {
158 throw invalid_argument(
159 "GraphNode does not "
160 "contain value");
161 }
162 return double_vals_[str];
163}
164
165std::string GraphNode::getStr(const string str) {
166 if (str_vals_.count(str) == 0) {
167 throw invalid_argument(
168 "GraphNode does not "
169 "contain value");
170 }
171 return str_vals_[str];
172}
173
174bool GraphNode::operator!=(const GraphNode gn) const {
175 return (str_id_.compare(gn.str_id_) != 0);
176}
177
178bool GraphNode::operator==(const GraphNode gn) const {
179 return !((*this) != gn);
180}
181
182ostream& operator<<(ostream& os, const GraphNode gn) {
183 os << "Integer Values" << endl;
184 for (const auto& int_val : gn.int_vals_) {
185 os << int_val.first << " " << int_val.second << endl;
186 }
187 os << "Double Values" << endl;
188 for (const auto& double_val : gn.double_vals_) {
189 os << double_val.first << " " << double_val.second << endl;
190 }
191 os << "String Values" << endl;
192 for (const auto& str_val : gn.str_vals_) {
193 os << str_val.first << " " << str_val.second << endl;
194 }
195 return os;
196}
197
199 string str1_Id = gn1.getStringId();
200 return str1_Id.compare(gn2.getStringId()) < 0;
201}
202} // namespace tools
203} // namespace votca
A graph node that will take a variety of different values.
Definition graphnode.h:46
std::unordered_map< std::string, std::string > str_vals_
Definition graphnode.h:51
bool operator==(const GraphNode gn) const
Definition graphnode.cc:178
std::string getStringId() const
Get the string id unique to the contents of the graph node.
Definition graphnode.h:74
void setDouble(const std::unordered_map< std::string, double > double_vals)
Definition graphnode.cc:137
std::unordered_map< std::string, Index > int_vals_
Definition graphnode.h:49
std::string getStr(const std::string str)
Definition graphnode.cc:165
Index getInt(const std::string str)
Basic getters.
Definition graphnode.cc:147
std::unordered_map< std::string, double > double_vals_
Definition graphnode.h:50
double getDouble(const std::string str)
Definition graphnode.cc:156
bool operator!=(const GraphNode gn) const
Definition graphnode.cc:174
void setStr(const std::unordered_map< std::string, std::string > str_vals)
Definition graphnode.cc:142
void setInt(const std::unordered_map< std::string, Index > int_vals)
Basic setters.
Definition graphnode.cc:132
STL namespace.
string getIntStringId_(const unordered_map< string, Index > int_vals)
Definition graphnode.cc:56
string getDoubleStringId_(const unordered_map< string, double > double_vals)
Definition graphnode.cc:74
std::ostream & operator<<(std::ostream &out, const Correlate &c)
Definition correlate.h:53
string sig_fig_(double val, Index sf)
Definition graphnode.cc:46
bool cmpNode(GraphNode gn1, GraphNode gn2)
Comparison function to be used with stl sort algorithm.
Definition graphnode.cc:198
string getStrStringId_(const unordered_map< string, string > str_vals)
Definition graphnode.cc:92
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26