votca 2024.2-dev
Loading...
Searching...
No Matches
pairlist.h
Go to the documentation of this file.
1/*
2 * Copyright 2009-2019 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_CSG_PAIRLIST_H
19#define VOTCA_CSG_PAIRLIST_H
20
21// Standard includes
22#include <map>
23#include <vector>
24
25// VOTCA includes
26#include <votca/tools/types.h>
27
28namespace votca {
29namespace csg {
30
31template <typename element_type, typename pair_type>
32class PairList {
33 public:
34 PairList() = default;
35 virtual ~PairList() { Cleanup(); }
36
37 // this method takes ownership of p
38 void AddPair(pair_type *p);
39
40 using iterator = typename std::vector<pair_type *>::iterator;
41 using const_iterator = typename std::vector<pair_type *>::const_iterator;
42 typedef typename std::map<element_type, pair_type *> partners;
43
44 iterator begin() { return pairs_.begin(); }
45 iterator end() { return pairs_.end(); }
46
47 const_iterator begin() const { return pairs_.begin(); }
48 const_iterator end() const { return pairs_.end(); }
49 pair_type *front() { return pairs_.front(); }
50 pair_type *back() { return pairs_.back(); }
51 bool empty() const { return pairs_.empty(); }
52
53 Index size() const { return Index(pairs_.size()); }
54
55 void Cleanup();
56
57 pair_type *FindPair(element_type e1, element_type e2);
58
59 const pair_type *FindPair(element_type e1, element_type e2) const;
60
61 partners *FindPartners(element_type e1);
62
63 using element_t = element_type;
64 using pair_t = pair_type;
65
66 protected:
67 std::vector<pair_type *> pairs_;
68
69 std::map<element_type, std::map<element_type, pair_type *>> pair_map_;
70};
71
72// this method takes ownership of p
73template <typename element_type, typename pair_type>
77 pair_map_[p->first()][p->second()] = p;
78 pair_map_[p->second()][p->first()] = p;
80 pairs_.push_back(p);
81}
82
83template <typename element_type, typename pair_type>
85 for (auto &pair : pairs_) {
86 delete pair;
87 }
88 pairs_.clear();
89 pair_map_.clear();
90}
91
92template <typename element_type, typename pair_type>
93inline pair_type *PairList<element_type, pair_type>::FindPair(element_type e1,
94 element_type e2) {
95 typename std::map<element_type, std::map<element_type, pair_type *>>::iterator
96 iter1;
97 iter1 = pair_map_.find(e1);
98 if (iter1 == pair_map_.end()) {
99 return nullptr;
100 }
101
102 typename partners::iterator iter2;
103 iter2 = iter1->second.find(e2);
104 if (iter2 == iter1->second.end()) {
105 return nullptr;
106 }
107
108 return iter2->second;
109}
110
111template <typename element_type, typename pair_type>
113 element_type e1, element_type e2) const {
114 typename std::map<element_type,
115 std::map<element_type, pair_type *>>::const_iterator iter1;
116 iter1 = pair_map_.find(e1);
117 if (iter1 == pair_map_.end()) {
118 return nullptr;
119 }
120
121 typename partners::const_iterator iter2;
122 iter2 = iter1->second.find(e2);
123 if (iter2 == iter1->second.end()) {
124 return nullptr;
125 }
126
127 return iter2->second;
128}
129
130template <typename element_type, typename pair_type>
133 typename std::map<element_type, std::map<element_type, pair_type *>>::iterator
134 iter;
135 if ((iter = pair_map_.find(e1)) == pair_map_.end()) {
136 return nullptr;
137 }
138 return &(iter->second);
139}
140
141} // namespace csg
142} // namespace votca
143
144#endif /* VOTCA_CSG_PAIRLIST_H */
std::map< element_type, std::map< element_type, pair_type * > > pair_map_
Definition pairlist.h:69
bool empty() const
Definition pairlist.h:51
std::vector< pair_type * > pairs_
Definition pairlist.h:67
typename std::vector< pair_type * >::const_iterator const_iterator
Definition pairlist.h:41
void AddPair(pair_type *p)
Definition pairlist.h:74
typename std::vector< pair_type * >::iterator iterator
Definition pairlist.h:40
const pair_type * FindPair(element_type e1, element_type e2) const
Definition pairlist.h:112
element_type element_t
Definition pairlist.h:63
virtual ~PairList()
Definition pairlist.h:35
pair_type * FindPair(element_type e1, element_type e2)
Definition pairlist.h:93
iterator begin()
Definition pairlist.h:44
const_iterator begin() const
Definition pairlist.h:47
const_iterator end() const
Definition pairlist.h:48
Index size() const
Definition pairlist.h:53
partners * FindPartners(element_type e1)
Definition pairlist.h:132
pair_type * back()
Definition pairlist.h:50
pair_type * front()
Definition pairlist.h:49
std::map< element_type, pair_type * > partners
Definition pairlist.h:42
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26