votca 2024.2-dev
Loading...
Searching...
No Matches
IndexParser.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 inlcudes
21#include <numeric>
22#include <set>
23
24// Third party includes
25#include <boost/algorithm/string.hpp>
26#include <boost/lexical_cast.hpp>
27
28// VOTCA includes
30
31// Local VOTCA includes
33
34namespace votca {
35namespace xtp {
36
38 const std::string& Ids) const {
39 std::vector<Index> result;
40 std::vector<std::string> results = tools::Tokenizer(Ids, " ,\n\t").ToVector();
41 const std::string delimiter = ":";
42 for (std::string s : results) {
43 if (s.find(delimiter) != std::string::npos) {
44 std::string copy_s = s;
45 try {
46 Index start =
47 boost::lexical_cast<Index>(s.substr(0, s.find(delimiter)));
48 Index stop = boost::lexical_cast<Index>(
49 s.erase(0, s.find(delimiter) + delimiter.length()));
50 for (Index i = start; i <= stop; i++) {
51 result.push_back(i);
52 }
53 } catch (boost::bad_lexical_cast&) {
54 throw std::runtime_error("Could not convert " + copy_s +
55 " to range of integers.");
56 }
57 } else {
58 try {
59 result.push_back(boost::lexical_cast<Index>(s));
60 } catch (boost::bad_lexical_cast&) {
61 throw std::runtime_error("Could not convert " + s + " to integer.");
62 }
63 }
64 }
65 // Eliminates duplicates and sorts the vector
66 std::set<Index> s(result.begin(), result.end());
67 result.assign(s.begin(), s.end());
68 return result;
69}
70
72 const std::vector<Index>& indeces) const {
73 std::set<Index> s(indeces.begin(), indeces.end());
74 std::vector<Index> sorted_unique(s.begin(), s.end());
75 std::string result = "";
76 if (sorted_unique.empty()) {
77 return result;
78 }
79
80 std::vector<Index> difference(sorted_unique.size() + 1);
81 difference.back() = 0;
82 std::adjacent_difference(sorted_unique.begin(), sorted_unique.end(),
83 difference.begin());
84
85 bool range_started = false;
86 Index startindex;
87 for (Index i = 0; i < Index(sorted_unique.size()); i++) {
88 if (difference[i + 1] == 1) {
89 if (range_started) {
90 continue;
91 } else {
92 range_started = true;
93 startindex = sorted_unique[i];
94 }
95 } else {
96 if (range_started) {
97 result += std::to_string(startindex) + ":" +
98 (std::to_string(sorted_unique[i])) + " ";
99 range_started = false;
100 } else {
101 result += (std::to_string(sorted_unique[i])) + " ";
102 }
103 }
104 }
105 boost::trim(result);
106 return result;
107}
108
109} // namespace xtp
110} // namespace votca
break string into words
Definition tokenizer.h:72
std::vector< T > ToVector()
store all words in a vector of type T, does type conversion.
Definition tokenizer.h:109
std::vector< Index > CreateIndexVector(const std::string &Ids) const
std::string CreateIndexString(const std::vector< Index > &indeces) const
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26