votca 2024-dev
Loading...
Searching...
No Matches
rangeparser.h
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#ifndef VOTCA_TOOLS_RANGEPARSER_H
19#define VOTCA_TOOLS_RANGEPARSER_H
20
21// Standard includes
22#include <list>
23#include <ostream>
24#include <string>
25
26// Local VOTCA includes
27#include "types.h"
28
29namespace votca {
30namespace tools {
31
38 public:
40
41 void Parse(std::string str);
42
43 void Add(Index begin, Index end, Index stride = 1);
44
45 private:
46 struct block_t {
47 block_t() = default;
48 block_t(const Index &begin, const Index &end, const Index &stride)
49 : begin_(begin), end_(end), stride_(stride) {}
50
52 };
53
54 public:
55 struct iterator {
56 iterator() = default;
57
58 Index operator*() const { return current_; }
59
61
64
65 private:
67
68 iterator(RangeParser *, std::list<block_t>::iterator);
69 std::list<block_t>::iterator block_;
71
72 friend class RangeParser;
73 };
74
77
78 private:
79 void ParseBlock(std::string str);
80
81 std::list<block_t> blocks_;
82
83 friend std::ostream &operator<<(std::ostream &out, const RangeParser &rp);
84};
85
86inline void RangeParser::Add(Index begin, Index end, Index stride) {
87 blocks_.push_back(block_t(begin, end, stride));
88}
89
91 return RangeParser::iterator(this, blocks_.begin());
92}
93
95 return RangeParser::iterator(this, blocks_.end());
96}
97
99 std::list<block_t>::iterator block)
100 : parent_(parent), block_(block) {
101 if (block != parent->blocks_.end()) {
102 current_ = (*block).begin_;
103 } else {
104 current_ = -1;
105 }
106}
107
109 return (block_ == i.block_) && (current_ == i.current_);
110}
111
113 return !((block_ == i.block_) && (current_ == i.current_));
114}
115
116inline std::ostream &operator<<(std::ostream &out, const RangeParser &rp) {
117 std::list<RangeParser::block_t>::const_iterator iter(rp.blocks_.begin());
118 for (; iter != rp.blocks_.end(); ++iter) {
119 if (iter != rp.blocks_.begin()) {
120 out << ",";
121 }
122 if (iter->begin_ == iter->end_) {
123 out << iter->begin_;
124 } else if (iter->stride_ == 1) {
125 out << iter->begin_ << ":" << iter->end_;
126 } else {
127 out << iter->begin_ << ":" << iter->stride_ << ":" << iter->end_;
128 }
129 }
130 return out;
131}
132
133} // namespace tools
134} // namespace votca
135
136#endif // VOTCA_TOOLS_RANGEPARSER_H
RangeParser::iterator begin()
Definition rangeparser.h:90
friend std::ostream & operator<<(std::ostream &out, const RangeParser &rp)
void Add(Index begin, Index end, Index stride=1)
Definition rangeparser.h:86
void Parse(std::string str)
void ParseBlock(std::string str)
RangeParser::iterator end()
Definition rangeparser.h:94
std::list< block_t > blocks_
Definition rangeparser.h:81
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26
block_t(const Index &begin, const Index &end, const Index &stride)
Definition rangeparser.h:48
bool operator==(const RangeParser::iterator &)
RangeParser::iterator & operator++()
bool operator!=(const RangeParser::iterator &)
std::list< block_t >::iterator block_
Definition rangeparser.h:69