LIBINT 2.9.0
key.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_key_h_
22#define _libint2_src_bin_libint_key_h_
23
24#include <gmpxx.h>
25#include <libint2/util/intrinsic_types.h>
26
27#include <cstddef>
28#include <sstream>
29
30namespace libint2 {
31
34template <typename T, typename I>
36 public:
37 typedef T Type;
38 typedef I Instance;
39 TypeAndInstance() : t_(invalid_type_), i_(invalid_instance_) {}
40 TypeAndInstance(const Type& t, const Instance& i) : t_(t), i_(i) {}
41 TypeAndInstance(const TypeAndInstance& i) : t_(i.t_), i_(i.i_) {}
42 const TypeAndInstance& operator=(const TypeAndInstance& i) {
43 t_ = i.t_;
44 i_ = i.i_;
45 return *this;
46 }
47
48 const Type& type() const { return t_; }
49 const Instance& instance() const { return i_; }
50
51 private:
52 Type t_;
53 Instance i_;
54
55 static Type invalid_type_;
56 static Instance invalid_instance_;
57};
58
59template <typename T, typename I>
60typename TypeAndInstance<T, I>::Type TypeAndInstance<T, I>::invalid_type_(-1);
61template <typename T, typename I>
62typename TypeAndInstance<T, I>::Instance
64
65template <typename T, typename I>
66bool operator==(const TypeAndInstance<T, I>& a,
67 const TypeAndInstance<T, I>& b) {
68 return a.type() == b.type() && a.instance() == b.instance();
69}
70
71template <typename T, typename I>
72bool operator<(const TypeAndInstance<T, I>& a, const TypeAndInstance<T, I>& b) {
73 bool result = (a.type() < b.type()) ||
74 ((a.type() == b.type()) && (a.instance() < b.instance()));
75 return result;
76}
77
81struct KeyTypes {
83 typedef unsigned int ClassID;
86 typedef mpz_class InstanceID;
87
88 private:
91 template <typename Target, typename Source>
92 static Target string_cast(Source s) {
93 std::ostringstream oss;
94 oss << s;
95 return Target(oss.str());
96 }
97
98 public:
99 template <typename U>
100 inline static InstanceID cast(U i) {
101 return InstanceID(i);
102 }
103};
104
105template <>
106inline KeyTypes::InstanceID KeyTypes::cast<long long>(long long i) {
107 return string_cast<InstanceID>(i);
108}
109template <>
110inline KeyTypes::InstanceID KeyTypes::cast<unsigned long long>(
111 unsigned long long i) {
112 return string_cast<InstanceID>(i);
113}
114
117
118}; // namespace libint2
119
120#endif
121
122// Local Variables:
123// mode: c++
124// End:
Type/Instance combination serves as a key to quickly compare 2 polymorphic Singletons.
Definition key.h:35
Defaults definitions for various parameters assumed by Libint.
Definition algebra.cc:24
TypeAndInstance< KeyTypes::ClassID, KeyTypes::InstanceID > DGVertexKey
this composite hashing key works for DGVertex
Definition key.h:116
Collection of types used for constructing keys in libint2.
Definition key.h:81
mpz_class InstanceID
some classes need to have distinct instances to have unique InstanceID's, e.g.
Definition key.h:86
unsigned int ClassID
distinct classes have unique ClassID's
Definition key.h:83