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