LIBINT 2.9.0
iter.h
1/*
2 * Copyright (C) 2004-2024 Edward F. Valeev
3 *
4 * This file is part of Libint compiler.
5 *
6 * Libint compiler 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 compiler 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 compiler. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <exception.h>
22#include <policy.h>
23#include <polyconstr.h>
24#include <smart_ptr.h>
25
26#include <vector>
27
28#ifndef _libint2_src_bin_libint_iter_h_
29#define _libint2_src_bin_libint_iter_h_
30
31// gcc 3.4 doesn't seem to allow
32// #define ALLOW_PARTIALLY_SPECIALIZED_NESTED_TEMPLATES
33
34namespace libint2 {
35
36struct DummyIterator;
37
44 public:
47 virtual unsigned int num_iter() const = 0;
49 virtual void init() = 0;
51 virtual SubIterator& operator++() = 0;
53 virtual operator int() const = 0;
58 virtual const ConstructablePolymorphically& pelem() const;
59 virtual ~SubIterator();
60
61 protected:
63
64 private:
65 // SubIterator operator++(int);
66};
67
72template <class T, template <class> class Tr = Policy>
74 public:
75 typedef typename T::iter_type iter_type;
76 typedef Tr<T> TPolicy;
77 typedef typename TPolicy::obj_stype tref;
78 typedef typename TPolicy::subobj_stype iref;
81 SubIteratorBase(const tref&);
82 virtual ~SubIteratorBase();
83
85 const iref& elem() const;
87 cp_rettype pelem() const override;
88
91 unsigned int num_iter() const override;
93 void init() override;
95 SubIterator& operator++() override;
98 operator int() const override;
99
100 protected:
101 const tref obj_;
102 std::vector<iref> subobj_;
103
104 private:
106 unsigned int iter_;
107
108 // These templates are used as a trick to make possible "partial
109 // specialization of a template with multiple template params". Default
110 // implementations are not provided so user must provide specialization for
111 // the case X=T
112 template <class X>
113 void init_subobj();
114 template <class X>
115 void delete_subobj();
116 void init_subobj();
117 void delete_subobj();
118
119 // implementation of pelem()
120 template <typename X, bool return_smart_ptr>
121 struct PElemImpl {};
122 template <typename X>
123 struct PElemImpl<X, true> {
124 static cp_rettype pelem(const iref& elem) {
125 std::shared_ptr<ConstructablePolymorphically> elem_cast =
126 std::dynamic_pointer_cast<ConstructablePolymorphically, X>(elem);
127 return *(elem_cast.get());
128 }
129 };
130 template <typename X>
131 struct PElemImpl<X, false> {
132 static cp_rettype pelem(const iref& elem) { return elem; }
133 };
134};
135
136template <class T, template <class> class P>
137SubIteratorBase<T, P>::SubIteratorBase(const tref& obj)
138 : obj_(obj), subobj_(0), iter_(0) {
139#ifdef ALLOW_PARTIALLY_SPECIALIZED_NESTED_TEMPLATES
140 init_subobj<T>();
141#else
142 init_subobj();
143#endif
144}
145
146template <class T, template <class> class P>
147SubIteratorBase<T, P>::~SubIteratorBase() {
148#ifdef ALLOW_PARTIALLY_SPECIALIZED_NESTED_TEMPLATES
149 delete_subobj<T>();
150#else
151 delete_subobj();
152#endif
153}
154
155template <class T, template <class> class P>
157 return subobj_.size();
158}
159
160template <class T, template <class> class P>
161const typename SubIteratorBase<T, P>::iref& SubIteratorBase<T, P>::elem()
162 const {
163 return subobj_.at(iter_);
164}
165
166template <class T, template <class> class P>
168 const {
169 return PElemImpl<iter_type, detail::IsSharedPtr<iref>::value>::pelem(elem());
170}
171
172#if 0
173 template <class T, template <class> class P>
174 const std::shared_ptr<ConstructablePolymorphically>
176 {
177 return std::dynamic_pointer_cast<ConstructablePolymorphically,iter_type>(elem());
178 }
179#endif
180
181template <class T, template <class> class P>
183 iter_ = 0;
184}
185
186template <class T, template <class> class P>
188 ++iter_;
189 return *this;
190}
191
192template <class T, template <class> class P>
194 return (iter_ < num_iter()) ? 1 : 0;
195}
196
197template <class T, template <class> class P>
199 P<T>::init_subobj(obj_, subobj_);
200}
201
202template <class T, template <class> class P>
203void SubIteratorBase<T, P>::delete_subobj() {
204 P<T>::dealloc_subobj(subobj_);
205}
206
207}; // namespace libint2
208
209#endif
ConstructablePolymorphically is a base for all objects which can be constructed using a std::shared_p...
Definition polyconstr.h:30
SubIteratorBase<T> provides a base class for a sub-iterator class for T.
Definition iter.h:73
void init() override
Initializes the iterator.
Definition iter.h:182
cp_rettype pelem() const override
Returns current element. Implements SubIterator's pelem().
Definition iter.h:167
const ConstructablePolymorphically & cp_rettype
Return reference to ConstructablePolymorphically as object of this type.
Definition iter.h:80
const iref & elem() const
Returns current element.
Definition iter.h:161
unsigned int num_iter() const override
Returns a number of iterations (number of elements in a set over which to iterate).
Definition iter.h:156
SubIterator & operator++() override
Iterates to the next element. Only prefix form is provided.
Definition iter.h:187
Iterator provides a base class for all object iterator classes.
Definition iter.h:43
virtual const ConstructablePolymorphically & pelem() const
Return current element via base class.
Definition iter.cc:33
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