votca
2024.2-dev
Loading...
Searching...
No Matches
tools
src
libtools
rangeparser.cc
Go to the documentation of this file.
1
/*
2
* Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*
16
*/
17
18
// Standard includes
19
#include <functional>
20
#include <iostream>
21
#include <stdexcept>
22
23
// Local VOTCA includes
24
#include "
votca/tools/rangeparser.h
"
25
#include "
votca/tools/tokenizer.h
"
26
27
namespace
votca
{
28
namespace
tools {
29
30
RangeParser::RangeParser
() =
default
;
31
32
void
RangeParser::Parse
(std::string str) {
33
// remove all spaces in string
34
std::string::iterator it =
35
std::remove_if(str.begin(), str.end(), [](
char
a) { return a ==
' '
; });
36
str = std::string(str.begin(), it);
37
38
Tokenizer
tok(str,
","
);
39
for
(std::string bl : tok) {
40
ParseBlock
(bl);
41
}
42
}
43
44
void
RangeParser::ParseBlock
(std::string str) {
45
std::vector<std::string> toks =
Tokenizer
(str,
":"
).
ToVector
();
46
47
block_t
block;
48
block.
stride_
= 1;
49
50
if
(toks.size() > 3 || toks.size() < 1) {
51
throw
std::runtime_error(
"invalid range"
);
52
}
53
54
block.
begin_
= block.
end_
= std::stoi(toks[0]);
55
56
if
(toks.size() == 2) {
57
block.
end_
= std::stoi(toks[1]);
58
}
59
60
if
(toks.size() == 3) {
61
block.
stride_
= std::stoi(toks[1]);
62
block.
end_
= std::stoi(toks[2]);
63
}
64
65
if
(block.
begin_
* block.
stride_
> block.
end_
* block.
stride_
) {
66
throw
std::runtime_error(
67
std::string(
"invalid range "
+ str +
68
": begin, end and stride do not form a closed interval"
));
69
}
70
71
blocks_
.push_back(block);
72
}
73
74
RangeParser::iterator
&
RangeParser::iterator::operator++
() {
75
current_
+= (*block_).stride_;
76
if
(
current_
> (*block_).end_) {
77
++
block_
;
78
if
(
block_
!=
parent_
->
blocks_
.end()) {
79
current_
= (*block_).begin_;
80
}
else
{
81
current_
= -1;
82
}
83
}
84
return
*
this
;
85
}
86
87
}
// namespace tools
88
}
// namespace votca
votca::tools::RangeParser::Parse
void Parse(std::string str)
Definition
rangeparser.cc:32
votca::tools::RangeParser::ParseBlock
void ParseBlock(std::string str)
Definition
rangeparser.cc:44
votca::tools::RangeParser::RangeParser
RangeParser()
votca::tools::RangeParser::blocks_
std::list< block_t > blocks_
Definition
rangeparser.h:81
votca::tools::Tokenizer
break string into words
Definition
tokenizer.h:72
votca::tools::Tokenizer::ToVector
std::vector< T > ToVector()
store all words in a vector of type T, does type conversion.
Definition
tokenizer.h:109
votca
base class for all analysis tools
Definition
basebead.h:33
rangeparser.h
votca::tools::RangeParser::block_t
Definition
rangeparser.h:46
votca::tools::RangeParser::block_t::stride_
Index stride_
Definition
rangeparser.h:51
votca::tools::RangeParser::block_t::begin_
Index begin_
Definition
rangeparser.h:51
votca::tools::RangeParser::block_t::end_
Index end_
Definition
rangeparser.h:51
votca::tools::RangeParser::iterator
Definition
rangeparser.h:55
votca::tools::RangeParser::iterator::parent_
RangeParser * parent_
Definition
rangeparser.h:66
votca::tools::RangeParser::iterator::current_
Index current_
Definition
rangeparser.h:70
votca::tools::RangeParser::iterator::operator++
RangeParser::iterator & operator++()
Definition
rangeparser.cc:74
votca::tools::RangeParser::iterator::block_
std::list< block_t >::iterator block_
Definition
rangeparser.h:69
tokenizer.h
Generated by
1.12.0