LIBINT 2.7.2
iter.h
1/*
2 * Copyright (C) 2004-2021 Edward F. Valeev
3 *
4 * This file is part of Libint.
5 *
6 * Libint is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Libint is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Libint. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <vector>
22#include <smart_ptr.h>
23#include <policy.h>
24#include <polyconstr.h>
25#include <exception.h>
26
27#ifndef _libint2_src_bin_libint_iter_h_
28#define _libint2_src_bin_libint_iter_h_
29
30// gcc 3.4 doesn't seem to allow
31//#define ALLOW_PARTIALLY_SPECIALIZED_NESTED_TEMPLATES
32
33namespace libint2 {
34
35 struct DummyIterator;
36
43
44 public:
46 virtual unsigned int num_iter() const =0;
48 virtual void init() =0;
50 virtual SubIterator& operator++() =0;
52 virtual operator int() const =0;
56 virtual const ConstructablePolymorphically& pelem() const;
57 virtual ~SubIterator();
58
59 protected:
61
62 private:
63 //SubIterator operator++(int);
64 };
65
70 template <class T, template <class> class Tr = Policy > class SubIteratorBase : public SubIterator {
71
72 public:
73 typedef typename T::iter_type iter_type;
74 typedef Tr<T> TPolicy;
75 typedef typename TPolicy::obj_stype tref;
76 typedef typename TPolicy::subobj_stype iref;
79 SubIteratorBase(const tref&);
80 virtual ~SubIteratorBase();
81
83 const iref& elem() const;
85 cp_rettype pelem() const override;
86
88 unsigned int num_iter() const override;
90 void init() override;
92 SubIterator& operator++() override;
94 operator int() const override;
95
96 protected:
97 const tref obj_;
98 std::vector<iref> subobj_;
99
100 private:
102 unsigned int iter_;
103
104 // These templates are used as a trick to make possible "partial specialization
105 // of a template with multiple template params". Default implementations are not provided
106 // so user must provide specialization for the case X=T
107 template <class X> void init_subobj();
108 template <class X> void delete_subobj();
109 void init_subobj();
110 void delete_subobj();
111
112 // implementation of pelem()
113 template <typename X, bool return_smart_ptr>
114 struct PElemImpl {
115 };
116 template <typename X>
117 struct PElemImpl<X,true> {
118 static cp_rettype pelem(const iref& elem) {
119 SafePtr<ConstructablePolymorphically> elem_cast = dynamic_pointer_cast<ConstructablePolymorphically,X>(elem);
120 return *(elem_cast.get());
121 }
122 };
123 template <typename X>
124 struct PElemImpl<X,false> {
125 static cp_rettype pelem(const iref& elem) {
126 return elem;
127 }
128 };
129
130 };
131
132 template <class T, template <class> class P>
133 SubIteratorBase<T,P>::SubIteratorBase(const tref& obj) :
134 obj_(obj), subobj_(0), iter_(0)
135 {
136#ifdef ALLOW_PARTIALLY_SPECIALIZED_NESTED_TEMPLATES
137 init_subobj<T>();
138#else
139 init_subobj();
140#endif
141 }
142
143 template <class T, template <class> class P>
144 SubIteratorBase<T,P>::~SubIteratorBase()
145 {
146#ifdef ALLOW_PARTIALLY_SPECIALIZED_NESTED_TEMPLATES
147 delete_subobj<T>();
148#else
149 delete_subobj();
150#endif
151 }
152
153 template <class T, template <class> class P>
154 unsigned int
156 {
157 return subobj_.size();
158 }
159
160 template <class T, template <class> class P>
161 const typename SubIteratorBase<T,P>::iref&
163 {
164 return subobj_.at(iter_);
165 }
166
167 template <class T, template <class> class P>
170 {
171 return PElemImpl<iter_type,detail::IsSafePtr<iref>::result>::pelem(elem());
172 }
173
174#if 0
175 template <class T, template <class> class P>
176 const SafePtr<ConstructablePolymorphically>
178 {
179 return dynamic_pointer_cast<ConstructablePolymorphically,iter_type>(elem());
180 }
181#endif
182
183 template <class T, template <class> class P>
184 void
186 {
187 iter_ = 0;
188 }
189
190 template <class T, template <class> class P>
193 {
194 ++iter_;
195 return *this;
196 }
197
198 template <class T, template <class> class P>
200 {
201 return (iter_ < num_iter()) ? 1 : 0;
202 }
203
204 template <class T, template <class> class P>
205 void
207 {
208 P<T>::init_subobj(obj_,subobj_);
209 }
210
211 template <class T, template <class> class P>
212 void
213 SubIteratorBase<T,P>::delete_subobj()
214 {
215 P<T>::dealloc_subobj(subobj_);
216 }
217
218};
219
220#endif
ConstructablePolymorphically is a base for all objects which can be constructed using a SafePtr to a ...
Definition: polyconstr.h:31
SubIteratorBase<T> provides a base class for a sub-iterator class for T.
Definition: iter.h:70
void init() override
Initializes the iterator.
Definition: iter.h:185
const ConstructablePolymorphically & cp_rettype
Return reference to ConstructablePolymorphically as object of this type.
Definition: iter.h:78
const iref & elem() const
Returns current element.
Definition: iter.h:162
unsigned int num_iter() const override
Returns a number of iterations (number of elements in a set over which to iterate).
Definition: iter.h:155
cp_rettype pelem() const override
Returns current element. Implements SubIterator's pelem().
Definition: iter.h:169
SubIterator & operator++() override
Iterates to the next element. Only prefix form is provided.
Definition: iter.h:192
Iterator provides a base class for all object iterator classes.
Definition: iter.h:42
virtual const ConstructablePolymorphically & pelem() const
Return current element via base class.
Definition: iter.cc:38
virtual SubIterator & operator++()=0
Iterates to the next element. Only prefix form is provided.
virtual unsigned int num_iter() const =0
Returns a number of iterations (number of elements in a set over which to iterate).
virtual void init()=0
Initializes the iterator.
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:24