LIBINT  2.1.0-stable
key.h
1 /*
2  * This file is a part of Libint.
3  * Copyright (C) 2004-2014 Edward F. Valeev
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see http://www.gnu.org/licenses/.
17  *
18  */
19 
20 #ifndef _libint2_src_bin_libint_key_h_
21 #define _libint2_src_bin_libint_key_h_
22 
23 #include <libint2/intrinsic_types.h>
24 #include <cstddef>
25 #include <gmpxx.h>
26 #include <sstream>
27 
28 namespace libint2 {
29 
31  template <typename T, typename I>
33  public:
34  typedef T Type;
35  typedef I Instance;
36  TypeAndInstance() : t_(invalid_type_), i_(invalid_instance_) {}
37  TypeAndInstance(const Type& t, const Instance& i) : t_(t), i_(i) {}
38  TypeAndInstance(const TypeAndInstance& i) : t_(i.t_), i_(i.i_) {}
39  const TypeAndInstance& operator=(const TypeAndInstance& i) { t_ = i.t_; i_ = i.i_; return *this; }
40 
41  const Type& type() const { return t_; }
42  const Instance& instance() const { return i_; }
43 
44  private:
45  Type t_;
46  Instance i_;
47 
48  static Type invalid_type_;
49  static Instance invalid_instance_;
50  };
51 
52  template <typename T, typename I> typename TypeAndInstance<T,I>::Type TypeAndInstance<T,I>::invalid_type_(-1);
53  template <typename T, typename I> typename TypeAndInstance<T,I>::Instance TypeAndInstance<T,I>::invalid_instance_(-1);
54 
55  template <typename T, typename I>
56  bool operator==(const TypeAndInstance<T,I>& a,
57  const TypeAndInstance<T,I>& b) {
58  return a.type() == b.type() && a.instance() == b.instance();
59  }
60 
61  template <typename T, typename I>
62  bool operator<(const TypeAndInstance<T,I>& a,
63  const TypeAndInstance<T,I>& b) {
64  bool result =
65  (a.type() < b.type()) ||
66  ( (a.type() == b.type()) &&
67  (a.instance() < b.instance())
68  );
69  return result;
70  }
71 
72 
76  struct KeyTypes {
78  typedef unsigned int ClassID;
80  typedef mpz_class InstanceID;
81 
82  private:
84  template <typename Target, typename Source>
85  static Target
86  string_cast(Source s) {
87  std::ostringstream oss;
88  oss << s;
89  return Target(oss.str());
90  }
91 
92  public:
93  template <typename U>
94  inline static InstanceID cast(U i) {
95  return InstanceID(i);
96  }
97  };
98 
99  template <>
100  inline KeyTypes::InstanceID
101  KeyTypes::cast<long long>(long long i) {
102  return string_cast<InstanceID>(i);
103  }
104  template <>
105  inline KeyTypes::InstanceID
106  KeyTypes::cast<unsigned long long>(unsigned long long i) {
107  return string_cast<InstanceID>(i);
108  }
109 
110 
113 
114 };
115 
116 #endif
117 
118 // Local Variables:
119 // mode: c++
120 // End:
mpz_class InstanceID
some classes need to have distinct instances to have unique InstanceID&#39;s, e.g. generalized Singletons...
Definition: key.h:80
Type/Instance combination serves as a key to quickly compare 2 polymorphic Singletons.
Definition: key.h:32
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:23
unsigned int ClassID
distinct classes have unique ClassID&#39;s
Definition: key.h:78
TypeAndInstance< KeyTypes::ClassID, KeyTypes::InstanceID > DGVertexKey
this composite hashing key works for DGVertex
Definition: key.h:112
Collection of types used for constructing keys in libint2.
Definition: key.h:76