votca 2024.2-dev
Loading...
Searching...
No Matches
datacollection.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_DATACOLLECTION_H
19#define VOTCA_TOOLS_DATACOLLECTION_H
20
21// Standard includes
22#include <cassert>
23#include <map>
24#include <sstream>
25#include <vector>
26
27// Local VOTCA includes
28#include "tokenizer.h"
29#include "types.h"
30
31namespace votca {
32namespace tools {
33
42template <typename T>
44 public:
48 class array : public std::vector<T> {
49 public:
50 array(std::string name) { name_ = name; }
51 const std::string &getName() const { return name_; }
52
53 private:
54 std::string name_;
55 };
56
57 using iterator = typename std::vector<array *>::iterator;
58 using const_iterator = typename std::vector<array *>::const_iterator;
59
63 class selection {
64 public:
65 selection() = default;
66 ~selection() = default;
67
68 Index size() const { return Index(arrays_.size()); }
69 bool empty() const { return arrays_.empty(); }
71 assert(i < Index(arrays_.size()));
72 return *(arrays_[i]);
73 }
74
75 const array &operator[](Index i) const {
76 assert(i < Index(arrays_.size()));
77 return *(arrays_[i]);
78 }
79
80 void push_back(array *a) { arrays_.push_back(a); }
82 arrays_.insert(arrays_.end(), s->begin(), s->end());
83 }
84
85 iterator begin() { return arrays_.begin(); }
86 iterator end() { return arrays_.end(); }
87 const_iterator begin() const { return arrays_.begin(); }
88 const_iterator end() const { return arrays_.end(); }
89
90 private:
91 std::vector<array *> arrays_;
92 };
93
95 DataCollection() = default;
98
102 void clear();
106 Index size() const { return Index(data_.size()); }
107 bool empty() const { return data_.empty(); }
109 assert(i < Index(data_.size()));
110 return *(data_[i]);
111 }
112 iterator begin() { return data_.begin(); }
113 iterator end() { return data_.end(); }
114 const_iterator begin() const { return data_.begin(); }
115 const_iterator end() const { return data_.end(); }
116
120 array *CreateArray(std::string name);
121
125 std::vector<array *> &Data() { return data_; }
126 const std::vector<array *> &Data() const { return data_; }
127
131 array *ArrayByName(std::string name);
132
140 selection *select(std::string strselection, selection *sel_append = nullptr);
141
142 private:
143 std::vector<array *> data_;
144
145 std::map<std::string, array *> array_by_name_;
146};
147
148template <typename T>
150
151 for (auto &d : data_) {
152 delete d;
153 }
154 data_.clear();
155}
156
157template <typename T>
159 std::string name) {
160 assert(ArrayByName(name) == nullptr);
161 array *a = new array(name);
162 data_.push_back(a);
163 array_by_name_[name] = a;
164
165 return a;
166}
167
168template <typename T>
170 std::string name) {
171 typename std::map<std::string, array *>::iterator i =
172 array_by_name_.find(name);
173 if (i == array_by_name_.end()) {
174 return nullptr;
175 }
176 return (*i).second;
177}
178
179template <typename T>
181 std::string strselection, selection *sel_append) {
182
183 typename DataCollection<T>::selection *sel = sel_append;
184 if (!sel_append) {
185 sel = new typename DataCollection<T>::selection;
186 }
187
188 for (auto &pair : array_by_name_) {
189 if (wildcmp(strselection.c_str(), pair.second->getName().c_str())) {
190 sel->push_back(pair.second);
191 }
192 }
193 return sel;
194}
195
196std::ostream &operator<<(std::ostream &out,
198} // namespace tools
199} // namespace votca
200
201#endif // VOTCA_TOOLS_DATACOLLECTION_H
The array class, extends vector by a name tag.
const std::string & getName() const
const array & operator[](Index i) const
This class handles a set of arrays which can be identified by name tags.
std::vector< array * > & Data()
access the data container
const_iterator end() const
const std::vector< array * > & Data() const
typename std::vector< array * >::const_iterator const_iterator
DataCollection()=default
constructor
std::vector< array * > data_
void clear()
clears the data collection
array * CreateArray(std::string name)
create a new array
typename std::vector< array * >::iterator iterator
array * ArrayByName(std::string name)
access an array by name
const_iterator begin() const
selection * select(std::string strselection, selection *sel_append=nullptr)
select a set of arrays
std::map< std::string, array * > array_by_name_
int wildcmp(const char *wild, const char *string)
Definition tokenizer.cc:28
std::ostream & operator<<(std::ostream &out, const Correlate &c)
Definition correlate.h:53
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26