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#include <unordered_map> // std::unordered_map
28#include <algorithm> // std::find
29
30namespace nnp
31{
32
39std::vector<std::string> split(std::string const& input,
40 char delimiter = ' ');
48std::string trim(std::string const& line,
49 std::string const& whitespace = " \t");
60std::string reduce(std::string const& line,
61 std::string const& whitespace = " \t",
62 std::string const& fill = " ");
73std::string pad(std::string const& input,
74 std::size_t num,
75 char fill = ' ',
76 bool right = true);
79std::string strpr(const char* format, ...);
86std::string cap(std::string word);
98std::vector<std::string> createFileHeader(
99 std::vector<std::string> const& title,
100 std::vector<std::size_t> const& colLength,
101 std::vector<std::string> const& colName,
102 std::vector<std::string> const& colInfo,
103 char const& commentChar = '#');
111 std::ofstream& file,
112 std::vector<std::string> const lines);
120 FILE* const& file,
121 std::vector<std::string> const lines);
130std::map<std::size_t,
131 std::vector<double>>
133 std::string fileName,
134 std::vector<std::size_t> columns,
135 char comment = '#');
143template<typename K, typename V>
144V const& safeFind(std::map<K, V> const& stdMap,
145 typename
146 std::map<K, V>::key_type const& key)
147{
148 if (stdMap.find(key) == stdMap.end())
149 {
150 std::stringstream message;
151 message << "ERROR: No map entry found for key \"";
152 message << key;
153 message << "\".\n";
154 throw std::range_error(message.str());
155 }
156 return stdMap.find(key)->second;
157}
158
165template<typename T>
166bool vectorContains(std::vector<T> const& stdVec,
167 T value)
168{
169 return (std::find(stdVec.begin(), stdVec.end(), value) != stdVec.end());
170}
178template<typename T, typename U>
180 std::unordered_map<T, U> const& stdMap,
181 T key)
182{
183 return ( stdMap.find(key) != stdMap.end() );
184}
185
193template<typename T>
194bool comparePointerTargets(T* lhs, T* rhs)
195{
196 return ((*lhs) < (*rhs));
197}
198
206double pow_int(double x, int n);
207
208}
209
210#endif
Definition: Atom.h:29
string pad(string const &input, size_t num, char fill, bool right)
Definition: utility.cpp:79
string cap(string word)
Capitalize first letter of word.
Definition: utility.cpp:104
string strpr(const char *format,...)
String version of printf function.
Definition: utility.cpp:90
bool vectorContains(std::vector< T > const &stdVec, T value)
Test if vector contains specified value.
Definition: utility.h:166
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:111
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:247
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:194
double pow_int(double x, int n)
Integer version of power function, "fast exponentiation algorithm".
Definition: utility.cpp:292
void appendLinesToFile(ofstream &file, vector< string > const lines)
Append multiple lines of strings to open file stream.
Definition: utility.cpp:225
bool unorderedMapContainsKey(std::unordered_map< T, U > const &stdMap, T key)
Test if unordered map contains specified key.
Definition: utility.h:179
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:144