n2p2 - A neural network potential package
utility.h
Go to the documentation of this file.
1// n2p2 - A neural network potential package
2// Copyright (C) 2018 Andreas Singraber (University of Vienna)
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#ifndef UTILITY_H
18#define UTILITY_H
19
20#include <cstdio> // FILE
21#include <fstream> // std::ofstream
22#include <map> // std::map
23#include <sstream> // std::stringstream
24#include <stdexcept> // std::range_error
25#include <string> // std::string
26#include <vector> // std::vector
27
28namespace nnp
29{
30
37std::vector<std::string> split(std::string const& input,
38 char delimiter = ' ');
46std::string trim(std::string const& line,
47 std::string const& whitespace = " \t");
58std::string reduce(std::string const& line,
59 std::string const& whitespace = " \t",
60 std::string const& fill = " ");
71std::string pad(std::string const& input,
72 std::size_t num,
73 char fill = ' ',
74 bool right = true);
77std::string strpr(const char* format, ...);
89std::vector<std::string> createFileHeader(
90 std::vector<std::string> const& title,
91 std::vector<std::size_t> const& colLength,
92 std::vector<std::string> const& colName,
93 std::vector<std::string> const& colInfo,
94 char const& commentChar = '#');
102 std::ofstream& file,
103 std::vector<std::string> const lines);
111 FILE* const& file,
112 std::vector<std::string> const lines);
121std::map<std::size_t,
122 std::vector<double>>
124 std::string fileName,
125 std::vector<std::size_t> columns,
126 char comment = '#');
134template<typename K, typename V>
135V const& safeFind(std::map<K, V> const& stdMap,
136 typename
137 std::map<K, V>::key_type const& key)
138{
139 if (stdMap.find(key) == stdMap.end())
140 {
141 std::stringstream message;
142 message << "ERROR: No map entry found for key \"";
143 message << key;
144 message << "\".\n";
145 throw std::range_error(message.str());
146 }
147 return stdMap.find(key)->second;
148}
149
157template<typename T>
158bool comparePointerTargets(T* lhs, T* rhs)
159{
160 return ((*lhs) < (*rhs));
161}
162
170double pow_int(double x, int n);
171
172}
173
174#endif
Definition: Atom.h:28
string pad(string const &input, size_t num, char fill, bool right)
Definition: utility.cpp:79
string strpr(const char *format,...)
String version of printf function.
Definition: utility.cpp:90
vector< string > createFileHeader(vector< string > const &title, vector< size_t > const &colSize, vector< string > const &colName, vector< string > const &colInfo, char const &commentChar)
Definition: utility.cpp:104
string trim(string const &line, string const &whitespace)
Remove leading and trailing whitespaces from string.
Definition: utility.cpp:47
vector< string > split(string const &input, char delimiter)
Split string at each delimiter.
Definition: utility.cpp:33
map< size_t, vector< double > > readColumnsFromFile(string fileName, vector< size_t > columns, char comment)
Definition: utility.cpp:240
string reduce(string const &line, string const &whitespace, string const &fill)
Replace multiple whitespaces with fill.
Definition: utility.cpp:60
bool comparePointerTargets(T *lhs, T *rhs)
Compare pointer targets.
Definition: utility.h:158
double pow_int(double x, int n)
Integer version of power function, "fast exponentiation algorithm".
Definition: utility.cpp:285
void appendLinesToFile(ofstream &file, vector< string > const lines)
Append multiple lines of strings to open file stream.
Definition: utility.cpp:218
V const & safeFind(std::map< K, V > const &stdMap, typename std::map< K, V >::key_type const &key)
Safely access map entry.
Definition: utility.h:135