LIBINT 2.9.0
entity.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#ifndef _libint2_src_bin_libint_entity_h_
22#define _libint2_src_bin_libint_entity_h_
23
24#include <class_registry.h>
25#include <dgvertex.h>
26
27#include <iomanip>
28#include <limits>
29#include <sstream>
30#include <string>
31
32namespace libint2 {
33
37namespace EntityTypes {
38typedef enum { fp, integer } EntityTypeEnum;
39
40template <unsigned int TypeIndex>
41struct EntityType {
42 static unsigned int type2int() { return TypeIndex; }
43};
44
45typedef EntityType<fp> FP;
47
48static const unsigned int ntypes = 2;
49}; // namespace EntityTypes
50
52template <typename T, typename U>
54template <>
55struct ProductType<int, int> {
56 typedef int result;
57};
58template <>
59struct ProductType<int, double> {
60 typedef double result;
61};
62template <>
63struct ProductType<double, int> {
64 typedef double result;
65};
66template <>
67struct ProductType<double, double> {
68 typedef double result;
69};
70template <>
71struct ProductType<EntityTypes::Int, int> {
73};
74template <>
75struct ProductType<EntityTypes::Int, double> {
76 typedef EntityTypes::FP result;
77};
78template <>
79struct ProductType<EntityTypes::FP, int> {
80 typedef EntityTypes::FP result;
81};
82template <>
83struct ProductType<EntityTypes::FP, double> {
84 typedef EntityTypes::FP result;
85};
86template <>
87struct ProductType<int, EntityTypes::Int> {
89};
90template <>
91struct ProductType<double, EntityTypes::Int> {
92 typedef EntityTypes::FP result;
93};
94template <>
95struct ProductType<int, EntityTypes::FP> {
96 typedef EntityTypes::FP result;
97};
98template <>
99struct ProductType<double, EntityTypes::FP> {
100 typedef EntityTypes::FP result;
101};
102template <>
103struct ProductType<EntityTypes::Int, EntityTypes::Int> {
104 typedef EntityTypes::Int result;
105};
106template <>
107struct ProductType<EntityTypes::Int, EntityTypes::FP> {
108 typedef EntityTypes::FP result;
109};
110template <>
111struct ProductType<EntityTypes::FP, EntityTypes::Int> {
112 typedef EntityTypes::FP result;
113};
114template <>
115struct ProductType<EntityTypes::FP, EntityTypes::FP> {
116 typedef EntityTypes::FP result;
117};
118
120template <typename T>
121std::string to_string(const T& x) {
122 std::ostringstream oss;
123 oss << std::scientific
124 << std::setprecision(std::numeric_limits<T>::digits10 + 1) << x;
125 return oss.str();
126}
127
132class Entity {
133 public:
134 virtual ~Entity() {}
136 const std::string& id() const { return id_; }
137
138 protected:
139 Entity(const std::string& id) : id_(id) {}
140
141 private:
143 std::string id_;
144};
145
150template <class T>
151class RTimeEntity : public Entity, public DGVertex {
152 public:
153 typedef typename DGVertex::KeyType key_type;
154
155 RTimeEntity(const std::string& id, bool p = true)
156 : Entity(id),
158 precomputed_(p) {
159 FNVStringHash SH;
160 key_ = KeyTypes::cast(SH.hash(id));
161#if DEBUG
162 std::cout << "Allocated RTimeEntity id = " << this->id() << std::endl;
163#endif
164 }
165
166 virtual ~RTimeEntity() {
167#if DEBUG
168 std::cout << "Deallocated RTimeEntity id = " << this->id() << std::endl;
169#endif
170 }
171
173 unsigned int size() const override { return 1; }
174
176 bool equiv(const std::shared_ptr<DGVertex>& a) const override {
177 if (a->typeid_ == typeid_) {
178#if USE_INT_KEY_TO_COMPARE
179 return key() == a->key() && label() == a->label();
180#else
181 std::shared_ptr<RTimeEntity> a_cast =
182 std::static_pointer_cast<RTimeEntity, DGVertex>(a);
183 return id() == a_cast->id();
184#endif
185 } else
186 return false;
187 }
188
190 const std::string& label() const override { return Entity::id(); }
192 const std::string& id() const override { return label(); }
194 std::string description() const override {
195 std::ostringstream os;
196 os << "RTimeEntity: " << id();
197 const std::string descr = os.str();
198 return descr;
199 }
201 typename DGVertex::KeyReturnType key() const override { return key_; }
202
203 private:
205 bool this_precomputed() const override { return precomputed_; }
206
207 key_type key_;
212 bool precomputed_;
213};
214
219template <class T>
220class CTimeEntity : public Entity, public DGVertex {
221 public:
222 CTimeEntity(const T& val)
223 : Entity(to_string(val)),
225 value_(val) {
226#if DEBUG
227 std::cout << "Allocated CTimeEntity id = " << this->id()
228 << " value = " << value() << std::endl;
229#endif
230 }
231
232 virtual ~CTimeEntity() {
233#if DEBUG
234 std::cout << "Deallocated CTimeEntity id = " << this->id()
235 << " value = " << value() << std::endl;
236#endif
237 }
238
240 unsigned int size() const override { return 1; }
241
243 bool equiv(const std::shared_ptr<DGVertex>& a) const override {
244 if (a->typeid_ == typeid_) {
245#if USE_INT_KEY_TO_COMPARE
246 return key() == a->key();
247#else
248 std::shared_ptr<CTimeEntity> a_cast =
249 std::static_pointer_cast<CTimeEntity, DGVertex>(a);
250 return id() == a_cast->id();
251#endif
252 } else
253 return false;
254 }
255
257 const std::string& label() const override { return Entity::id(); }
259 const std::string& id() const override { return label(); }
261 std::string description() const override {
262 std::ostringstream os;
263 os << "CTimeEntity: " << id();
264 const std::string descr = os.str();
265 return descr;
266 }
267
269 typename KeyTraits<T>::ReturnType value() const { return value_; }
270
272 typename DGVertex::KeyReturnType key() const override {
273 if (std::is_floating_point<T>::value) {
274 if (not std::is_same<T, double>::value)
275 throw std::runtime_error(
276 "CTimeEntity<Real> only supported when Real==double");
277 return static_cast<typename DGVertex::KeyReturnType>(
278 *reinterpret_cast<const unsigned long*>(&value_));
279 } else
280 return static_cast<typename DGVertex::KeyReturnType>(value());
281 }
282
283 private:
284 T value_;
285
287 bool this_precomputed() const override { return true; }
288};
289
294// template <typename T>
295// std::shared_ptr<Entity>
296// operator*(const std::shared_ptr<Entity>& A, const std::shared_ptr<
297// CTimeEntity<T> >& B);
298
301template <typename T, typename U>
302std::shared_ptr<CTimeEntity<typename ProductType<T, U>::result> > operator*(
303 const std::shared_ptr<CTimeEntity<T> >& A,
304 const std::shared_ptr<CTimeEntity<U> >& B) {
306 return std::shared_ptr<prodtype>(new prodtype(A->value() * B->value()));
307}
308
311template <typename T, typename U>
312std::shared_ptr<RTimeEntity<typename ProductType<T, U>::result> > operator*(
313 const std::shared_ptr<RTimeEntity<T> >& A,
314 const std::shared_ptr<CTimeEntity<U> >& B) {
316 std::ostringstream oss;
317 oss << A->id() << "*" << B->id();
318 // TODO this should be false, but the logic of DirectedGraph construction
319 // depends on this being true
320 const bool not_precomputed = true;
321 return std::shared_ptr<prodtype>(new prodtype(oss.str(), not_precomputed));
322}
323// TODO should be possible to enable this, but this creates RTimeEntities that
324// should not be precomputed, see the comment above
325#if 0
328 template <typename T, typename U>
329 std::shared_ptr< RTimeEntity< typename ProductType<T,U>::result > >
330 operator*(const std::shared_ptr< CTimeEntity<U> >& B, const std::shared_ptr< RTimeEntity<T> >& A)
331 {
332 return A * B;
333 }
334#endif
335
336}; // namespace libint2
337
338#endif
CTimeEntity is an Entity of type T that exists at compile-time of the generated code (hence has a val...
Definition entity.h:220
bool equiv(const std::shared_ptr< DGVertex > &a) const override
Implementation of DGVertex::equiv()
Definition entity.h:243
DGVertex::KeyReturnType key() const override
Implements Hashable::key()
Definition entity.h:272
std::string description() const override
Implementation of DGVertex::description()
Definition entity.h:261
const std::string & id() const override
Implementation of DGVertex::id()
Definition entity.h:259
const std::string & label() const override
Implementation of DGVertex::label()
Definition entity.h:257
unsigned int size() const override
Implementation of DGVertex::size()
Definition entity.h:240
KeyTraits< T >::ReturnType value() const
returns the value
Definition entity.h:269
Objects of this type provide limited information about the class at runtime.
Definition class_registry.h:46
This is a vertex of a Directed Graph (DG)
Definition dgvertex.h:44
ClassID typeid_
typeid stores the ClassID of the concrete type.
Definition dgvertex.h:71
KeyTypes::InstanceID KeyType
DGVertex provides function key() which computes key of type KeyType and returns it using KeyReturnTyp...
Definition dgvertex.h:63
DGVertex(ClassID tid)
Sets typeid to tid.
Definition dgvertex.cc:32
Entity is a base class for all objects that exist at compile or runtime of the generated code.
Definition entity.h:132
const std::string & id() const
Return id string.
Definition entity.h:136
FNVStringHash uses Fowler/Noll/Vo algorithm to hash a char string to a 64-bit integer.
Definition hashable.h:94
LIBINT2_UINT_LEAST64 hash(const std::string &S)
Returns 64-bit integer hash of S.
Definition hashable.h:116
RTimeEntity is an Entity of type T that exists at runtime of the generated code (hence has no value k...
Definition entity.h:151
const std::string & id() const override
Implementation of DGVertex::id()
Definition entity.h:192
bool equiv(const std::shared_ptr< DGVertex > &a) const override
Implementation of DGVertex::equiv()
Definition entity.h:176
DGVertex::KeyReturnType key() const override
Implements Hashable::key()
Definition entity.h:201
unsigned int size() const override
Implementation of DGVertex::size()
Definition entity.h:173
const std::string & label() const override
Implementation of DGVertex::label()
Definition entity.h:190
std::string description() const override
Implementation of DGVertex::description()
Definition entity.h:194
Defaults definitions for various parameters assumed by Libint.
Definition algebra.cc:24
std::string to_string(const T &x)
Converts x to its string representation.
Definition entity.h:121
std::shared_ptr< CTimeEntity< typename ProductType< T, U >::result > > operator*(const std::shared_ptr< CTimeEntity< T > > &A, const std::shared_ptr< CTimeEntity< U > > &B)
Creates product A*B.
Definition entity.h:302
Definition entity.h:41
Product of 2 types.
Definition entity.h:53